Bonjour à la communauté…
je sèche sur la création d’une condition de validité d’une prise de position, j’ai parcouru les lignes de code disponibles dans la bibliothèque sans trouver mon bonheur.
J’ai dans mon code un certain nombre de condition pour prendre position en LONG ou SHORT dont des croisements de moyenne mobile. l’algorithme fonctionne bien et me donne de bon résultat mais sur certaine prise de position j’ai des faut signal.
Je n’utilise pas de stop loss, uniquement un trailingstar et trailingstep.
je souhaiterai néanmoins insérer une condition à mes prises de positions LONG et SHORT pour couper mon trade juste après une prise de position, si l’interval entre la prise de position et un nouveau croisement de moyenne mobile est inférieur à (X) période.
Pour cela on peut tester si une condition booléenne a eu lieu dans les X dernière périodes :
f = average[7]
s = average[20]
test = f crosses over s //booléen
x = 10 //quantité de périodes
a = summation[x](test)>0 //au moins 1 test a eu lieu dans les X dernières périodes
return a
Merci Nicolas,
Merci pour la réponse, mais ce n’est pas exactement ce que je cherche.
Dans le code ci-dessus et ci j’ai bien compris, il s’agit de vérifier si dans les dix dernières periodes, les moyennes se sont croisées.
Dans mon cas ce que je cherche à faire, c’est couper la position que je viens de prendre, si, dans les futures [x] période les moyennes se croisent à nouveau, mais seulement ce croisement précis [x] periode après la prise de position…. je ne veux pas tenir compte des croisements futures.
// Conditions pour ouvrir une position acheteuse
c1a = (low > MM5)
c2a = (MM5 > MM50)
c3a = (MM5 CROSSES OVER MM20)
IF (c1a AND c2a AND c3a AND (myInverseFisherTransformsRSI CROSSES OVER 53)) AND not daysForbiddenEntry THEN
BUY 10 SHARES AT MARKET
ENDIF
// Conditions pour fermer une position acheteuse
IF summation[x](MM5 CROSSES UNDER MM20)>1 THEN // ici ce qui m'interesse c'est de pouvoir fermer la position si et seulement si un croisement intervient [x] periodes après la prise de position.
SELL AT MARKET
ENDIF
Slts
Très bien, dans ce cas on fait une différence des BARINDEX et TRADEINDEX:
if onmarket and barindex-tradeindex<=10 and croisement then
//ferme la position
endif
Fantasio2020 je reformate le code dans ton post ci-dessus, merci d’utiliser directement le bouton “insert prt code” dans les prochains messages, voir image si besoin de le localiser pour la première fois.
Bonjour à la communauté,
ci-dessous une petite stratégie pour le DOW en M1
Moyennes Mobiles + MACD + Inverse Transforme RSI
je suis débutant et je suis pas a l’abris de faire des erreurs.
votre avis m’interesse et des retouche de code également
// Définition des paramètres du code
DEFPARAM CumulateOrders = True // Cumul des positions désactivé
//DEFPARAM FLATBEFORE = 091500
//DEFPARAM FLATAFTER = 154500
daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
// Moyenne Mobile et MACD
MMA = (ExponentialAverage[8](close)[1]*1.00002)
MMB = (Average[20](close)[3]*1.00003)
MMC = (MACDline[8,24,8](close)[1])
MMD = (TriangularAverage[8](MACDline[8,24,8](close)[1]))
// Inverse Transform RSI
Ind = RSI[11](Close)
x = 0.1 * (Ind - 50)
y = (EXP (2 * x) - 1) / (EXP (2 * x) + 1)
z = 50 * (y + 1)
// Conditions pour ouvrir une position acheteuse
c1a = close > MMA
c2a = MMA CROSSES OVER MMB
c3a = MMA > MMB
c4a = MMC > MMD
c5a = z CROSSES OVER 50
IF (c1a AND c2a AND c3a AND c4a AND c5a) AND not daysForbiddenEntry THEN
BUY 0.2 SHARES AT MARKET
ENDIF
// Conditions pour fermer une position acheteuse
if onmarket and barindex-tradeindex <=7 and (MMA CROSSES UNDER MMB) then
SELL AT MARKET
endif
// Conditions pour ouvrir une position en vente à découvert
c1b = open < MMA
c2b = MMA CROSSES UNDER MMB
c3b = MMA < MMB
c4b = MMC < MMD
c5b = z CROSSES UNDER 47
IF (c1b AND c2b AND c3b AND c4b AND c5b) AND not daysForbiddenEntry THEN
SELLSHORT 0.2 SHARES AT MARKET
ENDIF
// Conditions pour fermer une position vendeuse
if onmarket and barindex-tradeindex <=5 and (MMA CROSSES OVER MMB) then
SELL AT MARKET
endif
//************************************************************************
//trailing stop function
trailingstart = 6 //trailing will start @trailinstart points profit
trailingstep = 18 //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
//************************************************************************
//Stops et objectifs
SET STOP %LOSS 2
SET TARGET %PROFIT 1.15
//************************************************************************
ci-joint mon code actuel pour le Wall Street Cash M1….
j’ai l’impression que mes paramètres stop et objectif ne fonctionne pas.
je suis pas contre un peu d’optimisation du code…
Slts
Slts
Bsr Nicolas,
j’ai essayé ton code, mais j’ai l’impression que ça ne fonctionne pas!
// Conditions pour fermer une position vendeuse
IF ONMARKET and barindex-tradeindex<=6 AND (MMA CROSSES OVER MMB) THEN
SELL AT MARKET
ENDIF
voici l’exemple ci-dessus…et le résultat en image
j’ai une prise de position le 29 avril à 1H58 mais la position ne se cloture pas lorsque MMA CROSSES OVER MMB….Quid
Merci d’avance
Tes moyennes mobiles ne correspondent pas à ceux du graphique, voir ton autre sujet à ce propos (décalage verticale en pourcentage).
Bonjour Nicolas,
Non Non, les moyennes sur le graph, sont celles retracées par le contrôle ci-dessous:
MMA = (ExponentialAverage[8](close)[1]*1.0000027)
MMB = (Average[21](close)[2]*1.000047)
MMC = (MACDline[8,24,8](TypicalPrice)[1])
MMD = (TriangularAverage[7](MACDline[8,24,8](TypicalPrice)[1]))
GRAPH (c1b AND c2b AND c3b AND c4b AND c5b)
graphonprice MMA
graphonprice MMB
graphonprice MMC
graphonprice MMD
à te lire
Le code complet (DAX M1)
// Définition des paramètres du code
DEFPARAM CumulateOrders = True // Cumul des positions désactivé
//DEFPARAM FLATBEFORE = 091500
//DEFPARAM FLATAFTER = 154500
daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
// Moyenne Mobile et MACD
MMA = (ExponentialAverage[8](close)[1]*1.0000027)
MMB = (Average[21](close)[2]*1.000047)
MMC = (MACDline[8,24,8](TypicalPrice)[1])
MMD = (TriangularAverage[7](MACDline[8,24,8](TypicalPrice)[1]))
// Inverse Transform RSI
Ind = RSI[11](Close)
x = 0.1 * (Ind - 50)
y = (EXP (2 * x) - 1) / (EXP (2 * x) + 1)
z = 50 * (y + 1)
// Conditions pour ouvrir une position acheteuse
c1a = close > MMA
c2a = MMA CROSSES OVER MMB
c3a = MMA > MMB
c4a = MMC > MMD
c5a = z CROSSES OVER 50
IF (c1a AND c2a AND c3a AND c4a AND c5a) AND not daysForbiddenEntry THEN
BUY 2 SHARES AT MARKET
SET STOP %LOSS 2
SET TARGET %PROFIT 1.15
// Conditions pour fermer une position acheteuse
IF ONMARKET AND barindex-tradeindex<=6 and (MMA CROSSES UNDER MMB) then
SELL AT MARKET
ENDIF
ENDIF
// Conditions pour ouvrir une position en vente à découvert
c1b = open < MMA
c2b = MMA CROSSES UNDER MMB
c3b = MMA < MMB
c4b = MMC < MMD
c5b = z CROSSES UNDER 47
IF (c1b AND c2b AND c3b AND c4b AND c5b) AND not daysForbiddenEntry THEN
SELLSHORT 2 SHARES AT MARKET
SET STOP %LOSS 2
SET TARGET %PROFIT 1.15
// Conditions pour fermer une position vendeuse
IF ONMARKET and barindex-tradeindex<=6 AND (MMA CROSSES OVER MMB) THEN
SELL AT MARKET
ENDIF
ENDIF
//************************************************************************
//trailing stop function
trailingstart = 7 //trailing will start @trailinstart points profit
trailingstep = 13 //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
//************************************************************************
//GRAPH (c1b AND c2b AND c3b AND c4b AND c5b)
//graphonprice MMA
//graphonprice MMB
//graphonprice MMC
//graphonprice MMD
Donc tu as graphé (MMA CROSSES OVER MMB) ? pour voir si cette condition était respectée ?
Bonjour Nicolas,
Oui j’ai graphé le code pour m’assurer que les conditions soient respectées…et la position qui aurait dû se couper au croisement de MMA et MMB ne s’est pas coupé.
pour l’instant, c’est ce point qui me tracasse…ça me crée du drawdown quand le trade par dans le mauvais sens.
Si tu as une autre idée ou une solution, je suis preneur.
Slts
IF (c1a AND c2a AND c3a AND c4a AND c5a) AND not daysForbiddenEntry THEN
BUY 0.3 SHARES AT MARKET
SET STOP %LOSS 2
SET TARGET %PROFIT 1.15
ENDIF
// Conditions pour fermer une position acheteuse
IF ONMARKET AND barindex-tradeindex<=6 and (MMA CROSSES UNDER MMB) then
SELL AT MARKET
ENDIF
// Conditions pour ouvrir une position en vente à découvert
c1b = open < MMA
c2b = MMA CROSSES UNDER MMB
c3b = MMA < MMB
c4b = MMC < MMD
c5b = z CROSSES UNDER 47
IF (c1b AND c2b AND c3b AND c4b AND c5b) AND not daysForbiddenEntry THEN
SELLSHORT 0.3 SHARES AT MARKET
SET STOP %LOSS 2
SET TARGET %PROFIT 1.15
ENDIF
// Conditions pour fermer une position vendeuse
IF ONMARKET and barindex-tradeindex<=6 AND (MMA CROSSES OVER MMB) THEN
EXITSHORT AT MARKET
ENDIF
Comme ceci?