Error 130 Help

 

Hello guys, just started learning MQL4 programming 2 days ago and I decided to code out an EA based on a strategy that I used to trade after

watching some tutorial videos online. It is still quite raw and needs a lot of adjustment. However, when I run the EA (it should at least work for

the Order Sending part), it keeps returning me back the error code #130. I done some research online and found out the possible reasons could

be the decimal points issue, order needs to be send and modified to have SL/TP and also the SL level being too close to the price. All of which I

had try to solve using NormalizeDouble, creating another function to send and modify immediately afterwards with SL/TP, and checked that the

SL level is more than the market stop loss level. Any advice/tips would be greatly appreciated!!

//+------------------------------------------------------------------+
//|                                                    34EMATest.mq4 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//--- input parameters
input double  Lots=1;            //Number of lots per trade
input int     maximumLots = 3;   //Maximum number of lots to hold
input int     riskTaken=3;       //Risk to be taken (1-Low,2-Med,3-High)
input double  riskToReward=1;    //Risk-to-Reward Ratio

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
      //Get stoploss from EMA 89
      double stopLoss = iMA(Symbol(),0,89,0,1,0,0); 
      
      
      // Calculate Entry Price, Take Profit, Buy or Sell Order, Current Price
      double entryPrice = 0;
      double takeProfit = 0, takeProfitAmount = 0; 
      double currentPriceBid = 0, currentPriceAsk = 0 , currentPrice= 0;
      MqlTick last_tick;
      int order=0; 
      
      //Get Current Price
      if(SymbolInfoTick(Symbol(),last_tick))
        {
         currentPriceBid = last_tick.bid;
         currentPriceAsk = last_tick.ask;
        }
         else Print("SymbolInfoTick() failed, error = ",GetLastError());
      
      
      // If EMA 89 is higher than current price
      // Calculate the entry price based on the EMA 34 according to the risk appetite (Current TimeFrame and Current Symbol)
      if (stopLoss > currentPriceBid)          
      {  
         order = 3; // Sell Pending Order
         currentPrice = currentPriceAsk;
         if (riskTaken == 1)
         entryPrice = iMA(Symbol(),PERIOD_CURRENT,34,0,MODE_EMA,PRICE_HIGH,0);   //Low Risk
         else if (riskTaken == 2)
         entryPrice = iMA(Symbol(),PERIOD_CURRENT,34,0,MODE_EMA,PRICE_CLOSE,0);  //Medium Risk
         else if (riskTaken == 3)
         entryPrice = iMA(Symbol(),PERIOD_CURRENT,34,0,MODE_EMA,PRICE_LOW,0);    //High Risk
         takeProfitAmount = (stopLoss - entryPrice) * riskToReward;
         takeProfit = entryPrice - takeProfitAmount;
         if (currentPrice >= entryPrice)
         {
            // Send Buy/Sell Pending Order
            int ticket = MarketOrderSend(Symbol(),order,Lots,NormalizeDouble(entryPrice,5),3,NormalizeDouble(stopLoss,5),NormalizeDouble(takeProfit,5),"",0);
            if(ticket<0)
              {
               Print("OrderSend failed with error #",GetLastError());
              }
            else
            {
               Print("OrderSend placed successfully");
            }
               
         }
         
      }
      // If EMA 89 is lower than current price
      // Calculate the entry price based on the EMA 34 according to the risk appetite (Current TimeFrame and Current Symbol)
      else
      {
         order = 2; // Buy Pending Order
         currentPrice = currentPriceBid;
         if (riskTaken == 3)
         entryPrice = iMA(Symbol(),PERIOD_CURRENT,34,0,MODE_EMA,PRICE_HIGH,0);   //High Risk
         else if (riskTaken == 2)
         entryPrice = iMA(Symbol(),PERIOD_CURRENT,34,0,MODE_EMA,PRICE_CLOSE,0);  //Medium Risk
         else if (riskTaken == 1)
         entryPrice = iMA(Symbol(),PERIOD_CURRENT,34,0,MODE_EMA,PRICE_LOW,0);    //Low Risk
         takeProfitAmount = (entryPrice - stopLoss) * riskToReward;
         takeProfit = entryPrice + takeProfitAmount;
         if (currentPrice <= entryPrice)
         {
            // Send Buy/Sell Pending Order
            int ticket = MarketOrderSend(Symbol(),order,Lots,NormalizeDouble(entryPrice,5),3,NormalizeDouble(stopLoss,5),NormalizeDouble(takeProfit,5),"",0);
            if(ticket<0)
              {
               Print("OrderSend failed with error #",GetLastError());
              }
            else
               Print("OrderSend placed successfully");
         }
         
         
      }
      
 }
 

//+------------------------------------------------------------------+
//+----------------------User-Defined Functions----------------------+
//+------------------------------------------------------------------+

int MarketOrderSend(string symbol, int cmd, double volume, double price, int slippage, double stoploss, double takeprofit, string comment, int magic)
{
   int ticket;
   
   ticket = OrderSend(symbol, cmd, volume, price, slippage, 0, 0, NULL, magic);
   if(ticket <= 0) Alert("OrderSend Error: ", GetLastError());
   else
   {
      bool res = OrderModify(ticket, 0, stoploss, takeprofit, 0);
      if(!res) {  Alert("OrderModify Error: ", GetLastError());
                  Alert("IMPORTANT: ORDER #", ticket, " HAS NO STOPLOSS AND TAKEPROFIT");}
   }
   return(ticket);
}
 

I met this situation before too,  StopLoss too small, or slippage pips too small, try to make slippage bigger. untill it works. 

May be it's in OrderSend function, or OrderClose function. I solved this already. Beginner too. 

 
         order = 3; // Sell Pending Order

Especially, when posting code here, it is a good idea to get into the habit of using enums.

         order = OP_SELLLIMIT; // Sell Pending Order

is instantly understood.

         currentPrice = currentPriceAsk;

Why are you getting the Ask price for a Sell limit order? Sells are at Bid.

         if (currentPrice >= entryPrice)

Current price must be smaller than the entry price for a sell limit

Placing a sell limit order at a lower price than the current price will give error 130

 
You are using slippage of 3. On a 5 digit broker that is 0.3 pips. On common pairs try 3 pip. On others, like EURNOK and USDMXN the spread ranges 30-300 pips! I use slippage=2(spread)
Reason: