Additional buy condition

i already have a basic buy signal and short signal

have completed the code for the same

i wish to add a second condition wherein the buy happens only when the current bar

value cuts the previous bar high and short happens when current bar value cuts previous bars low

just the price moving above/ beyond is sufficient not required to wait for close

i wish to add this as a second condition to my already existing buy condition

already existing buy condition one is based on cci crossing above + 50 to buy and - 50 to short

the candle stick needs to close above or below this value for buy condi 1

this is condition one and once this happens i want the next bar to check for previous high and low

and complete condition two to buy/short

i trade in 30 mins time frame

the trigger should happen only when both conditions are completed.

guys please help me with the code along with the plots

need coding help only for condition 2

would be of great help


Bcond1 = // defined by you
Bcond2 = Ref( Bcond1, -1) AND H > Ref( H, -1);
Buy = Bcond1 AND Bcond2;

Scond1 = // defined by you
Scond2 = Ref( Scond1, -1) AND L < Ref( L, -1);
Sell = Scond1 AND Scond2;

@sadayappan it is NOT good form to create duplicate threads on the same topic

Your original thread AFL based on two buy conditions had a great example from @marcel.

Part of what makes this forum great is the helpful people posting code. BUT as a new person coming in, we would prefer to see you showing us what you have done, and learning from others when they post potential or partial solutions.

If you learn from these, then you will be better able to solve more of your own issues, and perhaps be able to help other newbies when you have more experience.

So, check out @travick's code and @Marcel's code in the other thread and see what you can learn from them.

When you come back with more questions, show us what you have done (post your code) and tell us what is not working the way you want or think it should. And as always... try reading the manual first.


A minor correction, Sell variable is supposed to be Short.

1 Like

hi snoopy

extremely sorry for creating a duplicate thread

was really immature on my part

i apologize again

will never repeat the same

will develop the code as per travicks advice and post questions when need be


hi travick

thx for the help

will update my codes as per your advice and if any questions arise will ask

thx for your time and patience in guiding me

appreciate it


_SECTION_BEGIN("Donchian Channel trading System");

//_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C ));
Plot( C, "Close", ParamColor( "Color", colorBlack ), styleNoTitle | ParamStyle( "Style" ) | GetPriceStyle() );
Title = NumToStr( DateTime(), formatDateTime ) + " O " + O + " H " + H + " L " + L + " C " + C

//Initial Parameters
SetTradeDelays( 0,0,0, 0 );
SetOption( "InitialEquity", 200000);
SetOption("FuturesMode" ,True);
SetOption( "AllowPositionShrinking", True );

Plot( Close, "Price", colorWhite, styleCandle );

DonchianUpper =HHV(Ref(H,-1),pds);
DonchianLower = LLV(Ref(L,-1),pds);
DonchianMiddle = (DonchianUpper+DonchianLower)/2;

per1 = Param( "RSI", 14, 1, 100, 1 );
per2 = Param( "OverBought Level", 50, 1, 100, 1 );
per3 = Param( "OverSold Level", 40, 1, 100, 1 );

RSI1= RSI(per1);
printf("\nDonchianUpper : " + DonchianUpper );  
printf("\nDonchianLower : " + DonchianLower );  
printf("\nDonchianMiddle : " + DonchianMiddle );  


Bcond1 = Cross(RSI1,per2);
Bcond2 = Ref( Bcond1, -1) AND H > Ref( H, -1);
Buy = Bcond1 AND Bcond2;
Scond1 = Cross(per3, RSI1) ;
Scond2 = Ref( Scond1, -1) AND L < Ref( L, -1);
Short = Scond1 AND Scond2;



Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);

printf("\nBuy : " + Buy );  
printf("\nSell : " + Sell );  
printf("\nShort : " + Short );  
printf("\nCover : " + Cover );  

/* Plot Buy and Sell Signal Arrows */
//PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-25);
//PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-35);
//PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-30);
//PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=25);
//PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=35);
//PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-30);
PlotShapes(IIf(Sell, shapeStar, shapeNone),colorGold, 0, L, Offset=-15);
PlotShapes(IIf(Cover, shapeStar, shapeNone),colorGold, 0,L, Offset=-15);

PlotShapes( IIf( Buy, shapeSquare, shapeNone ), colorGreen, 0, L, Offset = -40 );
PlotShapes( IIf( Buy, shapeSquare, shapeNone ), colorLime, 0, L, Offset = -50 );
PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorWhite, 0, L, Offset = -45 );
//PlotShapes( IIf( Sell, shapeDownArrow, shapeNone ), colorRed, 0, H, Offset = -45 );
PlotShapes( IIf( Short, shapeSquare, shapeNone ), colorRed, 0, H, Offset = 40 );
PlotShapes( IIf( Short, shapeSquare, shapeNone ), colorOrange, 0, H, Offset = 50 );
PlotShapes( IIf( Short, shapeDownArrow, shapeNone ), colorWhite, 0, H, Offset = -45 );
//PlotShapes( IIf( Cover, shapeUpArrow, shapeNone ), colorBlue, 0, L, Offset = -45 );


hi travick modified the code as per ur advice

but unable to get buy/ short signals ( up/down ) arrow and unable to back test either

can u please help me here


Bcond1 = Cross(RSI1,per2);
Bcond2 = Ref( Bcond1, -1) AND H > Ref( H, -1);
Buy = Bcond1 AND Bcond2;

This will double the check, Now you are checking for Cross on previous bar as well as current bar.
That's that happens when you don't post properly in the opening.

Bcond1 = Cross( RSI1, per2);
Bcond2 = H > Ref( H, -1);
Buy = Ref( Bcond1, -1) AND Bcond2;

same mod for Short.


hi travick

thx for your continuous support travick

Bcond1 = Cross( RSI1, per2);
Bcond2 = H > Ref( H, -1);
Buy = Ref( Bcond1, -1) AND Bcond2;

works perfectly but the difference is that once the second condition is completed
it buys only after close of that bar , thats after 15 mins since i use 15 tf

would it be possible to buy immediately once the earlier bar high value is cut in current bar

condi 1 rsi 50 after close of bar , condi 1 will be completed
condi 2 - if this bar cuts previous bar high value ( condi 1 bar high value ) enter long , need not wait for bar to complete

is it possible to code the same

my apologies for too many queries

Search for SetTradeDelays and see if that is the cause.
Set them to 0 for no delay.


ok will try now thx travick

hi travick
jus checked
trade delays are 0,0,0,0

still unsure whats causing the isuue

Can you explain in detail how and where you are running the code


i am running the code on 6.3 version amibroker Screenshot%20(18)

the buy signal bar is the cond2 and the earlier bar is cond1
it buys in close of the bar only instead of buying when previous bar high is cut

@sadayappan please give @travick multiple "likes" :heart: as he is giving you so much hand holding and private tutoring!! You are not making much effort to solve your own problem.

Are you looking for a LIMIT order to buy the breakout? Because your signal is not generated until the data arrives - which obviously is at the Close of the bar. If you want to execute an order before the next bar's Open then you must figure out how to place that order in anticipation of a signal being triggered, which would be either at the Close of the signal bar or on a Limit order anticipating a possible signal.

Good luck @travick


thank you travick issue sorted out


1 Like