Trailing stop / Coder needed ;)
Forums › ProRealTime English forum › ProOrder support › Trailing stop / Coder needed ;)
- This topic has 4 replies, 2 voices, and was last updated 7 years ago by
Derek.
-
-
09/18/2017 at 5:38 PM #46529
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 stop12345678910111213141516171819202122232425262728293031323334//trailing stoptrailingstop = 34//resetting variables when no trades are on marketif not onmarket thenMAXPRICE = 0MINPRICE = closepriceexit = 0endif//case SHORT orderif shortonmarket thenMINPRICE = MIN(MINPRICE,close) //saving the MFE of the current tradeif tradeprice(1)-MINPRICE>=trailingstop*pointsize then //if the MFE is higher than the trailingstop thenpriceexit = MINPRICE+trailingstop*pointsize //set the exit price at the MFE + trailing stop price levelendifendif//case LONG orderif longonmarket thenMAXPRICE = MAX(MAXPRICE,close) //saving the MFE of the current tradeif MAXPRICE-tradeprice(1)>=trailingstop*pointsize then //if the MFE is higher than the trailingstop thenpriceexit = MAXPRICE-trailingstop*pointsize //set the exit price at the MFE - trailing stop price levelendifendif//exit on trailing stop price levelsif onmarket and priceexit>0 thenEXITSHORT AT priceexit STOPSELL AT priceexit STOPendifSET STOP ploss 62//20SET TARGET pPROFIT 98//5409/18/2017 at 6:14 PM #46531You can start to count the bars when you are onmarket.
12345If onmarket and init_timer[1]=0 thenInit_timer=barindexEndifNow, every bar you decrease the trailing distance by “number of bars” onmarket.
1Priceexit=maxprice- trailingstop*pointsize - (barindex - init_timer)*pointsizeThat’s the idea, maybe?
09/18/2017 at 6:52 PM #46533Thx 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 😉
09/19/2017 at 9:37 PM #46628Hey 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.
1234If init_timer[1]>0 thenPriceexit= maxprice- trailingstop*pointsize - (your_personal_function_of_risk)*pointsizeEndif1 user thanked author for this post.
09/19/2017 at 10:04 PM #46629Here is the code for including a predefined starting distance for the risk function in your exit order decision:
123456789101112Starting_Distance = 34 //how much pips/points in gain to activate the risk function?If close-tradeprice(1)>=starting_distance * pipsize thenRiskf = 1ElseRiskf = 0Endifif onmarket and priceexit>0 and Riskf = 1 thenEXITSHORT AT priceexit STOPSELL AT priceexit STOPendif -
AuthorPosts
Find exclusive trading pro-tools on