Hello. I share with the community an automatic strategy that works on the DAX for M5 timeframe.
I wanted to test a strategy that involves observing the angular orientation of a moving average. The higher the angle and slope of the moving average, the stronger and more directional the movement. It then took me back into my distant memories of mathematics courses, trigonometry and vector calculus to try to develop this code.
The strategy consists first of all in calculating the angular orientation of a 10-period moving average (PeriodA) over a period of 15 bars (nbChandelierA). Then I calculate the slope of this moving average. This amounts to calculating a “vector” hence the name of the algorithm.
I then added a trailing stop and optimized the entry points with particular playing on the variable “lag”.
I launched the code in real mode and in demo mode. There are some differences in the positions, but overall it works pretty well.
Note that the attached backtest images correspond to a backtest on 200,000 bars (Premium version of PRT) with a spread of 1.5 points.
The code looks good on other timeframes (including H4) by changing the variables.
Looking forward to having your feedback, feedback and suggestions for improvements.
// ROBOT VECTORIAL DAX
// M5
// SPREAD 1.5
// by BALMORA 74 - FEBRUARY 2019
DEFPARAM CumulateOrders = false
DEFPARAM Preloadbars = 50000
//VARIABLES
CtimeA = time >= 080000 and time <= 220000
CtimeB = time >= 080000 and time <= 220000
ONCE BarLong = 950 //EXIT ZOMBIE TRADE LONG
ONCE BarShort = 650 //EXIT ZOMBIE TRADE SHORT
// TAILLE DES POSITIONS
ONCE PositionSizeLong = 1
ONCE PositionSizeShort = 2
//STRATEGIE
//VECTEUR = CALCUL DE L'ANGLE
ONCE PeriodeA = 10
ONCE nbChandelierA= 15
MMA = Exponentialaverage[PeriodeA](close)
ADJASUROPPO = (MMA-MMA[nbchandelierA]*pipsize) / nbChandelierA
ANGLE = (ATAN(ADJASUROPPO)) //FONCTION ARC TANGENTE
CondBuy1 = ANGLE >= 45
CondSell1 = ANGLE <= - 37
//VECTEUR = CALCUL DE LA PENTE ET SA MOYENNE MOBILE
ONCE PeriodeB = 20
ONCE nbChandelierB= 35
lag = 5
MMB = Exponentialaverage[PeriodeB](close)
pente = (MMB-MMB[nbchandelierB]*pipsize) / nbchandelierB
trigger = Exponentialaverage[PeriodeB+lag](pente)
CondBuy2 = (pente > trigger) AND (pente < 0)
CondSell2 = (pente CROSSES UNDER trigger) AND (pente > -1)
//ENTREES EN POSITION
CONDBUY = CondBuy1 and CondBuy2 and CTimeA
CONDSELL = CondSell1 and CondSell2 and CtimeB
//POSITION LONGUE
IF CONDBUY THEN
buy PositionSizeLong contract at market
SET TARGET %PROFIT 4.25
ENDIF
//POSITION COURTE
IF CONDSELL THEN
Sellshort PositionSizeShort contract at market
SET TARGET %PROFIT 1.25
ENDIF
//VARIABLES STOP SUIVEUR
ONCE trailingStopType = 1 // Trailing Stop - 0 OFF, 1 ON
ONCE trailingstoplong = 7.5 // Trailing Stop Atr Relative Distance
ONCE trailingstopshort = 4 // Trailing Stop Atr Relative Distance
ONCE atrtrailingperiod = 25 // Atr parameter Value
ONCE minstop = 0 // Minimum Trailing Stop Distance
// TRAILINGSTOP
//----------------------------------------------
atrtrail = AverageTrueRange[atrtrailingperiod]((close/10)*pipsize)/1000
trailingstartl = round(atrtrail*trailingstoplong)
trailingstartS = round(atrtrail*trailingstopshort)
if trailingStopType = 1 THEN
TGL =trailingstartl
TGS=trailingstarts
if not onmarket then
MAXPRICE = 0
MINPRICE = close
PREZZOUSCITA = 0
ENDIF
if longonmarket then
MAXPRICE = MAX(MAXPRICE,close)
if MAXPRICE-tradeprice(1)>=TGL*pointsize then
if MAXPRICE-tradeprice(1)>=MINSTOP then
PREZZOUSCITA = MAXPRICE-TGL*pointsize
ELSE
PREZZOUSCITA = MAXPRICE - MINSTOP*pointsize
ENDIF
ENDIF
ENDIF
if shortonmarket then
MINPRICE = MIN(MINPRICE,close)
if tradeprice(1)-MINPRICE>=TGS*pointsize then
if tradeprice(1)-MINPRICE>=MINSTOP then
PREZZOUSCITA = MINPRICE+TGS*pointsize
ELSE
PREZZOUSCITA = MINPRICE + MINSTOP*pointsize
ENDIF
ENDIF
ENDIF
if onmarket and PREZZOUSCITA>0 then
EXITSHORT AT PREZZOUSCITA STOP
SELL AT PREZZOUSCITA STOP
ENDIF
ENDIF
//EXIT ZOMBIE TRADE
IF POSITIONPERF<0 THEN
IF shortOnMarket AND BARINDEX-TRADEINDEX(1)>= barshort THEN
EXITSHORT AT MARKET
ENDIF
ENDIF
IF POSITIONPERF<0 THEN
IF LongOnMarket AND BARINDEX-TRADEINDEX(1)>= barlong THEN
SELL AT MARKET
ENDIF
ENDIF