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

Besoin d'aide sur le code de mon EA, fermeture des ordres au croisement

  • stephane116

    Bonjour à tous, je voudrais que mon EA (stratégie simple) ferme les ordres au CROISEMENT des moyennes mobiles, tout simplement.
    Mais je n'arrive pas à trouver "la formule" qui peut exécuter cette fonction.
    J'ai pourtant essayé des tas de solutions, comme par ex: if(Ask<=(ma5(1)<ma8(1)&&ma5(0)>ma8(0))).....CloseAll(); etc...et bien d'autres,
    mais ça ne donne rien. Pire, à chaque modif, le compilateur m'indique: 0 errors/ 0 warning ! Mais sur le test, il ne se passe rien.
    Le résultat est tjs le même, comme si je n'avais rien ajouté ou écrisdans le code.
    Autrement, L'EA fonctionne à merveille, il exécute les ordres Buy/Sell , ferme au TP et SL, sans problème.
    Pouvez-vous m'aider pour trouver ce bug svp?
    Je vous remercie d'avance.
    Stéphane.


    Code
    [ input string EAsettings = "[EA settings]"; //Paramètres input double lots = 0.1; //Lot size input double TakeProfit = 20; //Take profit input double StopLoss = 0; //SoptLoss input double distance= 0; //X Pips (Distance) input int MagicNumber = 55545; //Magic Nb input string EA_Comment = ""; // ~~~~ //------------- input string Rsi = "RSI Settings"; //RSI input int rsi_period = 14; //Rsi period input ENUM_APPLIED_PRICE rsi_applied_price=PRICE_CLOSE;//~~ //------------- input string Ma1 = "[===MA1===]"; //Fast MA input int ma_period1 = 5; //MA 1 period input ENUM_MA_METHOD ma_type1 = MODE_EMA; //MA 1 type //------------- input string Ma2 = "[===MA2===]"; //Low MA input int ma_period2 = 8; //MA 2 period input ENUM_MA_METHOD ma_type2 = MODE_EMA; //MA 2 type datetime time; double _point=1; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- create timer string _sym=Symbol(); double _digits=MarketInfo(_sym,MODE_DIGITS); if(_digits==5||_digits==3) _point=1/MathPow(10,(_digits-1)); if(_digits==4||_digits==2) _point=1/MathPow(10,(_digits)); if(_digits==1) _point=0.1; EventSetTimer(60); time=Time[0]; { return (0); } //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- destroy timer EventKillTimer(); } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { if( time==Time[0])return; time=Time[0]; //-----Fonctions Achat et Vente ---------------- if(OrdersTotalT(OP_BUY)==0 && ma1(1)>ma2(1) && Ask>ma1(0)+distance*_point && rsi(1)>=50) SendOrder(OP_BUY); if(OrdersTotalT(OP_SELL)==0 && ma1(1)<ma2(1) && Bid<ma1(0)-distance*_point && rsi(1)<50) SendOrder(OP_SELL); //-------- Fonction de fermeture d'ordres au croisement des moyennes mobiles -------------- double Cr1 = (ma1(1)<ma2(1)&&ma1(0)>ma2(0)); //(buy) double Cr2 = (ma1(1)>ma2(1)&&ma1(0)<ma2(0)); //(sell) if (Ask == Cr1) CloseAll(); if (Bid == Cr2) CloseAll(); //----------------------------------------------------------------------------------------- } //+------------------------------------------------------------------+ //| Timer function | //+------------------------------------------------------------------+ void OnTimer() { //--- } //+------------------------------------------------------------------+ //| ChartEvent function | //+------------------------------------------------------------------+ void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam) { //--- } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ bool SendOrder(int type,double _price=0) { while(IsTradeContextBusy()); int ticket=-1; double SL,TP; if(type==OP_BUY) { if(StopLoss==0) {SL=0;}else{SL=Ask-StopLoss*_point;} if(TakeProfit==0) {TP=0;} else {TP=Ask+TakeProfit*_point;} ticket=OrderSend(Symbol(),OP_BUY,NormalizeLots(LotManage(),Symbol()),Ask,3,SL,TP,EA_Comment,MagicNumber,0,clrBlue); } if(type==OP_SELL) { if(StopLoss==0){SL=0;}else{SL=Bid+StopLoss*_point;} if(TakeProfit==0) {TP=0;} else {TP=Bid-TakeProfit*_point;} ticket=OrderSend(Symbol(),OP_SELL,NormalizeLots(LotManage(),Symbol()),Bid,3,SL,TP,EA_Comment,MagicNumber,0,clrRed); } if(ticket<0) { Print("OrderSend failed with error #",GetLastError()); return(false); } return(true); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ double LotManage() { return (NormalizeDouble(lots,2)); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ double NormalizeLots(double _lots,string pair="") { if(pair=="") pair=Symbol(); double lotStep=MarketInfo(pair,MODE_LOTSTEP), minLot=MarketInfo(pair,MODE_MINLOT); _lots=MathRound(_lots/lotStep)*lotStep; if(_lots<MarketInfo(pair,MODE_MINLOT)) _lots=MarketInfo(pair,MODE_MINLOT); if(_lots>MarketInfo(pair,MODE_MAXLOT)) _lots=MarketInfo(pair,MODE_MAXLOT); return(_lots); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ double rsi(int _shift) { return(iRSI(NULL,0,rsi_period,rsi_applied_price,_shift)); } double ma1(int _shift=0) { return(iMA(NULL,0,ma_period1,0,ma_type1,PRICE_CLOSE,_shift));} double ma2(int _shift=0) { return(iMA(NULL,0,ma_period2,0,ma_type2,PRICE_CLOSE,_shift));} //+------------------------------------------------------------------+ // | //-------------------------------------------------------------------+ int OrdersTotalT(int _type) { int _total=0; for(int cnt=OrdersTotal()-1;cnt>=0;cnt--) { bool select=OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES); if(OrderMagicNumber()==MagicNumber && OrderSymbol()==Symbol() && OrderType()==_type) { _total++; } } return(_total); } //........................................................................................................ void CloseOrders(int type) { bool result=false; int count=0,total=OrdersTotal(); for(count=total-1; count>=0; count--) { if(OrderSelect(count,SELECT_BY_POS,MODE_TRADES)) if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber && OrderType()==type) { if(OrderType()<2) result=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,clrNONE); else result=OrderDelete(OrderTicket()); } } } //...................................................................................................... void CloseAll() { bool result=false; for(int _count=OrdersTotal()-1; _count>=0; _count--) { RefreshRates(); if(OrderSelect(_count,SELECT_BY_POS,MODE_TRADES)) { if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber) { if(OrderType()<2) result=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,clrYellow); } } } } //........................................................................................................]
    Modifié le 2017-12-18 11:52:20 par AliX : [code] [/code]
    stephane116 a joint une image
    totalement-deconcerte-quelqu-un-peut-il-m-aider-sur-le-code-de-mon-ea-svp-10772
  • riden

    Salut
    Tu as ecrit:

    Code
    //-------- Fonction de fermeture d'ordres au croisement des moyennes mobiles -------------- double Cr1 = (ma1(1)<ma2(1)&&ma1(0)>ma2(0)); //(buy) double Cr2 = (ma1(1)>ma2(1)&&ma1(0)<ma2(0)); //(sell) if (Ask == Cr1) CloseAll(); if (Bid == Cr2) CloseAll();

    Cr1 et Cr2 vont recevoir une valeur bool (0 ou 1), donc Ask ou Bid ne seront jamais égal à Cr1 et Cr2. CloseAll() ne s'exécutera pas.


    Essayes:

    Code
    //-------- Fonction de fermeture d'ordres au croisement des moyennes mobiles -------------- bool Cr1, Cr2; Cr1 = (ma1(1)<ma2(1)&&ma1(0)>ma2(0)); //(buy) Cr2 = (ma1(1)>ma2(1)&&ma1(0)<ma2(0)); //(sell) if (Cr1 || Cr2) CloseAll();


    Après, si tu veux fermer uniquement les positions de ventes (ou d'achat) tu peux modifier CloseAll en ajoutant un string (par exemple CloseAll("achat";);
    Modifié le 2017-12-18 11:56:47 par AliX : [code][/code]
  • stephane116

    Merci, je vais essayer de cette manière.
    C'est vrai, mélanger bool et double ne peut pas fonctionner.
  • stephane116 — en réponse à riden dans son message #110133

    Salut, j'ai trouvé la solution,
    il faut écrire le code comme ceci:
    if ((ma1(1)>ma2(2)&&ma1(0)<ma2(0)) || (ma1(1)<ma1(1)&&ma1(0)>ma2(0)) == True) CloseAll();
    ou plus simplement:
    if (Cr1 || Cr2 == True) Closeall();

    Salutations et tout mes meilleurs voeux pour 2018 ))))))))))))))
  • riden — en réponse à stephane116 dans son message #110151

    Salut,

    if ( A == true) c'est la même chose que if(A)
    if (Cr1 || Cr2 == true) ---> if(Cr1 || Cr2)