# How do i check the buy price and date of an open position?

What i am trying to do is i having a check of the gain of the position 20, 40 and 60 days after the position is opened (if it does not hit the exit trigger which in this case is the Close dropping below the Moving Average line). i tried looking back for the Open price to calculate but i realised i am only looking back from current day and not the looking forward from the date of the trade opening.

so how do i check 20, 40 and 60 days after the trade is opened to check on the gains? i tried using barssince() but i do not know what to put in between the brackets.

can anyone help me?

``````SetOption("InitialEquity", 10000);
SetOption("MaxOpenPositions", 20);
SetPositionSize(5, spsPercentOfEquity );

//SMA
MAline=MA(C, 200);

//profit taking optimisations
ptake1=Optimize("1 month",0,0,0.02,0.01);//optimise from 0% to 2%
ptake2=Optimize("2 months",0.02,0.02,0.05,0.01);//optimise from 2% to 5%
ptake3=Optimize("3 months",0.05,0.05,0.15,0.01);//optimise from 5% to 15%

//profit taking exits
pt1=((C-Ref(O,-19))/Ref(O,-19))<ptake1; //no gains within 20 trading days from position opening
pt2=((C-Ref(O,-39))/Ref(O,-39))<ptake2; //Under 2% gains within 40 trading days from position opening
pt3=((C-Ref(O,-59))/Ref(O,-59))<ptake3; //Under 5% gains within 60 trading days from position opening

SellPrice=Open;

Buy=C>MAline AND O>MAline; //Candle open and close above 200 SMA
PositionScore=roc( CLOSE, 100 );

Sell= C<MAline OR pt1 OR pt2 OR pt3;//
``````

You might try this, replicate for pt2 and pt3.
`pt1 = IIf(BarsSince(Buy) == 19 AND 100 * (O / ValueWhen(Buy, O, 1) > ptake1, True, False);`

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.