Help on afl..partial booking

#1
Hi all

can someone help me with an afl code to partially exit my position in 2 stages..
a) 50% when criterion one is me .. like if stochastics is over bought
b) 50%when criterion b is met ..like price is below ema 13...
OR if i b is met before condition a being met the whole 100% position should be squared off.

i tried a combination of applystops and setpositionsize...but is not working .. may be because i am not doing it properly..I will be very grateful if someone can help me ... i dont have much knowledge of afl.. Thanks in advance..

regards
Hardit
 
#2
Hi i forgot to mention that this is only for back testing...my whole idea of using two exits is to avoid losing money on whipsaws..or at least minimize the losses on whipsaws... so basically what i am trying to do is book 50% of my position at a price which will be enough to make remaining 50% position free (even if i hit stop loss on it) and also cover brokerage...

so basically what i ideally need here is three things...
a) first exit at SL + points needed to cover broekrage
lets assume i want to keep the SL as previous low... and say it is 15 points away ... lets also assume i need 5 points to cover the brokerage... so my first exit will be buyprice + 15 +5 (viceversa on short positions)
b) now second 50% of the position will be free to run till the trend changes... so i want this to be exited at average crossover ..so may be when price comes below ema 13......
c) in case if the price goes below previous low or below ema without hitting the first target .. then i would like to square off the whole thing..

I imagine this would help avoiding whipsaws and will result in losses when the trade taken was absolutely wrong...

will really appreciate any help on this..... request the experts to help please...

thanks and regards
Hardit
 
#5
hmm thanks do or die.. i figured some kind of loop code is need...however as i indicated in my earlier post i am not good with this ..i tired a lot of things but nothing has helped me yet since i don't even know the basics of programming.. i can do simple stuff in afl but not sure if i can do this.. would be great if you can share the solution you got for ur issue or if u can suggest anything that fits my requirement, as in the post link i can only see what u wanted but not the solution :-(...

ur query is also very important in making a system...apart from the concern u mention i also think it is important to set maximum position size coz the Compounded annual rate of return of a system will be unduly high if we cannot fix the max traded quantity as the number of contracts traded will increase as the equity of the account increases..(i tested a simple system where in towards the end the auto analysis will trading nifty in thousands of lots,which i think is not a fair assumption) thus towards the end the trades will be huge and the profit or losses of later trades will have a bigger impact on the overall profit profile of the system...since i did not know a fix to this i simple limited the quantity to a single lot of nifty so that i can analyse the profit profile per lot ...

kindly let me know if u have the coding for partial booking .. thanks....
 

Rajvir

Active Member
#7
Read the artical below, and if you get any idea that how to exit positions in 2 steps, Pls let me know too.



//EXAMPLE For example to liquidate 50% of position simply use
SetPositionSize( 50, spsPercentOfPosition * ( Buy == sigScaleOut ) );

Special value spsNoChange (=0) means don't change previously set size for given bar (allows to write constructs like that):

SetPositionSize( 100, spsShares ); // 100 shares by default
SetPositionSize( 50, IIf( Buy == sigScaleOut, spsPercentOfPosition, spsNoChange ) ); // for scale-out use 50% of current position size


Example of code that exits 50% on first profit target, 50% on next profit target and everything at trailing stop:

Buy = Cross( MA( C, 10 ), MA( C, 50 ) );
Sell = 0;

// the system will exit
// 50% of position if FIRST PROFIT TARGET stop is hit
// 50% of position is SECOND PROFIT TARGET stop is hit
// 100% of position if TRAILING STOP is hit

FirstProfitTarget = 10; // profit
SecondProfitTarget = 20; // in percent
TrailingStop = 10; // also in percent

priceatbuy=0;
highsincebuy = 0;

exit = 0;

for( i = 0; i < BarCount; i++ )
{
if( priceatbuy == 0 AND Buy[ i ] )
{
priceatbuy = BuyPrice[ i ];
}

if( priceatbuy > 0 )
{
highsincebuy = Max( High[ i ], highsincebuy );

if( exit == 0 AND
High[ i ] >= ( 1 + FirstProfitTarget * 0.01 ) * priceatbuy )
{
// first profit target hit - scale-out
exit = 1;
Buy[ i ] = sigScaleOut;
}

if( exit == 1 AND
High[ i ] >= ( 1 + SecondProfitTarget * 0.01 ) * priceatbuy )
{
// second profit target hit - exit
exit = 2;
SellPrice[ i ] = Max( Open[ i ], ( 1 + SecondProfitTarget * 0.01 ) * priceatbuy );
}

if( Low[ i ] <= ( 1 - TrailingStop * 0.01 ) * highsincebuy )
{
// trailing stop hit - exit
exit = 3;
SellPrice[ i ] = Min( Open[ i ], ( 1 - TrailingStop * 0.01 ) * highsincebuy );
}

if( exit >= 2 )
{
Buy[ i ] = 0;
Sell[ i ] = exit + 1; // mark appropriate exit code
exit = 0;
priceatbuy = 0; // reset price
highsincebuy = 0;
}
}
}

SetPositionSize( 50, spsPercentOfEquity );
SetPositionSize( 50, spsPercentOfPosition * ( Buy == sigScaleOut ) ); // scale out 50% of position
 
#8
You can do it in very easy way. maintain one simple variable. let it be 'a'. set a=1. As you are exiting in two phase. Make it
iif(buy==1 and a==1,do this)
iif(buy==1 and a==2,do this)
ok? I hope you can do it now.


Hi all

can someone help me with an afl code to partially exit my position in 2 stages..
a) 50% when criterion one is me .. like if stochastics is over bought
b) 50%when criterion b is met ..like price is below ema 13...
OR if i b is met before condition a being met the whole 100% position should be squared off.

i tried a combination of applystops and setpositionsize...but is not working .. may be because i am not doing it properly..I will be very grateful if someone can help me ... i dont have much knowledge of afl.. Thanks in advance..

regards
Hardit
 
#10
Debhargya will this require running a loop.. cos if i simply put iif statements it isnt working..or may be there is something wrong with the statements itself..

a=0;
b=5;
IIf(Buy==1 AND a==0 AND Close<Ref(Low,-BarsSince(Buy)),a==1,a==0);
IIf(Buy==1 AND a==0 AND Close-BuyPrice>(Close-(Ref(Low,-BarsSince(Buy)))+b),a==2,a==0);
IIf(Buy==1 AND a==2 AND Cross(EMA(Close,Y),Close),a==3,a==0);
IIf(a==1,Sell=Close,False);
IIf(a==2 OR a==3,SetPositionSize(50,3),False);
IIf(a==2 OR a==3,Sell=Close,False);

is this close to what u are suggesting or am i still way off..

PS please pardon my inadequate skill of writing afl codes ... i am new to this .. never done any kind of programming in my life
 

Similar threads