BreakEven & Trailing Profit: complete function
Forums › ProRealTime English forum › ProOrder support › BreakEven & Trailing Profit: complete function
- This topic has 132 replies, 23 voices, and was last updated 14 hours ago by
Wim.
Tagged: BreakEven, Profit, stop, trailing, trailingstop
-
-
08/05/2025 at 3:24 PM #249381
Great snippet Roberto. But . . . am I wrong that this code still allows for a loss, even if the Trailing Stop has been in positive area already? The screen capture should help here. On the far left is the position entry, white line gives entry price, red line is initial StopLoss. The orange circle is where the Trailing Stop enters the positive area for the first time. And since the candle close is above this level, it will be a Stop order. We feel safe. But at the next step of the Trailing Stop, due to a bigger Step size, we end up above the candle close, blue circle. This time, therefore, it will be a Limit order. If price will go down from now on, we will end up at the original StopLoss! That should not happen, given that we already decided once to trail into positive area. Maybe a “set stop …” in stead of the pending stop orders (dying at the end of the candle) would be better, it is like lifting the thin red line (original StopLoss) to the level of the thick red line in the capture.
1 user thanked author for this post.
08/05/2025 at 3:42 PM #249384@Roberto I found an exemple of a situation where indeed the returning of the price resulted in a loss, despite the Trailing already adjusted several times in positive area. So when flipping from Stop Order to Limit Order, I think it is necessary to adjust the level of the original StopLoss.
1 user thanked author for this post.
08/05/2025 at 6:06 PM #249397Can you post the whole code, or the link to my snippet?
08/05/2025 at 6:32 PM #24940608/05/2025 at 6:41 PM #249408Forgot to notice: there is also the situation that the first Trailing is immediately a Limit type of order. In that case there is no previous Trailing Stop order in the positive. Do you accept that price can return to the original StopLoss, or do you “invent” a safety net at for instance halfway current close and entry price?
08/06/2025 at 3:50 PM #249442Replace this line:
1StepPerCent = 0.01 //50% (of the 0.25% above) as a Trailing Step (set to 100 to make StepSize=TrailStart, set to 200 to make it twice TrailStart)with this one:
1StepPerCent = 50 //50% (of the 0.25% above) as a Trailing Step (set to 100 to make StepSize=TrailStart, set to 200 to make it twice TrailStart)or 20, etc…
The value 0.01 is so tiny that it raises issues in calculations.
08/06/2025 at 7:41 PM #249451Hello @Roberto. My guess is that you checked another version than the one I attached to my post. The attached version has the following section for Trailing stop settings:
Trailing Stop settings12345StartPerCentLong = 0.25 //0.25% to start triggering Trailing StopStartPerCentShort = 0.20StepPerCent = 0.15 //50% (of the 0.25% above) as a Trailing Step (set to 100 to make StepSize=TrailStart, set to 200 to make it twice TrailStart)BasePerCent = 0.03 //0.1-1 Profit percentage to keep when setting BreakEvenPerCentInc = 0.1 // 0.1-1 PerCent increment after each StepSize chunkIn your original version, Il-Mio-Sistema3.itf, StepPerCent is set to 0.5 (according to the comment this means 50%). I lowered this to 0.15, not 0.01. But whatever value you enter here, this doesn’t prevent the problem I mentioned. So let’s forget about calculation issues.
The problem is a design problem, and here follows the code that causes it:
case Long12345IF close >= SellPrice THENSELL AT SellPrice STOPELSESELL AT SellPrice LIMITENDIFAs long as “close >= SellPrice” you’re safe, a Stop order is issued in positive area, once Trailing has been activated of course. But … if “close < SellPrice”, and this can happen (!), the Stop order is no longer re-animated at candle end, but a Limit order is issued above current price. There is no longer a Stop order in positive area to prevent the price from falling back to the original StopLoss (the biggest loss allowed per trade).
Sending a Limit order alone to the broker is not enough. There has to be a Stop order in positive area also. You could use the most recent Stop order. But in case the first Trailing stop is immediately a Limit order, you have to come up with another idea (half the limit value, half the close value???)
Have another look at my last screen capture, should clarify what I am trying to express here. Afraid I wasn’t clear the first time.08/06/2025 at 10:00 PM #249455I used the file you attached to your post.
Anyway, I’ll check again tomorrow, but to make a thorough check, please attach again the sane ITF file you used and tell me the instrument, date and time of an incorrect trade.
08/06/2025 at 10:43 PM #249456As requested, itf and another screen capture of a losing trade after activation of trailing.
Date and time of trade: 2025-07-11 @ 17:39:00
Instrument: MicroGold 1025, future with IB.
The current code allows for this to happen. But I am pretty sure that this is not what you want to happen.08/08/2025 at 9:36 PM #249528I don’t have IB, sorry. I can only test with IG and PRT.
In any case, I will do my utmost to find where your issue may come from.
I’ll be back to you next Monday.
08/12/2025 at 5:13 PM #249678I slightly modified the code, try this one (I am also attaching the ITF file):
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166SL = 30TP = SL * 10Sma = average[50,0](close)IF close CROSSES OVER Sma and Not OnMarket THENBUY at MarketELSIF close CROSSES UNDER Sma and Not OnMarket THENSELLSHORT at MarketENDIFSET STOP pLOSS SLSET TARGET pPROFIT TP////------------------------------------------------------------------------------------------------------------------------------------------------// Trailing Start//------------------------------------------------------------------------------------------------------------------------------------------------If 1 = 1 thenDirectionSwitch = (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//StartPerCentLong = 0.25 //0.25% to start triggering Trailing StopStartPerCentShort = 0.20StepPerCent = 0.15 //50% (of the 0.25% above) as a Trailing Step (set to 100 to make StepSize=TrailStart, set to 200 to make it twice TrailStart)BasePerCent = 0.03 //0.1-1 Profit percentage to keep when setting BreakEvenPerCentInc = 0.1 // 0.1-1 PerCent increment after each StepSize chunk//TrailStartLong = (close / PipSize) * StartPerCentLong / 100 //use current price (CLOSE) for calculationsTrailStartShort = (close / PipSize) * StartPerCentShort / 100 //use current price (CLOSE) for calculationsStepSizeLong = TrailStartLong * StepPerCent / 100StepSizeShort = TrailStartShort * StepPerCent / 100//RoundTO = -0.5 //-0.5 rounds always to Lower integer, +0.4 rounds always to Higher integer, 0 defaults PRT behaviourPriceDistance = 8 * 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//9999999 THEN //go on only if Trailing Stop had already started trailingIF LongOnMarket THENq1 = PositionPrice + ((Close - 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 - Close) * 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 Close > (PositionPrice + (y1 * pipsize)) THEN //LONG positions//// compute the value of the Percentage of profits, if any, to lock in for LONG trades//x1 = (Close - PositionPrice) / pipsize //convert price to pipsIF x1 >= TrailStartLong THEN // go ahead only if N+ pipsDiff1 = abs(TrailStartLong - x1) //difference from current profit and TrailStartChunks1 = max(0,round((Diff1 / StepSizeLong) + 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 profit)ENDIFELSIF ShortOnMarket AND Close < (PositionPrice - (y2 * pipsize)) THEN //SHORT positions//// compute the value of the Percentage of profits, if any, to lock in for SHORT trades//x2 = (PositionPrice - Close) / pipsize //convert price to pipsIF x2 >= TrailStartShort THEN // go ahead only if N+ pipsDiff2 = abs(TrailStartShort - x2) //difference from current profit and TrailStartChunks2 = max(0,round((Diff2 / StepSizeShort) + 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 MarketENDIFENDIFEndif////EntryPrice = TradePrice//IF Not OnMarket THEN//EntryPrice = 0//StopPrice = 0//TargetPrice = 0//TrailPrice = 0//ENDIF//IF LongOnMarket THEN//StopPrice = EntryPrice - (SL * PipSize)//TargetPrice = EntryPrice + (TP * PipSize)//TrailPrice = SellPrice//ELSIF ShortOnMarket THEN//StopPrice = EntryPrice + (SL * PipSize)//TargetPrice = EntryPrice - (TP * PipSize)//TrailPrice = ExitPrice//ENDIF//IF OnMarket THEN//graphonprice TradePrice coloured("white")//graphonprice StopPrice coloured("red")//graphonprice TargetPrice coloured("green")//graphonprice TrailPrice coloured("cyan")//ENDIF//graph TrailStartLong//graph x1//graph y1//graph ((PositionPerf * PositionPrice) / PipSize) AS "tempGAIN"08/12/2025 at 6:40 PM #249686Thanks for the effort @Roberto. But I am not sure you sent the right version. This ….WW.itf is slightly different from the ….W.itf I sent you. Only some comments are modified (lines 55 and 79) and some blanks have been added in formulas to align “=”s and “*”s on consecutive lines (lines 29 and 32). And at the end all is commented out that only served for displaying levels with graphonprice commands, not affecting calculations at all. So, the results of …WW and …W are identical, problem still there.
The problem will be there as long as the following part of the code stays the same, because when switching to Limit orders, there currently is no remaining Stop order to protect against return to negative areas.
Something has to change here123456IF close >= SellPrice THENSELL AT SellPrice STOPELSESELL AT SellPrice LIMITMAINTAIN SAFETYNET // <--- HAS TO BE ADDEDENDIF08/15/2025 at 2:51 PM #249786I fixed the problem @Roberto! The solution requires a variable StopIssued that has to be reset at every new position (like your Y1, y2 etc.). And next to that some code to detect whether the Limit order has been preceded by Trailing Stop Order(s) or not. If preceded by at least 1 Stop order, we retain the last Stop level as a SafetyNet. If not preceded by a Stop order, we have to invent our own SafetNet level. I have chosen to pick the level halfway tradeprice and the Limit order. The variable safetyMult (from Multiplier) allows to optimise this value if you wish. I just set it at 0.5, meaning halfway.
For Longs the code is:SafetyNet code for Longs1234567891011IF close >= SellPrice THENSELL AT SellPrice STOPStopIssued = StopIssued + 1 // WM: Count number of Stop levels issuedSafetyNet = SellPrice // WM: Retain most recent Stop levelELSESELL AT SellPrice LIMITif StopIssued = 0 then // WM: No previous Trailing STOP ordersSafetyNet = PositionPrice + safetyMult * (SellPrice - PositionPrice) // WM: Halfway entry and limit levelendifsell at SafetyNet stopENDIFSee the 2 screen captures for both situations, Limit order preceded by Stop and immediate Limit order. The example with the preceding Stop order would have reached the original StopLoss without the SafetyNet adaptation! The .ITF is attached also.
-
AuthorPosts
Find exclusive trading pro-tools on