How to add ATR multiplier to Entry order after X consecutive highs/lows

Hi All, I'm trying to code an entry that adds a ATR number to the previous consecutive number of 3 higher highs (Short trade) and 3 lower lows (Long Trade) per the following. Any assistance appreciated:

MovingAv1 = MA(C, 100);
MovingAv2 = MA(C, 5);
MyATR = 0.5 * ATR(10);

// Long Signal
// 1) Close > 100 day MA
// 2) Close < 5 day MA
// 3) 3 consecutive lower lows
// 4) Set next day Limit Buy order below prior Close @ 0.5 x 10 day ATR
// Long Exit - close is > prior close, then exit @ next day's open

Buy = C > MovingAv1 AND Ref(C, -1) > MovingAv1 AND Ref(C, -2) > MovingAv1 AND Ref(C, -3) > MovingAv1 AND 
C < MovingAv2 AND Ref(C, -1) < MovingAv2 AND Ref(C, -2) < MovingAv2 AND Ref(C, -3) < MovingAv2;

// Short Signal
// 1) Close < 100 day MA
// 2) Close > 5 day MA
// 3) 3 consecutive higher highs
// 4) Set next day Limit Sell order above prior Close @ 0.5 x 10 day ATR
// Short Exit - close is < prior close, then exit @ next day's open

Sell = C < MovingAv1 AND Ref(C, -1) < MovingAv1 AND Ref(C, -2) < MovingAv1 AND Ref(C, -3) < MovingAv1 AND 
C > MovingAv2 AND Ref(C, -1) > MovingAv2 AND Ref(C, -2) > MovingAv2 AND Ref(C, -3) > MovingAv2;

Buy = ExRem(Buy, Sell);
Sell = ExRem(Sell, Buy);
	
PlotShapes(Buy*shapeUpArrow, colorGreen, 0);
PlotShapes(Sell*shapeDownArrow, colorRed, 0);

@DazN a suggestion might be to use Explorations and other methods of debugging the code you wrote as it is not what you seem to be trying to achieve.

Another idea while learning is to take each piece you want to code and get that correct before trying to add everything in one long line of code.

An example of one of the many ways to code 3 consecutive lower lows

Down3 = BarsSince( C >= Ref( C, -1 ) ) >= 3;

And of course there are many ways to code that particular condition

Down3 = C < Ref( C, -1 ) and Ref( C, -1 ) < Ref( C, -2 ) and Ref( C, -2 ) < Ref( C, -3 );
Down3 = Sum( C < Ref( C, -1 ), 3 ) == 3;

etc. etc.

As for the limit order, a useful Knowledge Base article (and you should really read them all) will help you

Welcome to AmiBroker and good luck!

1 Like

Thanks and very much appreciated.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.