Waiting fixed number of bars before next trade

Posted By: deweymcg

Waiting fixed number of bars before next trade - 05/13/13 09:24

Before entering another trade I want to make sure it has been at least 6 bars since the last trade was opened.

Any suggestions?

Thanks
Posted By: jcl

Re: Waiting fixed number of bars before next trade - 05/13/13 09:46

Look in Workshop 4: http://manual.zorro-trader.com/tutorial_trade.htm

There you can see how to make sure it has been at least 3 bars since the last peak or valley before entering another trade. Instead of a peak or valley, use the trade entering itself for setting a signal, and then wait 6 bars instead of 3 after the last signal.
Posted By: deweymcg

Re: Waiting fixed number of bars before next trade - 05/13/13 22:07

Thanks!
Posted By: JerryS

Re: Waiting fixed number of bars before next trade - 06/08/16 16:55

I don't see in the code how this makes sure it's been at least 3 bars.

This is the code from the workshop that was referenced:
function run()
{
vars Price = series(price());
vars Trend = series(LowPass(Price,500));

Stop = 4*ATR(100);
if(valley(Trend))
enterLong();
else if(peak(Trend))
enterShort();
}


It doesn't appear that lite-C or Zorro has a function for "bars since" something has occurred. How do we handle this situation?
There are many situations when you would want to test waiting at least one or two bars since some event has occurred.
Posted By: JerryS

Re: Waiting fixed number of bars before next trade - 06/08/16 17:33

Each bar i'm using is 30 mins, so I'm trying the following code:

//////-------------MA LINE CROSS CHECK (crossNOW, crossTF)
bool MAcrossOver = MAshort[1] < MAlong[1] and MAshort[0] > MAlong[0];
bool MAcrossUnder = MAshort[1] > MAlong[1] and MAshort[0] < MAlong[0];
bool MAcrossNOW = MAcrossOver or MAcrossUnder;
if (MAcrossNOW){
timer();
}
var nback = 3;
bool MAcrossNOWrecent = timer() < (nback*1800000);

I'm hoping "MAcrossNOWrecent" will tell me if the event (crossing of two MA lines) occurred less than 3 bars ago (1800000 milliseconds *3).
Posted By: boatman

Re: Waiting fixed number of bars before next trade - 06/09/16 00:01

Try this:

Code:
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.
Posted By: JerryS

Re: Waiting fixed number of bars before next trade - 06/15/16 15:56

Ok after taking a second glance at this, I'm starting to understand it...
At first I didn't realize you were creating a series of zero's... i didn't know that's how you would do that...

Now from your description I understand that:
entries+1 is not simple 1 plus each value in the entries series, but rather something much more like entries[1]. I'm not sure why we don't use entries[1] but that's fine... (that doesn't seem to be accepted)

So this code says: "If it has crossed over in the prior 6 bars, enter long, and if it crossed under in the prior 6 bars, enter short."


And when you said "the if(Sum(...)) statement would always be true" you mean false I believe.

This makes sense, thank you!!
Posted By: boatman

Re: Waiting fixed number of bars before next trade - 06/16/16 02:56

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
© 2024 lite-C Forums