EA invalid order error 10035

 
Having trouble finding out what’s cause this error in my code. Very green at this and after some help. It’s a very basic EA, just want it to actually work and will build from here. 

#property  version   "4"
#property  strict
#property  indicator_separate_window

#include <Trade\Trade.mqh> // import trade functions

// Expert Advisor input parameters
input double StopLoss = 100;           // Stop loss value in points
input double TakeProfit = 200;        // Take profit value in points
input double LotSize = 0.01;          // Fixed lot size
input int MagicNumber = 12345;        // Magic number for trades
input int MaxSpread = 50;              // Maximum allowed spread in points

// Indicator handles
int ma10_handle, ma20_handle, ma40_handle;

// Trade signal variables
bool buy_signal;

// Initialization function
int OnInit()
{
    // Get handles for all the indicators
    ma10_handle = iMA(Symbol(), PERIOD_H1, 10, 0, MODE_EMA, PRICE_CLOSE);
    ma20_handle = iMA(Symbol(), PERIOD_H1, 20, 0, MODE_EMA, PRICE_CLOSE);
    ma40_handle = iMA(Symbol(), PERIOD_H4, 40, 0, MODE_EMA, PRICE_CLOSE);

    return(INIT_SUCCEEDED);
}

// Execution function
void OnTick()
{
    double Ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
    double Bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
    double spread = Ask - Bid;

    // Check for buy signal
    double ma10_buffer[2], ma20_buffer[2], ma40_buffer[2];
    CopyBuffer(ma10_handle, 0, 0, 2, ma10_buffer);
    CopyBuffer(ma20_handle, 0, 0, 2, ma20_buffer);
    CopyBuffer(ma40_handle, 0, 0, 2, ma40_buffer);

    buy_signal = ma10_buffer[1] > ma20_buffer[1]
        && Ask > ma10_buffer[1]
        && Ask > ma40_buffer[1]
        && spread <= MaxSpread * _Point;

    // If buy signal is detected, open a buy trade
    if (buy_signal) {
        double sl = Ask - StopLoss * _Point;
        double tp = Ask + TakeProfit * _Point;
        CTrade trade;
        bool is_order_sent = trade.Buy(LotSize, Symbol, Ask, sl, tp, 0, DoubleToString(MagicNumber));
        
        if (!is_order_sent) {
            Print("OrderSend failed with error ", trade.ResultRetcode(), " - ", trade.ResultRetcodeDescription());
        }
    }
}

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Order Properties - Trade Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Roondogg010: Having trouble finding out what’s cause this error in my code. Very green at this and after some help.
  1. Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum #25 (2019)
              Forum rules and recommendations - General - MQL5 programming forum (2023)
              Messages Editor

  2. double sl = Ask - StopLoss * _Point;
    double tp = Ask + TakeProfit * _Point;

    You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)

      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes.
      My GBPJPY shows average spread = 26 points, average maximum spread = 134.
      My EURCHF shows average spread = 18 points, average maximum spread = 106.
      (your broker will be similar).
                Is it reasonable to have such a huge spreads (20 PIP spreads) in EURCHF? - General - MQL5 programming forum (2022)

  3. Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
              Code debugging - Developing programs - MetaEditor Help
              Error Handling and Logging in MQL5 - MQL5 Articles (2015)
              Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
              Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)

 
Roondogg010:
Having trouble finding out what’s cause this error in my code. Very green at this and after some help. It’s a very basic EA, just want it to actually work and will build from here. 

#property  version   "5.00"
#property  strict
#property  indicator_separate_window

#include <Trade\Trade.mqh> // import trade functions

// Expert Advisor input parameters
input double StopLoss = 100;           // Stop loss value in points
input double TakeProfit = 200;        // Take profit value in points
input double LotSize = 0.01;          // Fixed lot size
input int MagicNumber = 12345;        // Magic number for trades
input int MaxSpread = 50;              // Maximum allowed spread in points

// Indicator handles
int ma10_handle, ma20_handle, ma40_handle;

// Trade signal variables
bool buy_signal;

// Initialization function
void OnInit()
{
    // Get handles for all the indicators
    ma10_handle = iMA(Symbol(), PERIOD_H1, 10, 0, MODE_EMA, PRICE_CLOSE);
    ma20_handle = iMA(Symbol(), PERIOD_H1, 20, 0, MODE_EMA, PRICE_CLOSE);
    ma40_handle = iMA(Symbol(), PERIOD_H4, 40, 0, MODE_EMA, PRICE_CLOSE);
}

// Execution function
void OnTick()
{
    double Ask = SymbolInfoDouble(Symbol(), SYMBOL_ASK);
    double Bid = SymbolInfoDouble(Symbol(), SYMBOL_BID);
    double spread = Ask - Bid;

    // Check for buy signal
    double ma10_buffer[2], ma20_buffer[2], ma40_buffer[2];
    CopyBuffer(ma10_handle, 0, 0, 2, ma10_buffer);
    CopyBuffer(ma20_handle, 0, 0, 2, ma20_buffer);
    CopyBuffer(ma40_handle, 0, 0, 2, ma40_buffer);

    buy_signal = ma10_buffer[1] > ma20_buffer[1]
        && Ask > ma10_buffer[1]
        && Ask > ma40_buffer[1]
        && spread <= MaxSpread * _Point;

    // If buy signal is detected, open a buy trade
    if (buy_signal) {
        double sl = Ask - StopLoss * _Point;
        double tp = Ask + TakeProfit * _Point;
        CTrade trade;
        bool is_order_sent = trade.Buy(LotSize,_SymbolAsk, sl, tp, 0, DoubleToString(MagicNumber));
        
        if (!is_order_sent) {
            Print("OrderSend failed with error ", trade.ResultRetcode(), " - ", trade.ResultRetcodeDescription());
        }
    }
}


Your Symbol is Ask… 

 
Daniel Cioca #:

Your Symbol is Ask… 

Thank you, have rectified this.
 
William Roeder #:
  1. Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum #25 (2019)
              Forum rules and recommendations - General - MQL5 programming forum (2023)
              Messages Editor

  2. You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)

      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes.
      My GBPJPY shows average spread = 26 points, average maximum spread = 134.
      My EURCHF shows average spread = 18 points, average maximum spread = 106.
      (your broker will be similar).
                Is it reasonable to have such a huge spreads (20 PIP spreads) in EURCHF? - General - MQL5 programming forum (2022)

  3. Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
              Code debugging - Developing programs - MetaEditor Help
              Error Handling and Logging in MQL5 - MQL5 Articles (2015)
              Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
              Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)

Thanks for the information, i have compiled and debugged with print function around ordersend. the following is a snippet from my journal. i originally had issues around incorrect indicator values, but now i am stuck here. Testing on XAUUSD
Files:
 
Roondogg010 #:
Thanks for the information, i have compiled and debugged with print function around ordersend. the following is a snippet from my journal. i originally had issues around incorrect indicator values, but now i am stuck here. Testing on XAUUSD
Have tested your code with the modification I mentioned above and it works. It is opening trades, sometimes it gives Nor enough money, but it works… kind off … your LotSize might be wrong
Reason: