Help on afl..partial booking

#21
aditya after reading both your posts i tried to combine both the posts .. i have arrived at some set of codes however they are not working ...for some reason my i am unable to send you a pm it is blocked or something.. i have written to the admin but till then here is what i have..

//i have taken exmaple of this simple ema cross over signal system.
//lets Say Buy is at EMA Cross over AND the Sell is at the condidionts mentioned below...its just that this is NOT generating any trades at all
//i am trying to generate multiple sell signals for one buy signal.. ie multiple ways in which the position can be exited..and in multiple portions
//what i am trying here is to exit 50% at points enough to make my position breakeven ..even if after first exit the rest hits stoploss
//second i want the rest 50% to run free till the trend is over.. in this case i am judging the trend by ema cross over..
//third thing if the price goes below stop loss before even achieving the first target then book loss on whole position
//can you please correct this and point the mistakes so that i know where i was wrong...i should say where all i was wrong ;-)


SetPositionSize(100,4);//to analyse the system on trading just 2 lots..
Buy= Cross(Close,EMA(Close,34));
Sell=0;

lastBuyPrice = ValueWhen(Buy,BuyPrice,1);
brokeragePoints=5;
stopLossInPoints = abs(LastBuyPrice - Ref(Low,-BarsSince(Buy))); //as i want the stop loss to be the low of the bar i bought at.
FirstExitPrice = LastBuyPrice + stopLossInPoints + brokeragePoints;//earning sl + brokerage will make rest 50% position free

doScaleOut1 = Buy==1 AND Cross(Close,FirstExitPrice);//first 50% to be sold at this price
doscaleout2=Buy==1 AND Cross(EMA(Close,34),Close); // second 50% to be sold at this price..this is to allow the trade to capture the whole move...in this case let assume the second exit is at ema,price cross over

Sell = IIf( doScaleOut1 OR doscaleout2, sigScaleOut, Sell);
PosSizeScaleOut = 50;
SetPositionSize(IIf( Sell == sigScaleOut , PosSizeScaleOut, 100 ), spsPercentOfPosition); //i have replaces flase with 100 thinking that the position should be exited 100% in event of stoploss hit

ApplyStop(stopTypeLoss, stopModePoint, stopLossInPoints); //this is in case the stop loss hits..

thanks for the help and guidance ..

hi hardit, on giving a glance at your code, I found several errors (i stopped counting once my fingers weren't sufficient).

1. first I told you already, it will always be Buy = sigScaleOut and never ever Sell.

2. variable names are case sensitive, please check in the code, i find few instances of that

3. when you say doScaleOut1 = Buy ==1 and ..... , now this is ill-logical, because buy will be 1 only on the bar when there was original buy signal, and the buy signal will not remain 1 , so above line is actually trying to scale out in the same bar ??? that is why I had introduced InTrade variable, use InTrade variable instead of Buy inside that line

4. same for doScaleOut2

5. best way to debug would be to print the values of the variable using writeval() and see them bar by bar , because I suspect your calculation of stopLossInPoints and FirstExitPrice doesn't make logical sense, especially the part Ref(Low,-BarsSince(Buy)) , you know that this will give you the low of the bar in which buy took place and it will be constant till next buy ??
if this is what you wanted then you should rather use ValueWhen(Buy, Low,1);

do one thing
just save the following code in a fresh afl and plot it as indicator

Buy= Cross(Close,EMA(Close,34));
Sell=0;

lastBuyPrice = ValueWhen(Buy,BuyPrice,1);
xx = Ref(Low,-BarsSince(Buy));
stopLossInPoints = abs(LastBuyPrice - xx);

Plot(C, "c", colorBlack, styleCandle);
Plot(Buy, "buy", colorBlue, styleLine | styleOwnScale);
Plot(lastBuyPrice , "lastBuyPrice ", colorRed, styleLine);
Plot(xx, "xx", colorGreen, styleLine);

and observe the lines, add other criteria and observe the behaviour and make sure that the logic which you are trying to incorporate is correct.

all the best and god speed
 
#22
hmm okay .. since i last posted i has already correct few out of the mentioned previous.. like buy not sell and intrade not b==1 :)...

for Ref(Low,-BarsSince(Buy)) i switched it to Ref(Low,-BarsSince(Buy)-1) as i wanted the low of the bar prior the the bar where buy is triggered (basically thought this is what it ll give)..but ya valuewhen would have been simpler
i ll run the code and observe.. thanks !!!!
i hope i am not pestering too much.. cant thank you enough for the guidance..
 
#23
thanks a lot Aditya for all your help.. and the plot function was a great help... all my statements were indeed giving what i wanted but i think the mistake is that i am putting sell=0;

correct me if i am wrong in understanding what scale out does.. scale out is just to exit a part of the position and is not the primary sell in itself.. so if i do not have any sell condition or an applystoploss only one trade (open) will be generated..right...

hope i got it right..
 
#24
thanks a lot Aditya for all your help.. and the plot function was a great help... all my statements were indeed giving what i wanted but i think the mistake is that i am putting sell=0;

correct me if i am wrong in understanding what scale out does.. scale out is just to exit a part of the position and is not the primary sell in itself.. so if i do not have any sell condition or an applystoploss only one trade (open) will be generated..right...

hope i got it right..
Yes you got it right ! (but becareful, if the sigScaleOut closes 100% of the position, then it equivalently becomes sell signal (ofcourse it would be meaning less to use sigScaleOut in that way, but sigScaleOut can occur multiple times and slowly close the position out)

I am not sure if you are aware of this, but in the settings you have the option to see the detailed log, when you enable it, you will get the bar by bar details during backtest and what signals were generated, how much fund was available and how much position was entered (and if not then why not !)

also the best way to approach system building will be bottoms up, that is first put the simplest version, see the results, check the balance, equity, and if everything fine, keep adding more category one step at a time,
believe me, this may take few hours more but will save lot of your hair !!!

enjoy coding and have fun.
RIP the Father of Coding - Dennis Ritchie
 
#25
Hi Aditya i just look that auto analysis result generated and then plot all the signlas on the chart and try to figure out if the system is working right.. i ll try to find the setting you have taked about .. it should be very useful..

Planning to start reading Introduction to Amibroker this weekend..thanks for help and guidance..
 
#26
Hi Aditya i just look that auto analysis result generated and then plot all the signlas on the chart and try to figure out if the system is working right.. i ll try to find the setting you have taked about .. it should be very useful..

Planning to start reading Introduction to Amibroker this weekend..thanks for help and guidance..
hi hardit, did u find the soln?partial bookin afl code?if yes pls post
 
#27
Can anyone help i need afl if suppose we buy A at 100 booked 102 then if we use EXREM the until short signal came it is not giving fresh buy....but as I booked at 102 if fresh buy came it should generate buy again.Please help with afl anyone please.
 

Similar threads