ProRealCode - Trading & Coding with ProRealTime™
User @Alessio posted a custom indicator on the italian forum, to tell Trending from Ranging markets (https://www.prorealcode.com/topic/indicatore-per-laterale-laterale-inclinato-trend/).
I liked it and wanted to give it a try on DAX, 1-hour TF, with Nicolas’trailing stop code on the 20-second TF. I decided to embed the indicator in the strategy, instead of CALLing it.
The indicators returns 1 for ranging market, 2 for sloping ranges and 3 for trending markets. I decided to enter a position (based on the direction of each candle) whenever it rises from 2 to 3.
Performance seems rewarding:
ONCE TP = 42 //42
ONCE SL = 21 //21
/////////////////////////////////////////////////////////////////////////////////
TIMEFRAME (1 hour, updateonclose) //h1
ONCE n = 5 //5
ONCE punti = 5 //5
ONCE mediamobile = 45 //45
ONCE puntiinclinato = 15 //15
ONCE x = punti * pipsize
MA50 = LinearRegression[mediamobile](close)
MAhigh = highest[n](MA50)
MAlow = lowest[n](MA50)
MA50range = Mahigh - MAlow
laterale = MA50range <= x
lateraleinclinato = MA50range>x and ma50range<puntiinclinato+x
trend = ma50range>lateraleinclinato+puntiinclinato
if laterale then
result=1
elsif lateraleinclinato then
result=2
elsif trend then
result=3
endif
// --- LONG conditions
a1 = close > open
a2 = (Result = 3) AND (Result > Result[1])
// --- SHORT conditions
b1 = close < open
b2 = (Result = 3) AND (Result > Result[1])
/////////////////////////////////////////////////////////////////////////////////
TIMEFRAME (default) //20 sec.
// --- LONG trades
IF a1 AND a2 AND Not Onmarket THEN
BUY 1 CONTRACT AT MARKET
ENDIF
// --- SHORT trades
IF b1 AND b2 AND Not Onmarket THEN
SELLSHORT 1 CONTRACT AT MARKET
ENDIF
//
SET TARGET pPROFIT TP
SET STOP pLOSS SL
//**************************************************************************
//trailing stop function
trailingstart = 10 //10 trailing will start @trailinstart points profit
trailingstep = 3 //3 trailing step to move the "stoploss"
//reset the stoploss value
IF NOT ONMARKET THEN
newSL=0
ENDIF
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THEN
newSL = tradeprice(1)+trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
newSL = newSL+trailingstep*pipsize
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THEN
newSL = tradeprice(1)-trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND newSL-close>=trailingstep*pipsize THEN
newSL = newSL-trailingstep*pipsize
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
the trend phase, I thought only for the exit, nice trailing
Dear fellow traders,
First, thanks to robertogozzi for this very effective trailing stop. You can see from the screenshot very little difference between MFE and exit price.
I was impressed with the effectiveness of the indicator shared by Alessio on the Italian forum, so I called it “Da Vinci”. Thank you Alessio!
I’m stuck with an MTF issue. I’m testing this H1 System run on 5min chart, which I’m testing only on 2k for the example. I expect the signals to be taken on the 1 hour basis. However, some of the trades are taken outside of this rule. For example, on 4 Jan. 2022 at 12:15 the System took a Long trade, while there is no Signal on the H1 chart between 11 and 13 and the Signal is Short on the 5min chart.
Can you please help to fix this MTF issue?
Any suggestions how to this strategy? Your ideas or comments are welcome!
DEFPARAM CUMULATEORDERS= FALSE
DEFPARAM PreLoadBars = 2000
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TIMEFRAME(60 MINUTES, UPDATEONCLOSE)
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Ehlers RMS
flen = 8//5//9//8//40 //fast length
slen = 21//26//21//60 //slow length
if barindex>slen then
a1= 5/flen
a2= 5/slen
PB = (a1 - a2) * close + (a2*(1 - a1) - a1 * (1 - a2))* close[1] + ((1 - a1) + (1 - a2))*(PB[1])- (1 - a1)* (1 - a2)*(PB[2])
RMSa = summation[50](PB*PB)
RMSplus = sqrt(RMSa/50)
RMSminus = -RMSplus
endif
IF PB > RMSminus and PB > PB[1] THEN
LongRMS=+1
ENDIF
IF PB < RMSplus and PB < PB[1] THEN
ShortRMS=-1
ENDIF
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Stochastics
StoK = Stochastic[8,1](close)
StoD = Average[3](Stochastic[8,1](close))
IF (StoK >= 20) and (StoK > StoD) and (StoK > StoK[1]) THEN
LongSto=+1
ENDIF
IF (StoK <= 80) and (StoK < StoD) and (StoK < StoK[1]) THEN
ShortSto=-1
ENDIF
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Vigor Index
PeriodRVI = 8 //5//12
diffRVI = close - open
ind1RVI = (diffRVI + 2*diffRVI[1] + 2*diffRVI[2] + diffRVI[3]) / 6
ind2RVI = (Range + 2*Range[1] + 2*Range[2] + Range[3]) / 6
condRVI = Summation[periodRVI](ind2RVI)
IF condRVI = 0 THEN
temp = 0.0001
ELSE
temp = condRVI
ENDIF
RVI = Summation[periodRVI](ind1RVI) / temp
RVIsig = (RVI + 2*RVI[1] + 2*RVI[2] + RVI[3]) / 6
IF (RVI > RVI[1]) and (RVI > RVIsig) THEN
LongRVI=+1
ENDIF
IF (RVI < RVI[1]) and (RVI < RVIsig) THEN
ShortRVI=-1
ENDIF
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Moving Average Crossing
PFast = 8//5
PSlow = 21//13
HullFast = HullAverage[PFast](close)
HullSlow = HullAverage[PSlow](close)
EndPFast = EndPointAverage[PFast](close)
EndPSlow = EndPointAverage[PSlow](close)
TSFast = TimeSeriesAverage[PFast](close)
TSSlow = TimeSeriesAverage[PSlow](close)
LRFast = LinearRegression[PFast](close)
LRSlow = LinearRegression[PSlow](close)
ZLEMAFast = ZLEMA[PFast](close)
ZLEMASlow = ZLEMA[PSlow](close)
iRSI = RSI[14](close)
IF HullFast > HullSlow and iRSI > iRSI[1] THEN
LongMA1=1
elsif HullFast < HullSlow and iRSI < iRSI[1] THEN
ShortMA1=-1
ENDIF
IF EndPFast > EndPSlow and iRSI > iRSI[1] THEN
LongMA2=2
elsif EndPFast < EndPSlow and iRSI < iRSI[1] THEN
ShortMA2=-2
ENDIF
IF TSFast > TSSlow and iRSI > iRSI[1] THEN
LongMA3=3
elsif TSFast < TSSlow and iRSI < iRSI[1] THEN
ShortMA3=-3
ENDIF
IF LRFast > LRSlow and iRSI > iRSI[1] THEN
LongMA4=4
elsif LRFast < LRSlow and iRSI < iRSI[1] THEN
ShortMA4=-4
ENDIF
IF ZLEMAFast > ZLEMASlow and iRSI > iRSI[1] THEN
LongMA5=5
elsif ZLEMAFast < ZLEMASlow and iRSI < iRSI[1] THEN
ShortMA5=-5
ENDIF
IF LongMA1=1 OR LongMA2=2 OR LongMA3=3 OR LongMA4=4 OR LongMA5=5 THEN
LongMA=+1
ENDIF
IF ShortMA1=-1 OR ShortMA2=-2 OR ShortMA3=-3 OR ShortMA4=-4 OR ShortMA5=-5 THEN
ShortMA=-1
ENDIF
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Da Vinci Trend Detector
PeriodRangelateral = 8//5
MultiplierATR = 0.5
Points = AverageTrueRange[14](close) * MultiplierATR
InclinedPoints = 15
MAPeriod = 50
n = PeriodRangelateral //4 dax
x = Points * pipsize //5 a 15 laterale dax
MA50 = LinearRegression[MAPeriod](close) //da 20 a 60 triangolare
MAhigh = highest[n](MA50)
MAlow = lowest[n](MA50)
MA50range = Mahigh - MAlow
laterale = MA50range <= x
lateraleinclinato = MA50range>x and ma50range<InclinedPoints+x
trend = ma50range>lateraleinclinato+InclinedPoints
if laterale then
ResultDaVinci=1
elsif lateraleinclinato then
ResultDaVinci=2
elsif trend then
ResultDaVinci=3
endif
IF ResultDaVinci>=2 THEN //and (ResultDaVinci <> ResultDaVinci[1])
TrendDaVinci=+1
ENDIF
// TSI
PeriodTSIFast = 9
PeriodTSISlow = 2 * PeriodTSIFast
aTSI = (ExponentialAverage[PeriodTSIFast](ExponentialAverage[PeriodTSISlow](ROC[1](close))))
bTSI = (ExponentialAverage[PeriodTSIFast](ExponentialAverage[PeriodTSISlow](ABS(ROC[1](close)))))
TSI = 100 * (aTSI/bTSI)
LongMAExit = HullAverage[PExitL](close)
ShortMAExit = HullAverage[PExitS](close)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Combo
LongDaVinci=0
ShortDaVinci=0
IF LongRMS=+1 AND LongSto=+1 AND LongRVI=+1 AND LongMA=+1 AND TrendDaVinci=+1 AND (TSI>TSI[1]) AND (close>open and close>close[1] and close>open[1]) THEN
LongDaVinci=+1
ENDIF
IF ShortRMS=-1 AND ShortSto=-1 AND ShortRVI=-1 AND ShortMA=-1 AND TrendDaVinci=+1 AND (TSI<TSI[1]) AND (close<open and close<close[1] and close<open[1]) THEN
ShortDaVinci=-1
ENDIF
IF LongDaVinci=+1 THEN
MySignal = LongDaVinci
ENDIF
IF ShortDaVinci=-1 THEN
MySignal = ShortDaVinci
ENDIF
x = MySignal
IF MySignal = MySignal[1] THEN
x = 0
ENDIF
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TIMEFRAME(DEFAULT)
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
IF NOT LongOnMarket AND (x = 1) THEN
BUY 1 CONTRACTS AT MARKET
endif
if LongOnMarket and -(x = -1) then
SELL AT MARKET
SELLSHORT 1 CONTRACTS AT MARKET
ENDIF
IF NOT ShortOnMarket AND -(x = -1) THEN
SELLSHORT 1 CONTRACTS AT MARKET
endif
if ShortOnMarket and (x = 1) then
EXITSHORT AT MARKET
BUY 1 CONTRACTS AT MARKET
ENDIF
//TIMEFRAME(DEFAULT)
If LongOnMarket AND (close crosses under LongMAExit OR -(x = -1)) THEN
SELL AT MARKET
ENDIF
IF ShortOnMarket AND (close crosses over ShortMAExit OR (x = 1)) THEN
EXITSHORT AT MARKET
ENDIF
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////// BREAKEAVEN ///////////
once breakeaven = 1//0 //1 on - 0 off
//StartBreakeven = 30
//PointsToKeep = 5
//reset the breakevenLevel when no trade are on market
if breakeaven>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*pipsize THEN
//calculate the breakevenLevel
breakevenLevel = tradeprice(1)+PointsToKeep*pipsize
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*pipsize THEN
//calculate the breakevenLevel
breakevenLevel = tradeprice(1)-PointsToKeep*pipsize
ENDIF
//place the new stop orders on market at breakevenLevel
IF breakevenLevel>0 THEN
EXITSHORT AT breakevenLevel STOP
ENDIF
endif
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// Trailing Stop
//------------------------------------------------------------------------------------
IF Not OnMarket THEN
//TrailStart = 10 //10 Start trailing profits from this point
//BasePerCent = 0.100 //10.0% Profit to keep
//StepSize = 6 //6 Pips chunks to increase Percentage
//PerCentInc = 0.100 //10.0% PerCent increment after each StepSize chunk
RoundTO = -0.5 //-0.5 rounds to Lower integer, +0.4 rounds to Higher integer
PriceDistance = 4 * pipsize//8.9 minimun distance from current price
y1 = 0
y2 = 0
ProfitPerCent = BasePerCent
ELSIF LongOnMarket AND close > (TradePrice + (y1 * pipsize)) THEN //LONG
x1 = (close - tradeprice) / pipsize //convert price to pips
IF x1 >= TrailStart THEN //go ahead only if N+ pips
Diff1 = abs(TrailStart - x1)
Chunks1 = max(0,round((Diff1 / StepSize) + RoundTO))
ProfitPerCent = BasePerCent + (BasePerCent * (Chunks1 * PerCentInc))
ProfitPerCent = max(ProfitPerCent[1],min(100,ProfitPerCent))
y1 = max(x1 * ProfitPerCent, y1) //y = % of max profit
ENDIF
ELSIF ShortOnMarket AND close < (TradePrice - (y2 * pipsize)) THEN//SHORT
x2 = (tradeprice - close) / pipsize //convert price to pips
IF x2 >= TrailStart THEN //go ahead only if N+ pips
Diff2 = abs(TrailStart - x2)
Chunks2 = max(0,round((Diff2 / StepSize) + RoundTO))
ProfitPerCent = BasePerCent + (BasePerCent * (Chunks2 * PerCentInc))
ProfitPerCent = max(ProfitPerCent[1],min(100,ProfitPerCent))
y2 = max(x2 * ProfitPerCent, y2) //y = % of max profit
ENDIF
ENDIF
IF y1 THEN //Place pending STOP order when y>0
SellPrice = Tradeprice + (y1 * pipsize) //convert pips to price
IF abs(close - SellPrice) > PriceDistance THEN
IF close >= SellPrice THEN
SELL AT SellPrice STOP
ELSE
SELL AT SellPrice LIMIT
ENDIF
ELSE
SELL AT Market
ENDIF
ENDIF
IF y2 THEN //Place pending STOP order when y>0
ExitPrice = Tradeprice - (y2 * pipsize) //convert pips to price
IF abs(close - ExitPrice) > PriceDistance THEN
IF close <= ExitPrice THEN
EXITSHORT AT ExitPrice STOP
ELSE
EXITSHORT AT ExitPrice LIMIT
ENDIF
ELSE
EXITSHORT AT Market
ENDIF
ENDIF
SET STOP %LOSS 0.5
I expect the signals to be taken on the 1 hour basis
TIMEFRAME(DEFAULT) will read the signal from the TF you are running on (5 minute). You can either run it on 1 hour, or delete TIMEFRAME(DEFAULT)
A LONG signal occurred at the 11:00 candle, which closes at 11:59, so it is valid till 12:59.
A LONG trade was entered at 12:00 (as soon as the 11:00 candle closed) and, since it was exited quite soon, it entered another one according to the same signal.
Thank you robertogozzi and nonetheless!
Do you think of any filter I can add to avoid this or to improve the strategy overall?
Thank you phoentzs
I tried to limit to 1 trade per hour on H1/M1 TF, but results were not satisfactory.
I kept H1/M1 and optimized Trailing Stop, Stop Loss and Exit Strategy and I now have a reasonable outcome on 5k, R/R 2.28, Sharpe 1.2, Win Rate 69% and Net Gain/Drawdown = 6.4. Now, time to test on 100k and see.
I am excited. I always find it very difficult to track trends on the DAX. SP500 or Nasdaq are easier there.
Choppy market is THE PROBLEM !
I know that’s why I don’t like Dax anymore. Or I’m doing something wrong.
I trade manually only Dow (usually directional and you can capture 50 to 100pts quickly), and I have one Algo on NQ (because usually volatile), which I tested in Demo mode for one year, it generated 500% with 1 lot and an SL of 140pts. I’m trying to write an Algo targeting moves of 200pts on Dow, but it requires large SL (200pts)… Lately, I found myself letting the NQ Algo take the trade, then manage the rest manually. The Algo does usually better than me: Psychology is key! The NQ Algo is extremly simple (39 lines), Stoch + MACD with a few filters.
Hello – thank you for posting this. What time zone are you running it on, as my results are negative when I run a backtest? I know I must be doing something wrong 🙂 Thank you
I am testing on an IG CFD demo account, UK time zone on the NASDAQ 5m time frame
The last screenshot is NQ, (k units, H1/1M TF, which is different from the code I posted above (H1/M5), But when I switched to H1M5, I adapted the SL, Trailing Stop, etc. I’m running under French time zone.
Please note the System above is meant to be an example, it’s not a finished, so please feel free to tweak it and let us know how you improved it.
DAX Mtf strategy using a custom trend indicator
This topic contains 13 replies,
has 5 voices, and was last updated by Khaled
4 years, 1 month ago.
| Forum: | ProOrder: Automated Strategies & Backtesting |
| Language: | English |
| Started: | 01/25/2019 |
| Status: | Active |
| Attachments: | 5 files |
The information collected on this form is stored in a computer file by ProRealCode to create and access your ProRealCode profile. This data is kept in a secure database for the duration of the member's membership. They will be kept as long as you use our services and will be automatically deleted after 3 years of inactivity. Your personal data is used to create your private profile on ProRealCode. This data is maintained by SAS ProRealCode, 407 rue Freycinet, 59151 Arleux, France. If you subscribe to our newsletters, your email address is provided to our service provider "MailChimp" located in the United States, with whom we have signed a confidentiality agreement. This company is also compliant with the EU/Swiss Privacy Shield, and the GDPR. For any request for correction or deletion concerning your data, you can directly contact the ProRealCode team by email at privacy@prorealcode.com If you would like to lodge a complaint regarding the use of your personal data, you can contact your data protection supervisory authority.