Order wird nicht ausgeführt [invalid stops]

 

Hallo comunity, vieleicht kann mir ja einer Helfen.

bekommen diesen EA nicht zum laufen, zwar werden Conditionen für eine Buy/Sell order erfüllt aber er möchte keine Trades absetzen

es kommt der Fehler   beim MNQU21 

2021.06.27 17:44:36.556 2021.06.18 12:40:00   failed exchange sell 1 MNQU21 at 14077.25 sl: 14077.55 tp: 14076.25 [Invalid stops]

2021.06.27 17:44:36.556 2021.06.18 12:40:00   CTrade::OrderSend: exchange sell 1.00 MNQU21 sl: 14077.55 tp: 14076.25 [invalid stops]

und das bei jedem Versuch  einen Trade auszuführen.

bin bei AMP/CQG Futures handel 


bei EUR/USD geht die Orderausführung aber ganz normal


//+------------------------------------------------------------------+
//|                                                        MA_EA.mq5 |
//|                                                   Copyright 2014 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014"
#property version   "1.00"
//--- input parameters
int            StopLoss=50;      // Stop Loss
input int      TakeProfit=100;   // Take Profit
input int      ADX_Period=8;     // ADX Period
input int      MA_Period=8;      // Moving Average Period
input int      EA_Magic=12345;   // EA Magic Number
input double   Adx_Min=22.0;     // Minimum ADX Value
double         Lot=1.0;          // Lots to Trade
//--- Other parameters
int adxHandle; // handle for our ADX indicator
int maHandle;  // handle for our Moving Average indicator
double plsDI[],minDI[],adxVal[]; // Dynamic arrays to hold the values of +DI, -DI and ADX values for each bars
double maVal[]; // Dynamic array to hold the values of Moving Average for each bars
double p_close; // Variable to store the close value of a bar
int STP, TKP;   // To be used for Stop Loss & Take Profit values
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- Get handle for ADX indicator
   adxHandle=iADX(NULL,0,ADX_Period);
//--- Get the handle for Moving Average indicator
   maHandle=iMA(_Symbol,_Period,MA_Period,0,MODE_EMA,PRICE_CLOSE);
//--- What if handle returns Invalid Handle
   if(adxHandle<0 || maHandle<0)
     {
      Alert("Error Creating Handles for indicators - error: ",GetLastError(),"!!");
      return(-1);
     }

//--- Let us handle currency pairs with 5 or 3 digit prices instead of 4
   STP = StopLoss;
   TKP = TakeProfit;
   if(_Digits==5 || _Digits==3)
     {
      STP = STP*10;
      TKP = TKP*10;
     }
   return(0);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- Release our indicator handles
   IndicatorRelease(adxHandle);
   IndicatorRelease(maHandle);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//--- Do we have enough bars to work with
   if(Bars(_Symbol,_Period)<60) // if total bars is less than 60 bars
     {
      Alert("We have less than 60 bars, EA will now exit!!");
      return;
     }  

// We will use the static Old_Time variable to serve the bar time.
// At each OnTick execution we will check the current bar time with the saved one.
// If the bar time isn't equal to the saved time, it indicates that we have a new tick.

   static datetime Old_Time;
   datetime New_Time[1];
   bool IsNewBar=false;

// copying the last bar time to the element New_Time[0]
   int copied=CopyTime(_Symbol,_Period,0,1,New_Time);
   if(copied>0) // ok, the data has been copied successfully
     {
      if(Old_Time!=New_Time[0]) // if old time isn't equal to new bar time
        {
         IsNewBar=true;   // if it isn't a first call, the new bar has appeared
         if(MQL5InfoInteger(MQL5_DEBUGGING)) Print("We have new bar here ",New_Time[0]," old time was ",Old_Time);
         Old_Time=New_Time[0];            // saving bar time
        }
     }
   else
     {
      Alert("Error in copying historical times data, error =",GetLastError());
      ResetLastError();
      return;
     }

//--- EA should only check for new trade if we have a new bar
   if(IsNewBar==false)
     {
      return;
     }
 
//--- Do we have enough bars to work with
   int Mybars=Bars(_Symbol,_Period);
   if(Mybars<60) // if total bars is less than 60 bars
     {
      Alert("We have less than 60 bars, EA will now exit!!");
      return;
     }

