Bonjour,
J’utilise cette routine pour gérer un stop suiveur, cela fonctionne très bien avec les longs mais le trade est tout de suite coupé à la bougie suivante sur les short.
Je ne suis pas sûr que mon rachat de short stop fonctionne correctement, bien que je ne vois pas comment je pourrais le coder autrement.
Une idée de ce qui ne fonctionne pas ?
Merci pour votre aide
IF TSL = 1 THEN
//trailing stop function
trailingstart = 8 //trailing will start @trailinstart points profit
trailingstep = 4 // 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 THEN
newSL = tradeprice(1)+trailingstep
ENDIF
//next moves
IF newSL>0 AND close-newSL>=trailingstep THEN
newSL = newSL+trailingstep
ENDIF
Sell at newSL stop // Place un ordre de vente stop en guise de seuil de stopsuiveur
graph newSL coloured (0,255,255) as "newSL"
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND tradeprice(1)-close>=trailingstart THEN
newSL = tradeprice(1)-trailingstep
ENDIF
//next moves
IF newSL>0 AND newSL-close>=trailingstep THEN
newSL = newSL-trailingstep
ENDIF
Exitshort at newSL stop
graph newSL coloured (255,255,255) as "newSL SHORT"
ENDIF
ENDIF
Durant les backtests ou en trading live ?
Voici le code complet à backtester en UT 4 minutes.
Je suis chez IG sur CFD
// UT = 4 minutes
DEFPARAM CumulateOrders = False // Cumul des positions désactivé
//Annule tous les ordres en attente et ferme toutes les positions à 0:00, puis empêche toute création d'ordre avant l'heure "FLATBEFORE".
DEFPARAM FLATBEFORE = 070000
////////// Annule tous les ordres en attente et ferme toutes les positions à l'heure "FLATAFTER"
DEFPARAM FLATAFTER = 213000
DEFPARAM Preloadbars = 3000
ONCE TSL = 1 // Use TSL?
// Evite l'ouverture de WS
Temps1 = time >= 150000 and time < 160000
MME100 = Average[100,1](close)
//// LONG ////
c1 = high > MME100 and close > open
c2 = close - open > 14// bougie de x point minimum
c3 = high[1] < MME100 and open[1] < close[1]
c4 = 1
c5 = 1
C13 = c1 and c2 and c3 and c4 and c5 and not Temps1 and NOT LongOnMarket
IF C13 THEN
BUY 1 CONTRACT AT MARKET
SET STOP LOSS (close-low) // SL sous le plus bas de la bougie précédente
ENDIF
///// SHORT ////
c6 = low < MME100 and close < open
c7 = open - close > 14// bougie de x point minimum
c8 = low[1] > MME100 and open[1] > close[1]
c9 = 1
c10 = 1
C14 = c6 and c7 and c8 and c9 and c10 and not Temps1 and NOT ShortOnMarket
IF C14 THEN
Sellshort 1 CONTRACT AT MARKET
SET STOP LOSS (low-close) // SL sur le plus haut de la bougie précédente
graph (high-close) coloured (255,0,0) as "SL SELL"
ENDIF
// *****************************
IF TSL = 1 THEN
//trailing stop function
trailingstart = 8 //trailing will start @trailinstart points profit
trailingstep = 4 // 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 THEN
newSL = tradeprice(1)+trailingstep
ENDIF
//next moves
IF newSL>0 AND close-newSL>=trailingstep THEN
newSL = newSL+trailingstep
ENDIF
Sell at newSL stop // Place un ordre de vente stop en guise de seuil de stopsuiveur
graph newSL coloured (0,255,255) as "newSL"
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND tradeprice(1)-close>=trailingstart THEN
newSL = tradeprice(1)-trailingstep
ENDIF
//next moves
IF newSL>0 AND newSL-close>=trailingstep THEN
newSL = newSL-trailingstep
ENDIF
Exitshort at newSL stop // Place un ordre d'achat stop en guise de seuil de stopsuiveur
graph newSL coloured (255,255,255) as "newSL SHORT"
ENDIF
ENDIF
Ligne 55, tu poses une valeur de stoploss négative :
SET STOP LOSS (low–close)
Le Low ne peut pas être au dessus de Close.
Donc rien à voir avec le trailing stop 🙂