Need help with Inside bar breakout AFL i am trying--new to coding

Hi brothers, very happy to join the community here. I am quite new to coding and amibroker. After purchasing AB, I read user manual once, and introduction to Amibroker by Howard Bandy. But did not understand around 40% of it, as its my first exposure to coding world. Nevertheless i believe i have a simple strategy so went ahead to test my code-writing ability. Since its my first attempt, i understand i may have made many mistakes. If anyone can kindly correct me it would be a great help, Thank you in advance.

What I want to tell amibroker--In 15 min Timeframe, If i get a babycandle [low is higher than prev candle and high is lower than that of prev candle], I would like to enter long on cross of high of baby candle and/or enter short on crossing of low of babycandle, with stoploss at baby candle's opposite end and with a profit target of 2 times the risk taken.

My code follows:

_SECTION_BEGIN ("criteria");

babycandle=Ref(H,-1)<Ref(H,-2) AND Ref(L,-1)>Ref(L,-2);

Breakout=Cross(C, babycandle);



Sell=BuyPrice-risk OR buytarget;
Cover=SellPrice+risk OR shorttarget;

SetTradeDelays (0,0,0,0)

1 Like

Only for back test.

_SECTION_BEGIN ("criteria");

babycandle=Ref(H,-1)<Ref(H,-2) AND Ref(L,-1)>Ref(L,-2);

Breakout= High > Ref(H,-1) AND babycandle;
Breakdown= Low < Ref(Low, -1) AND babycandle;

High_At_babycandle = ValueWhen(babycandle, High);
Low_At_babycandle = ValueWhen(babycandle, Low);
SL_Point = High_At_babycandle - Low_At_babycandle;


Sell = Cover = 0;

ApplyStop(stopTypeLoss,stopModePoint,  SL_Point);
ApplyStop(stopTypeProfit,stopModePoint,  SL_Point*2);

SetTradeDelays (0,0,0,0) ;

1 Like

Thank you @Yogyatrader for the prompt correction. Learnt some things from the code :ok_hand:. So also, I wanted to enter right when the price is 'crossing' babycandle's high or low and not when the candle closes or not when the next candle opens. But in your code i think you programmed to enter when the third candle [First is mother candle, second is baby candle and third is the current candle, fourth seems to be the entry candle in your code] closes, or fourth candle opens. Could you please correct me if i am wrong and do necessary change for the criteria mentioned? Thanks in advance :+1:

@Uddhav your code is identifying the Inside Bar (your "baby candle") one bar after it has occurred. You could code it differently to make the identification at the close of the actual Inside Bar.

ib 			= Inside();
ib_HIGH 	= ValueWhen( ib, High ); // will continue to be same value until a new inside bar occurs
ib_LOW 		= ValueWhen( ib, Low );


If you prefer a graphic presentation, your babycandle is where the circles are whereas the "ib" is the up arrows.

Or if you prefer your code method, perhaps

babycandle = H < Ref( H, -1 ) AND Low > Ref( L, -1 );

As for your placing an order before the Close it seems to me you want to place a Limit Order for crossing the Inside Bar prices and you can read about Limit Order's in the Knowledge Base


Dear @portfoliobuilder I understood the right way of identifying an inside bar from you. Also your graphical and tabular representation helped me visualise what happens behind the scenes in an array actually and how we can help AB identify which scalar value we have in mind :heart_eyes:. Yes, I read about the inside() function, but could not use it properly, so tried to do it differently. I will go through the knowledge base link you shared to understand limit order.

As for limit placing orders before price breaches high or low of babycandle, yes in equity that would help. Whereas, if we are getting signal in underlying instrument and would want an option or future to be traded at that time of breakout/down, then we would not know exactly which value to set as limit order because of the unpredictable behaviour based on option-greeks. Therefore, in that case, we send a market order [or a limit order with 1% margin, to be safe] to an ATM option contract, exactly when baby candle's boundary is breached. And based on the price it got filled, we have to set the Stoploss and profit target. Well, this is the idea. But whether Amibroker can recognise the price at which the order got filled or not, from the interactive broker--I do not know yet :thinking:. Please enlighten me on the same. Thank you in advance.

1 Like

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