Error opening buy order 129

 

Hi, can any one help.  i keep getting error codes and can't find the solution.  compiles fine, just won't open the trades.  idea is to run the EA on a single chart and open all trades at a set time of day.


Thanks in advance.


extern string  TradeComment = "D1Long";

extern int     MagicNumber = 200;

//double  TakeProfit = 5000;

//double  StopLoss = 5000;

extern double  Lots = 0.1;

extern int     OpenTradeTime = 1100;      // Open Trade time

extern int     MinutesToWaitForTick = 2;

extern bool    OpenBuy = true;

bool    OpenSell = false;

int     NumBuys = 1;

int     NumSells = 1;

extern int     Slippage = 200;



string  mySymbol;



double ask;

double bid;



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

//| program start function                                             |

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

int init()

  {

   mySymbol="AUDJPY";  OpenBuy(); 

   mySymbol="AUDUSD";  OpenBuy();

   mySymbol="NZDJPY";  OpenBuy();

   mySymbol="CHFJPY";  OpenBuy();

   mySymbol="EURUSD";  OpenBuy();

   mySymbol="EURJPY";  OpenBuy();

   mySymbol="GBPJPY";  OpenBuy();

   mySymbol="GBPUSD";  OpenBuy();

   mySymbol="USDJPY";  OpenBuy();

   mySymbol="NZDUSD";  OpenBuy(); 

   }





int deinit()

  {

  }

  

void OpenBuy()

{

}  

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

//|                                                                  |

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

int start()

  {

   int cnt, ticket, total;

   int ct, EndTradeTime;

   

   

// initial data checks

// it is important to make sure that the expert works with a normal

// chart and the user did not make any mistakes setting external 

// variables (Lots, StopLoss, TakeProfit, 

// TrailingStop) in our case, we check TakeProfit

// on a chart of less than 100 bars

   if(Bars<100)

     {

      Print("bars less than 100");

      return(0);  

     }

//   if(TakeProfit<10)

//     {

//      Print("TakeProfit less than 10");

//      return(0);  // check TakeProfit

//     }



   ct = Hour() * 100 + Minute();

   EndTradeTime = OpenTradeTime + MinutesToWaitForTick;



   total=OrdersTotal();

   if(total<1) 

     {

      // no opened orders identified

      if(AccountFreeMargin()<(1000*Lots))

        {

         Print("We have no money. Free Margin = ", AccountFreeMargin());

         return(0);  

        }

      // check for long position (BUY) possibility

      if(ct >= OpenTradeTime && ct < EndTradeTime)

      {

         if (OpenBuy)

         {

           for ( cnt = 0; cnt < NumBuys; cnt++)

           

      //int  loopcount;

      //loopcount=0;

      //while(true)     

           {

       bid=MarketInfo(mySymbol,MODE_BID);

      ask=MarketInfo(mySymbol,MODE_ASK);     

                   

           

             ticket=OrderSend(mySymbol,OP_BUY,Lots,ask,Slippage,0,0,TradeComment,MagicNumber,Green);

             if(ticket>0)

             {

              if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());

             }

             else Print("Error opening BUY order : ",GetLastError()); 

          RefreshRates();

          Sleep(500);

                   }

          }

      // check for short position (SELL) possibility

          if(OpenSell)

          {

           for ( cnt = 0; cnt < NumSells; cnt++)

           {

             ticket=OrderSend(mySymbol,OP_SELL,Lots,bid,Slippage,0,0,TradeComment,MagicNumber,0,Red);

             if(ticket>0)

             {

              if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());

             }

             else Print("Error opening SELL order : ",GetLastError());

           } 

          }

       }

     }

   return(0);

  }

// the end.
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Orders in DOM
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Orders in DOM
  • www.mql5.com
