BreakEven & Trailing Profit: complete function
Forums › ProRealTime English forum › ProOrder support › BreakEven & Trailing Profit: complete function
- This topic has 119 replies, 22 voices, and was last updated 7 months ago by robertogozzi.
Tagged: BreakEven, Profit, stop, trailing, trailingstop
-
-
01/21/2021 at 3:40 PM #158837
Yes, you have to.
1 user thanked author for this post.
01/21/2021 at 5:26 PM #158841One more thing: to use high/low instead of close, is this right?
123456IF LongOnMarket THENq1 = PositionPrice + ((high - PositionPrice) * ProfitPerCent) //calculate new SLSellPriceX = max(max(SellPriceX,SellPrice),q1)SellPrice = max(max(SellPriceX,SellPrice),PositionPrice + (y1 * pipsize)) //set exit price to whatever grants greater profits, comopared to the previous oneELSIF ShortOnMarket THENr1 = ((PositionPrice - low) * ProfitPerCent) //calculate new SL01/21/2021 at 5:52 PM #158844Yes, that’s correct, but you’ll have to replace CLOSE also at lines 44 (X1) and 56 (X2).
You have to keep CLOSE, instead, after line 68 when it deals with minimum distance required by the broker, which must ALWAYS be computed from current price (CLOSE).
1 user thanked author for this post.
01/21/2021 at 6:19 PM #158848To make it available to anyone, I am posting the code updated with the choice of using either CLOSE or HIGH/LOW (line 1):
Trailing Stop with accumulation and choice between Close and High/Low123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116ONCE UseCLOSE = 1 //1=use CLOSE, 0=use High/LowSrcH = close //defaults to CLOSESrcL = close //defaults to CLOSEIF UseCLOSE = 0 THENSrcH = highSrcL = lowENDIFDirectionSwitch = (LongOnMarket AND ShortOnMarket[1]) OR (LongOnMarket[1] AND ShortOnMarket) //True when there's been a change in the direction (likely to be due to a Stop & Reverse)IF Not OnMarket OR DirectionSwitch THEN//// when NOT OnMarket or thare's been a change in direction, reset values to their default settings//TrailStart = 30 //30 Start trailing profits from this pointBasePerCent = 0.200 //20.0% Profit percentage to keep when setting BerakEvenStepSize = 10 //10 Pip chunks to increase PercentagePerCentInc = 0.100 //10.0% PerCent increment after each StepSize chunkRoundTO = -0.5 //-0.5 rounds always to Lower integer, +0.4 rounds always to Higher integer, 0 defaults PRT behaviourPriceDistance = 7 * pipsize //7 minimun distance from current pricey1 = 0 //reset to 0y2 = 0 //reset to 0ProfitPerCent = BasePerCent //reset to desired default valuePositionCount = 0SellPrice = 0ExitPrice = 9999999ELSE//------------------------------------------------------// --- Update Stop Loss after accumulating new positions//------------------------------------------------------PositionCount = max(PositionCount,abs(CountOfPosition))//// update Stop Loss only when PositionCount has changed (actually when increased, we don't move it if there's been some positions exited)//IF PositionCount <> PositionCount[1] AND (ExitPrice + SellPrice) <> 9999999 THEN //go on only if Trailing Stop had already started trailingIF LongOnMarket THENq1 = PositionPrice + ((SrcH - PositionPrice) * ProfitPerCent) //calculate new SLSellPriceX = max(max(SellPriceX,SellPrice),q1)SellPrice = max(max(SellPriceX,SellPrice),PositionPrice + (y1 * pipsize)) //set exit price to whatever grants greater profits, comopared to the previous oneELSIF ShortOnMarket THENr1 = ((PositionPrice - SrcL) * ProfitPerCent) //calculate new SLExitPriceX = min(min(ExitPriceX,ExitPrice),r1)ExitPrice = min(min(ExitPriceX,ExitPrice),PositionPrice - (y2 * pipsize)) //set exit price to whatever grants greater profits, comopared to the previous oneENDIFENDIF// --- Update ENDENDIF//IF LongOnMarket AND SrcH > (TradePrice + (y1 * pipsize)) THEN //LONG positions//// compute the value of the Percentage of profits, if any, to lock in for LONG trades//x1 = (SrcH - tradeprice) / pipsize //convert price to pipsIF x1 >= TrailStart THEN // go ahead only if N+ pipsDiff1 = abs(TrailStart - x1) //difference from current profit and TrailStartChunks1 = max(0,round((Diff1 / StepSize) + RoundTO)) //number of STEPSIZE chunksProfitPerCent = BasePerCent + (BasePerCent * (Chunks1 * PerCentInc)) //compute new size of ProfitPerCentProfitPerCent = max(ProfitPerCent[1],min(100,ProfitPerCent)) //make sure ProfitPerCent doess not exceed 100%y1 = max(x1 * ProfitPerCent, y1) //y1 = % of max profitENDIFELSIF ShortOnMarket AND SrcL < (TradePrice - (y2 * pipsize)) THEN //SHORT positions//// compute the value of the Percentage of profits, if any, to lock in for SHORT trades//x2 = (tradeprice - SrcL) / pipsize //convert price to pipsIF x2 >= TrailStart THEN // go ahead only if N+ pipsDiff2 = abs(TrailStart - x2) //difference from current profit and TrailStartChunks2 = max(0,round((Diff2 / StepSize) + RoundTO)) //number of STEPSIZE chunksProfitPerCent = BasePerCent + (BasePerCent * (Chunks2 * PerCentInc)) //compute new size of ProfitPerCentProfitPerCent = max(ProfitPerCent[1],min(100,ProfitPerCent)) //make sure ProfitPerCent doess not exceed 100%y2 = max(x2 * ProfitPerCent, y2) //y2 = % of max profitENDIFENDIF//------------------------------------------------------------------------------// manage actual Exit, if needed//------------------------------------------------------------------------------IF y1 THEN //Place pending STOP order when y1 > 0 (LONG positions)SellPrice = max(SellPrice,Tradeprice + (y1 * pipsize)) //convert pips to price//// check the minimun distance between ExitPrice and current price//IF abs(close - SellPrice) > PriceDistance THEN//// place either a LIMIT or STOP pending order according to current price positioning//IF close >= SellPrice THENSELL AT SellPrice STOPELSESELL AT SellPrice LIMITENDIFELSE////sell AT MARKET when EXITPRICE does not meet the broker's minimun distance from current price//SELL AT MarketENDIFENDIFIF y2 THEN //Place pending STOP order when y2 > 0 (SHORT positions)ExitPrice = min(ExitPrice,Tradeprice - (y2 * pipsize)) //convert pips to price//// check the minimun distance between ExitPrice and current price//IF abs(close - ExitPrice) > PriceDistance THEN//// place either a LIMIT or STOP pending order according to current price positioning//IF close <= ExitPrice THENEXITSHORT AT ExitPrice STOPELSEEXITSHORT AT ExitPrice LIMITENDIFELSE////ExitShort AT MARKET when EXITPRICE does not meet the broker's minimun distance from current price//EXITSHORT AT MarketENDIFENDIF6 users thanked author for this post.
01/22/2021 at 11:47 AM #158898Sorry guys, I had to change the above code:
- I had forgotten to replace TradePrice with PositionPrice in some lines
- I commented out line 34 and all references to PositionCount and added line 35 to use PositionPrice, instead, because it is most reliable in telling us there’s been a change.
I am also attaching the ITF file.
Trailing Stop with Accumulation of Positions and option to use of High/Low instead of Close123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118//------------------------------------------------------------------------------------------------------------------------------------------------ONCE UseCLOSE = 1 //1=use CLOSE, 0=use High/LowsrcH = close //defaults to CLOSEsrcL = close //defaults to CLOSEIF UseCLOSE = 0 THENsrcH = highsrcL = lowENDIFDirectionSwitch = (LongOnMarket AND ShortOnMarket[1]) OR (LongOnMarket[1] AND ShortOnMarket) //True when there's been a change in the direction (likely to be due to a Stop & Reverse)IF Not OnMarket OR DirectionSwitch THEN//// when NOT OnMarket or thare's been a change in direction, reset values to their default settings//TrailStart = 30 //30 Start trailing profits from this pointBasePerCent = 0.200 //20.0% Profit percentage to keep when setting BerakEvenStepSize = 10 //10 Pip chunks to increase PercentagePerCentInc = 0.100 //10.0% PerCent increment after each StepSize chunkRoundTO = -0.5 //-0.5 rounds always to Lower integer, +0.4 rounds always to Higher integer, 0 defaults PRT behaviourPriceDistance = 7 * pipsize //7 minimun distance from current pricey1 = 0 //reset to 0y2 = 0 //reset to 0ProfitPerCent = BasePerCent //reset to desired default value//PositionCount = 0SellPrice = 0ExitPrice = 9999999ELSE//------------------------------------------------------// --- Update Stop Loss after accumulating new positions//------------------------------------------------------//PositionCount = max(PositionCount,abs(CountOfPosition))//// update Stop Loss only when PositionPrice has changed (actually when increased, we don't move it if there's been some positions exited)////IF PositionCount <> PositionCount[1] AND (ExitPrice + SellPrice)<>9999999 THEN //go on only if Trailing Stop had already started trailingIF PositionPrice <> PositionPrice[1] AND (ExitPrice + SellPrice) <> 9999999 THEN //go on only if Trailing Stop had already started trailingIF LongOnMarket THENq1 = PositionPrice + ((srcH - PositionPrice) * ProfitPerCent) //calculate new SLSellPriceX = max(max(SellPriceX,SellPrice),q1)SellPrice = max(max(SellPriceX,SellPrice),PositionPrice + (y1 * pipsize)) //set exit price to whatever grants greater profits, comopared to the previous oneELSIF ShortOnMarket THENr1 = ((PositionPrice - srcL) * ProfitPerCent) //calculate new SLExitPriceX = min(min(ExitPriceX,ExitPrice),r1)ExitPrice = min(min(ExitPriceX,ExitPrice),PositionPrice - (y2 * pipsize)) //set exit price to whatever grants greater profits, comopared to the previous oneENDIFENDIF// --- Update ENDENDIF//IF LongOnMarket AND srcH > (PositionPrice + (y1 * pipsize)) THEN //LONG positions//// compute the value of the Percentage of profits, if any, to lock in for LONG trades//x1 = (srcH - PositionPrice) / pipsize //convert price to pipsIF x1 >= TrailStart THEN // go ahead only if N+ pipsDiff1 = abs(TrailStart - x1) //difference from current profit and TrailStartChunks1 = max(0,round((Diff1 / StepSize) + RoundTO)) //number of STEPSIZE chunksProfitPerCent = BasePerCent + (BasePerCent * (Chunks1 * PerCentInc)) //compute new size of ProfitPerCentProfitPerCent = max(ProfitPerCent[1],min(100,ProfitPerCent)) //make sure ProfitPerCent doess not exceed 100%y1 = max(x1 * ProfitPerCent, y1) //y1 = % of max profitENDIFELSIF ShortOnMarket AND srcL < (PositionPrice - (y2 * pipsize)) THEN //SHORT positions//// compute the value of the Percentage of profits, if any, to lock in for SHORT trades//x2 = (PositionPrice - srcL) / pipsize //convert price to pipsIF x2 >= TrailStart THEN // go ahead only if N+ pipsDiff2 = abs(TrailStart - x2) //difference from current profit and TrailStartChunks2 = max(0,round((Diff2 / StepSize) + RoundTO)) //number of STEPSIZE chunksProfitPerCent = BasePerCent + (BasePerCent * (Chunks2 * PerCentInc)) //compute new size of ProfitPerCentProfitPerCent = max(ProfitPerCent[1],min(100,ProfitPerCent)) //make sure ProfitPerCent doess not exceed 100%y2 = max(x2 * ProfitPerCent, y2) //y2 = % of max profitENDIFENDIF//------------------------------------------------------------------------------// manage actual Exit, if needed//------------------------------------------------------------------------------IF y1 THEN //Place pending STOP order when y1 > 0 (LONG positions)SellPrice = max(SellPrice,PositionPrice + (y1 * pipsize)) //convert pips to price//// check the minimun distance between ExitPrice and current price//IF abs(close - SellPrice) > PriceDistance THEN//// place either a LIMIT or STOP pending order according to current price positioning//IF close >= SellPrice THENSELL AT SellPrice STOPELSESELL AT SellPrice LIMITENDIFELSE////sell AT MARKET when EXITPRICE does not meet the broker's minimun distance from current price//SELL AT MarketENDIFENDIFIF y2 THEN //Place pending STOP order when y2 > 0 (SHORT positions)ExitPrice = min(ExitPrice,PositionPrice - (y2 * pipsize)) //convert pips to price//// check the minimun distance between ExitPrice and current price//IF abs(close - ExitPrice) > PriceDistance THEN//// place either a LIMIT or STOP pending order according to current price positioning//IF close <= ExitPrice THENEXITSHORT AT ExitPrice STOPELSEEXITSHORT AT ExitPrice LIMITENDIFELSE////ExitShort AT MARKET when EXITPRICE does not meet the broker's minimun distance from current price//EXITSHORT AT MarketENDIFENDIF3 users thanked author for this post.
01/22/2021 at 2:24 PM #158909@Paul
it would be great if you could develop a new modified version of my last posted code, with your interesting additions, if possible, so that it be used with position accumulation.
01/24/2021 at 1:49 AM #159153@Roberto, Thnx for your interest about the additions, a few are covered like sensitivity. The way implemented for me is sufficient. I always prefer % from the latest close instead of a fixed number of points. The Donchian channels I don’t use. Nice to see you included the directionswitch. For now I looking at the % aspects of the code. Looks simple but is it?
If using percentage, it runs into trouble sometimes. You shouldn’t use tradeprice or positionprice but “close” from the moment condtions are true to enter and a new position is opened at the next bar.
But not onmarket or directionswitch does not cover each scenario, because if long & adding long it doesn’t take the correct close.
Anyway i’am suggestion something like this below. What do you think?
Besides this above, while I ‘ve seen working it correctly, also i’ve seen sellprice & exitprice at 0/999999. pffff
Here’s the relevant part.
12345678910111213141516171819202122232425262728293031DirectionSwitch = (LongOnMarket AND ShortOnMarket[1]) OR (LongOnMarket[1] AND ShortOnMarket)IF Not OnMarket OR DirectionSwitch THENts = 0.2 //% start trailing profits from this pointBasePerCent = 0.200 //20.0% Profit percentage to keep when setting BerakEvenStepSize = 10 //10 Pip chunks to increase PercentagePerCentInc = 0.100 //10.0% PerCent increment after each StepSize chunkRoundTO = -0.5 //-0.5 rounds always to Lower integer, +0.4 rounds always to Higher integer,0 defaults PRTPriceDistance = 7 * pipsize //7 minimun distance from current pricey1 = 0 //reset to 0y2 = 0 //reset to 0ProfitPerCent = BasePerCent //reset to desired default value//PositionCount = 0SellPrice = 0ExitPrice = 9999999trailstart = ((close*ts)/100)*pipsizeELSEif abs(countofposition)>abs(countofposition)[1] then // if adding to a long/short position, update trailstarttrailstart = ((close*ts)/100)*pipsizeendifIF PositionPrice <> PositionPrice[1] AND (ExitPrice + SellPrice) <> 9999999 THENIF LongOnMarket THENq1 = PositionPrice + ((srcH - PositionPrice) * ProfitPerCent)SellPriceX = max(max(SellPriceX,SellPrice),q1)SellPrice = max(max(SellPriceX,SellPrice),PositionPrice + (y1 * pipsize))ELSIF ShortOnMarket THENr1 = ((PositionPrice - srcL) * ProfitPerCent)ExitPriceX = min(min(ExitPriceX,ExitPrice),r1)ExitPrice = min(min(ExitPriceX,ExitPrice),PositionPrice - (y2 * pipsize))ENDIFENDIFENDIF1 user thanked author for this post.
01/24/2021 at 2:33 AM #159156Thank you Paul for your useful suggestions.
01/24/2021 at 2:22 PM #159246Having looked at it again I found an other reason for the differences I was experiencing. The format above works with close or positionprice and it can also be placed outside that area to calculate the trailstart and just use position price without additional lines of code. Always more ways then one to achieve the same thing…
1 user thanked author for this post.
03/11/2021 at 10:09 AM #163804BarNumber = 10 //10 Add further % so that trades don’t keep running too longBarPerCent = 0.100 //10% Add this additional percentage every BarNumber bars
Lets say I run a system on 5 minutes WIHTOUT a target profit (this trailing should work anyway right?). This means every 50 min the trailing will move up another 10% right (That is not how it works now).Can someone explain this?03/11/2021 at 10:56 AM #163814It will add 10% to the current percentage every 50 minutes.
So if your base percentage was 10%, PerCentInc may have raised that to, say 11% or 12%, BarPerCent will add a further 10% for a total of 21% or 22%.
If you set up percentages like that:
- 10% BsaePerCent
- 10% PerCentInc
- 10% BarPerCent
when PerCentInc INCREASES BasePerCent the first time, BasePerCent will become 11% (itself + 10% of itself), while BarPerCent will ADD 10% so that 11% becomes 21%.
03/11/2021 at 3:09 PM #163847hi Roberto, isn’t stepsize having the same problem as spread if calculated over longer time periods. A stepsize of 10 on the dow might be quite big 5-10 years back?
Second question is that stepsize is depended on which market it is applied to. On the dow it’s different then on forex. On forex (eurusd mini) it was guessing how small it should be. I settled on 0.0001. Does that make sense?
Is there some way to have a method that give it a value automatically?
03/11/2021 at 4:34 PM #163852Maybe the atr is something which will work and covers both questions. If changing over time is usefull or not it’s something to test, but atleast stepsize adjust itself to each market (atr/10)
123if (not onmarket[1] and onmarket) or directionswitch thenatr=(AverageTrueRange[200](close))/10endif03/11/2021 at 5:25 PM #163866Whenever you launch you strategy in autotrading you know:
- are launching it now, so it’s the current market, not years ago
- what the chosen TF is
- what is the instrument to be traded
so you can change STEP accordingly.
Of course using ATR makes it more portable (but you will still have to change other settings, such as Distance from current price, Start).
Actually there’s no perfect Trailing Stop (and no perfect strategy, indicator, ……), so everyone has to stick to the code he prefers, making custom changes, like you did, to suit best your needs (which may change across different strategies).
03/12/2021 at 9:28 AM #163906“isn’t stepsize having the same problem as spread if calculated over longer time periods. A stepsize of 10 on the dow might be quite big 5-10 years back?” That is 100% true. I also have been focusing on optimizing on 1million bars which means it is a big difference from 2011 and 2021. The backtest shows much better closes if looking at MFE from earlier on the backtest. I like the concept of this code and do you guys have any suggestion how I could work arround this?
-
AuthorPosts
Find exclusive trading pro-tools on