|
Re: Waiting fixed number of bars before next trade
[Re: jcl]
#422631
05/13/13 22:07
05/13/13 22:07
|
Joined: Nov 2012
Posts: 19 Texas, US
deweymcg
OP
Newbie
|
OP
Newbie
Joined: Nov 2012
Posts: 19
Texas, US
|
|
|
|
Re: Waiting fixed number of bars before next trade
[Re: JerryS]
#459858
06/09/16 00:01
06/09/16 00:01
|
Joined: Apr 2014
Posts: 482 Sydney, Australia
boatman
Senior Member
|
Senior Member
Joined: Apr 2014
Posts: 482
Sydney, Australia
|
Try this:
function run()
{
vars Price = series(price());
vars maFast = series(SMA(Price, 50));
vars maSlow = series(SMA(Price, 200));
vars entries = series(0);
if(crossOver(maFast, maSlow))
{
entries[0] = 1;
if(Sum(entries+1, 6) == 0)
enterLong();
}
if(crossUnder(maFast, maSlow))
{
entries[0] = 1;
if(Sum(entries+1, 6) == 0)
enterShort();
}
}
This script stores each crossing of the moving averages in a series. The current value of the series is 1 if there was a cross, and 0 if there was no cross. We then shift the series backwards by one bar and sum the number of crosses in the previous 6 bars. We shift the series backwards by one bar in order to exclude the current cross from the summation. If we didn't do this, the script would never enter a trade since the if(Sum(...)) statement would always be true. Hope that helps.
|
|
|
Re: Waiting fixed number of bars before next trade
[Re: JerryS]
#460092
06/16/16 02:56
06/16/16 02:56
|
Joined: Apr 2014
Posts: 482 Sydney, Australia
boatman
Senior Member
|
Senior Member
Joined: Apr 2014
Posts: 482
Sydney, Australia
|
Yes, I meant "false" - sorry about that.
Just to clarify, entries[1] is not the same as entries+1. The former is the value of the next-to-last index of the entries series. The latter is the entries series shifted backwards by 1 bar. That is, the former is a single value, the latter is an entire series.
Also, the code says "If it crossed over at the last bar and it HASN'T crossed over OR UNDER in the prior 6 bars, enter long, and if it crossed under at the last bar and HASN'T crossed under OR OVER in the prior 6 bars, enter short."
Hope that helps
|
|
|
|