Help with AFL for Parabolic SAR - Beginner

#1
Hi
I'm beginning to learn programming with AFL.

I tried to code a basic stop-and-reverse system which buys and sells on price crossing over SAR. I think I may have done some error in the coding, because instead of Buying and Selling on the crossover of the price on the SAR 'dot', the system buys and sells on the closing price of that bar.

The following is the AFL code I wrote -

-------------------------------------------------------------------
AF=0.02;
AF=Optimize("OAF",0.02,0.01,0.2,0.01);
VSARValue=SAR(AF,0.2);

Buy=Cross(H,VSARValue);
Sell=Cross(VSARValue,L);
Short=Sell;
Cover=Buy;

AddColumn(SAR(0.02),"SAR",1.2);
----------------------------------------------------------------------

I have gone through the book by Howard Bandy - Introduction to Amibroker. I'm trying to code some simple AFLs to begin with. Any help with this code, and suggestions on how to get better at AFL programming will be of great help.

Thanks.
 

iamaaditya

Active Member
#2
May be this post is few months late and apologies that no one replied on it.

FWIW (may be someone looking for same problem)

By default the buy price and sell price are set as "C" of the same candle (if trade delay is 0) or the candle of the trade delay added.
See, AA->>Settings (window) ->> Trades (Tab)

you can however change it by assigning it manually i.e in your case add the following lines :

BuyPrice = VSARValue;
SellPrice = VSARValue;

don't worry it will not affect those bars where buy and sell signal aren't present (though technically the arrays BuyPrice and SellPrice would always be filled with VSARValue)

Not technically but practically you should ask if this is tradable, in real world in such cases there will always be some slippage (personal experience) and slippage being random it will be very hard to backtest it )
**you can add the slippage to buyprice, if you know what your slippage mean would be , (again this would not be perfect but atleast something,
 

Similar threads