For equity securities, the Depth of Market window is available, where you can see the current Buy and Sell orders. Desired direction of a trade operation, required amount and requested price are specified for each order. To obtain information...
 
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2. int init(){
       mySymbol="AUDJPY";  OpenBuy(); 
       mySymbol="AUDUSD";  OpenBuy();
    ⋮
    Don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:
    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

    Broker's use a variety of naming patterns: EURUSD, EURUSDc, EURUSDct, EURUSDecn, EURUSDi, EURUSDm, EURUSDme, EURUSDpro, EURUSDt, "EUR.USD", "EUR/USD", "EURUSD!", "EURUSD#", "EURUSD.", "EURUSD..", "EURUSD.c", "EURUSD.cfx", "EURUSD.G", "EURUSD.i", "EURUSD.r", "EURUSD.SBe", "EURUSD.stp", "EURUSD+", "EURUSD-5", "EURUSD-m", "EURUSD-sb", etc., Financial symbols with '-', '=', '_' and '+'

    If the pattern your broker uses doesn't match the pattern of your signal provider's broker, you can't copy the signal/open a trade.

    If the naming pattern of your charts isn't exactly "BasQuo" then hard coded symbols fails.

    Don't hard code things; just use the predefined _Symbol, or remove the adornments during processing.
              I need to know how to fill an enum or get a drop down list, with a string array :)) - MQL4 programming forum #10 2020.06.12

  3. int start(){

    You should stop using the old event handlers and IndicatorCounted() and start using new event handlers.
              Event Handling Functions - MQL4 Reference
              How to do your lookbacks correctly - MQL4 programming forum #9-14 & #19 2016.05.11

  4. ask=MarketInfo(mySymbol,MODE_ASK);     
    ticket=OrderSend(mySymbol,OP_BUY,Lots,ask,Slippage,0,0,TradeComment,MagicNumber,Green);

    On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26 № 4 2019.05.20

  5.    total=OrdersTotal();
    
       if(total<1) 
    You understand that is all orders on any symbol?
 

Try placing RefreshRates above the bid + ask price requests .

Also place RefreshRates and bid price request in the sell for loop.

bid request not needed in buys loop.

ask request not needed in sells loop.

 
Lorentzos Roussos: Try placing RefreshRates above the bid + ask price requests .

Not using Bid/Ask predefined symbols. RefreshRates is irrevalent.

 
William Roeder:

Not using Bid/Ask predefined symbols. RefreshRates is irrevalent.

did not know that thanks . 

Should he/she use SymbolInfoDouble ?

 

Tip 1 done - thank you


I am old, but very new to this, to much time spent copy/paste code together.  thanks for the help, will give it a crack and see how we go.

 
William Roeder:
  1. Don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:
    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

    Broker's use a variety of naming patterns: EURUSD, EURUSDc, EURUSDct, EURUSDecn, EURUSDi, EURUSDm, EURUSDme, EURUSDpro, EURUSDt, "EUR.USD", "EUR/USD", "EURUSD!", "EURUSD#", "EURUSD.", "EURUSD..", "EURUSD.c", "EURUSD.cfx", "EURUSD.G", "EURUSD.i", "EURUSD.r", "EURUSD.SBe", "EURUSD.stp", "EURUSD+", "EURUSD-5", "EURUSD-m", "EURUSD-sb", etc., Financial symbols with '-', '=', '_' and '+'

    If the pattern your broker uses doesn't match the pattern of your signal provider's broker, you can't copy the signal/open a trade.

    If the naming pattern of your charts isn't exactly "BasQuo" then hard coded symbols fails.

    Don't hard code things; just use the predefined _Symbol, or remove the adornments during processing.
              I need to know how to fill an enum or get a drop down list, with a string array :)) - MQL4 programming forum #10 2020.06.1

I think i understand this, and most likly my issue.

This part of the code came from a Script i use, which is on the go as soon as you load it, where as the newer code is waiting for a time setting before doing anything, by which time the first part has been closed out.  so it has nothing to relate to.


will try moving this and see how i go.


thank you.

 
Lorentzos Roussos: Should he/she use SymbolInfoDouble ?

MT4: MarketInfo or SymbolInfoDouble is the same.

 
On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.

          Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26 № 4 2019.05.20

Reason: