How i set limit price buy within certain number of bar by AFL?


I want to set up limit price order, and i know how to write the AFL.

BuySignal = Cross( Close, MA(Close, 100 ) );

Buy = Ref( BuySignal, -1);
BuyLimitPrice = Ref( Close, -1) * 0.99;

Buy = Buy AND L < BuyLimitPrice;

BuyPrice = Min( Open, BuyLimitPrice );

However, all we know that, the limit price order may not be executed if the price did not push back little bit, if the “unplaced” limit price order still keep here, and when the market reverse, it may hit it and it’s not what i want.

Therefore, i want that limit price order will be cancel with certain number bar (e.g. 3 bars), if the limit price order could be executed within 3 bars, then great, if didn’t then the limit price order would be cancelled automatically. How can i write that AFL?

You did not specify what “bars” you mean. Minutes, seconds, hours? If you want to keep your order active just say for three 1-minute bars, you need to use intraday data and code for intraday.

Generally the code you posted, when applied to EOD data, would trigger anywhere during the day as soon as price drops below your limit price and it won’t trigger next bar (next day).

thx for reply, lets say 5 mins bar, and want my limit price order be active just 3 bars, afterward, the order will be cancelled, how i code it?

You can use Hold function for that. It will keep any condition valid for specified number of bars.

BTW: please follow forum rules. Place your post in proper category (you posted in AB instead of AFL programming where it belongs), use proper grammar and proper spelling. Formulate your questions clearly. Don’t leave room for guessing. If you expect people to answer you, show some respect by writing your question carefully, without spelling errors and without “thx”.
If you want to thank somebody, don’t be lazy and write proper “thank you” instead of “thx”. If your fingers hurt, and you really can not press those 9 keys, you can click on :heart: to send “like”.



Suppose in the above example, Buysignal happened to be on Monday(This could be any other day), and I want my "Buylimitprice" to be valid through Thursday. How can I code this ? From Friday onwards, I can have trades from the new setups. For example, on Monday ABC stock closed at 100 has a Buysignal and I want to buy at limit price of 95. I want "Buylimitprice" of 95 to be valid for 3 days and after 3 days new Buysignals can be considered for new positions if my position size allows.

Pardon me if I have repeated the query...



Can anyone provide some guidance on the above query ?


Buy = Hold(Buy, 3) AND L < BuyLimitPrice;

Thanks for the reply...

What above code does is calculates limit orders everyday for 3 days... What I would like to see is limit order for day 1 is valid through day 3... For example I want to buy XYZ stock at particular price only, say 95. If price come to 95 within 3 days, then only you have a position else move on to next signal...

I hope I have been able to clarify properly...