Demande de boucle pour pyramidage, trailing stop et breakeven

Viewing 15 posts - 1 through 15 (of 29 total)
  • Author
    Posts
  • #88887 quote
    FREDNC
    Participant
    Senior

    Bonjours à tous.

    Après des heures recherche sur le site, de debugging et des dizaines d’essais infructueux, je passe la main….Si quelqu’un pouvait me coder ça, ce serait un grand merci.  Il fort probable qu’il fasse tout reprendre à zéro pour obtenir un résultat correspondant à l’idée de départ mais je ne vois pas comment faire….

    Je souhaite mettre en place un renforcement de position si le premier achat est arrivé à son niveau Trailing stop avec son Breakeven et le deuxième achat doit aussi avoir son Trailing stop avec son Breakeven

    Le calcule du SL est basé sur Close-Donchian et Niveau de start du Trailing stop c’est  abs(SL /2 ).

    Le trailing stop est sur une base d’un de code @Nicolas que j’ai essayé d’adapter sans succès.

    Soit je double mes positions, soit quand les conditions du renforcement deviennent valides, je coupe ma premier position au niveau d’entrée.

    Voici le code qui me semble se rapprocher le plus du but. J’ai supprimé tous mes filtres pour simplifier la compréhension se qui explique le nombre de positions et les résultats très médiocre. Dans tous les cas les, la condition de Breakeven de la première position n’est pas respecté pour Pyramidé. La limite de 2 positions non plus. En rajoutant les conditions « onmarket » ou « not onmarket » on crée des conflits, J’ai même eu des résultats que je n’explique même pas….lol  Bref, Je tourne en rond. Je pense qu’il faut une boucle, mais je ne sais pas faire HELP !

     

    //Dax30 M1
    // Définition des paramètres du code
    DEFPARAM CumulateOrders =  true //false // Cumul des positions ACTIVEES***Voir option Pyramidage ***
    DEFPARAM Preloadbars = 20000
    //Nbr de contrats*******************
    N=2
    
    Spread=1
    
    //Pyramidage si Be + limitation de position************************
    //**Nonbre de Positions autorisées**
    NbrPosi=2
    
    if Pyra1=0 and Pyra2=0 and breakeven1=0 and breakeven2=0 and (abs(COUNTOFPOSITION)< NbrPosi) then
    Pyra1=1
    pyra2=0
    elsif Pyra1 and breakeven1>0 or (abs(COUNTOFPOSITION)>0) then//
    pyra2=1
    Pyra1=0
    elsif Pyra2 and breakeven2>0 or (abs(COUNTOFPOSITION)>NbrPosi) then
    Pyra1=0
    pyra2=0
    else
    breakeven1=0
    breakeven2=0
    endif
    
    
    //elsif Pyra1=0 and Pyra2=0 and breakeven1=0 and breakeven2=0 and (abs(COUNTOFPOSITION)< NbrPosi) then
    //Pyra1=1
    //pyra2=0
    //elsif Pyra1 and breakeven1>0  then//
    //Pyra1=0
    //pyra2=1
    //elsif Pyra1=0 and Pyra2=0 and Breakeven2=0 and (abs(COUNTOFPOSITION)< NbrPosi) then
    //Pyra1=1
    //pyra2=0
    //endif
    //elsif Pyra1 and breakevenLevel1>0 and (abs(COUNTOFPOSITION)<NbrPosi) then//
    //Pyra1=0
    //pyra2=1
    //elsif Pyra2 and breakevenLeve2 =0 and (abs(COUNTOFPOSITION)<NbrPosi) then
    //Pyra1=0
    //pyra2=1
    //elsif Pyra2 and breakevenLeve2>0 or (abs(COUNTOFPOSITION)>NbrPosi) then
    //Pyra1=0
    //pyra2=0
    //endif
    //elsif Pyra2 and breakevenLeve2>0 and (abs(COUNTOFPOSITION)>NbrPosi) then
    //Pyra1=0
    //pyra2=0
    //endif
    
    //Canal Donchian************************************
    DC=15//
    CDH=Highest[DC](high)
    CDB=Lowest[DC](low)
    
    //TDI
    lengthRSI = 13
    lengthband = 34
    lengthrsipl = 2
    lengthtradesl = 7
    
    r = rsi[lengthrsi](close)
    ma = average[lengthband](r)
    offs = (1.6185 * std[lengthband](r))
    up = ma+offs
    dn = ma-offs
    mid = (up+dn)/2
    TDIVert = average[lengthrsipl](r)
    TDIRouge = average[lengthtradesl](r)
    
    // "CROSS 1"******
    IF TDIVert crosses over TDIRouge and TDIVert > mid THEN
    CROSSTa = 1
    elsif TDIVert crosses under TDIRouge and TDIVert < mid THEN
    CROSSTb = 1
    else
    CROSSTa=0
    CROSSTb=0
    ENDIF
    
    // "CROSS 2"*****************************
    IF TDIVert crosses over up THEN
    CROSST2A = 1
    elsif TDIVert crosses under dn THEN
    CROSST2V = -1
    else
    CROSST2A=0
    CROSST2V=0
    ENDIF
    
    //Position 1***********************************************************************************************
    //Long1
    If Pyra1 and CROSSTa then
    BUY N CONTRACT AT MARKET
    SL1 = abs (close-CDB)+(SPREAD/2)
    Bot1=3 //Pour graph la variable. Inutine quand tous fonctionne
    else
    Bot1=0 //Pour graph la variable. Inutine quand tous fonctionne
    endif
    //Short1
    if Pyra1 and CROSSTb then
    SELLSHORT N CONTRACT AT MARKET
    SL1 = abs (CDH-close)+(SPREAD/2)
    Bot1=3 //Pour graph la variable. Inutine quand tous fonctionne
    else
    Bot1=0 //Pour graph la variable. Inutine quand tous fonctionne
    ENDIF
    
    //Trail 1 ************************
    startBreakeven1 = SL1/2 //how much pips/points in gain to activate the breakeven function?
    PointsToKeep1= 1 //how much pips/points to keep in profit above of below our entry price when the breakeven is activated (beware of spread)
    //reset the breakevenLevel1 when no trade are on market
    IF NOT ONMARKET THEN
    breakevenLeveL1=0
    ENDIF
    // --- BUY SIDE ---
    //test if the price have moved favourably of "startBreakeven1" points already
    IF LONGONMARKET and close-tradeprice(1)>=startBreakeven1*pipsize THEN
    //calculate the breakevenLevel1
    breakevenLeveL1 = tradeprice(1)+PointsToKeep1*pipsize
    ENDIF
    //place the new stop orders on market at breakevenLevel1
    IF breakevenLeveL1>0 THEN
    SELL AT breakevenLeveL1 STOP
    Breakeven1=2//-----------------ATTENTION RAJOUTE DANS LE CODE------------------------------------
    SL1=0//------------------------ATTENTION RAJOUTE DANS LE CODE------------------------------------
    ENDIF
    // --- SHORT SIDE ---
    IF SHORTONMARKET and tradeprice(1)-close>startBreakeven1*pipsize THEN
    //calculate the breakevenLevel1
    breakevenLeveL1 = tradeprice(1)-PointsToKeep1*pipsize
    ENDIF
    //place the new stop orders on market at breakevenLevel1
    IF breakevenLeveL1>0 THEN
    EXITSHORT AT breakevenLeveL1 STOP
    Breakeven1=2//-----------------ATTENTION RAJOUTE DANS LE CODE------------------------------------
    SL1=0//------------------------ATTENTION RAJOUTE DANS LE CODE------------------------------------
    ENDIF
    
    
    
    //Position 2***************************************************************************************************
    //Long2
    If Pyra2 and CROSST2A then
    BUY N CONTRACT AT MARKET
    SL2 = abs (close-CDB)+(SPREAD/2)
    TP2 = SL2
    Bot2=5//Pour graph la variable. Inutine quand tous fonctionne
    else
    Bot2=0 //Pour graph la variable. Inutine quand tous fonctionne
    endif
    //Short2
    if Pyra2 and CROSST2V  then//or CROSS1=-1 or CROSS2=-1
    SELLSHORT N CONTRACT AT MARKET
    SL2 = abs (CDH-close)+(SPREAD/2)
    TP2 = SL2
    Bot2=5//Pour graph la variable. Inutine quand tous fonctionne
    else
    Bot2=0 //Pour graph la variable. Inutine quand tous fonctionne
    endif
    
    //Trail 2 **********************
    startBreakeven2= SL2/2 //how much pips/points in gain to activate the breakeven function?
    PointsToKeep2 = 1 //how much pips/points to keep in profit above of below our entry price when the breakeven is activated (beware of spread)
    //reset the breakevenLevel1 when no trade are on market
    IF NOT ONMARKET THEN
    breakevenLeveL2=0
    ENDIF
     
    // --- BUY SIDE ---
    //test if the price have moved favourably of "startBreakeven2" points already
    IF LONGONMARKET and close-tradeprice(1)>=startBreakeven2*pipsize THEN
    //calculate the breakevenLevel1
    breakevenLeveL2 = tradeprice(1)+PointsToKeep2*pipsize
    ENDIF
     
    //place the new stop orders on market at breakevenLevel1
    IF breakevenLeveL2>0 THEN
    SELL AT breakevenLeveL2 STOP
    Breakeven2=2.5//-----------------ATTENTION RAJOUTE DANS LE CODE------------------------------------
    SL2=0//------------------------ATTENTION RAJOUTE DANS LE CODE------------------------------------
    //bot2=0
    ENDIF
    // --- SHORT SIDE ---
    IF SHORTONMARKET and tradeprice(1)-close>startBreakeven2*pipsize THEN
    //calculate the breakevenLevel1
    breakevenLeveL2 = tradeprice(1)-PointsToKeep2*pipsize
    ENDIF
    //place the new stop orders on market at breakevenLevel1
    IF breakevenLeveL2>0 THEN
    EXITSHORT AT breakevenLeveL2 STOP
    Breakeven2=2.5//-----------------ATTENTION RAJOUTE DANS LE CODE------------------------------------
    SL2=0//------------------------ATTENTION RAJOUTE DANS LE CODE------------------------------------
    //bot2=0
    ENDIF
    
    
    
    //Stops et objectifs
    If SL2=0 and SL1 > 0 Then
    SL=SL1
    elsif Sl1=0 and SL2 > 0 then
    SL=SL2
    endif
    SET STOP pLOSS SL
    SET TARGET pPROFIT TP2
    
    
    
    graph Breakeven1
    graph Breakeven2
    graph pyra1
    graph pyra2
    //graph SL1
    //graph SL2
    Graph Bot1
    graph Bot2
    
    #88977 quote
    Nicolas
    Keymaster
    Master

    On ne peut pas placer 2 principes de trailing stop différents par ce biais, impossible de différencier les ordres.

    #89003 quote
    FREDNC
    Participant
    Senior

    Merci, pour ta réponse Nicolas. Effectivement, à partir du moment ou une deuxième position est prise Il faut raisonner et terme de prix moyen pondéré. Je vais donc opter pour un seul trailing  stop basé sur le PRU.  Les heures de debugging ont fini pat altérer mon résonnement, no comment 😉  …..

    Pourrais tu  me trouver tu une solution pour gérer le renforcement (ou pyramidage) et quel type de trailing stop serait le plus adapté ?  l’idée c’est : Si, Breakaven  du prix moyen pondéré des positions en cours (que se soit une ou X positions) donne la possibilité d’ouvrir une position supplémentaire . Par avance merci.

    #89013 quote
    FREDNC
    Participant
    Senior

    Voilà ci-dessous ce que j’utilise actuellement. Le Trailing c’est le tien, le système de pyramidage, je l’ai écrit avec les posts que j’ai trouvé sur le sujet.

    Peut-on améliorer ce concept ?

    Comment le trailing va t-il se comporter sur les entrée suivantes compte tenue qu’actuellement c’est (TP/2) de la première position qui active le trail ?

    A chaque nouvelles positions, nouveau TP et donc la variable “startBreakeven1” de la précédente devrait être recalculé et on perd le BE précédemment acquis … En cas de retournement et si la dernière position n’a pas pu activé son breakeven on peut redescendre au SL de la première position…

    //Pyramidage si Be + limitation de position************************
    //**Nonbre de Position autorisé**
    NbrPosi=5
    
    If onmarket And (close>=startBreakeven1) and (abs(COUNTOFPOSITION)<NbrPosi) then// 
    Pyra=1
    elsif not onmarket then
    Pyra=1
    else
    Pyra=0
    endif
    
    //Canal Donchian************************************
    DC=15//A optimiser de temps en temps
    CDH=Highest[DC](high)
    CDB=Lowest[DC](low)
    
    If Pyra and C1 then
    SL= abs (close-CDB)+(SPREAD/2)
    TP= SL
    BUY N CONTRACTS AT MARKET
    ENDIF
    
    //Trail  *****************************************************************
    startBreakeven1 = TP/2 //how much pips/points in gain to activate the breakeven function?
    PointsToKeep1= 1 //how much pips/points to keep in profit above of below our entry price when the breakeven is activated (beware of spread)
    //reset the breakevenLevel when no trade are on market
    IF NOT ONMARKET THEN
    breakevenLevel=0
    ENDIF
     
    // --- BUY SIDE ---
    //test if the price have moved favourably of "startBreakeven1" points already
    IF LONGONMARKET and close-tradeprice(1)>=startBreakeven1*pipsize THEN
    //calculate the breakevenLevel
    breakevenLevel = tradeprice(1)+PointsToKeep1*pipsize
    ENDIF
     
    //place the new stop orders on market at breakevenLevel
    IF breakevenLevel>0 THEN
    SELL AT breakevenLevel STOP
    ENDIF
    // --- SHORT SIDE ---
    IF SHORTONMARKET and tradeprice(1)-close>startBreakeven1*pipsize THEN
    //calculate the breakevenLevel
    breakevenLevel = tradeprice(1)-PointsToKeep1*pipsize
    ENDIF
    //place the new stop orders on market at breakevenLevel
    IF breakevenLevel>0 THEN
    EXITSHORT AT breakevenLevel STOP
    ENDIF
    
    #89018 quote
    Nicolas
    Keymaster
    Master

    A chaque nouvelles positions, nouveau TP et donc la variable “startBreakeven1” de la précédente devrait être recalculé et on perd le BE précédemment acquis … En cas de retournement et si la dernière position n’a pas pu activé son breakeven on peut redescendre au SL de la première position…

    Ton raisonnement est correct. Peu importe où tu placeras ton SELL ou ton EXITSHORT, l’ensemble des positions sera liquidé. Je te conseillerai d’utiliser POSITIONPRICE qui est le prix moyen de tous les ordres ouverts au marché. Par exemple, dans le cas d’une série d’ordres LONG, si tu places le stop au dessus de POSITIONPRICE, tu seras en gain global sur ton panier même si certains ordres seront perdants.

    FREDNC thanked this post
    #89093 quote
    FREDNC
    Participant
    Senior

    Bonjour Nicolas,

    J’ai donc fait les modifications au niveau du code du “traling stop” en remplacent traderprice par  POSITIONPRICE.  Ça fonctionne bien, j’ai graphé les variables tous est OK.  Comme prévue, la deuxième position est autorisé uniquement pendant la durée du BE de la première position et si moins de x positions en cours.

    Ça ne lui laisser pas assez de temps, donc j’ai du j’ai dût redescendre mon niveau d’entré du BE et gardé le strict minimum afin que le signal de la deuxième position arrive à se caler pendant l’espace temps du BE de la première.

    Ca pourrait être rentable dans l’état, mais dans les faits on se retrouve à trailer le panier avec les paramètres de la première position et ce n’est pas rentable…

    J’ai donc imaginer une astuce. A l’aide d’un deuxième Trailling  “stop 3 bars trailing stop Williams” qui trail le “point to keept” .  A l’aide de quelque ligne de code, on indique a ton trailling stop quelle “point to keep” à utiliser en fonction du nombre de position encours.   Le résultat et très satisfaisant. (Au passage, merci à l’auteur du “stop 3 bars trailing stop Williams”)

    Voici l’astuce pour “point to keep”

    If abs(COUNTOFPOSITION)<2 then
    PtK=TP/4
    elsif abs(COUNTOFPOSITION)>1 then
    PtK=ref//variable de “stop 3 bars trailing stop Williams”
    Endif

     

    Et pour mémo voici le :  “3 bars trailing stop Williams”

    count=1
    i=0
    j=i+1
    tot=0
    while count<3 do// optimisable de temps en temps entre 2 et 10 pas de 1 ,
    tot=tot+1
    if (low[j]>=low[i]) and (high[j]<=high[i]) then
    //inside bar
    j=j+1
    else
    count=count+1
    i=i+1
    J=i+1
    endif
    wend
    basso=lowest[tot](low)
    alto=highest[tot](high)

    if close>alto[1] then
    ref=basso
    endif
    if close<basso[1] then
    ref=alto
    endif

    #89096 quote
    FREDNC
    Participant
    Senior

    Un commentaire, des suggestions ?

    Il y a certainement moyen d’améliorer…

    #89170 quote
    FREDNC
    Participant
    Senior

    Bon finalement après quelques heures de debugging et le rajout du PPJ et  2 filtres perso, la stratégie est rentable. J’ai résolu de nombreux bogs.  Le principe du renforcement si BE est validé. La sortie se fait bien sur niveau BE de la première position.  En général, le gain de la multiplication des positions est intéressant. Seul la dernière position de la série est parfois faiblement en perte et toutes les autre sont positives.

    Pour autant il me reste 2 bogs que je n’explique pas…

    -1er bog: Impossible de “grapher” le Bot1. J’ai tester, il fait bien des positions. Si conditions= buy et bot1=3 ne donne rien en “graph”.

    En revanche, pour le “Bot2”, Si conditions= buy et bot2=5 tout fonctionne , il est bien “grapher”…..bizarre.

    -2eme bog : Le BE de la première position initialise la variable “Pyra” pour autoriser le renforcement ou pyramidage. Ca fonctionne dans 90% des cas mais de temps en temps au sort sur le SL (de la première positions) et ce avec l’ensemble des positions, (ça fait mal voir le: 22 aout). Ce comportement ne devrait pas se produire car si “Pyra=1” ça veut dire que la première position est déjà à BE donc on doit sortir au BE avec ou sans renforcement. Je ne comprends pas….


    @nicolas
    et les autres pouvez-vous tester et me faire part de vos suggestions ?

    Je n’ai pas réussi à importer le ITF donc voici le code final à appliquer sur  Dax (1euro), M1 , 100 000 bouigie , spread 1

    // Définition des paramètres du code
    DEFPARAM CumulateOrders =  true //false // Cumul des positions ACTIVEES***Voir option Pyramidage Tout à la fin, sous le BreakEven***
    DEFPARAM Preloadbars = 20000
    //Nbr de contrats*******************
    N=2
    //**********************************
    // Empêche le système de placer des ordres pour entrer sur le marché ou augmenter la taille d'une position avant l'heure spécifiée
    noEntryBeforeTime = 091500//080000//091500
    timeEnterBefore = time >= noEntryBeforeTime
    
    //Empêche le système de placer des ordres pour entrer sur le marché ou augmenter la taille d'une position après l'heure spécifiée
    noEntryAfterTime = 170000//2130000//170000
    timeEnterAfter = time < noEntryAfterTime
    
    //Jour de trading du lundi au vendredi inclus
    if DayOfWeek <= 5 then
    Tradeday=1
    elsif tradeday=1 and DayOfWeek > 5 then
    TradeDay=0
    endif
    
    IF ONMARKET and (TIME>=171500 and close-tradeprice>1) THEN
    SELL AT MARKET
    EXITSHORT AT MARKET
    ENDIF
    Spread=1
    
    //Taille en Pips*******************
    TMini=9//7
    TMaxi=13//12
    TailleA = (abs(close-CDB))
    TailleV = (abs(CDH-close))
    PC=9//14// ***********************************Optimisable entre 10% et 40% admit Entre position/objectif/MM34
    If Abs (close[1]-close) < abs(tailleA*PC/100) then
    Dist=1
    elsif Abs (close[1]-close) < abs(TailleV*PC/100) then
    Dist=1
    else
    Dist=0
    endif
    
    //PPJ
    PPJ= (DHigh(1) + DLow(1) + DClose(1))/3
    
    //Canal Donchian************************************
    DC=15//
    CDH=Highest[DC](high)
    CDB=Lowest[DC](low)
    
    //TDI
    lengthRSI = 13
    lengthband = 34
    lengthrsipl = 2
    lengthtradesl = 7
    
    r = rsi[lengthrsi](close)
    ma = average[lengthband](r)
    offs = (1.6185 * std[lengthband](r))
    up = ma+offs
    dn = ma-offs
    mid = (up+dn)/2
    TDIVert = average[lengthrsipl](r)
    TDIRouge = average[lengthtradesl](r)
    
    // "CROSS 1"******
    IF not onmarket and TDIVert > mid and TDIRouge > mid and mid > 50 and TMini<abs((CDB[0]-Close)+(SPREAD/2)) and TMaxi>abs((CDB[0]-close)+(SPREAD/2)) and close >= PPJ and dist  and Tradeday  AND  timeEnterBefore AND  timeEnterAfter  THEN
    CROSSTa = 1
    elsif not onmarket  and TDIVert < mid and TDIRouge < mid and mid < 50 and TMini<abs((close-CDH[0])+(SPREAD/2)) and TMaxi>abs((close-CDH[0]+(SPREAD/2))) and close <= PPJ and dist  and Tradeday  AND  timeEnterBefore AND  timeEnterAfter THEN
    CROSSTb = 1
    else
    CROSSTa=0
    CROSSTb=0
    ENDIF
    
    // "CROSS 2"*****************************
    IF  onmarket and r[1]<r or TDIVert[1]< TDIVert or TDIVert crosses over TDIRouge THEN//longonmarket and
    CROSST2A = 1
    elsif onmarket and r[1]<r or TDIVert[1]< TDIVert or TDIVert crosses under TDIRouge and TDIVert THEN//shortonmarket and
    CROSST2V = 1
    else
    CROSST2A=0
    CROSST2V=0
    ENDIF
    
    //Bot1 ***********************************************************************************************
    //Long1
    If CROSSTa and TDIVert crosses over TDIRouge then
    Bot1=3 //Pour graph la variable. Inutine quand tous fonctionne
    BUY N CONTRACT AT MARKET
    SL = abs (close-CDB)+(SPREAD/2)
    TP = abs (close-CDB)+(SPREAD/2)
    endif
    //Short1
    if CROSSTb and TDIVert crosses under TDIRouge then
    Bot1=3 //Pour graph la variable. Inutine quand tous fonctionne
    SELLSHORT N CONTRACT AT MARKET
    SL = abs (CDH-close)+(SPREAD/2)
    TP = abs (CDH-close)+(SPREAD/2)
    ENDIF
    if not onmarket then//Pour graph la variable. Inutine quand tous fonctionne
    Bot1=0
    endif
    
    //Bot2***************************************************************************************************
    //Long2
    If PyraA and CROSST2A then
    Bot2=5//Pour graph la variable. Inutine quand tous fonctionne
    BUY N CONTRACT AT MARKET
    SL = abs (close-CDB)+(SPREAD/2)
    endif
    //Short2
    if PyraV and CROSST2V  then//or CROSS1=-1 or CROSS2=-1
    Bot2=5//Pour graph la variable. Inutine quand tous fonctionne
    SELLSHORT N CONTRACT AT MARKET
    SL = abs (CDH-close)+(SPREAD/2)
    endif
    if not onmarket then//Pour graph la variable. Inutine quand tous fonctionne
    Bot2=0
    endif
    
    //3 bars trailing stop Williams
    count=1
    i=0
    j=i+1
    tot=0
    while count<3 do//optimisable de tempts en temps 
    tot=tot+1
    if (low[j]>=low[i]) and (high[j]<=high[i]) then
    //inside bar
    j=j+1
    else
    count=count+1
    i=i+1
    J=i+1
    endif
    wend
    basso=lowest[tot](low)
    alto=highest[tot](high)
    
    if close>alto[1] then
    ref=basso
    endif
    if close<basso[1] then
    ref=alto
    endif
    
    //Bascule "point to keep"
    If abs(COUNTOFPOSITION)<=1 then
    StBe=tp/4
    PtK=TP/8
    elsif abs(COUNTOFPOSITION)>1 then
    PtK=ref
    endif
    
    //Trail************************************************************************
    startBreakeven = StBe//how much pips/points in gain to activate the breakeven function?
    PointsToKeep = PtK//ref //how much pips/points to keep in profit above of below our entry price when the breakeven is activated (beware of spread)
    //reset the breakevenLevel when no trade are on market
    IF NOT ONMARKET THEN
    breakevenLevelA=0
    breakevenLevelV=0
    ENDIF
     
    // --- BUY SIDE ---
    //test if the price have moved favourably of "startBreakeven" points already
    IF LONGONMARKET AND close-POSITIONPRICE>=startBreakeven*pipsize THEN
    //calculate the breakevenLevel
    breakevenLevelA = POSITIONPRICE+PointsToKeep*pipsize
    ENDIF
     
    //place the new stop orders on market at breakevenLevel
    IF breakevenLevelA>0 THEN
    SELL AT breakevenLevelA STOP
    ENDIF
    // --- SHORT SIDE ---
    IF SHORTONMARKET AND POSITIONPRICE-close>startBreakeven*pipsize THEN
    //calculate the breakevenLevel
    breakevenLevelV = POSITIONPRICE-PointsToKeep*pipsize
    ENDIF
    //place the new stop orders on market at breakevenLevel
    IF breakevenLevelV>0 THEN
    EXITSHORT AT breakevenLevelV STOP
    ENDIF
    
    //Pyramidage si Be + limitation de position************************
    NbrPosi=5
    if not onmarket then
    PyraA=0
    Pyrav=0
    TP=0
    elsif abs(COUNTOFPOSITION)>NbrPosi then
    PyraA=0
    PyraV=0
    TP=0
    endif
    If (close-tradeprice(1))>0 and longonmarket then//
    PyraA=1
    elsif (tradeprice(1)-close)>0 and shortonmarket then//
    PyraV=1
    endif
    
    
    SET STOP pLOSS SL
    //SET TARGET pPROFIT TP
    
    
    
    graph pyraA
    graph pyraV
    graph TP
    //graph (positionprice-close)>0
    graph Bot1
    graph Bot2
    //graph breakevenLevelA>0
    //graph breakevenLevelV>0
    
    #89196 quote
    FREDNC
    Participant
    Senior

    Voilà la dernière, l’autre était vraiment boguer , Pour autant de positions de sont pas parfaites. @Nicolas ou quelqu’un d’autre pourrais y jeter un œil et m’arranger ça. Par avance merci. voici le code:

    // Définition des paramètres du code: Dax M1 , 100 000 bougies, 1 de spread
    // Définition des paramètres du code
    DEFPARAM CumulateOrders =  true //false // Cumul des positions ACTIVEES***Voir option Pyramidage Tout à la fin, sous le BreakEven***
    DEFPARAM Preloadbars = 20000
    //Nbr de contrats*******************
    N=2
    //**********************************
    // Empêche le système de placer des ordres pour entrer sur le marché ou augmenter la taille d'une position avant l'heure spécifiée
    noEntryBeforeTime = 091500//080000//091500
    timeEnterBefore = time >= noEntryBeforeTime
    
    //Empêche le système de placer des ordres pour entrer sur le marché ou augmenter la taille d'une position après l'heure spécifiée
    noEntryAfterTime = 170000//2130000//170000
    timeEnterAfter = time < noEntryAfterTime
    
    //Jour de trading du lundi au vendredi inclus
    if DayOfWeek <= 5 then
    Tradeday=1
    elsif tradeday=1 and DayOfWeek > 5 then
    TradeDay=0
    endif
    
    IF ONMARKET and (TIME>=171500 and close-tradeprice>1) THEN
    SELL AT MARKET
    EXITSHORT AT MARKET
    ENDIF
    
    Spread=1
    
    //Pyramidage si Be + limitation de position************************
    NbrPosi=10
    if not onmarket then
    PyraA=0
    Pyrav=0
    TP=0
    elsif abs(COUNTOFPOSITION)>=NbrPosi then
    PyraA=0
    PyraV=0
    TP=0
    endif
    
    //Long
    If close > breakevenLevelA and longonmarket and abs(COUNTOFPOSITION)<=NbrPosi then//
    PyraA=1
    elsif close-positionprice<0 then//
    SELL AT close-positionprice<0 STOP
    PyraA=0
    endif
    //Short
    if close < breakevenLevelV and shortonmarket and abs(COUNTOFPOSITION)<=NbrPosi then//breakevenLevelV>0
    PyraV=1
    elsif positionprice-close>0 then //
    EXITSHORT AT positionprice-close>0 STOP
    PyraV=0
    endif
    
    //Taille en Pips*******************
    TMini=9//Sert à la limite de 6 pips Mini chez IG
    TMaxi=13//sert à limité les tailles de positions et SL
    TailleA = (abs(close-CDB))
    TailleV = (abs(CDH-close))
    PC=9//14// Filtres***********************************Optimisable entre 10% et 40% admit Entre position/objectif/MM34
    If Abs (close[1]-close) < abs(tailleA*PC/100) then
    Dist=1
    elsif Abs (close[1]-close) < abs(TailleV*PC/100) then
    Dist=1
    else
    Dist=0
    endif
    
    //PPJ
    PPJ= (DHigh(1) + DLow(1) + DClose(1))/3
    
    //Canal Donchian************************************
    DC=15//
    CDH=Highest[DC](high)
    CDB=Lowest[DC](low)
    
    //TDI
    lengthRSI = 13
    lengthband = 34
    lengthrsipl = 2
    lengthtradesl = 7
    
    r = rsi[lengthrsi](close)
    ma = average[lengthband](r)
    offs = (1.6185 * std[lengthband](r))
    up = ma+offs
    dn = ma-offs
    mid = (up+dn)/2
    TDIVert = average[lengthrsipl](r)
    TDIRouge = average[lengthtradesl](r)
    
    // "CROSS 1"******
    IF  not onmarket and TMini<abs((CDB[0]-Close)+(SPREAD/2)) and TMaxi>abs((CDB[0]-close)+(SPREAD/2)) and Dist and close > Ppj and Tradeday  AND  timeEnterBefore AND  timeEnterAfter and TDIVert > mid and TDIRouge > mid and mid > 5 THEN
    CROSSTa = 1
    elsif not onmarket and TMini<abs((close-CDH[0])+(SPREAD/2)) and TMaxi>abs((close-CDH[0]+(SPREAD/2)))and Dist  and close < Ppj and Tradeday and   timeEnterBefore AND  timeEnterAfter and TDIVert < mid and TDIRouge < mid and mid < 50 THEN
    CROSSTb = 1
    else
    CROSSTa=0
    CROSSTb=0
    ENDIF
    
    // "CROSS 2"*****************************
    IF  longonmarket and r[1]<r or TDIVert[1]< TDIVert or TDIVert crosses over TDIRouge THEN//
    CROSST2A = 1
    elsif shortonmarket and r[1]>r or TDIVert[1]> TDIVert or TDIVert crosses under TDIRouge and TDIVert THEN//
    CROSST2V = 1
    else
    CROSST2A=0
    CROSST2V=0
    ENDIF
    
    
    bot1=0//Reset Bot pour graph
    //Bot1 ***********************************************************************************************
    //Long1
    If CROSSTa and TDIVert crosses over TDIRouge then
    SL = abs (close-CDB)+(SPREAD/2)
    TP = abs (close-CDB)+(SPREAD/2)
    BUY N CONTRACT AT MARKET
    Bot1=3 //Pour graph la variable. Inutine quand tous fonctionne
    endif
    //Short1
    if CROSSTb and TDIVert crosses under TDIRouge then
    SL = abs (CDH-close)+(SPREAD/2)
    TP = abs (CDH-close)+(SPREAD/2)
    SELLSHORT N CONTRACT AT MARKET
    Bot1=3 //Pour graph la variable. Inutine quand tous fonctionne
    ENDIF
    
    Bot2=0//Reset Bot pour graph
    //Bot2***************************************************************************************************
    //Long2
    If PyraA and CROSST2A then
    SL = abs (close-CDB)+(SPREAD/2)
    BUY N CONTRACT AT MARKET
    Bot2=5//Pour graph la variable. Inutine quand tous fonctionne
    endif
    //Short2
    if PyraV and CROSST2V  then//or CROSS1=-1 or CROSS2=-1
    SL = abs (CDH-close)+(SPREAD/2)
    SELLSHORT N CONTRACT AT MARKET
    Bot2=5//Pour graph la variable. Inutine quand tous fonctionne
    endif
    
    //3 bars trailing stop Williams
    count=1
    i=0
    j=i+1
    tot=0
    while count<3 do//4 optimisable de tempts en temps , original =4
    tot=tot+1
    if (low[j]>=low[i]) and (high[j]<=high[i]) then
    //inside bar
    j=j+1
    else
    count=count+1
    i=i+1
    J=i+1
    endif
    wend
    basso=lowest[tot](low)
    alto=highest[tot](high)
    
    if close>alto[1] then
    ref=basso
    endif
    if close<basso[1] then
    ref=alto
    endif
    
    //Trail************************************************************************
    //Bascule "point to keep"
    If abs(COUNTOFPOSITION)<=1 then
    StBe=tp/4
    PtK=ref
    elsif abs(COUNTOFPOSITION)>1 then
    StBe=tp/2
    PtK=ref
    endif
    
    startBreakeven = StBe//how much pips/points in gain to activate the breakeven function?
    PointsToKeep = PtK//ref //how much pips/points to keep in profit above of below our entry price when the breakeven is activated (beware of spread)
    //reset the breakevenLevel when no trade are on market
    IF NOT ONMARKET THEN
    breakevenLevelA=0
    breakevenLevelV=0
    ENDIF
     
    // --- BUY SIDE ---
    //test if the price have moved favourably of "startBreakeven" points already
    IF LONGONMARKET AND close-tradeprice(1)>=startBreakeven*pipsize THEN
    //calculate the breakevenLevel
    breakevenLevelA = positionprice+PointsToKeep*pipsize
    ENDIF
     
    //place the new stop orders on market at breakevenLevel
    IF breakevenLevelA>0 THEN
    SELL AT breakevenLevelA STOP
    ENDIF
    // --- SHORT SIDE ---
    IF SHORTONMARKET AND tradeprice(1)-close>startBreakeven*pipsize THEN
    //calculate the breakevenLevel
    breakevenLevelV = positionprice-PointsToKeep*pipsize
    ENDIF
    //place the new stop orders on market at breakevenLevel
    IF breakevenLevelV>0 THEN
    EXITSHORT AT breakevenLevelV STOP
    ENDIF
    
    
    
    
    SET STOP pLOSS SL
    //SET TARGET pPROFIT TP
    
    
    
    graph pyraA
    graph pyraV
    //graph TP
    //graph (positionprice-close)>0
    graph Bot1
    graph Bot2
    //graph breakevenLevelA>0
    //graph breakevenLevelV>0
    
    Capture-2.png Capture-2.png
    #89225 quote
    Nicolas
    Keymaster
    Master

    Désolé, mais qu’est ce qui n’est “pas parfait” et que faut-il “arranger” ? Difficile pour moi de m’investir  complètement  dans les projets de chacun sur le forum, donc il faut être très explicite pour me mettre sur la voie. Merci.

    #89303 quote
    FREDNC
    Participant
    Senior

    Bonjour @Nicolas,

    Effectivement j’aurais dût être plus précis.

    Ci dessous tu trouveras le new updabe du code à cette heure, j’ai encore des soucis et je t’ai fais un screen shot pour plus de compréhension .

    C’est un cycle. On commence par faire un achat avec avec le bote1.  Si breakeven+”Point to keep”+ conditions du bot2 on achète.  Puis si breakeven+”Point to keep” du panier + conditions du bot2 on achète encore et ainsi de suite

    La fin cycle c’est soit: (mono position) sortie à SL ou sortie à breakeven+”Point to keep” . Ou bien, si (Multi-positions) sortie à breakeven+”Point to keep” (du panier).

     

    Dans le screen shot (14/11/2018 à 13h18):

    1)On voit la position 1 et la 2,  rien à dire jusque là… Concernant la position 3 j’ai un doute sur le fait que le panier soit à breakeven+”Point to keep”  et la 4 et carrément en négative.

    2)Dans le graph (des variables) le cycle n’est pas respecté non plus.  (Pour info j’ai numéroté sur le graph Bot1=1 , Bot2=2, Pyra=3)

    Le cycle devrait être celà: On commence toujours par le bot1 , ensuite si BE   Pyra monte et autorise une position supplémentaire puis le bot2 et quand Pyra retombe c’est la fin du cycle .

    Le seul moyen de refaire une position c’est de repartir  du début de cycle. Dans ce cas , on voit clairement que Pyra retombe à zero mais d’autre positions du bot2 sont faites alors que Bot1 est inactif. 

    Voilà, peut tu me solutionner cela, 

    // Définition des paramètres du code*************** Dax M1, 100 000 bougies, spread 1******************
    DEFPARAM CumulateOrders =  true //false // Cumul des positions ACTIVEES***Voir option Pyramidage Tout à la fin, sous le BreakEven***
    DEFPARAM Preloadbars = 20000
    //Nbr de contrats*******************
    N=2
    //**********************************
    // Empêche le système de placer des ordres pour entrer sur le marché ou augmenter la taille d'une position avant l'heure spécifiée
    noEntryBeforeTime = 091500//080000//091500
    timeEnterBefore = time >= noEntryBeforeTime
    
    //Empêche le système de placer des ordres pour entrer sur le marché ou augmenter la taille d'une position après l'heure spécifiée
    noEntryAfterTime = 170000//2130000//170000
    timeEnterAfter = time < noEntryAfterTime
    
    //Jour de trading du lundi au vendredi inclus
    if DayOfWeek <= 5 then
    Tradeday=1
    elsif tradeday=1 and DayOfWeek > 5 then
    TradeDay=0
    endif
    
    IF ONMARKET and (TIME>=171500 and close-tradeprice>1) THEN
    SELL AT MARKET
    EXITSHORT AT MARKET
    ENDIF
    Spread=1
    
    //Taille en Pips*******************
    TMini=9//7
    TMaxi=13//12
    TailleA = (abs(close-CDB))
    TailleV = (abs(CDH-close))
    PC=9//14// ***********************************Optimisable entre 10% et 40% admit Entre position/objectif/MM34
    If Abs (close[1]-close) < abs(tailleA*PC/100) then
    Dist=1
    elsif Abs (close[1]-close) < abs(TailleV*PC/100) then
    Dist=1
    else
    Dist=0
    endif
    
    //PPJ
    PPJ= (DHigh(1) + DLow(1) + DClose(1))/3
    
    
    //Canal Donchian************************************
    DC=15//
    CDH=Highest[DC](high)
    CDB=Lowest[DC](low)
    
    //TDI
    lengthRSI = 13
    lengthband = 34
    lengthrsipl = 2
    lengthtradesl = 7
    
    r = rsi[lengthrsi](close)
    ma = average[lengthband](r)
    offs = (1.6185 * std[lengthband](r))
    up = ma+offs
    dn = ma-offs
    mid = (up+dn)/2
    TDIVert = average[lengthrsipl](r)
    TDIRouge = average[lengthtradesl](r)
    
    // "CROSS 1"******
    IF not onmarket and TDIVert > mid and TDIRouge > mid and mid > 50 and TMini<abs((CDB[0]-Close)+(SPREAD/2)) and TMaxi>abs((CDB[0]-close)+(SPREAD/2)) and close >= PPJ and dist  and Tradeday  AND  timeEnterBefore AND  timeEnterAfter  THEN
    CROSSTa = 1
    elsif not onmarket  and TDIVert < mid and TDIRouge < mid and mid < 50 and TMini<abs((close-CDH[0])+(SPREAD/2)) and TMaxi>abs((close-CDH[0]+(SPREAD/2))) and close <= PPJ and dist  and Tradeday  AND  timeEnterBefore AND  timeEnterAfter THEN
    CROSSTb = 1
    else
    CROSSTa=0
    CROSSTb=0
    ENDIF
    
    // "CROSS 2"*****************************
    IF onmarket and r[1]<r or TDIVert[1]< TDIVert or TDIVert crosses over TDIRouge THEN//longonmarket and
    CROSST2A = 1
    elsif onmarket and r[1]>r or TDIVert[1]> TDIVert or TDIVert crosses under TDIRouge and TDIVert THEN //shortonmarket and
    CROSST2V = 1
    else
    CROSST2A=0
    CROSST2V=0
    ENDIF
    
    //Bot1 ***********************************************************************************************
    //Long1
    Bot1=0
    If CROSSTa and TDIVert crosses over TDIRouge then
    Bot1=3 //Pour graph la variable. Inutine quand tous fonctionne
    BUY N CONTRACT AT MARKET
    SL = abs (close-CDB)+(SPREAD/2)
    TP = abs (close-CDB)+(SPREAD/2)
    endif
    //Short1
    if CROSSTb and TDIVert crosses under TDIRouge then
    Bot1=3 //Pour graph la variable. Inutine quand tous fonctionne
    SELLSHORT N CONTRACT AT MARKET
    SL = abs (CDH-close)+(SPREAD/2)
    TP = abs (CDH-close)+(SPREAD/2)
    ENDIF
    
    //Bot2***************************************************************************************************
    //Long2
    Bot2=0
    If PyraA and CROSST2A then
    Bot2=5//Pour graph la variable. Inutine quand tous fonctionne
    BUY N CONTRACT AT MARKET
    SL = abs (close-CDB)+(SPREAD/2)
    endif
    //Short2
    if PyraV and CROSST2V  then//or CROSS1=-1 or CROSS2=-1
    Bot2=5//Pour graph la variable. Inutine quand tous fonctionne
    SELLSHORT N CONTRACT AT MARKET
    SL = abs (CDH-close)+(SPREAD/2)
    endif
    
    
    //3 bars trailing stop Williams
    count=1
    i=0
    j=i+1
    tot=0
    while count<3 do//4 optimisable de tempts en temps , original =4
    tot=tot+1
    if (low[j]>=low[i]) and (high[j]<=high[i]) then
    //inside bar
    j=j+1
    else
    count=count+1
    i=i+1
    J=i+1
    endif
    wend
    basso=lowest[tot](low)
    alto=highest[tot](high)
    
    if close>alto[1] then
    ref=basso
    endif
    if close<basso[1] then
    ref=alto
    endif
    
    
    
    
    //Trail Bascule "point to keep"************************************************************************
    
    //Mono position
    If abs(COUNTOFPOSITION) then
    StBe=tp/4
    PtK=TP/3
    //Muti-poditions
    elsif abs(COUNTOFPOSITION)>1 and POSITIONPRICE > breakevenLevelA then
    StBe=tp/4
    PtK=ref
    endif
    //******************************************************************************
    
    startBreakeven = StBe//how much pips/points in gain to activate the breakeven function?
    PointsToKeep = PtK//ref //how much pips/points to keep in profit above of below our entry price when the breakeven is activated (beware of spread)
    //reset the breakevenLevel when no trade are on market
    IF NOT ONMARKET THEN
    breakevenLevelA=0
    breakevenLevelV=0
    breakeven=0
    ENDIF
     
    // --- BUY SIDE ---
    //test if the price have moved favourably of "startBreakeven" points already
    IF LONGONMARKET AND close-POSITIONPRICE>=startBreakeven*pipsize THEN
    //calculate the breakevenLevel
    breakevenLevelA= POSITIONPRICE+PointsToKeep*pipsize
    ENDIF
     
    //place the new stop orders on market at breakevenLevel
    IF breakevenLevelA>0 THEN
    SELL AT breakevenLevelA STOP
    Breakeven=1
    ENDIF
    // --- SHORT SIDE ---
    IF SHORTONMARKET AND POSITIONPRICE-close>startBreakeven*pipsize THEN
    //calculate the breakevenLevel
    breakevenLevelV = POSITIONPRICE-PointsToKeep*pipsize
    ENDIF
    //place the new stop orders on market at breakevenLevel
    IF breakevenLevelV>0 THEN
    EXITSHORT AT breakevenLevelV STOP
    breakeven=1
    ENDIF
    
    //Pyramidage si Be + limitation de position************************
    NbrPosi=50
    if not onmarket then
    PyraA=0
    PyraV=0
    TP=0
    elsif abs(COUNTOFPOSITION)>NbrPosi then
    PyraA=0
    PyraV=0
    endif
    
    //long
    if longonmarket and  breakeven then //and breakevenLevelA>0
    PyraA=1
    endif
    If longonmarket and POSITIONPRICE < breakevenLevelA or abs(COUNTOFPOSITION)>NbrPosi then//
    SELL AT breakevenLevelA STOP
    breakevenLevelA=0
    breakeven=0
    PyraA=0
    endif
    
    //short
    if shortonmarket and breakeven then//
    PyraV=1
    endif
    if shortonmarket and POSITIONPRICE > breakevenLevelV or abs(COUNTOFPOSITION)>NbrPosi then//
    EXITSHORT AT breakevenLevelV STOP
    breakevenLevelV=0
    breakeven=0
    PyraV=0
    endif
    
    SET STOP pLOSS SL
    //SET TARGET pPROFIT TP
    
    graph pyraA
    graph pyraV
    //graph positionprice
    //graph TP
    //graph (positionprice-close)>0
    graph Bot1
    graph Bot2
    //graph breakevenLevelA>0
    //graph breakevenLevelV>0
    If bot1 then
    elsif bot2 then
    elsif ref then
    endif
    

     

    PRC.jpg PRC.jpg
    #89351 quote
    Nicolas
    Keymaster
    Master

    Pas été dans le détail mais il me semble qu’il y a une erreur à la ligne 151, en effet cette ligne retournera vrai dés que countofposition sera supérieure à 0:

    If abs(COUNTOFPOSITION) then

    par conséquent le eslif après ne sera jamais testé. Je pense que tu devrais réécrire l’ensemble comme ceci:

    //Mono position
    If abs(COUNTOFPOSITION)=1 then
     StBe=tp/4
     PtK=TP/3
     //Muti-poditions
    elsif abs(COUNTOFPOSITION)>1 and POSITIONPRICE > breakevenLevelA then
     StBe=tp/4
     PtK=ref
    endif
    #89385 quote
    FREDNC
    Participant
    Senior

    Merci, j’ai corrigé,

    mais on est pas encore rendu ….

    Encore plein de bogs, c’est dommage car l’idée est rentable (Screen shot joint), mais c’est il y a beaucoup de pièges quand il s’agit de l’écriture du code.

    Du reste, j’aime pas l’utilisation  “COUNTOFPOSITION” qui compte le nombre de contrats et non le nombre d’entrées. Quand on change le nombre de contrats à l’achat(valeur N) , il faut revoir tout les valeurs de “COUNTOFPOSITION” dans l’intégralité du code…. Existe t-il une fonction qui compte les entrées plutôt que les contrats ?

    QuandCaMarche.jpg QuandCaMarche.jpg
    #89410 quote
    Nicolas
    Keymaster
    Master

    Existe t-il une fonction qui compte les entrées plutôt que les contrats ?

    tu peux facilement incrémenter une variable dés que tu rentres en position et la reset quand tu n’es plus au marché.

    #89425 quote
    FREDNC
    Participant
    Senior

    Bonjour et merci pour ta réponse.

    Si je comprends bien tu pense à une boucle dans lequel on comptabilise le nombre de 1 envoyé au moment de l’entrée en position des Bot 1 et Bot2 ?

    Je ne suis pas un expert en boucle donc pourrais tu me corriger stp ?

    J’ai écris cela mais quand je graph Nbr j’ai :0

    If not onmarket then
    Bot1=0
    Bot2=0
    Nbr=0
    endif
    for i= 1 to 50
    if bot1=1 then
    elsif bot2=1 then
    Nbr=i =i+1
    endif
    next
Viewing 15 posts - 1 through 15 (of 29 total)
  • You must be logged in to reply to this topic.

Demande de boucle pour pyramidage, trailing stop et breakeven


ProOrder : Trading Automatique & Backtests

New Reply
Author
author-avatar
FREDNC @frednc Participant
Summary

This topic contains 28 replies,
has 2 voices, and was last updated by FREDNC
7 years, 1 month ago.

Topic Details
Forum: ProOrder : Trading Automatique & Backtests
Language: French
Started: 01/17/2019
Status: Active
Attachments: 9 files
Logo Logo
Loading...