Bonjour Nicolas
J’utilise un code réalisé par Juan Jacobs “trend breakout” . Celui-ci m’interesse particulièrement car il fonctionne en général très bien sauf que de temps en temps j’ai une erreur de division par zero.
Aurais-tu la possibilité d’y regarder pour peut-être m’écrire le bout de code manquant
Cordialement
jp
//-------------------------------------------------------------------------
// Code principal : juan final fractal
//-------------------------------------------------------------------------
//Stategy: Trend Breakout
//Author: Juan Jacobs
//Market: Neutral
//Timezone: UTC +2
//Timeframe: 1Hr but not 'completely' timeframe dependant
Defparam cumulateorders = False
defparam preloadbars = 10000
If hour <= 8 or hour >= 22 then //outside market hours
possize = 0
If longonmarket Then
sell at market
Bear = 0
Bull = 0
Trendbreak = 0
optimize = optimize + 1
ElsIf shortonmarket Then
exitshort at market
Bear = 0
Bull = 0
Trendbreak = 0
optimize = optimize + 1
EndIf
Else
possize = 1 //position contract size
EndIf
// Heuristics Algorithm Start
once period =8
Increment = 1
MaxIncrement = 5
Reps = 7 //Number of bars to use for analysis
once PIncPos = 1 //Positive Increment Position
once NIncPos = 1 //Neative Increment Position
once Optimize = 0 ////Initialize Heuristicks Engine Counter (Must be Incremented at Position Start or Exit)
once Mode = 1 //Switches between negative and positive increments
once WinCountB = 0 //Initialize Best Win Count
once StratAvgB = 0 //Initialize Best Avg Strategy Profit
If Optimize = Reps Then
WinCountA = 0 //Initialize current Win Count
StratAvgA = 0 //Initialize current Avg Strategy Profit
For z = 1 to Reps Do
If positionperf(z) > 0 Then
WinCountA = WinCountA + 1 //Increment Current WinCount
EndIf
StratAvgA = StratAvgA + StrategyProfit[z]
Next
StratAvgA = StratAvgA/Reps //Calculate Current Avg Strategy Profit
If StratAvgA >= StratAvgB Then
StratAvgB = StratAvgA //Update Best Strategy Profit
BestA = Period
EndIf
If WinCountA >= WinCountB Then
WinCountB = WinCountB //Update Best Win Count
BestB = Period
EndIf
If WinCountA > WinCountB and StratAvgA > StratAvgB Then
Mode = 0
ElsIf WinCountA < WinCountB and StratAvgA < StratAvgB and Mode = 1 Then
Period = Period - (Increment*NIncPos)
NIncPos = NIncPos + Increment
Mode = 2
ElsIf WinCountA >= WinCountB or StratAvgA >= StratAvgB and Mode = 1 Then
Period = Period + (Increment*PIncPos)
PIncPos = PIncPos + Increment
Mode = 1
ElsIf WinCountA < WinCountB and StratAvgA < StratAvgB and Mode = 2 Then
Period = Period + (Increment*PIncPos)
PIncPos = PIncPos + Increment
Mode = 1
ElsIf WinCountA > WinCountB or StratAvgA > StratAvgB and Mode = 2 Then
Period = Period - (Increment*NIncPos)
NIncPos = NIncPos + Increment
Mode = 2
EndIf
If NIncPos > MaxIncrement or PIncPos > MaxIncrement Then
If BestA = BestB Then
Period = BestA
Else
Period = (BestA+BestB)/2
EndIf
NIncPos = 1
PIncPos = 1
EndIF
Optimize = 0
EndIf
// Heuristics Algorithm End
//Calculate Fractal
once Trendbreak = 0
If Trendbreak = 0 Then //No potential trendbreaks have been identified
once FracH = 0 //initialize fractal count
once FracL = 0 //initialize fractal count
For i = 0 to 4 Do
If high[2] > high[i] Then //look for high fractal with 2 lower highs on either side
FracH = FracH + 1
EndIf
If low[2] < low[i] Then //look for low fractal with 2 higher lows on either side
FracL = FracL + 1
EndIf
Next
If FracH = 4 Then //High Fractal Identified
If Bear = 0 and Bull = 0 Then
Bear = 1 //Initialize first trend direction
EndIf
FractalH = high[2] //High Fractal Price Level
LFractalH = low[2] //Low of High Fractal
If Bear = 1 Then
FractalP = barindex - 2 //High Fractal Bar Position
EndIf
ElsIf FracL = 4 Then //Low Fractal Identified
If Bear = 0 and Bull = 0 Then
Bull = 1 //Initialize first trend direction
EndIf
FractalL = low[2] //Low Fractal Price Level
HFractalL = high[2] //High of Low Fractal
If Bull = 1 Then
FractalP = barindex - 2 //Low Fractal Bar Position
EndIf
EndIf
FracH = 0 //reset fractal count
FracL = 0 //reset fractal count
//Calculate trendline using widest angle from extreme of last fractal to curent extreme (Trigonometry Function)
If barindex = FractalP + 2 Then //Initialize angle using 1st candle after fractal candle
If Bear = 1 Then
MaxAngle = Tan(1/(FractalH - high[1])) //Initial value of the angle between the fractal and the last high
ElsIf Bull = 1 Then
MaxAngle = Tan(1/(low[1]-FractalL)) //Initial value of the angle between the fractal and the last high
EndIf
EndIf
EndIf
If Bear = 1 Then //Down trend
Height = FractalH - high //Calcululate height between high fractal and high of current bar
HeightB = FractalH - close[1] //Calcululate height between high fractal and close of current bar (used to determine trend violation)
ElsIf Bull = 1 Then //Up trend
Height = Low - FractalL //Calcululate height between low fractal and low of current bar
HeightB = close[1] - FractalL //Calcululate height between high fractal and close of current bar (used to determine trend violation)
EndIf
If barindex > FractalP + 2 Then //If current bar greater than end of fractal set:
If Bear = 1 Then //For Down Trend
If Trendbreak = 1 and close >= close[1] Then
Trendbreak = 2 //Trend violation confirmed, there was a close outside the trend line (close angle > trend)
BreakoutP = Barindex //Breakout position
OrderLevel = LFractalH + (AverageTrueRange[PERIOD](close)*1.618) //Enter into long position at specified price level
PreviousLow = LFractalH //Level to re-initate preious trend
ElsIf Trendbreak = 1 and close <= open[1] Then
Trendbreak = 0//Trend violation invalidated, latest close did not confirm
MaxAngle = TAngle //New max trend as trend break was invalidated
EndIf
If Trendbreak = 0 and (Tan((barindex-FractalP)/Height) > MaxAngle) and close <= open Then
MaxAngle = Tan((barindex-FractalP)/Height) //calculate new max trend if down trend rules are valid
ElsIf Trendbreak = 0 and (Tan((barindex-FractalP)/HeightB) > MaxAngle) and close > open Then
TrendBreak = 1 //Trend violation potentially present, wait for confirmation on close of next bar
TAngle = Tan((barindex-FractalP)/Height) //Save temporary angle to use if trend violation invalidated
EndIF
ElsIf Bull = 1 Then //For Up Trend
If Trendbreak = 1 and close <= close[1] Then
Trendbreak = 2 //Trend violation confirmed, there was a close outside the trend line (close angle < trend)
BreakoutP = Barindex //Breakout position
OrderLevel = HFractalL - (AverageTrueRange[PERIOD](close)*1.618) //Enter into short position at specified price level
PreviousHigh = HFractalL //Level to re-initiate previous trend
ElsIf Trendbreak = 1 and close >= open[1] Then
MaxAngle = TAngle //New max trend as trend break was invalidated
Trendbreak = 0
EndIf
If Trendbreak = 0 and (Tan((barindex-FractalP)/Height) > MaxAngle) and close >= open Then
MaxAngle = Tan((barindex-FractalP)/Height) //calculate new max trend if up trend rules are valid
ElsIf Trendbreak = 0 and (Tan((barindex-FractalP)/HeightB) > MaxAngle) and close < open Then
TrendBreak = 1 //Trend violation potentially present, wait for confirmation on close of next bar
TAngle = Tan((barindex-FractalP)/Height) //Save temporary angle to use if trend violation invalidated
EndIf
EndIf
EndIf
If Trendbreak = 2 Then //Trend violation is confirmed:
If Bear = 1 and close > PreviousLow Then
Buy possize contract at OrderLevel stop
ElsIf Bull = 1 and close < PreviousHigh Then
Sellshort possize contract at OrderLevel stop
EndIf
If barindex >= BreakoutP Then
If Bear = 1 and longonmarket Then //If position is opened counter last trend, reset variables and initialize new trend
Trendbreak = 0
Bear = 0
Bull = 1
optimize = optimize + 1
ElsIf Bear = 1 and close < PreviousLow Then //New trend invalidated and previous trend resumed
Trendbreak = 0
ElsIf Bull = 1 and shortonmarket Then //If position is opened counter last trend, reset variables and initialize new trend
Trendbreak = 0
Bear = 1
Bull = 0
optimize = optimize + 1
ElsIf Bull = 1 and close > PreviousHigh Then //New trend invalidated and previous trend resumed
Trendbreak = 0
EndIf
EndIf
EndIf
//Trade Management (Note that Trend Direction need to be reset with exits's)
Deviations = 1.5
periods = 32
PRICE = LOG(customclose)
alpha = 2/(PERIODS+1)
if barindex < PERIODS then
EWMA = AVERAGE[3](PRICE)
else
EWMA = alpha * PRICE + (1-alpha)*EWMA
endif
error = PRICE - EWMA
dev = SQUARE(error)
if barindex < PERIODS+1 then
var = dev
else
var = alpha * dev + (1-alpha) * var
endif
ESD = SQRT(var)
BollU = EXP(EWMA + (DEVIATIONS*ESD))
BollL = EXP(EWMA - (DEVIATIONS*ESD))
RS2 = ROUND(RSI[2](close))
PSH = Highest[100](high)[1]
PSL = Lowest[100](low)[1]
If longonmarket and ((close[1] > BollU and close < BollU) or (high[1] > BollU and high < BollU) or (close > PSH or close < PSL)) Then
LE = 1
ElsIf shortonmarket and ((close[1] < BollL and close > BollL) or (low[1] < BollL and low > BollL) or (close < PSL or close > PSH)) Then
SE = 1
EndIf
If longonmarket and LE = 1 and RS2 >= 85 and close < BollU Then
Sell at market
Bear = 0
Bull = 0
optimize = optimize + 1
ElsIf shortonmarket and SE = 1 and RS2 <= 15 and close > BollL Then
Exitshort at market
Bear = 0
Bull = 0
optimize = optimize + 1
EndIf
Re bonjour
je pensais avoir inséré le code dans “prt insert code”.Désolé mais ce logo n’est plus dans la barre outil ??
cdt
jp
Bonsoir, Nicolas n’est pas disponible, pour débugguer une div par zéro si on ne la trouve pas facilement, on peut se transformer sa stratégie en indicateur, identifier les dénominateurs de toutes les divisions faites, et les mettre dans des variables retournées dans la ligne return de l’indicateur. Quand tu tombes visuellement sur l’une d’entre elle à zéro, tu en conclus la modif à faire selon le cas.
Nicolas est au courant qu’il y a effectivement un bug avec le bouton “insert PRT code” parfois disparu. Je reformate le code ci-dessus. En attendant que le bug soit résolu, si avant de taper son message on voit que le bouton est absent, on peut faire un CTRL+touche F5 qui devrait le faire revenir (et si on a commencé un message et qu’on ne veut pas le perdre, penser à le copier quelque part ailleurs dans un word ou un bloc-note, car ctrl+f5 va obliger à recommencer le message à zéro).
thanked this post
Bonjour @saccucci,
le mieux est d’utiliser l’instruction GRAPH pour afficher la valeur des variables utilisées comme dénominateur et/ou le résultat des calculs entre parenthèses.
Sinon, une autre possibilité serait d’ajouter un très petit nombre au dénominateur de chaque division, par exemple 0.0000001. Cela vous garantirait de ne plus avoir cette erreur mais cela pourrait changer le comportement du robot…
Finalement la question serait : est-il normal de vous retrouver avec un zéro au dénominateur ? Il y a peut-être un problème de conception dans votre algo ? ou quelque chose à revoir ?