//--- Define some MQL5 Structures we will use for our trade
   MqlTick latest_price;      // To be used for getting recent/latest price quotes
   MqlTradeRequest mrequest;  // To be used for sending our trade requests
   MqlTradeResult mresult;    // To be used to get our trade results
   MqlRates mrate[];          // To be used to store the prices, volumes and spread of each bar
   ZeroMemory(mrequest);      // Initialization of mrequest structure
   
/*
     Let's make sure our arrays values for the Rates, ADX Values and MA values 
     is store serially similar to the timeseries array
*/
// the rates arrays
   ArraySetAsSeries(mrate,true);
// the ADX DI+values array
   ArraySetAsSeries(plsDI,true);
// the ADX DI-values array
   ArraySetAsSeries(minDI,true);
// the ADX values arrays
   ArraySetAsSeries(adxVal,true);
// the MA-8 values arrays
   ArraySetAsSeries(maVal,true);


//--- Get the last price quote using the MQL5 MqlTick Structure
   if(!SymbolInfoTick(_Symbol,latest_price))
     {
      Alert("Error getting the latest price quote - error:",GetLastError(),"!!");
      return;
     }

//--- Get the details of the latest 3 bars
   if(CopyRates(_Symbol,_Period,0,3,mrate)<0)
     {
      Alert("Error copying rates/history data - error:",GetLastError(),"!!");
      ResetLastError();
      return;
     }

//--- Copy the new values of our indicators to buffers (arrays) using the handle
   if(CopyBuffer(adxHandle,0,0,3,adxVal)<0 || CopyBuffer(adxHandle,1,0,3,plsDI)<0
      || CopyBuffer(adxHandle,2,0,3,minDI)<0)
     {
      Alert("Error copying ADX indicator Buffers - error:",GetLastError(),"!!");
      ResetLastError();
      return;
     }
   if(CopyBuffer(maHandle,0,0,3,maVal)<0)
     {
      Alert("Error copying Moving Average indicator buffer - error:",GetLastError());
      ResetLastError();
      return;
     }
//--- we have no errors, so continue
//--- Do we have positions opened already?
   bool Buy_opened=false;  // variable to hold the result of Buy opened position
   bool Sell_opened=false; // variables to hold the result of Sell opened position


   int total= PositionsTotal();
   for(int i=0;i<total;i++)
     {
      if(PositionGetSymbol(i)==_Symbol);
        {
         if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
           {
            Buy_opened=true;  //It is a Buy
           }
         else if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
           {
            Sell_opened=true; // It is a Sell
           }
        }
      }

// Copy the bar close price for the previous bar prior to the current bar, that is Bar 1
   p_close=mrate[1].close;  // bar 1 close price

/*
    1. Check for a long/Buy Setup : MA-8 increasing upwards, 
    previous price close above it, ADX > 22, +DI > -DI
*/
//--- Declare bool type variables to hold our Buy Conditions
   bool Buy_Condition_1=(maVal[0]>maVal[1]) && (maVal[1]>maVal[2]); // MA-8 Increasing upwards
   bool Buy_Condition_2 = (p_close > maVal[1]);         // previuos price closed above MA-8
   bool Buy_Condition_3 = (adxVal[0]>Adx_Min);          // Current ADX value greater than minimum value (22)
   bool Buy_Condition_4 = (plsDI[0]>minDI[0]);          // +DI greater than -DI

//--- Putting all together   
   if(Buy_Condition_1 && Buy_Condition_2)
     {
      if(Buy_Condition_3 && Buy_Condition_4)
        {
         // any opened Buy position?
         if(Buy_opened)
           {
            Alert("We already have a Buy Position!!!");
            return;    // Don't open a new Buy Position
           }
         ZeroMemory(mrequest);
         mrequest.action = TRADE_ACTION_DEAL;                                  // immediate order execution
         mrequest.price = NormalizeDouble(latest_price.ask,_Digits);           // latest ask price
         mrequest.sl = NormalizeDouble(latest_price.ask - STP*_Point,_Digits); // Stop Loss
         mrequest.tp = NormalizeDouble(latest_price.ask + TKP*_Point,_Digits); // Take Profit
         mrequest.symbol = _Symbol;                                            // currency pair
         mrequest.volume = Lot;                                                 // number of lots to trade
         mrequest.magic = EA_Magic;                                             // Order Magic Number
         mrequest.type = ORDER_TYPE_BUY;                                        // Buy Order
         mrequest.type_filling = ORDER_FILLING_FOK;                             // Order execution type
         mrequest.deviation=100;                                                // Deviation from current price
         //--- send order
         OrderSend(mrequest,mresult);
         // get the result code
         if(mresult.retcode==10009 || mresult.retcode==10008) //Request is completed or order placed
           {
            Alert("A Buy order has been successfully placed with Ticket#:",mresult.order,"!!");
           }
         else
           {
            Alert("The Buy order request could not be completed -error:",GetLastError());
            ResetLastError();           
            return;
           }
        }
     }
