Breakout of signal candle

Hello friends,
Someone plz help me in coding the breakout of high of a signal candle and short on breakout of low of signal candle. I am not able to refrence the signal candle. for instance the buy should happen when the High of the signal candle (cross Ema 20, Ema 50), is broke. viceversa for short signal.

Have you got a sample code of what you have tried so far???

Conditionbuy = Cross(EMA(C,20),EMA(C,50));

Conditionshort = Cross(EMA(C,50),EMA(C,20));

Conditionexit = 3 ;

Buy = H > BarsSince(Conditionbuy);

Sell = L < LLV(L,Conditionexit) OR Conditionshort ;

Short = L < BarsSince(Conditionshort);

Cover = H > HHV(H,Conditionexit) OR Conditionbuy ;

You are an old member, atleast use code tags like this and format the code for readability.
It is a mandatory forum rule.

Conditionbuy = Cross( EMA( C, 20), EMA( C, 50) );

I am sorry, though i am old member, havent post much in the grp and so forgot. Sorry once again


Conditionbuy = Cross(EMA(C,20),EMA(C,50));

Conditionshort = Cross(EMA(C,50),EMA(C,20));

Conditionexit = 3 ;

Buy = H > BarsSince(Conditionbuy);

Sell = L < LLV(L,Conditionexit) OR Conditionshort ;

Short = L < BarsSince(Conditionshort);

Cover = H > HHV(H,Conditionexit) OR Conditionbuy ;

Your Buy and Short rules should probably be using ValueWhen() instead of BarsSince(). Right now you are comparing a price (High or Low) to the number of bars since a condition occurred, which makes no sense.

so how valuewhen to be used? if u can help for a buy condition

I'm assuming that he will wait for signal Bar to complete and get the actual H.

in that case,

BarsSH = BarsSince( Conditionbuy );    // Bars Since condition
BuyH = Ref( H, -1 * BarsSH);    // H of signal Bar also written as  -( BarsSH)
Buy = Cross( C, BuyH) AND BarsSH > 0;
// Buy when price crosses over the signal High and signal bar has completed

then ValueWhen() of H when condition true maybe skipped if do need the count of bars.
else it is

BuyH = ValueWhen( H, Conditionbuy, 1);
1 Like

@travick I think you're changing the OP's logic. He didn't ask for a Close above the signal condition high, he asked for a new high above that price. Also, your ValueWhen() function has the first two parameters reversed. Therefore, this single line should achieve the OP's goal:

Buy = H > ValueWhen(conditionBuy, H);

That can't possibly be true on the signal bar, so no need to add logic that looks for number of bars since signal >0.

1 Like

That's why I put two different codes but OP has to be clear and that was my interpretation.

I can't edit my post but

I've used Cross(), which is not Close above Signal Bar High.

As you said, new High above that price, when C will cross above that H, the successive bar will automatically form a new H above that H, isn't it ?

I'm just asserting that the signal bar is complete, so that the True High of that bar becomes known.

Edit: oh ok
My 2nd code snippet isn't correct, it was

Buy = H > ValueWhen( H, Conditionbuy, 1);

The same thing you posted. I messed it up in copy-paste while formatting. That's the same code you posted :slight_smile:

Will try the codes..

just to clear the air, I am looking out a new high after the signal candle high is formed. so basically my buy price would be just abv the high of the signal candle.

I see... you're assuming the OP is trying to run this in real time for signal generation, and I'm assuming he wants to run a back test, in which case there is no concern about multiple triggers being generated from an in-progress bar.

Edit: oh ok
My 2nd code snippet isn't correct, it was

Buy = H > ValueWhen( H, Conditionbuy, 1);

The same thing you posted. I messed it up in copy-paste while formatting. That's the same code you posted :slight_smile:

You've changed your previous code snippet to generate the Buy rather than the signal high, but you still have the first two parameters reversed. The condition comes first, and then the array, and then the optional instance number. :wink:

2 Likes

Hi. Can you elaborate this with a complete code. I have just started to learning "afl" and this is a very interesting topic, so it will help me a lot.