Problems with custom "QuickOrderSend" function

 

Hi everyone,

I'm quite new to the EA trading but I want to learn and improve so I started an EA by myself and learned a bit of MQL4 coding...

So my first EA is about to start "working" but there starts the first problems, I encountered some trouble while trying to use functions to place and close my orders and the 4051 error is appearing when the code tries to place any order. I think that it's because of the way i'm declaring the parameters of the "ordersend" line.

Can anyone help me in any way by looking to my code ? I'm too much of a noob in english and MQL4 coding to find it by myself on internet...

//+------------------------------------------------------------------+
//|                         CUSTOM FUNCTIONS                         |
//+------------------------------------------------------------------+

int SimpleOrderOpen(double SimpleOrder_Lot,int SimpleOrder_Type)
   {
   
   int Order_Ticket;
   double OrderPrice;
   int Price_Type;
   
   int Order_Retry = 0;
   int Order_RetryLimit = 5;
   
   {
   if (SimpleOrder_Type == OP_BUY)
      Price_Type = Ask;
   else if (SimpleOrder_Type == OP_SELL)
      Price_Type = Bid;
   }
   
   {
   double MinStopLevel = MarketInfo(Symbol(),MODE_STOPLEVEL);
   double TakeProfit = NormalizeDouble(Price_Type+MinStopLevel*Point,Digits);
   double StopLoss = NormalizeDouble(Price_Type-MinStopLevel*Point,Digits);
   }
   
   SimpleOrder_Lot = NormalizeDouble(SimpleOrder_Lot,2);
      
      {
                   do
                   {
                        Order_Ticket = OrderSend(Symbol(),SimpleOrder_Type,1,Price_Type,3,StopLoss,TakeProfit);
                        OrderPrice = Price_Type;
                        Order_Retry++;
              }
                   while((Order_Ticket<0) || (Order_Retry!=Order_RetryLimit));
      
                   if (Order_Ticket>0)
                   {
                        Print("OrderSend placed successfully");
                        Order_Retry=0;
                   }
                   else if (Order_Ticket<0)
                   {
                        Print("OrderSEND failed with error #",GetLastError()," After ",Order_RetryLimit," order(s) sent.");
                        ExpertRemove();
                   }
        
              return(Order_Ticket);
           }
        }
//+------------------------------------------------------------------+

I'm also opened to any advice and critics about my code!

Thanks by advance for your advices!

 
Please don't just post "4051 error", tell us what the error is so that we don't have to look it up. If you want help, help others to help you.
 
Keith Watford:
Please don't just post "4051 error", tell us what the error is so that we don't have to look it up. If you want help, help others to help you.

Hi sorry for the lack of informations, the exact message is "OrderSend error 4051", that translate to "invalid lots amount for OrderSend function", I tried to replace the variable directly by the lots amount I want to use but it didn't work either...

I hope you'll be able to help me ^^ I read on forums that this thing can happen in other cases than with a problem with the lots amount parameter...

 
Matthieu:

Hi sorry for the lack of informations, the exact message is "OrderSend error 4051", that translate to "invalid lots amount for OrderSend function"....

4051 is Invalid parameter value, not Invalid lots.

   int Price_Type;
   
   {
   if (SimpleOrder_Type == OP_BUY)
      Price_Type = Ask;
   else if (SimpleOrder_Type == OP_SELL)
      Price_Type = Bid;
   }

Price_Type should be a double.

Reason: