Download MetaTrader 5
To add comments, please log in or register
Publish programs in Code Base. Improve your rating!
Matthias
42
Matthias 2015.08.26 23:52 

Hi Guys,

 to practice my programming skills im writing a little EA at the moment which i want to place two pending orders at a specific time. Everything pretty much works but on the very first execution i always get an OrderSend Error 130. i am pretty sure that my stops are right though.

 After that 1 OrderSend Error the next orders get placed without any error and i dont understand why. If you have a minute please be so kind and have a look on my code, you might see something that i dont see at the moment which could be causing the error. 

 

Thanks in Advance!

 

#property strict

extern int OrderOpenHour = 11;      
extern int OrderOpenMinute = 00;
extern int OrderCloseHour = 23;     
extern int OrderCloseMinute = 59;   
extern int Slippage = 3;
extern int MagicNumber = 12345;
extern double MaxRisk = 0.03; 
extern int MaxOrders = 2; 

double PipValue = 10.0;
int MarketMinStop = MarketInfo(Symbol(), MODE_STOPLEVEL);

int BuyStopOrder, SellStopOrder;

double MarketDivisor[] = {0.0, 0.1, 0.01, 0.001, 0.0001, 0.00001};
int MarketPrecision = MarketInfo(Symbol(), MODE_DIGITS);

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
   //---   
        
        
        
   //---
   
   return(INIT_SUCCEEDED);
}

int start()
{
   //---
      
      double MyPoint = Point;
      
      if(Digits==3 || Digits==5){ MyPoint=Point*10; }
   
      if(Hour() == OrderOpenHour && Minute() == OrderOpenMinute)
      {
        
         // Draw vline at current time and two hours back to make visualization of trade easier
         
         DrawVline(Time[2]);
         DrawVline(Time[0]);     
         
         // Get Entry prices
   
         double LongEntryPrice = High[1];
         double ShortEntryPrice = Low[1];
         
         // Check OrdersTotal()
         
         if(OrdersTotal() < MaxOrders)
         {
            // Risk Calculation
         
            double PositionSize = CalculatePositionSize(LongEntryPrice, ShortEntryPrice);
         
            // Place Pending Orders
         
            double EntryPriceBuy = (LongEntryPrice+(MarketMinStop*Point));
            double StopLossBuy = (ShortEntryPrice-(MarketMinStop*Point));     
                        
            BuyStopOrder = OrderSend(Symbol(), OP_BUYSTOP, PositionSize, EntryPriceBuy, Slippage, StopLossBuy, 0, "Long Pending Order", clrGreen);
         
            double EntryPriceSell = (ShortEntryPrice-(MarketMinStop*Point));
            double StopLossSell = (LongEntryPrice+(MarketMinStop*Point));
            
            SellStopOrder = OrderSend(Symbol(), OP_SELLSTOP, PositionSize, EntryPriceSell, Slippage, StopLossSell, 0, "Short Pending Order", clrRed);          
            
                  // Check if OrderType (OP_BUYSTOP -> OP_BUY) changed.
                  
                  if(BuyStopOrder > 0)
                  {
                     if(OrderSelect(BuyStopOrder, SELECT_BY_TICKET))
                     {
                        if(OrderType() == OP_BUY)
                        {
                           SetTakeProfit(OrderTicket(), OrderOpenPrice(), OrderStopLoss());
                           
                           // Delete Pending Sell Stop Order
                           Print("Ordertype OP_BUYSTOP changed to OP_BUY. - Deleting pending SELLSTOP order.");
                           DeleteOrder(SellStopOrder);                    
                        }
                     }
                     else
                     {
                        Print("Failed to select BuyStopOrder #", BuyStopOrder," | ", GetLastError());
                     }
                  }
            
                  // Check if OrderType (OP_SELLSTOP -> OP_SELL) changed.
                  
                  if(SellStopOrder > 0)
                  {
                     if(OrderSelect(SellStopOrder, SELECT_BY_TICKET))
                     {
                        if(OrderType() == OP_SELL)
                        {
                           SetTakeProfit(OrderTicket(), OrderOpenPrice(), OrderStopLoss());
                           
                           // Delete Pending Buy Stop Order
                           Print("Ordertype OP_SELLSTOP changed to OP_SELL. - Deleting pending BUYSTOP order.");
                           DeleteOrder(BuyStopOrder);                      
                        }
                     }
                     else
                     {
                        Print("Failed to select SellStopOrder #", SellStopOrder," | ", GetLastError());
                     }
                  }   
        }        
      }        
      
      
      // Close any open positions @ 23:59 Brokertime to avoid swap costs
      
      if(Hour() == OrderCloseHour && Minute() == OrderCloseMinute)
      {
         CloseDeleteAllOrders();
      }
   
   //---
   
   ResetTicket(BuyStopOrder);
   ResetTicket(SellStopOrder);
   
   return(0);
}

void DrawVline(datetime DrawTime)
{
   //---
     
     string LineName = "Vertical Line " + DoubleToStr(DrawTime, 0);
     
     if(!ObjectCreate(LineName, OBJ_VLINE, 0, DrawTime, 0))
     {
         Print("Failed to create Vertical Line. Error Code: ", GetLastError());
     }
     
     ObjectSet(LineName, OBJPROP_COLOR, Gray);
     
   //---
}

string PriceToStr(double p){ return( DoubleToStr(p, Digits) ); }

double CalculatePositionSize(double LongEntryPrice, double ShortEntryPrice)
{
   //---
           
      double StopLossPrice = LongEntryPrice - ShortEntryPrice;
      double StopLossPoints = MathAbs(StopLossPrice / MarketDivisor[MarketPrecision]);
      
      double Lots = NormalizeDouble((AccountFreeMargin() * MaxRisk) / (StopLossPoints * PipValue), 1);
   
   //---
   
   return(Lots);
   
   //--- 
}

