Stop and reverse question
Forums › ProRealTime English forum › ProOrder support › Stop and reverse question
- This topic has 7 replies, 5 voices, and was last updated 1 month ago by
nordhandel.
-
-
05/13/2025 at 9:29 PM #247086
Hi, first timer here and just learning ProRealTime with minimal prior coding experience, so apologize for naive questions. I’m building a mean reversion system, but I have certain scenarios where after some backtesting I can see that statistically at certain stop levels the odds are heavily indicating that it’s got caught in a trend. I’d like to in a sense “pause” the normal system rules and do a stop and reverse intrabar with a trailing stop when it hits the stop level. If I’m trading manually that’s easy enough, but I can’t figure out how to code that.
I know there’s a lot of logic likely involved there so not asking anyone to code stuff for me, but any pointers on the simple commands required to do a stop and reverse order instead of just a stop that I have at the end? My code is below, any assistance appreciated. Thanks.
1234567891011121314151617181920212223// Definition of code parametersDEFPARAM CumulateOrders = False // Cumulating positions deactivated// Conditions to enter long positionsindicator1 = Stochastic[10,3](close)c1 = (indicator1 CROSSES UNDER 20)indicator2 = CALL “#MACD_Direction”[20, 30, 7]c2 = (indicator2 > 0)IF c1 AND c2 THENBUY 1 CONTRACT AT MARKETENDIF// Conditions to exit long positionsindicator3 = Stochastic[10,3](close)c3 = (indicator3 > 50)IF c3 THENSELL AT MARKETENDIF// Stops and targetsSET STOP pLOSS 2505/14/2025 at 9:00 AM #247091Hi. @Grahal has a list of interesting post where you could find exactly what you are looking for.
Anyway here you have an example of how you could manage differents TP and SL.12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970// === Input Parameters ===sl1 = 40 // Stop Loss for first exitsl2 = 20 // Stop Loss for second exittp1 = 20 // Take Profit for first exittp2 = 40 // Take Profit for second exitn = 2 // Number of contracts per entry// === Indicators ===avg1 = average[10](close) // Fast moving averageavg2 = average[50](close) // Slow moving average// === Daily reset of day counter ===IF intradaybarindex = 0 THENdifday = 1ENDIF// === Entry Logic ===IF NOT longonmarket AND avg1 CROSSES OVER avg2 THENBUY n CONTRACT AT MARKETent = 1difday = 0SET STOP PLOSS sl1ELSIF longonmarket AND ent = 1 AND difday AND avg1 CROSSES OVER avg2 THENBUY n CONTRACT AT MARKETent = 2ENDIF// === Exit Logic ===IF longonmarket THEN// Store trade price and define TP/SL levels when enteringIF NOT longonmarket[1] THENbuyprice = tradepricestoploss1 = buyprice - sl1 * pipsizetakeprofit1 = buyprice + tp1 * pipsizestoploss2 = buyprice - sl2 * pipsizetakeprofit2 = buyprice + tp2 * pipsizesal = 1stoploss = stoploss1takeprofit = takeprofit1ENDIF// First exit: Half position at TP1 and stop at SL1IF sal = 1 THENSELL AT stoploss STOPSELL COUNTOFLONGSHARES * 0.5 CONTRACT AT takeprofit LIMITENDIF// When partial exit is executed, set new TP2 and SL2IF COUNTOFLONGSHARES <> 0 AND COUNTOFLONGSHARES < COUNTOFLONGSHARES[1] THENsal = 2stoploss = stoploss2takeprofit = takeprofit2ENDIF// Second exit: Remaining position at TP2 or SL2IF sal = 2 THENSELL AT stoploss STOPSELL AT takeprofit LIMITENDIFELSEsal = 0ENDIF// === Plotting indicators and levels on chart ===GRAPHONPRICE avg1 COLOURED("green")GRAPHONPRICE avg2 COLOURED("fuchsia")GRAPHONPRICE stoploss COLOURED("red")GRAPHONPRICE takeprofit COLOURED("blue")1 user thanked author for this post.
05/14/2025 at 9:06 AM #247092where you could find exactly what you are looking for.
Link below, hope you find what you need.
Snippet Link Library1 user thanked author for this post.
05/14/2025 at 10:51 AM #24709305/14/2025 at 12:09 PM #24709605/14/2025 at 1:12 PM #24709905/14/2025 at 4:20 PM #247124Thanks JS, I thought as much, but figured maybe I’d missed some changes in what is allowed etc.
Hey GraHal – not really that, but Backtest allows for it. Thus, easy to be confused a little. 🙂
1 user thanked author for this post.
05/14/2025 at 9:08 PM #247140Thank you @GraHal and @Iván Haven’t figured it out yet, but the pointers have got me sniffing it out. That google sheet with all the links is a fantastic resource, much appreciated.
1 user thanked author for this post.
-
AuthorPosts
Find exclusive trading pro-tools on