Rejoindre la communauté
banner_forum
Devenez membre de la plus grande
communauté francophone sur le Forex
Partagez, échangez et apprenez en gagnant des crédits sur votre compte de trading

code MT4

  • oleo (invité)

    Bonjour à tous,
    J’essaie de faire un EA pour automatiser mon trading mais je n'y connais rien.
    Quelqu'un pourrait me donner le code qui permettrait de placer un ordre d'achat apres l'ouverture d'une bougie qui augmente de X pip
    A l'inverse, un ordre de ventre apres l'ouverture d'une bougie qui baisse de X pips
    Merci d'avance,
  • DamienS

    Bonsoir;

    Voici qui pourrait t'aider, j'ai modifié une base d'un de mes templates ...
    La variable Externe PointsDiff définit l'écart entre l'open et le close pour entrer un trade .... les reste sont des variables habituelles :)


    #property copyright "Copyright © 2011, Damien Soudant"
    #property link "[email protected]"

    #define _STRAT "Forexagone Help"

    //---- Input Paramettres

    extern bool UseSl = true;
    extern int SlSize = 500;
    extern bool UseTp = true;
    extern int TpSize = 500;
    extern double Lots = 0.1;
    extern int Ea_ID = 5555;
    //----

    extern double PointsDif = 500;

    bool IsBuy;
    bool IsSell;

    //+------------------------------------------------------------------+
    //| expert initialization function |
    //+------------------------------------------------------------------+
    int init()
    {
    //----
    IsSell = false;
    IsBuy = false;
    //----
    return(0);
    }
    //+------------------------------------------------------------------+
    //| expert deinitialization function |
    //+------------------------------------------------------------------+
    int deinit()
    {
    //----
    Print("De- initialisation de l ea";);
    //----
    return(0);
    }
    //+------------------------------------------------------------------+
    //| expert start function |
    //+------------------------------------------------------------------+
    int start()
    {
    //----

    IsSell = false;
    IsBuy = false;


    for(int i= 0; i<=OrdersTotal(); i++)
    {
    OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

    if(OrderSymbol()!= Symbol()) continue;
    if(OrderMagicNumber() !=Ea_ID)continue;

    if(OrderType() == OP_BUY)
    {
    IsBuy = true;

    if(UseSl && OrderStopLoss()<=0)
    {
    if(OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(OrderOpenPrice()-SlSize*Point, Digits),OrderTakeProfit(), 0, CLR_NONE))
    {
    OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
    Print(_STRAT+" Validation du stop loss à " + DoubleToStr(OrderStopLoss(), Digits));
    }
    }

    if(UseTp && OrderTakeProfit()<=0)
    {
    if(OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), NormalizeDouble(OrderOpenPrice()+TpSize*Point, Digits), 0, CLR_NONE))
    {
    OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
    Print(_STRAT+" Validation du take profit à " + DoubleToStr(OrderTakeProfit(), Digits));
    }
    }
    }// Fin de gestion des ordres achats


    if(OrderType() == OP_SELL)
    {
    IsSell = true;

    if(UseSl && OrderStopLoss()<=0)
    {
    if(OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(OrderOpenPrice()+SlSize*Point, Digits),OrderTakeProfit(), 0, CLR_NONE))
    {
    OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
    Print(_STRAT+" Validation du stop loss à " + DoubleToStr(OrderStopLoss(), Digits));
    }
    }

    if(UseTp && OrderTakeProfit()<=0)
    {
    if(OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), NormalizeDouble(OrderOpenPrice()-TpSize*Point, Digits), 0, CLR_NONE))
    {
    OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
    Print(_STRAT+" Validation du take profit à " + DoubleToStr(OrderTakeProfit(), Digits));
    }
    }

    } // fin de gestion des ordres de ventes

    }// fin boucle ordres ouverts


    if(!IsSell && !IsBuy)
    {
    if(BuySignal())
    {
    // a faire lors d'une contiion d'achat;
    if(Trade(OP_BUY, Ask, Lots, Ea_ID))
    {
    Print(_STRAT+" Succes to open buy position";);
    }
    }
    if(SellSignal())
    {
    // a faire lors d'une condition de vente;
    if(Trade(OP_SELL, Ask, Lots, Ea_ID))
    {
    Print(_STRAT+" Succes to open sell position";);
    }
    }
    }// fin block non trade

    //----
    return(0);
    }
    //+------------------------------------------------------------------+

    //+------------------------------------------------------------------+
    //| Buy Conditions |
    //+------------------------------------------------------------------+
    bool BuySignal()
    {
    if((Close[0]-Open[0])/Point>=PointsDif)
    {
    return(true);
    }

    return(false);
    }

    //+------------------------------------------------------------------+
    //| Sell Conditions |
    //+------------------------------------------------------------------+
    bool SellSignal()
    {
    if((Open[0]-Close[0])/Point>=PointsDif)
    {
    return(true);
    }

    return(false);
    }

    //+------------------------------------------------------------------+
    //| Send Order Function |
    //+------------------------------------------------------------------+
    bool Trade(int type, double price, double vol, int magic)
    {
    if(type != OP_SELL && type!=OP_BUY)
    {
    Print(_STRAT+" Error top open trade := False argument";);
    return(false);
    }

    int ticket = OrderSend(Symbol(), type, vol, NormalizeDouble(price, Digits), 300, 0, 0, "", magic, 0, CLR_NONE);

    if(ticket>0)
    {
    return(true);
    }
    return(false);
    }
  • oleo (invité)

    Merci pour ta réponse.
    Le probleme, c'est que je ne souhaite pas attendre la cloture de la bougie pour entrer en trade. Je veux entrer des que X pips sont atteints depuis l'ouverture de la bougie.
    De plus, j'ai voulu tester ton code et lors de la compilation, j'ai 2 erreurs :

    ask variable not defined 121, 18
    ask variable not defined 129, 19

    A te lire,
  • DamienS

    Bizarre remplace ces erreurs par Ask (avec le "a" en majuscule), pourtant sur le code que j'ai ici sur mon pc il n'est pas en minuscule ...

    aussi à la ligne 129 remplace ask par Bid ... en effet on short au prix Bid, petite erreur de ma part
    Quand au reste il n'attend pas la clôture de bougie pour prendre positions ;).
    Une alternative sera de mettre des ordres stop à XX pips des Open ... plutôt que de prendre position directement, cela peux faire gagner quelques précieux pips :)
  • oleo

    Merci pour ces précisions.
    J'ai modifié les elements et j'ai pu compiler l'EA
    J'ai lancé un rapide backtest et malheureusement, il ne trade pas comme je voudrais. Precise moi une chose : si je mets point diff à 10 cela fait bien 10 pips ?
    Je repasse plus tard pour t'expliquer plus en détaille.
    Merci en tout cas.
  • DamienS

    10 pour une broker 4 digits et 100 pour un broker 5 digits ...
    à tout à l'heure
  • oleo

    autant pour moi ! Cela fonctionne car je suis chez un broker 5 digits.
    Sais tu implementer une fonction trailing stop ?
  • DamienS

    Voilà

    j'ai ajouté 3 variable

    UseTrainling = qui permet d'activer le traling ou pas;
    TrailingValue = la valeur en point du trailing stop;
    MinimumWin = permet de faire en sorte que le trailing ne soit actif que si ton trade gagne cette valeur en point ... à 0 le trailing est actif dès le départ du trade.

    Voilà voilà .. bons trades :)


    #property copyright "Copyright © 2011, Damien Soudant"
    #property link "[email protected]"

    #define _STRAT "Forexagone Help"

    //---- Input Paramettres

    extern bool UseSl = true;
    extern int SlSize = 500;
    extern bool UseTp = true;
    extern int TpSize = 500;
    extern double Lots = 0.1;
    extern int Ea_ID = 5555;
    //----

    extern double PointsDif = 500;

    extern bool UseTrailing = true;
    extern int TrailingValue = 250;
    extern int MinimumWin = 250;

    bool IsBuy;
    bool IsSell;

    //+------------------------------------------------------------------+
    //| expert initialization function |
    //+------------------------------------------------------------------+
    int init()
    {
    //----
    IsSell = false;
    IsBuy = false;
    //----
    return(0);
    }
    //+------------------------------------------------------------------+
    //| expert deinitialization function |
    //+------------------------------------------------------------------+
    int deinit()
    {
    //----
    Print("De- initialisation de l ea";);
    //----
    return(0);
    }
    //+------------------------------------------------------------------+
    //| expert start function |
    //+------------------------------------------------------------------+
    int start()
    {
    //----

    IsSell = false;
    IsBuy = false;


    for(int i= 0; i<=OrdersTotal(); i++)
    {
    OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

    if(OrderSymbol()!= Symbol()) continue;
    if(OrderMagicNumber() !=Ea_ID)continue;

    if(OrderType() == OP_BUY)
    {
    IsBuy = true;

    if(UseSl && OrderStopLoss()<=0)
    {
    if(OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(OrderOpenPrice()-SlSize*Point, Digits),OrderTakeProfit(), 0, CLR_NONE))
    {
    OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
    Print(_STRAT+" Validation du stop loss à " + DoubleToStr(OrderStopLoss(), Digits));
    }
    }

    if(UseTp && OrderTakeProfit()<=0)
    {
    if(OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), NormalizeDouble(OrderOpenPrice()+TpSize*Point, Digits), 0, CLR_NONE))
    {
    OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
    Print(_STRAT+" Validation du take profit à " + DoubleToStr(OrderTakeProfit(), Digits));
    }
    }

    if(UseTrailing)
    {
    double sl = OrderStopLoss();

    if(Ask-(TrailingValue*Point)>=sl&&(Ask-OrderOpenPrice())/Point>=MinimumWin)
    {
    sl = NormalizeDouble(Ask-(TrailingValue*Point), Digits);
    if(OrderStopLoss() !=sl)OrderModify(OrderTicket(), OrderOpenPrice(), sl, OrderTakeProfit(), NULL, CLR_NONE);
    }
    }
    }// Fin de gestion des ordres achats


    if(OrderType() == OP_SELL)
    {
    IsSell = true;

    if(UseSl && OrderStopLoss()<=0)
    {
    if(OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(OrderOpenPrice()+SlSize*Point, Digits),OrderTakeProfit(), 0, CLR_NONE))
    {
    OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
    Print(_STRAT+" Validation du stop loss à " + DoubleToStr(OrderStopLoss(), Digits));
    }
    }

    if(UseTp && OrderTakeProfit()<=0)
    {
    if(OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), NormalizeDouble(OrderOpenPrice()-TpSize*Point, Digits), 0, CLR_NONE))
    {
    OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
    Print(_STRAT+" Validation du take profit à " + DoubleToStr(OrderTakeProfit(), Digits));
    }
    }

    if(UseTrailing)
    {
    sl = OrderStopLoss();

    if(Bid+(TrailingValue*Point)<=sl && (OrderOpenPrice()-Bid)/Point>=MinimumWin)
    {
    sl = NormalizeDouble(Bid+(TrailingValue*Point), Digits);
    if(OrderStopLoss() !=sl)OrderModify(OrderTicket(), OrderOpenPrice(), sl, OrderTakeProfit(), NULL, CLR_NONE);
    }
    }

    } // fin de gestion des ordres de ventes

    }// fin boucle ordres ouverts


    if(!IsSell && !IsBuy)
    {
    if(BuySignal())
    {
    // a faire lors d'une contiion d'achat;
    if(Trade(OP_BUY, Ask, Lots, Ea_ID))
    {
    Print(_STRAT+" Succes to open buy position";);
    }
    }
    if(SellSignal())
    {
    // a faire lors d'une condition de vente;
    if(Trade(OP_SELL, Bid, Lots, Ea_ID))
    {
    Print(_STRAT+" Succes to open sell position";);
    }
    }
    }// fin block non trade

    //----
    return(0);
    }
    //+------------------------------------------------------------------+

    //+------------------------------------------------------------------+
    //| Buy Conditions |
    //+------------------------------------------------------------------+
    bool BuySignal()
    {
    if((Close[0]-Open[0])/Point>=PointsDif)
    {
    return(true);
    }

    return(false);
    }

    //+------------------------------------------------------------------+
    //| Sell Conditions |
    //+------------------------------------------------------------------+
    bool SellSignal()
    {
    if((Open[0]-Close[0])/Point>=PointsDif)
    {
    return(true);
    }

    return(false);
    }

    //+------------------------------------------------------------------+
    //| Send Order Function |
    //+------------------------------------------------------------------+
    bool Trade(int type, double price, double vol, int magic)
    {
    if(type != OP_SELL && type!=OP_BUY)
    {
    Print(_STRAT+" Error top open trade := False argument";);
    return(false);
    }

    int ticket = OrderSend(Symbol(), type, vol, NormalizeDouble(price, Digits), 300, 0, 0, "", magic, 0, CLR_NONE);

    if(ticket>0)
    {
    return(true);
    }
    return(false);
    }
  • DamienS

    J'ai trouvé pourquoi il y a une erreur avec le "Ask", c'est parce que les scripts du forum détectent ce mot pour donner un lien ^^ celui ci -> http://www.forexagone.com/apprendre/definition-forex/ask.html
  • oleo

    Ah oui ! effectivement, le forum modifie le texte !
    En tout cas, merci ! tu as fait considérablement avancer mon projet !!
    Si je peux me permettre d'etre exigeant avec toi : est il possible de limiter le nombre de trade à un par bougie ?
    a+
  • oleo

    Re,
    J'ai trouvé comment implémenter le code permettant un trade par bougie.
    De plus, pour bien comprendre comment cela fonctionne, j'ai essayer de changer quelques variables.
    Grace a tes précieux conseils, j'ai essayé de faire également un petit code.
    En revanche, je n'arrive pas à mettre dedans la partie lié au trailing stop
    Si tu peux y jeter un coup d'oeil

    extern double lots = 0.1; // Taille des lots
    extern double StopLoss = 150; // StopLoss en Pips
    extern double TakeProfit = 100; // TakeProfit en Pips
    extern double NbPips = 100; // Ecart par rapport à l'Open en Pips qui déclenche l'ordre
    extern double Periode = PERIOD_H1;


    int MAGIC = 1234;
    int TimeBar;


    //+------------------------------------------------------------------+
    //| expert initialization function |
    //+------------------------------------------------------------------+
    int init()
    {
    //----

    //----
    return(0);
    }
    //+------------------------------------------------------------------+
    //| expert deinitialization function |
    //+------------------------------------------------------------------+
    int deinit()
    {
    //----

    //----
    return(0);
    }
    //+------------------------------------------------------------------+
    //| expert start function |
    //+------------------------------------------------------------------+
    int start()
    {
    if (TimeBar==Time[0]) return(0);
    if (TimeBar==0) {TimeBar=Time[0];return(0);}

    if ( Ask > (iOpen(Symbol(),Periode,0)+(NbPips/10000)) ) // Bougie Up
    {
    TimeBar=Time[0];
    OrderSend(Symbol(), OP_BUY, lots, Ask, 1, Ask-(StopLoss/10000), Ask+(TakeProfit/10000), "", MAGIC, 0, Green);
    }

    if ( Bid < (iOpen(Symbol(),Periode,0)-(NbPips/10000)) ) // Bougie Up
    {
    TimeBar=Time[0];
    OrderSend(Symbol(), OP_SELL, lots, Bid, 1, Bid+(StopLoss/10000), Bid-(TakeProfit/10000), "", MAGIC, 0, Red);
    }

    return(0);


    }
    //+--------------------------+