/*
    2. Check for a Short/Sell Setup : MA-8 decreasing downwards, 
    previous price close below it, ADX > 22, -DI > +DI
*/
//--- Declare bool type variables to hold our Sell Conditions
   bool Sell_Condition_1 = (maVal[0]<maVal[1]) && (maVal[1]<maVal[2]);  // MA-8 decreasing downwards
   bool Sell_Condition_2 = (p_close <maVal[1]);                         // Previous price closed below MA-8
   bool Sell_Condition_3 = (adxVal[0]>Adx_Min);                         // Current ADX value greater than minimum (22)
   bool Sell_Condition_4 = (plsDI[0]<minDI[0]);                         // -DI greater than +DI

//--- Putting all together
   if(Sell_Condition_1 && Sell_Condition_2)
     {
      if(Sell_Condition_3 && Sell_Condition_4)
        {
         // any opened Sell position?
         if(Sell_opened)
           {
            Alert("We already have a Sell position!!!");
            return;    // Don't open a new Sell Position
           }
         ZeroMemory(mrequest);
         mrequest.action=TRADE_ACTION_DEAL;                                // immediate order execution
         mrequest.price = NormalizeDouble(latest_price.bid,_Digits);           // latest Bid price
         mrequest.sl = NormalizeDouble(latest_price.bid + STP*_Point,_Digits); // Stop Loss
         mrequest.tp = NormalizeDouble(latest_price.bid - TKP*_Point,_Digits); // Take Profit
         mrequest.symbol = _Symbol;                                          // currency pair
         mrequest.volume = Lot;                                              // number of lots to trade
         mrequest.magic = EA_Magic;                                          // Order Magic Number
         mrequest.type= ORDER_TYPE_SELL;                                     // Sell Order
         mrequest.type_filling = ORDER_FILLING_FOK;                          // Order execution type
         mrequest.deviation=100;                                             // Deviation from current price
         //--- send order
         OrderSend(mrequest,mresult);
         // get the result code
         if(mresult.retcode==10009 || mresult.retcode==10008) //Request is completed or order placed
           {
            Alert("A Sell order has been successfully placed with Ticket#:",mresult.order,"!!");
           }
         else
           {
            Alert("The Sell order request could not be completed -error:",GetLastError());
            ResetLastError();
            return;
           }
        }
     }
   return;
  }
//+------------------------------------------------------------------+



 
Eugen8519:

Hallo comunity, vieleicht kann mir ja einer Helfen.

bekommen diesen EA nicht zum laufen, zwar werden Conditionen für eine Buy/Sell order erfüllt aber er möchte keine Trades absetzen

es kommt der Fehler   beim MNQU21 

2021.06.27 17:44:36.556 2021.06.18 12:40:00   failed exchange sell 1 MNQU21 at 14077.25 sl: 14077.55 tp: 14076.25 [Invalid stops]

2021.06.27 17:44:36.556 2021.06.18 12:40:00   CTrade::OrderSend: exchange sell 1.00 MNQU21 sl: 14077.55 tp: 14076.25 [invalid stops]

und das bei jedem Versuch  einen Trade auszuführen.

bin bei AMP/CQG Futures handel 


bei EUR/USD geht die Orderausführung aber ganz normal




Steht ja da, das der abstand von sl nicht passt

 
amando:

Steht ja da, das der abstand von sl nicht passt

danke, habe den Test auch falsch laufen lassen am 1.6.21 war der MNQU21 ja noch inaktiv

 
kann den ea mal noch jemand testen, so richtig funktionieren tut er nicht, eröffnet und schließt positionen in der selben kerzen ohne das take profit oder sl ausgelöst wurden
 
Eugen8519:
kann den ea mal noch jemand testen, so richtig funktionieren tut er nicht, eröffnet und schließt positionen in der selben kerzen ohne das take profit oder sl ausgelöst wurden
Dann stimmt die Logik des Programms nicht. Man könnte prüfen ob es bereits offenen Positionen gibt (PositionsTotal()).
Documentation on MQL5: Trade Functions / PositionsTotal
Documentation on MQL5: Trade Functions / PositionsTotal
  • www.mql5.com
PositionsTotal - Trade Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Carl Schreiber:
Dann stimmt die Logik des Programms nicht. Man könnte prüfen ob es bereits offenen Positionen gibt (PositionsTotal()).
Nun ja, beschrieben wurde der EA halt so: wenn alle Konditionen übereinstimmen wird bei close über oder unter EMA 8 ein Trade  eröffnet und für close sollte SL, TP oder Position Umkehrung sein. 
Warum da so ein durcheinander ist, ist mir auch nicht klar. 
Wobei für die Prüfung von offenen Positionen haben wir bereits 

int total= PositionsTotal();
   for(int i=0;i<total;i++)
     {
      if(PositionGetSymbol(i)==_Symbol);
        {
         if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
           {
            Buy_opened=true;  //It is a Buy
           }
         else if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
           {
            Sell_opened=true; // It is a Sell
           }
        }


Aber ich glaube auch nicht, dass es deswegen zu vielen Trades kommt, er eröffnet fast bei jeder neuen Kerzen neue Positionen

 
Eugen8519:
Nun ja, beschrieben wurde der EA halt so: wenn alle Konditionen übereinstimmen wird bei close über oder unter EMA 8 ein Trade  eröffnet und für close sollte SL, TP oder Position Umkehrung sein. 
Warum da so ein durcheinander ist, ist mir auch nicht klar. 
Wobei für die Prüfung von offenen Positionen haben wir bereits 


Aber ich glaube auch nicht, dass es deswegen zu vielen Trades kommt, er eröffnet fast bei jeder neuen Kerzen neue Positionen

Ja weil bei deine verschachtelten if abfragen er mit return nur aus der eigenen schleife geht

 
Das heißt? Was kann man machen,  ich denke doch nicht, dass dieser EA so gedacht war, es ist also fehlerhaft? 
 
Eugen8519:
Das heißt? Was kann man machen,  ich denke doch nicht, dass dieser EA so gedacht war, es ist also fehlerhaft? 

Dachte du hast den selbst geschrieben

 

Na ja, soweit bin ich noch nicht. 

Ich habe den EA Schritt für Schritt hier https://www.mql5.com/ru/articles/100 Abgeschrieben. 

Alles genau durchgelesen wofür was steht, obwohl mir einiges noch nicht so einleuchtend ist, aber ich bin schon um einiges weiter als vor ein paar Wochen. 

Kann es natürlich nicht nachvollziehen warum er nach close über EMA überschüssige Positionen eröffnet und vor allem, nach welchen Angaben diese kurzer Zeit später wieder geschlossen werden, so stand es natürlich nicht in der Anleitung. Schon komisch

Пошаговое руководство по написанию MQL5-советников для начинающих
Пошаговое руководство по написанию MQL5-советников для начинающих
  • www.mql5.com
Написание советников на MQL5 проще чем кажется, вы легко можете этому научиться. В этом руководстве вы познакомитесь с основными моментами, необходимыми для написания простого советника на основе конкретной торговой стратегии. Рассмотрена структура советника, использование встроенных технических индикаторов и торговых функций, вопросы отладки и тестирования советника на исторических данных.
 

Wenn Du in dem Link: https://www.mql5.com/ru/articles/100  das ru durch de ersetzt hast Du den Artikel auf Deutsch: https://www.mql5.com/de/articles/100.

Aber kennst Du die Beispiele für EAs in \Experts\Advisors ?

Schrittweiser Leitfaden für Anfänger zum Schreiben eines Expert Advisors in MQL5
Schrittweiser Leitfaden für Anfänger zum Schreiben eines Expert Advisors in MQL5
  • www.mql5.com
Die Programmierung des Expert Advisors in MQL5 ist einfach und kann problemlos erlernt werden. In diesem Leitfaden werden nacheinander die zum Schreiben eines einfachen Expert Advisors auf Grundlage einer entwickelten Handels-Strategie erforderlichen, grundlegenden Schritte erklärt. Es werden hier die Struktur eines Expert Advisors, die Verwendung eingebauter technischer Indikatoren und Handels-Funktionen, die Details des Fehlersuch(Debug)-Modus und die Verwendung des Strategie-Testers präsentiert.