void SetTakeProfit(int ticket, double LongEntryPrice, double ShortEntryPrice)
{
   //--
   
      if(OrderSelect(ticket, SELECT_BY_TICKET))
      {
         if(OrderCloseTime() == 0 && OrderTakeProfit() == 0)
         {
            double TakeProfitPrice = LongEntryPrice - ShortEntryPrice;
            
            double TakeProfit = 0;
               
            int type = OrderType();
            
            switch(type)
            {  
               case OP_BUY: TakeProfit = OrderOpenPrice() + TakeProfitPrice; break;
               case OP_SELL: TakeProfit = OrderOpenPrice() - TakeProfitPrice; break;
            }
            
            if(!OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), TakeProfit, 0, Orange))
            {
               Print("Failed to set TP for Ordertype: ", OrderType(), "to ", TakeProfit, " Error: ", GetLastError());
            } 
         } 
      }
   
   //--
}

void DeleteOrder(int ticket)
{
   //--
      
      if(OrderDelete(ticket, clrNONE))
      {
         ticket = 0; 
      }
      else
      {
         Print("Failed to delete Ticket for Ordertype: ", OrderType(), ". Error: ", GetLastError());
      }
   
   //---  
}

void ResetTicket(int ticket)
{
   //--
      
      if(OrderSelect(ticket, SELECT_BY_TICKET))
      {
         if(OrderCloseTime() > 0)
         {
            ticket = 0;
         }
      }

   //--
}

void CloseDeleteAllOrders()
{
   //--

      int total = OrdersTotal();
      
      for(int i = total - 1; i >= 0 ; i--)
      {
         if(OrderSelect(i, SELECT_BY_POS))
         {
            int type   = OrderType();
            bool result = false;
          
            switch(type)
            {
               case OP_BUY: result = OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, clrNONE); break;
               case OP_SELL: result = OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, clrNONE); break;
               case OP_BUYSTOP: result = OrderDelete(OrderTicket()); break;
               case OP_SELLSTOP: result = OrderDelete(OrderTicket()); break;
            }
         
            if(!result)
            {
               Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
            } 
         }  
      }

   //--
}
Matthias
42
Matthias 2015.08.28 14:14  
Nobody has an idea? 
Zirkon
123
Zirkon 2015.08.28 17:17  
Ratio_O:
Nobody has an idea? 

You have to debug your code, print the values and check them.

Error 130 can also be when the open price doesn't match the Stoplevels.

See https://book.mql4.com/appendix/limits

Matthias
42
Matthias 2015.08.28 19:06  
2015.08.28 20:59:27.661 2010.01.07 11:00  London_Breakout EURUSD,H1: open #1 buy stop 1.40 EURUSD at 1.44039 sl: 1.43516 ok 
2015.08.28 20:59:27.661 2010.01.07 11:00  London_Breakout EURUSD,H1: /* Buy Stop Order Debug Information */
2015.08.28 20:59:27.661 2010.01.07 11:00  London_Breakout EURUSD,H1: OrderTicket: 1
2015.08.28 20:59:27.661 2010.01.07 11:00  London_Breakout EURUSD,H1: PositionSize: 1.4
2015.08.28 20:59:27.661 2010.01.07 11:00  London_Breakout EURUSD,H1: EntryPriceBuy: 1.44039
2015.08.28 20:59:27.661 2010.01.07 11:00  London_Breakout EURUSD,H1: StopLossBuy: 1.43516
2015.08.28 20:59:27.661 2010.01.07 11:00  London_Breakout EURUSD,H1: /* End Buy Stop Debug Information */

2015.08.28 20:59:27.661 2010.01.07 11:00  London_Breakout EURUSD,H1: OrderSend error 130
2015.08.28 20:59:27.661 2010.01.07 11:00  London_Breakout EURUSD,H1: /* Sell Stop Order Debug Information */
2015.08.28 20:59:27.661 2010.01.07 11:00  London_Breakout EURUSD,H1: OrderTicket: -1
2015.08.28 20:59:27.661 2010.01.07 11:00  London_Breakout EURUSD,H1: PositionSize: 1.4
2015.08.28 20:59:27.661 2010.01.07 11:00  London_Breakout EURUSD,H1: EntryPriceSell: 1.43516
2015.08.28 20:59:27.661 2010.01.07 11:00  London_Breakout EURUSD,H1: StopLossSell: 1.44039
2015.08.28 20:59:27.661 2010.01.07 11:00  London_Breakout EURUSD,H1: /* End Sell Stop Debug Information */
Matthias
42
Matthias 2015.08.28 19:09  

The EntryPriceSell and StopLossSell Values where the Error occures seem just fine to me. Maybe this happens because the Marketprice is too close to the EntryPriceSell at the moment of orderexecution.. Its the only reason i could think of but i dont know how to check that

Debug Information

whroeder1
13605
whroeder1 2015.08.29 01:02  

Pre build 600 I had problems with pending orders; seemed that SL was being compared to current market, not pending price.

There is no need for pending orders. Humans can't look at the chart 24/7, EAs can. Wait for market to reach your entry price and open. done.

Zirkon
123
Zirkon 2015.08.29 08:47  
Ratio_O:

The EntryPriceSell and StopLossSell Values where the Error occures seem just fine to me. Maybe this happens because the Marketprice is too close to the EntryPriceSell at the moment of orderexecution.. Its the only reason i could think of but i dont know how to check that


Most probable, add the Bid/Ask prices to your output.
/
To add comments, please log in or register