I am new to Amibroker and AFL coding. I am trying to code for the below
Short: In a 5 Min candle, When ADX 14 crosses +DI, sell 100 Stocks. I have made setting to 5 minutes interval for Back testing.
Cover: Stoploss @ 1% or Profit @ 2%.
marketstarttime = 091500;
IBendtime = 101500;
tradestarttime = 124500;
tradeendtime = 144500;
sqofftime = 144500;
Short= Cross (ADX(14),PDI(14)) AND
TimeNum() >= tradestarttime AND TimeNum() <=tradeendtime;
Cover = ApplyStop( stopTypeLoss, stopModePercent, 1 ) OR // 1% max loss from short/buy price
ApplyStop( stopTypeProfit, stopModePercent, 2 ) OR // 2% profit target from short/buy price
TimeNum() >= sqofftime;
Moderator comment: added required code tags
I am not getting the desired results and I know some where I am making some mistakes at code level. Could you please guideme?
First of all, ApplyStop() does NOT return any value so it is incorrect to use it in "OR" / "AND" statements. It is instruction ("switch") for the backtester. It just turns ON stops during backtest. The stop exits are added to existing rules so they work like "OR" without actually writing "OR" statements.
For proper usage examples see http://www.amibroker.com/f?applystop
See also https://forum.amibroker.com/search?q=applystop
Secondly please use code tags.
Thanks. It will be a great help If I get a sample AFL for Intraday. I can use it as a template for my strategies, not able to find any from the forum.
The code for intraday does not really differ from any other code. Rules are rules. If you rule is that ADX 14 crosses +DI that rule would work the same regardless of interval. And the code for it would be identical regardless of interval.
Periodicity (Interval) is controlled from SETTINGS window, not from AFL: http://www.amibroker.com/guide/w_settings.html
I have maintained the Intra day settings as per the screen shots. I am using the below AFL:
Short= Cross (DEMA(C,35),DEMA(C,15))
AND BOP < 0;
Cover = Cross (DEMA(C,15),DEMA(C,35));
But the results show Entry date in one date and Exit date in another date. For intraday, it will be squaredoff as per the close price for the day if doesn't satisfy the condition for cover. Please advise.
First and foremost follow this: How do I debug my formula? to debug your own code. The time when trades occur depend on YOUR code and YOUR data. You are responsible for both.
As to the settings, your own screenshots show that you currently have Periodicity: 15-minute selected in Analysis settings, not 5-minute as you wrote in original post.
This is the Window that matters (copy of your own screenshot)
Also quite obviously you actually must have those 15-minute data in the database. If you have end-of-day database, merely changing the setting does not magically create 15-minute data.
I have intraday data for the stocks that I am doing back testing.
For Intraday, I have maintained the required setting as per the screen shots shared earlier. But the back testing results show the date of Short as one date (let us say 25.06.2019) and Ex.date as another date (let us say 26.06.2019). For intraday, Short and cover should be done on the same day. If I sell a stock (Short), it should be bought on the same day either by the condition maintained in the AFL or by close price of the day. Do we need to code to buy the stock on close price of the day as well?