Dear all,
Good afternoon.
I do like the attached stop (THX) however I was wondering if one of you would be able to “modify/tweak” it as follows:
//In order to reduce gradually the distance between opened trade and initial stop,
After (n) bars, stop to move (n) pts Higher (if long), or lower (if short)
Of course, if the trade moves in the right direction i.e. 35pts, then this last one takes over to the gradually one…
Is my explanation clear ? Does this make sense for anyone ?
I would be delighted to share the results on any given strategy.
Thx you gents.
DJ
//trailing stop
trailingstop = 34
//resetting variables when no trades are on market
if not onmarket then
MAXPRICE = 0
MINPRICE = close
priceexit = 0
endif
//case SHORT order
if shortonmarket then
MINPRICE = MIN(MINPRICE,close) //saving the MFE of the current trade
if tradeprice(1)-MINPRICE>=trailingstop*pointsize then //if the MFE is higher than the trailingstop then
priceexit = MINPRICE+trailingstop*pointsize //set the exit price at the MFE + trailing stop price level
endif
endif
//case LONG order
if longonmarket then
MAXPRICE = MAX(MAXPRICE,close) //saving the MFE of the current trade
if MAXPRICE-tradeprice(1)>=trailingstop*pointsize then //if the MFE is higher than the trailingstop then
priceexit = MAXPRICE-trailingstop*pointsize //set the exit price at the MFE - trailing stop price level
endif
endif
//exit on trailing stop price levels
if onmarket and priceexit>0 then
EXITSHORT AT priceexit STOP
SELL AT priceexit STOP
endif
SET STOP ploss 62//20
SET TARGET pPROFIT 98//54
You can start to count the bars when you are onmarket.
If onmarket and init_timer[1]=0 then
Init_timer=barindex
Endif
Now, every bar you decrease the trailing distance by “number of bars” onmarket.
Priceexit=maxprice- trailingstop*pointsize - (barindex - init_timer)*pointsize
That’s the idea, maybe?
Thx Derek,
Looks good and I am sure it is…
But what about the OCO condition: Like if the position is going positive and crosses the 34 pts onbarclosed, a code might say cxl the timer somewhere ?
What do you think ?
Thx again 😉
Hey Inertia,
you can adapt the break-even code from the blog to start the trailing stop.
https://www.prorealcode.com/blog/learning/breakeven-code-automated-trading-strategy/
I guess the function for decreasing the trailing distance is dependent on your risk appetite, the type of strategy, like for example the initial distance etc…
You can get around the problem(?) of the barcounting variable by adding a second variable and replacing init_timer in the function for the priceexit.
If init_timer[1]>0 then
Priceexit= maxprice- trailingstop*pointsize - (your_personal_function_of_risk)*pointsize
Endif
Here is the code for including a predefined starting distance for the risk function in your exit order decision:
Starting_Distance = 34 //how much pips/points in gain to activate the risk function?
If close-tradeprice(1)>=starting_distance * pipsize then
Riskf = 1
Else
Riskf = 0
Endif
if onmarket and priceexit>0 and Riskf = 1 then
EXITSHORT AT priceexit STOP
SELL AT priceexit STOP
endif