Buy when stock crosses under a SMA twice in one month
Author: gbullr
Creation Date: 10/28/2009 11:17 PM
profile picture

gbullr

#1
For example:

I am trying to create a trade trigger for a Stock that crosses under and SMA twice in one month so you short it on the bar+1 after the SECOND cross under in one month or period or whatever. Thanks a lot.
profile picture

Eugene

#2
If they happen, would you ignore the 3rd, the 4th crossover of the month?
profile picture

Eugene

#3
CODE:
Please log in to see this code.
profile picture

Cone

#4
Eugene gave you the count by calendar month. But if you meant to count over that last rolling month (approx. 21 trading days), then this would do that -

CODE:
Please log in to see this code.
profile picture

gbullr

#5
Thanks

Will take a look and get back to you. Trying to understand generic counters on wealthlab.

Regarding the 3rd or 4th once I am in the trade I can ignore subsequent ones but I realize what you are saying will have to think about this some more.

Thanks a again to both of you.

Regards.
profile picture

gbullr

#6
Cone.

Why are you calling this DataSeries cross = new DataSeries(Bars, "Crossing=1"); new DataSeries what does this command do?


Thanks.

profile picture

gbullr

#7
For example.

W/ the following code I get Cannot implicitly convert type double to bool

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using WealthLab;
using WealthLab.Indicators;

namespace WealthLab.Strategies
{
public class MyStrategy : WealthScript
{

protected override void Execute()
{

int period = 50;
DataSeries sma = SMA.Series(Close, 50);
DataSeries cross = new DataSeries (Bars,"Corssing = 1");

PlotSeries(PricePane,SMA.Series(Close,50),Color.Blue,LineStyle.Solid,2);


for(int bar = sma.FirstValidValue; bar < Bars.Count; bar++) // what does this do?
{
if (CrossUnder(bar, Close, sma))
{
cross[bar] = 1d;
}
}



DataSeries xingCount = Sum.Series (cross,21);



for(int bar = GetTradingLoopStartBar(51); bar < Bars.Count; bar++)
{





if (IsLastPositionActive)
{
Position p = LastPosition;
if (p.EntrySignal.Contains("Group2|"))
{
double Stop = p.EntryPrice * (1 + 5 / 100.0d);
CoverAtStop(bar + 1, p, Stop, "Group2");
}
if (p.EntrySignal.Contains("Group1|"))
{
double Target = p.EntryPrice * (1 - 10 / 100.0d);
CoverAtLimit(bar + 1, p, Target, "Group1");
}

}
else
{
if (xingCount[bar] = 2) /how do I reset counter to zero once trade has been opened?
{
ShortAtMarket(bar + 1, "Group1|Group2|");
}

}
}
}
}
}
profile picture

Cone

#8
QUOTE:
Why are you calling this DataSeries cross = new DataSeries(Bars, "Crossing=1");
It creates a zero-filled DataSeries whose description is "Crossing=1". In other words, later the code puts the value 1 on the bars of the crossings. The Sum.Series indicator sums up the 1's over the period specified.

See WealthScript Programming Guide: DataSeries > Filling a Custom DataSeries > How to: Create a DataSeries filled with zeroes"

I'm not going to look at your code because you didn't use the CODE tags.
profile picture

gbullr

#9
Thanks... I don't even know what code tags is so I apologize. I any event I still get an error because when I write

if cross = 2 the SellAtMarket because the program has an issue w/ allowing me to check a dataseries w/ an int?

On the off chance you read this.

SystemMemoryProblems are flagrant even though I am running a 64 bit system w/ 4g of ram. If you are in contact w/ the developers you might want to tell them to give up speed but prevent system crashes by storing output anywhere other than ram when the program is processing lots of data. Just an idea. Problem arises both in backtests as well as strategy monitors.
profile picture

Cone

#10
QUOTE:
I don't even know what code tags is
When you type a response to my message, look at the buttons a few pixels above where you're typing. Click on them and you'll know.

QUOTE:
if cross = 2
That's the wrong syntax for checking equity. = is assignment, == checks equality.

By the way, make sure you program what you mean. If it must be exactly 2, then cross == 2 is correct. Just keep in mind that the indicator could be 2 or greater.

QUOTE:
SystemMemoryProblems are flagrant even though I am running a 64 bit system w/ 4g of ram.
Sounds like you're trying to process too much data. If you're running WL Pro, then neither 64-bit or 4G of Ram buys you anything. In reality it's running in WOW 32-bit mode that can address only 2G. See this General FAQ > Memory.
profile picture

gbullr

#11

The == did the trick. Thanks for helping me out w/ my own stupidity. Straying from basics on coding for me is an issue as you and everyone else reading this can see. One more question: Error (4) Index was out of range means what? Tks.
profile picture

