Bonjour a tous
j’essaie de coder une strategie qui short 1 contrat sur chacun des 3 niveaux: SMA200, SMA200+2.5$, SMA200+5$.
Si le niveau n’est pas atteint, la position n’est pas ouverte, donc le nombre de positions est compris entre 0 et 3.
Mon souci dans le code est de differencier chaque position. J’ai mis un flag pour qu’il ne puisse y avoir qu’une seule position a chaque niveau.
En bactest ma premiere position (SMA200) est ouverte mais pas les 2 suivantes. Et pourtant les conditions du “if .. then” semblent etre toutes satisfaites pour les 2 autres niveaux d’ouverture de position.
J’imagine que le probleme vient de ma gestion de la variable “flag” mais quand je la graph elle semble se comporter normalement.
defparam cumulateorders = true
MM200 = average[200](close)
ecart = 2.5
bandemax = MM200 + 2 * ecart
bandehaute = MM200 + 1 * ecart
bandebasse = MM200 - 1 * ecart
maxlong = countofshortshares
if high[1] < bandemax and high[0]>bandemax and not flag = 3 and maxlong < 3 then
flag = 3
sellshort 1 contract at market
endif
if high[1] < bandehaute and high[0] > bandehaute and not flag = 2 and maxlong < 3 then
flag = 2
sellshort 1 contract at market
endif
if high[1] < MM200 and high[0]>MM200 and not flag = 1 and maxlong < 3 then
flag = 1
sellshort 1 contract at market
endif
if low[0] < bandebasse then
exitshort at market
flag = 0
endif
set stop ploss 5
Toute aide sera la bienvenue, merci !
Greg
Je n’ai pas testé, mais le problème vient peut être de la façon dont tu testes ta variable “flag” justement:
defparam cumulateorders = true
MM200 = average[200](close)
ecart = 2.5
bandemax = MM200 + 2 * ecart
bandehaute = MM200 + 1 * ecart
bandebasse = MM200 - 1 * ecart
maxlong = countofshortshares
if high[1] < bandemax and high[0]>bandemax and flag <> 3 and maxlong < 3 then
flag = 3
sellshort 1 contract at market
endif
if high[1] < bandehaute and high[0] > bandehaute and flag <> 2 and maxlong < 3 then
flag = 2
sellshort 1 contract at market
endif
if high[1] < MM200 and high[0]>MM200 and flag <> 1 and maxlong < 3 then
flag = 1
sellshort 1 contract at market
endif
if low[0] < bandebasse then
exitshort at market
flag = 0
endif
set stop ploss 5
NOT test si une variable est fausse ou vrai, hors dés que celle-ci est renseignée (différente de 0), elle est forcément vrai.