Open reverse position when stopLoss reached
Forums › ProRealTime English forum › ProOrder support › Open reverse position when stopLoss reached
- This topic has 3 replies, 2 voices, and was last updated 5 years ago by
BobOgden.
-
-
11/06/2020 at 3:22 PM #149684
Hey,
I’ve tried coding a strategy found here (thank you btw), and I think I’ve managed to do it. (I’ve also used some BE and TS code designed by some users from the forum, thank you very much as well)
However, I would like to go further and add a bit of code to open a reverse position as soon as the stopLoss of the previous position has been reached and if RSI is still below 31 (I’m only focusing on long positions for now, one step at a time lol). I thouhgt it would be very simple but it doesn’t seem to work. Here is the bit of code :
Bit of code12345678// Conditions to close a long positionIf LongOnMarket AND (tradeprice - low >= 30) THENSELL AT MARKETif RSIdown thensellshort positionsize contracts at marketset stop pLoss lossNbendifENDIFI’ve also tried to create a variable that equals 1 when the stopLoss is reached and 0 when a long position is opened but it doesn’t seem to work either. I’ve tried using a “sell at market” order instead of the regular stopLoss but I can’t make it work either.
Here’s the full original code (without the short reverse position snippet) :
Full code123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288// RSI Powerbomb - DJ 1 min ? - Julian TradingCafeDEFPARAM CumulateOrders = false//*****************************//VariablesmyATR = averagetruerange[14]if myATR > 30 thenlossNb = myATR // lossNb = distance to set stopLosselselossNb = 30endif//profitNb = 30//Money ManagementMM = 0 // = 0 for optimizationif MM = 0 thenpositionsize = 1endifMyRSI = RSI[50]if MyRSI >= 69 thenRSIup = 1elsif MyRSI =< 31 thenRSIdown = 1elseRSIup = 0RSIdown = 0endif//graph RSIdown as "RSIdown"//graph RSIup as "RSIup"VosConditions = RSIup and RSIdown // Condition nulleif RSIup and RSIdown and VosConditions thenendif//-------------------------------------------------------------//************************// Round numbers : 50s and 100Sfloor100 = (round(close/100-0.51))*100ceiling100 = (round(close/100+0.51))*100//graph floor100 as "floor100"//graph ceiling100 as "ceiling100"floor50 = (round(close/50-0.51))*50ceiling50 = (round(close/50+0.51))*50//graph floor50 as "floor50"//graph ceiling50 as "ceiling50"//------------------------------------------------------------------------//**************************// Conditions : closing candle within the range of a round number// floor 100s : 85 <-> 115if (close > floor100 - 15) and (close < floor100 + 15) thenposfloor100 = 1elseposfloor100 = 0endif// floor 50s : 42 <-> 58if (close > floor50 - 8) and (close < floor50 + 8) thenposfloor50 = 1elseposfloor50 = 0endif//graph posfloor100 as "posfloor100"//graph posfloor50 as "posfloor50"// ceiling 100s : 85 <-> 115if (close < ceiling100 + 15) and (close > ceiling100 - 15) thenposceiling100 = 1elseposceiling100 = 0endif// ceiling 50s : 42 <-> 58if (close < ceiling50 + 8) and (close > ceiling50 - 8) thenposceiling50 = 1elseposceiling50 = 0endif//graph posceiling100 as "posceiling100"//graph posceiling50 as "posceiling50"//---------------------------------------------------------------//*********************// Conditions to go longIF NOT LongOnMarket and (posfloor100 or posfloor50 or posceiling100 or posceiling50) and RSIdown THENBUY positionsize CONTRACTS AT MARKETset stop pLoss lossNb//set target pProfit profitNbENDIF// Conditions to close a long position//If LongOnMarket AND (tradeprice - low >= 30) THEN//SELL AT MARKET//if RSIdown then//sellshort positionsize contracts at market//set stop pLoss lossNb//endif//ENDIF// Conditions to go shortIF NOT ShortOnMarket and (posfloor100 or posfloor50 or posceiling100 or posceiling50) and RSIup THENSELLSHORT positionsize CONTRACTS AT MARKETset stop pLoss lossNb//set target pProfit profitNbENDIF// Conditions pour fermer une position en vente à découvertIF ShortOnMarket AND VosConditions THENEXITSHORT AT MARKETENDIF//---------------------------------------------------------------//******************************//Break evenbreakevenMargin = 30PointsToKeep = 4startBreakeven = breakevenMarginonce breakeven = 1 // 1: on | 0: off//reset the breakevenLevel when no trade are on marketif breakeven>0 thenIF NOT ONMARKET THENbreakevenLevel=0ENDIF// --- BUY SIDE ---//test if the price have moved favourably of "startBreakeven" points alreadyIF LONGONMARKET AND close-tradeprice(1)>=startBreakeven THEN//calculate the breakevenLevelbreakevenLevel = tradeprice(1)+PointsToKeepENDIF//place the new stop orders on market at breakevenLevelIF breakevenLevel>0 THENSELL AT breakevenLevel STOPENDIF// --- end of BUY SIDE ---IF SHORTONMARKET AND tradeprice(1)-close>startBreakeven THEN//calculate the breakevenLevelbreakevenLevel = tradeprice(1)-PointsToKeepENDIF//place the new stop orders on market at breakevenLevelIF breakevenLevel>0 THENEXITSHORT AT breakevenLevel STOPENDIFendifgraph breakevenLevel as "breakevenLevel"//---------------------------------------------------------//***********************// trailing atr stoponce trailingstoptype = 1 // trailing stop - 0 off, 1 ononce tsincrements = .05 // set to 0 to ignore tsincrementsonce tsminatrdist = 5once tsatrperiod = 14 // ts atr parameteronce tsminstop = 12 // ts minimum stop distanceonce tssensitivity = 1 // [0]close;[1]high/lowif trailingstoptype thenif barindex=tradeindex thentrailingstoplong = 3 // ts atr distancetrailingstopshort = 3 // ts atr distanceelseif longonmarket thenif tsnewsl>0 thenif trailingstoplong>tsminatrdist thenif tsnewsl>tsnewsl[1] thentrailingstoplong=trailingstoplongelsetrailingstoplong=trailingstoplong-tsincrementsendifelsetrailingstoplong=tsminatrdistendifendifendifif shortonmarket thenif tsnewsl>0 thenif trailingstopshort>tsminatrdist thenif tsnewsl<tsnewsl[1] thentrailingstopshort=trailingstopshortelsetrailingstopshort=trailingstopshort-tsincrementsendifelsetrailingstopshort=tsminatrdistendifendifendifendiftsatr=averagetruerange[tsatrperiod]((close/10))/1000//tsatr=averagetruerange[tsatrperiod]((close/1)) // (forex)tgl=round(tsatr*trailingstoplong)tgs=round(tsatr*trailingstopshort)if not onmarket or ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) thentsmaxprice=0tsminprice=closetsnewsl=0endifif tssensitivity thentssensitivitylong=hightssensitivityshort=lowelsetssensitivitylong=closetssensitivityshort=closeendifif longonmarket thentsmaxprice=max(tsmaxprice,tssensitivitylong)if tsmaxprice-tradeprice(1)>=tgl*pointsize thenif tsmaxprice-tradeprice(1)>=tsminstop thentsnewsl=tsmaxprice-tgl*pointsizeelsetsnewsl=tsmaxprice-tsminstop*pointsizeendifendifendifif shortonmarket thentsminprice=min(tsminprice,tssensitivityshort)if tradeprice(1)-tsminprice>=tgs*pointsize thenif tradeprice(1)-tsminprice>=tsminstop thentsnewsl=tsminprice+tgs*pointsizeelsetsnewsl=tsminprice+tsminstop*pointsizeendifendifendifif longonmarket thenif tsnewsl>0 thensell at tsnewsl stopendifif tsnewsl>0 thenif low crosses under tsnewsl thensell at market // when stop is rejectedendifendifendifif shortonmarket thenif tsnewsl>0 thenexitshort at tsnewsl stopendifif tsnewsl>0 thenif high crosses over tsnewsl thenexitshort at market // when stop is rejectedendifendifendifendifif onmarket thenif dayofweek=0 and (hour=0 and minute>=57) and abs(dopen(0)-dclose(1))>50 and positionperf(0)>0 thenif shortonmarket and close>dopen(0) thenexitshort at marketendifif longonmarket and close<dopen(0) thensell at marketendifendifendif//-------------------------------------------------------------Any ideas ?
BobOgden
11/07/2020 at 11:49 AM #14975911/07/2020 at 2:30 PM #14977611/07/2020 at 7:00 PM #149792 -
AuthorPosts
Find exclusive trading pro-tools on