Hello,
Based on 3 lines break indicator and some other pieces of code found elsewhere, I propose this strategy, with several alternative conditions of exits. An optimization variable “strategy” needs to be implemented from 1 to 9, to compare the different variations.
Visually winning trades look beautiful, but at the end, % of winning rates ratio is average. Profit factor is often profitable when tested on various stocks.
Would appreciate some feedback to improve the strategy. I already know that the code may look pretty messy and not optimized.
Discussions about the strategy take place in this forum topic: https://www.prorealcode.com/topic/a-strategy-around-3-lines-break-on-stocks-daily/
// @Name DT_3LineBreak
// @Type Backtest
// @Date: 04.08.2021
// @Source: Tradosaure Youtube + code DocTrading
// @Timeframe: Daily
// @Entry:Breakout 3LB reversal with trend filter (close>MA200)
// @Stop Loss: 3LB low
// @Take Profit: 1 SL distance (RRR) - to be tested
DEFPARAM CumulateOrders=True
// Sub-Strategies:
// 1- TP = 1SL
// 2- TP = 1.5 SL
// 3- TP = 2 SL
// 4- TP = 5%
// 5- TBC
// 6- BE after 5%
// 7- TP =1.5 SL + BE 5%
// 8- TP =2 SL + BE 5%
// 9- BE + Trailing stop
strategy = 2
//////////////////////////////////////////////////////////////////////
//// GENERIC CODE
//////////////////////////////////////////////////////////////////////
// TAILLE DES POSITIONS : 3 VARIABLES A PARAMÉTRER
CAPITALInit = 1000
// Capital initial (pour le calcul de taille des trades)
LEVIER = 1
// Levier : ne pas en abuser, bien vérifier le drawdown
REINV = 0
// 0 = sans réinvestir / 1 = réinvestir les gains
RISQ = 0.01
distSL = 0.04 // minimum stop loss distance % to be profitable
RRR=1 // Risk Reward Ratio
if strategy = 2 or strategy =7 then
RRR=1.5
elsif strategy = 3 or strategy = 8 then
RRR=2
endif
IF REINV = 0 THEN
n = round((CapitalInit / close) * levier)
ELSIF REINV = 1 THEN
capital = (CAPITALinit + strategyprofit)
n = round((capital/close) * levier)
ENDIF
//////////////////////////////////////////////////////////////////////
//// INDICATOR
//////////////////////////////////////////////////////////////////////
once t=1
once a0=(close)
once a1=(close)
once a2=(close)
once a3=(close)
newClose = close
if t=1 and newClose>a3 then
a0=a1
a1=a2
a2=a3
a3=newClose
elsif t=1 and newClose<a0 then
t=-1
a1=a3
a0=a3
a3=newClose
elsif t=-1 and newClose<a3 then
a0=a1
a1=a2
a2=a3
a3=newClose
elsif t=-1 and newClose>a0 then
t=1
a1=a3
a0=a3
a3=newClose
cBuy=1
else
cBuy=0
endif
//////////////////////////////////////////////////////////////////////
//// BUY CONDITIONS
//////////////////////////////////////////////////////////////////////
i=0
c1 = cBuy
ma50=Average[50](close)
c2 = ma50>ma50[1] or close>ma50
//c2 = close>Average[200](close)
c0=1
if not longonmarket and c0 and c1 and c2 then
BUY n shares at market nextbaropen
sellprice = min(high*(1-distSL-0.001),a0)
targetprice = high+RRR*(high-sellprice)
if strategy = 4 then
targetprice = high*1.05
endif
ENDIF
//////////////////////////////////////////////////////////////////////
//// BREAK EVEN
//////////////////////////////////////////////////////////////////////
IF NOT ONMARKET THEN
breakevenLevel=0
ENDIF
//test if the price have moved favourably of "startBreakeven" points already
IF strategy >5 then
IF LONGONMARKET AND high>1.05*tradeprice(1) THEN
//calculate the breakevenLevel
breakevenLevel = tradeprice(1)*1.02
ENDIF
endif
//place the new stop orders on market at breakevenLevel
IF breakevenLevel>0 THEN
SELL AT breakevenLevel STOP
ENDIF
//////////////////////////////////////////////////////////////////////
//// REBUY
//////////////////////////////////////////////////////////////////////
if longonmarket and cBuy[0] and t[1]=-1 then
BUY n shares at market nextbaropen
endif
//////////////////////////////////////////////////////////////////////
//// SELL CONDITIONS
//////////////////////////////////////////////////////////////////////
IF longonmarket THEN
cSell = close < sellprice or close > targetprice
if strategy=9 then
cSell = close<a0
endif
IF cSell THEN
SELL at market nextbaropen
ENDIF
ENDIF