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.
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
// Conditions to enter long positions
indicator1 = Stochastic[10,3](close)
c1 = (indicator1 CROSSES UNDER 20)
indicator2 = CALL “#MACD_Direction”[20, 30, 7]
c2 = (indicator2 > 0)
IF c1 AND c2 THEN
BUY 1 CONTRACT AT MARKET
ENDIF
// Conditions to exit long positions
indicator3 = Stochastic[10,3](close)
c3 = (indicator3 > 50)
IF c3 THEN
SELL AT MARKET
ENDIF
// Stops and targets
SET STOP pLOSS 25
Hi. @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.
// === Input Parameters ===
sl1 = 40 // Stop Loss for first exit
sl2 = 20 // Stop Loss for second exit
tp1 = 20 // Take Profit for first exit
tp2 = 40 // Take Profit for second exit
n = 2 // Number of contracts per entry
// === Indicators ===
avg1 = average[10](close) // Fast moving average
avg2 = average[50](close) // Slow moving average
// === Daily reset of day counter ===
IF intradaybarindex = 0 THEN
difday = 1
ENDIF
// === Entry Logic ===
IF NOT longonmarket AND avg1 CROSSES OVER avg2 THEN
BUY n CONTRACT AT MARKET
ent = 1
difday = 0
SET STOP PLOSS sl1
ELSIF longonmarket AND ent = 1 AND difday AND avg1 CROSSES OVER avg2 THEN
BUY n CONTRACT AT MARKET
ent = 2
ENDIF
// === Exit Logic ===
IF longonmarket THEN
// Store trade price and define TP/SL levels when entering
IF NOT longonmarket[1] THEN
buyprice = tradeprice
stoploss1 = buyprice - sl1 * pipsize
takeprofit1 = buyprice + tp1 * pipsize
stoploss2 = buyprice - sl2 * pipsize
takeprofit2 = buyprice + tp2 * pipsize
sal = 1
stoploss = stoploss1
takeprofit = takeprofit1
ENDIF
// First exit: Half position at TP1 and stop at SL1
IF sal = 1 THEN
SELL AT stoploss STOP
SELL COUNTOFLONGSHARES * 0.5 CONTRACT AT takeprofit LIMIT
ENDIF
// When partial exit is executed, set new TP2 and SL2
IF COUNTOFLONGSHARES <> 0 AND COUNTOFLONGSHARES < COUNTOFLONGSHARES[1] THEN
sal = 2
stoploss = stoploss2
takeprofit = takeprofit2
ENDIF
// Second exit: Remaining position at TP2 or SL2
IF sal = 2 THEN
SELL AT stoploss STOP
SELL AT takeprofit LIMIT
ENDIF
ELSE
sal = 0
ENDIF
// === Plotting indicators and levels on chart ===
GRAPHONPRICE avg1 COLOURED("green")
GRAPHONPRICE avg2 COLOURED("fuchsia")
GRAPHONPRICE stoploss COLOURED("red")
GRAPHONPRICE takeprofit COLOURED("blue")
where you could find exactly what you are looking for.
Link below, hope you find what you need.
Snippet Link Library
I get attached error message when I try to send above code to proorder.
Is the fix simple Ivan please?
JSParticipant
Senior
Pending orders cannot be used to partially close positions…
(this can only be done with market orders within the strategy code…)
Thanks JS, I thought as much, but figured maybe I’d missed some changes in what is allowed etc.
Thanks 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. 🙂
Thank 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.