How to have multiple simultaneous open position in 'Same' security in amibroker.

abcpankaj

Active Member
#1
hi
I am trying to backtest a system which buys on certain condition being fulfilled.
After taking first open position it should take new position again & again for each entry signal (even if earlier positons are still open).
amibroker currently waits for first position to close before taking new position.
Any clues.
Thanks in advance.

What dint work :
setting position size & max open position (they seem to work on a portfolio & i am trying to take position in same security)
I also tried to 'fool' amibroker by adding same security multiple times in a group by different names & doing portfolio backtest on the group.but to no avail.
 

abcpankaj

Active Member
#3
Thanks Aditya for your reply. It worked like a charm.

I am using applystop (stopTypeLoss/stopTypeProfit ) to get out of the trade.on using scaling these stoploss settings considers values of first entry & sells 'all' scaled in positions when stoploss is hit.
Can amibroker be configured to consider values of a)Last scaled in position b) average of all open positions for stoploss calculation .

Or can each scaled in position have its own stoploss as per its own buy price.


any clues
Thanks in advance.
 

iamaaditya

Active Member
#4
Unfortunately the applystop will close complete positionsize of the open position and there is no way to limit that to only some part but there is a nice solution to this problem.

solution is not to use applystop but to scale out, lets say you want to scale out half of the position with stoploss of 5% then

e = Equity( 1 );
PcntProfit = 100 * ( e - ValueWhen( Buy, e ) ) / ValueWhen( Buy, e );
//here PcntProfit will give the Profit % till now in the trade

stopLoss = 5; // change it to any value
InTrade = Flip( Buy, Sell );
doScaleOut = InTrade AND Cross(stopLoss, PcntProfit) ;
//above Intrade is required to remove unnecessary calculations when we are not in trade
//and the cross represents that PcntProfit has moved below stopLoss value
Buy = IIf( doScaleOut, sigScaleOut, Buy);
//I am sure you get the meaning of above line
PosSizeScaleOut = 50; //change to any % (of Position size)
SetPositionSize(IIf( Buy == sigScaleOut , PosSizeScaleOut, False ), spsPercentOfPosition);

//kindly note above Position size has been used only for sigScaleOut, generally when i use I write a combine statement which caters for both original buy position size and scale out position size
for e.g
PosSizeBuy = 30;
PosSizeScaleOut = 50;

SetPositionSize( IIf( Buy == True , PosSizeBuy ,
IIf( Buy == sigScaleOut, PosSizeScaleOut , False ) ),
IIf( Buy == True, spsPercentOfEquity,
IIf( Buy == sigScaleOut, spsPercentOfPosition, False ) ) );


let me know if this was clear , else will show you with full code e.g
 
#5
hi iamaditya

i am trying to achieve something on the similar lines.. and i should say i do not know much about coding . i tried to modify the code you have written to suit my requirement (basically need an exit in terms of points instead of percentage)

here is what i have

e = Equity( 1 );
Profit = e - ValueWhen( Buy, e );
//here PcntProfit will give the Profit % till now in the trade


p=100;
stopLoss = BuyPrice-Ref(Low,-(BarsSince(Buy)+1)); // change it to any value
InTrade = Flip( Buy, Sell );
doScaleOut = InTrade AND Cross(stopLoss*P, Profit);

//above Intrade is required to remove unnecessary calculations when we are not in trade
//and the cross represents that PcntProfit has moved below stopLoss value
Buy = IIf( doScaleOut, sigScaleOut, Buy);
//I am sure you get the meaning of above line
PosSizeScaleOut = 50; //change to any % (of Position size)
SetPositionSize(IIf( Buy == sigScaleOut , PosSizeScaleOut, False ), spsPercentOfPosition);

this is using the code u given above...what i would idealy want is 100% exit if SL is hit or 50% exit at a price which equals buyprice+SL ie when the position has made profit equal to SL.. thus if i book 50% at this level rest 50% becomes free to run..


kindly suggest how i can achieve that .. secondly how can i set the rest 50% to be exited when price crosses over and ema..

my orignal query is on this post ...http://www.traderji.com/amibroker/60597-help-afl-partial-booking.html
it would be a great help if you can have a look at it and guide me..the link explains what i am trying to achieve..


thanks
regards
hardit
 

iamaaditya

Active Member
#6
sorry, got busy, hence couldn't reply at earliest

please note that BuyPrice is simple price array (which stores by default close, or any value set in parameters window, with the delay size given) and it changes on everybar (thus if you think that buyprice will hold the price of the last buy (until another buy occurs then that is wrong)

rather you should use something like

lastBuyPrice = ValueWhen(Buy,BuyPrice,1);

for the effects which you want which is rather very simple, use the applystop as given in help guide without any change (since you want to exit 100% of position, which is default behaviour)

regarding the 50% exit on reaching the target (i.e buyPrice + SL), then for that...

e = Equity( 1 );
ProfitInPoints = e - ValueWhen( Buy, e );

lastBuyPrice = ValueWhen(Buy,BuyPrice,1);
stopLossInPoints = 20; //here 20 represents points (since you want to use points)

profitTarget = lastBuyPrice + stopLossInPoints ;

InTrade = Flip( Buy, Sell );
doScaleOut = InTrade AND Cross(ProfitInPoints, profitTarget);


//below code is for 50% exit if profit reaches the ProfitInPoints
Buy = IIf( doScaleOut, sigScaleOut, Buy);
PosSizeScaleOut = 50; //change to any % (of Position size)
SetPositionSize(IIf( Buy == sigScaleOut , PosSizeScaleOut, False ), spsPercentOfPosition);

ApplyStop(stopTypeLoss, stopModePoint, stopLossInPoints); // this is for your 100% exit is SL reached
P.S: I didn't run it in AB, so please check for syntax errors, also if it doesn't work out, after posting your reply here, please buzz me by PM, this will get my attention so that you don't have to wait again.
 
#7
thanks a lot aditya...i will try and get my head around this .. and get back to you.. i have never done any kind of programming ever..so will need some time to understand all that you have suggested..

thanks for being considerate to let me pm you ... really appreciate the help...

regards
hardit
 
#8
SetPositionSize(IIf( Buy == sigScaleOut , PosSizeScaleOut, False ), spsPercentOfPosition);

ApplyStop(stopTypeLoss, stopModePoint, stopLossInPoints); // this is for your 100% exit is SL reached
Hi iamaaditya
with the solution that you give here you are using both technical sigScaleOut and ApplyStop()....
you mean we can use both of them sigScaleOut and ApplyStop() at the same time?

and what backtest we have to run in AA analysis window? portofolio or...?
 

iamaaditya

Active Member
#9
Hi iamaaditya
with the solution that you give here you are using both technical sigScaleOut and ApplyStop()....
you mean we can use both of them sigScaleOut and ApplyStop() at the same time?

and what backtest we have to run in AA analysis window? portofolio or...?

hi boufalo,

yes, both will be there, because sigScaleOut is working as Pyramiding (scaling out) on some condition while the ApplyStop is a hard stop and acts as Stop Loss.

if you are running this strategy for multiple instruments (and you want to have portfolio created amongst them) then you have to use Portfolio backtest only but if it is for single instrument (or multiple instruments but each being tested independently) then you can use either one.
 
#10
also note that ApplyStop will close all the open position (so if this occurs first it will close 100% of open position or if this occurs after sigScaleOut then whatever is left will be closed out)
 

Similar threads