EA issue : Ordersend error 131 / 130 at US30 indx

 

Hello

the EA works very well on all pairs but in US30 it gives me ordersend error 131 : invalide trade volume and order130 invalide stops

here is the code 

#property copyright "Copyright 2020"
#property version   "1.00"
#property strict



input  string A1="========== EA SETTINGS ==========";       //========== EA settings ==========
bool                        useSL=true;                 
extern int                  StopLoss=40;               // Stop Loss Pips
bool                        useTP=true;               
extern int                  TakeProfite=40;          // Take profit Pips
extern double               iLots    = 0.1;         // Lot size
extern int                  MagicNumber=123;      // Magic Number 


extern string A2="========== MA SETTINGS =========="; //========== MA settings ==========
extern int                  MAH_1period=9;          // Fast MA            
extern int                  MAH_shift=0;            // MA SHIFT                 
extern ENUM_MA_METHOD       MAH_method=MODE_EMA;   // MA METHOD        
extern ENUM_APPLIED_PRICE   MA_HApplied_price=PRICE_CLOSE; //MA APPLIED PRICE 


extern string B1="========== MA SETTINGS =========="; //========== MA settings ==========
extern int                  MAH_1period2=21;          // Slow MA            
extern int                  MAH_shift2=0;            // MA SHIFT                 
extern ENUM_MA_METHOD       MAH_method2=MODE_EMA;   // MA METHOD        
extern ENUM_APPLIED_PRICE   MA_HApplied_price2=PRICE_CLOSE; //MA APPLIED PRICE 

extern string C="=======ZigZag SETTINGS======"; //========== ZigZag settings ==========
extern int Depth=12;
extern int Deviation=5;
extern int Backstep=3;

extern string B2="========== MT4 Alerts ==========";//========== MT4 Alerts ==========
extern bool                 Email=true;            //Email Notification ?
extern bool                 PopUp=true;           //MT4 Alert ?
extern bool                 Mobile=true;         //Push Notification ?



//----------------
double pips;
double close;
double minsltp;
double TP,SL; 
double point;
int digits,Q;
int ThisBarTradeO=0;
bool NewBarO;
int ThisBarTrade=0;
bool NewBar;
int Lot_Decimal;
int _LotDigits;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   double ticksize=MarketInfo(Symbol(),MODE_TICKSIZE);
   if(ticksize==0.00001 || ticksize==0.001)
      pips=ticksize*10;
   else
      pips=ticksize;

       
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---

      if(IsConnected()!=true)
     {
      ObjectSetText("datf_0","Terminal not connected with i-net!",10,"Verdana",clrRed);
      return;
     }
   if(IsTradeAllowed()!=true)
     {
      ObjectSetText("datf_0","Expert trade not allowed (check mt4 settings)!",10,"Verdana",clrRed);
      return;
     }
   if(IsExpertEnabled()==false && IsTesting()==false)
     {
      ObjectSetText("datf_0","Expert trade not enabled! (check mt4 settings)",10,"Verdana",clrRed);
      return;
     }
   if(IsTradeContextBusy())
     {
      ObjectSetText("datf_0","Trade Context is Busy!",10,"Verdana",clrRed);
      return;
     }
//---
  
 

 
  
      double MAfast=iMA(NULL,0,MAH_1period,MAH_shift,MAH_method,MA_HApplied_price,1);
      double MAfast2=iMA(NULL,0,MAH_1period,MAH_shift,MAH_method,MA_HApplied_price,2);
       
      double MAslow=iMA(NULL,0,MAH_1period2,MAH_shift2,MAH_method2,MA_HApplied_price2,1);
      double MAslow2=iMA(NULL,0,MAH_1period2,MAH_shift2,MAH_method2,MA_HApplied_price2,2);
      

       
    if( MAfast2>MAslow2 && MAfast<=MAslow  && orderscnt(OP_SELL)==0   ){
          if(TakeProfite!=0){TP=Bid-TakeProfite*pips;}else TP=0;
        if (useTP==false)TP=0;
        if(StopLoss!=0){SL=Bid+StopLoss*pips;}else SL=0; 
        if(useSL==false)SL=0;
         
        double sell= OrderSend(NULL,OP_SELL,iLots,Bid,5*Q,SL,TP,NULL,MagicNumber,0,clrRed);
      
        if(Email)SendMail(Symbol(),"New Sell Order @ "+(string)Ask);
        if(PopUp)Alert(Symbol()+" New Sell Order @ "+(string)Ask);
        if(Mobile)SendNotification(Symbol()+" New Sell Order @ "+(string)Ask);
        
        }
        
       
       
    if( MAfast2<MAslow2 &&MAfast>=MAslow && orderscnt(OP_BUY)==0 ){
         if(TakeProfite!=0){TP=Ask+TakeProfite*pips;}else TP=0;
         if (useTP==false)TP=0;
        if(StopLoss!=0){SL=Ask-StopLoss*pips;}else SL=0;
        if(useSL==false)SL=0;
       
        double buy=OrderSend(NULL,OP_BUY,iLots,Ask,5*Q,SL,TP,NULL,MagicNumber,0,clrBlue);
       
        if(Email)SendMail(Symbol(),"New Buy Order @ "+(string)Ask);
        if(PopUp)Alert(Symbol()+" New Buy Order @ "+(string)Ask);
        if(Mobile)SendNotification(Symbol()+" New Buy Order @ "+(string)Ask);
     
        }
  

  
       
  }

     
//+------------------------------------------------------------------+




    


 int orderscnt(int tip)
     {
      int cnt=0;
      for(int i=0;i<OrdersTotal();i++)
        {
         if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
         if(OrderSymbol()==Symbol() && MagicNumber==OrderMagicNumber()&&OrderType()==tip)
           {
            cnt++;
           }
        }
      return(cnt);
      }
      

//-------------------------------------------------------------------+
    void CloseOrders(int tip) {
         int cnt=OrdersTotal();
         for(int i=cnt-1; i>=0; i--)
           {
            if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
            if(OrderSymbol()==Symbol()&& OrderMagicNumber()==MagicNumber&&OrderType()==tip)
                 {
                 
                     close=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),50,clrGreen);   
           }
       }
   



}
//------------------------------------------





//+------------------------------------------------------------------+
Documentation on MQL5: Constants, Enumerations and Structures / Indicator Constants / Price Constants
Documentation on MQL5: Constants, Enumerations and Structures / Indicator Constants / Price Constants
  • www.mql5.com
Calculations of technical indicators require price values and/or values of volumes, on which calculations will be performed. There are 7 predefined identifiers from the ENUM_APPLIED_PRICE enumeration, used to specify the desired price base for calculations. If a technical indicator uses for calculations price data, type of which is set by...
 
Topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.
I will move your topic to the MQL4 and Metatrader 4 section.
 
Oussama Mansour:

Hello

the EA works very well on all pairs but in US30 it gives me ordersend error 131 : invalide trade volume and order130 invalide stops

here is the code 

Hello,

check your minimal lot size, you will it on specification of US30 like me :



Here you see in mimimal volume it is 1.

 
  1. You are trading 5Q, but Q has no value.
  2. Risk depends on your initial stop loss, lot size, and the value of the symbol. It does not depend on margin and leverage. No SL means you have infinite risk. Never risk more than a small percentage of your trading funds, certainly less than 2% per trade, 6% total.

    1. You place the stop where it needs to be — where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.

    2. AccountBalance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the spread, and DeltaPerLot is usually around $10/pip but it takes account of the exchange rates of the pair vs. your account currency.)

    3. Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency.
                MODE_TICKVALUE is not reliable on non-fx instruments with many brokers - MQL4 programming forum 2017.10.10
                Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum 2018.02.11
                Lot value calculation off by a factor of 100 - MQL5 programming forum 2019.07.19

    4. You must normalize lots properly and check against min and max.

    5. You must also check FreeMargin to avoid stop out

    Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5 or 0.1 Lots maximum.

Reason: