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 :
// 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
I’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) :
// RSI Powerbomb - DJ 1 min ? - Julian TradingCafe
DEFPARAM CumulateOrders = false
//*****************************
//Variables
myATR = averagetruerange[14]
if myATR > 30 then
lossNb = myATR // lossNb = distance to set stopLoss
else
lossNb = 30
endif
//profitNb = 30
//Money Management
MM = 0 // = 0 for optimization
if MM = 0 then
positionsize = 1
endif
MyRSI = RSI[50]
if MyRSI >= 69 then
RSIup = 1
elsif MyRSI =< 31 then
RSIdown = 1
else
RSIup = 0
RSIdown = 0
endif
//graph RSIdown as "RSIdown"
//graph RSIup as "RSIup"
VosConditions = RSIup and RSIdown // Condition nulle
if RSIup and RSIdown and VosConditions then
endif
//-------------------------------------------------------------
//************************
// Round numbers : 50s and 100S
floor100 = (round(close/100-0.51))*100
ceiling100 = (round(close/100+0.51))*100
//graph floor100 as "floor100"
//graph ceiling100 as "ceiling100"
floor50 = (round(close/50-0.51))*50
ceiling50 = (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 <-> 115
if (close > floor100 - 15) and (close < floor100 + 15) then
posfloor100 = 1
else
posfloor100 = 0
endif
// floor 50s : 42 <-> 58
if (close > floor50 - 8) and (close < floor50 + 8) then
posfloor50 = 1
else
posfloor50 = 0
endif
//graph posfloor100 as "posfloor100"
//graph posfloor50 as "posfloor50"
// ceiling 100s : 85 <-> 115
if (close < ceiling100 + 15) and (close > ceiling100 - 15) then
posceiling100 = 1
else
posceiling100 = 0
endif
// ceiling 50s : 42 <-> 58
if (close < ceiling50 + 8) and (close > ceiling50 - 8) then
posceiling50 = 1
else
posceiling50 = 0
endif
//graph posceiling100 as "posceiling100"
//graph posceiling50 as "posceiling50"
//---------------------------------------------------------------
//*********************
// Conditions to go long
IF NOT LongOnMarket and (posfloor100 or posfloor50 or posceiling100 or posceiling50) and RSIdown THEN
BUY positionsize CONTRACTS AT MARKET
set stop pLoss lossNb
//set target pProfit profitNb
ENDIF
// 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 short
IF NOT ShortOnMarket and (posfloor100 or posfloor50 or posceiling100 or posceiling50) and RSIup THEN
SELLSHORT positionsize CONTRACTS AT MARKET
set stop pLoss lossNb
//set target pProfit profitNb
ENDIF
// Conditions pour fermer une position en vente à découvert
IF ShortOnMarket AND VosConditions THEN
EXITSHORT AT MARKET
ENDIF
//---------------------------------------------------------------
//******************************
//Break even
breakevenMargin = 30
PointsToKeep = 4
startBreakeven = breakevenMargin
once breakeven = 1 // 1: on | 0: off
//reset the breakevenLevel when no trade are on market
if breakeven>0 then
IF NOT ONMARKET THEN
breakevenLevel=0
ENDIF
// --- BUY SIDE ---
//test if the price have moved favourably of "startBreakeven" points already
IF LONGONMARKET AND close-tradeprice(1)>=startBreakeven THEN
//calculate the breakevenLevel
breakevenLevel = tradeprice(1)+PointsToKeep
ENDIF
//place the new stop orders on market at breakevenLevel
IF breakevenLevel>0 THEN
SELL AT breakevenLevel STOP
ENDIF
// --- end of BUY SIDE ---
IF SHORTONMARKET AND tradeprice(1)-close>startBreakeven THEN
//calculate the breakevenLevel
breakevenLevel = tradeprice(1)-PointsToKeep
ENDIF
//place the new stop orders on market at breakevenLevel
IF breakevenLevel>0 THEN
EXITSHORT AT breakevenLevel STOP
ENDIF
endif
graph breakevenLevel as "breakevenLevel"
//---------------------------------------------------------
//***********************
// trailing atr stop
once trailingstoptype = 1 // trailing stop - 0 off, 1 on
once tsincrements = .05 // set to 0 to ignore tsincrements
once tsminatrdist = 5
once tsatrperiod = 14 // ts atr parameter
once tsminstop = 12 // ts minimum stop distance
once tssensitivity = 1 // [0]close;[1]high/low
if trailingstoptype then
if barindex=tradeindex then
trailingstoplong = 3 // ts atr distance
trailingstopshort = 3 // ts atr distance
else
if longonmarket then
if tsnewsl>0 then
if trailingstoplong>tsminatrdist then
if tsnewsl>tsnewsl[1] then
trailingstoplong=trailingstoplong
else
trailingstoplong=trailingstoplong-tsincrements
endif
else
trailingstoplong=tsminatrdist
endif
endif
endif
if shortonmarket then
if tsnewsl>0 then
if trailingstopshort>tsminatrdist then
if tsnewsl<tsnewsl[1] then
trailingstopshort=trailingstopshort
else
trailingstopshort=trailingstopshort-tsincrements
endif
else
trailingstopshort=tsminatrdist
endif
endif
endif
endif
tsatr=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)) then
tsmaxprice=0
tsminprice=close
tsnewsl=0
endif
if tssensitivity then
tssensitivitylong=high
tssensitivityshort=low
else
tssensitivitylong=close
tssensitivityshort=close
endif
if longonmarket then
tsmaxprice=max(tsmaxprice,tssensitivitylong)
if tsmaxprice-tradeprice(1)>=tgl*pointsize then
if tsmaxprice-tradeprice(1)>=tsminstop then
tsnewsl=tsmaxprice-tgl*pointsize
else
tsnewsl=tsmaxprice-tsminstop*pointsize
endif
endif
endif
if shortonmarket then
tsminprice=min(tsminprice,tssensitivityshort)
if tradeprice(1)-tsminprice>=tgs*pointsize then
if tradeprice(1)-tsminprice>=tsminstop then
tsnewsl=tsminprice+tgs*pointsize
else
tsnewsl=tsminprice+tsminstop*pointsize
endif
endif
endif
if longonmarket then
if tsnewsl>0 then
sell at tsnewsl stop
endif
if tsnewsl>0 then
if low crosses under tsnewsl then
sell at market // when stop is rejected
endif
endif
endif
if shortonmarket then
if tsnewsl>0 then
exitshort at tsnewsl stop
endif
if tsnewsl>0 then
if high crosses over tsnewsl then
exitshort at market // when stop is rejected
endif
endif
endif
endif
if onmarket then
if dayofweek=0 and (hour=0 and minute>=57) and abs(dopen(0)-dclose(1))>50 and positionperf(0)>0 then
if shortonmarket and close>dopen(0) then
exitshort at market
endif
if longonmarket and close<dopen(0) then
sell at market
endif
endif
endif
//-------------------------------------------------------------
Any ideas ?
BobOgden
Hey @Fran55, I’m afraid I don’t understand what you mean…