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
-
-
03/14/2021 at 4:54 AM #16405503/16/2021 at 9:58 AM #16430603/23/2021 at 9:40 AM #165005
Hello Roberto,
I am trying to understand your great code. I have the following 2 questions:
- SellPriceX = max(max(SellPriceX,SellPrice),q1) What is the meaning of SellPriceX?
- ProfitPerCent = max(ProfitPerCent[1],min(100,ProfitPerCent)) Should the ‘100’ not be ‘1’?
Best regards,
Dré
03/23/2021 at 10:20 AM #165009- SellPriceX = max(max(SellPriceX,SellPrice),q1) What is the meaning of SellPriceX?
it makes sure that whatever the calculations are, the PRICE at which the position is closed is NEVER less than the previous one - ProfitPerCent = max(ProfitPerCent[1],min(100,ProfitPerCent)) Should the ‘100’ not be ‘1’?
yes, it should be 1, you can change it. I actually was reported by another user last year, but I always forgot to change it as it doesn’t affect calculations
03/24/2021 at 10:03 AM #165134Hello Roberto,
Thank you for your quick response. I understand the code now better.
I have the following remarks:
1. r1 = ((PositionPrice – SrcL) * ProfitPerCent) I think it should be: r1 = PositionPrice – ((PositionPrice – SrcL) * ProfitPerCent)
2. ExitPriceX should be inialized to 9999999 (ExitPriceX = 9999999) otherwise the ExitPrice will always be zero (0).
Best regards,
Dré
03/24/2021 at 10:50 AM #165141Well spotted DreaC, you are right for both glitches.
R1 must mirror Q1 (in the opposite direction) and ExitPriceX must be like ExitPrice.
Thank you for sharing with us.
03/29/2021 at 2:44 AM #16557703/29/2021 at 9:04 AM #165602You should try setting a STEP of 9999.
03/29/2021 at 1:52 PM #16563704/01/2021 at 6:25 PM #165972Well spotted DreaC, you are right for both glitches.
R1 must mirror Q1 (in the opposite direction) and ExitPriceX must be like ExitPrice.
Thank you for sharing with us.
Could you please post the correct code that should be in those lines?
04/01/2021 at 6:31 PM #165976There you go:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120//------------------------------------------------------------------------------------------------------------------------------------------------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 = 0SellPriceX = 0ExitPrice = 9999999ExitPriceX = 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 - ((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 MarketENDIFENDIF2 users thanked author for this post.
04/01/2021 at 7:50 PM #165989Here’s a very minor mod, if you want to use % instead of points
12ts = (tradeprice*pc)/100 // % trailing startTrailStart = ts // Start trailing profits from this pointThanks again Roberto for all your work on this!
1 user thanked author for this post.
04/01/2021 at 9:21 PM #16599304/01/2021 at 9:45 PM #165994Hmm … I’d say that the trailing stop has an extremely low chance of starting if you’re not onmarket. 😁
But it does make me think that perhaps it should be positionprice, to allow for cumulative orders ??
04/01/2021 at 10:15 PM #165995I personally struggle to fully understand Roberto’s code 🙂
But from what I read the trailing stop is set up when you are not on market or when there is a direction switch. By the way I dont really understand why, but there must be a logic behind this!
-
AuthorPosts
Find exclusive trading pro-tools on