Eugene

#12
QUOTE:
Error (4) Index was out of range means what?

Without some context, it's too general to mean something. See this KB article for some possible causes of this error:

Index was out of range
profile picture

gbullr

#13
How do I reset cross and xingcount to zero lets say once I enter a trade?
profile picture

Eugene

#14
When you need to reset a variable:
CODE:
Please log in to see this code.
profile picture

gbullr

#15
this does not work for nulling a dataseries. I need to set the nee Dataseries to Zero.

Additionally want to know if there is a way to plot the value of a double upclose (# of up closes )

Thanks very much.
profile picture

Eugene

#16
QUOTE:
this does not work for nulling a dataseries.

Right, because it hardly rings a bell for me why'd you want to null a DataSeries during the main loop. It works for resetting a variable though. It's not obvious.
QUOTE:
I need to set the nee Dataseries to Zero.

To initialize a new data series:
CODE:
Please log in to see this code.

QUOTE:
Additionally want to know if there is a way to plot the value of a double upclose (# of up closes )

Not sure if I understood you correctly:
CODE:
Please log in to see this code.
profile picture

gbullr

#17
I am going crazy w/ this thing.

What I am trying to do is the following: If there is a close < less than 2 days ago then you set two counters to zero (A).
If there are 9 consecutive days where the Close is > than the close 4 days ago then one counter gets activated lets call it its value becomes 9. The second counter increases every time a close is greater than the close 2 days ago. Both counters start at zero at A. When the first counter is activated and the second counter becomes 13 then you short.

Clearly I get the first count to 9 cause that only gets activated if all the closes are as programmed. The second counter I am having a big problem w/.

CODE:
Please log in to see this code.
profile picture

Eugene

#18
Here you go.

Some notes:

1. Two counters are not required.
2. Note how the usage of CumUp makes coding of these "consecutive days up" conditions more straighforward.
3. Take a look at this Tutorial: Setups, Triggers, Delays, and Timeouts to learn the principle this code follows:
CODE:
Please log in to see this code.

profile picture

gbullr

#19
I will take a look. If I could hug you I would!!!.
profile picture

ss161

#20
if you're trying to implement ***'s *** indicator, I think after you've reached a 9 count, the second counter starts at 0 and then needs to reach 13 before the setup is complete.
profile picture

Eugene

#21
Hey Steve, please don't mention that guy's trademarks here :) Thank you.
profile picture

gbullr

#22
He shall remain nameless.

Unless I did this wrong with help from above .... yet another guy that sells a somewhat infrequent signal which actually does not make you any money... c'est la vie.

Next question.

If you goto http://www.google.com/finance/company_news?q=NASDAQ:MSFT you will see the number of stories printed on msft for the last 4 months (right hand side of the screen). From that you could estimate the average number of daily stories or monthly or weekly or whatever. Is there a way to incorporate that data? Lets say that there is a big increase in news as per this data feed and I want to buy or sell.

Thanks.
profile picture

gbullr

#23
Looks like it is not worth it cause it does not give the news count on the latest day.
profile picture

Eugene

#24
Hmm, parsing numerous HTML pages (651 news, "Showing stories 1 - 10 of about 584") is pretty much involved even with the help of a godsend that is HTML Agility Pack.

profile picture

gbullr

#25
Thanks... I can't even get counters right so I think I am going to try to get that down before I "parse" anything and look like an "arse." Sorry about my poetry.

Relatively bored here.

profile picture

Eugene

#26
Actually, your news idea is interesting, just pretty involving at this point. Let's mark it for the later.
profile picture

gbullr

#27
Eugene: What am I doing wrong. I am trying to do this as ss151 suggested and working for 13 closes after the initial set up to go short. There is something wrong with the counter as per usual in the sense that I am setting it to zero at the wrong time or the loop is wrong. ? Comp Sci would have helped me. Regards. If you would like to talk off line about the news thing you can contact me at the email that I input to login to here etc. There is a specific system I would like to backtest which would include news.

Thanks.



CODE:
Please log in to see this code.
profile picture

Eugene

#28
I'm not familiar with the pattern, but it seems that it could be because you broke the construction by dissecting the "setup" and "entry" parts. Now they no longer act together.

Before doing any further tweaking with the code above and potentially wasting time with another iteration due to incomplete/misunderstood rules, please include a complete, unambiguous rule set. I'm not sure how does Steve's comment on the 2nd counter should be integrated.

1. ...
2. ...
3. ...
... ...
profile picture

gbullr

#29
Hi.

Sorry.

So 9 consecutive up closes from 4 days ago. That is the set up. Once that happens. 13 non consecutive up closes from 2 days ago.


profile picture

Eugene

#30
Here we go:
CODE:
Please log in to see this code.
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).