EA not sending orders

 

Hi i have made an ea and it has no errors but fails to send orders, could someone help me find out why

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

//|                                              SMR HFT.mq5         |

//|                        Copyright 2024, Your Company Name         |

//|                                       https://www.yourwebsite.com|

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

#property strict



// Include the trade functions

#include <Trade\Trade.mqh> 

#include <Trade\PositionInfo.mqh>

#include <Trade\OrderInfo.mqh>

#include <Trade\DealInfo.mqh>

#include <Trade\HistoryOrderInfo.mqh>

#include <Indicators\Trend\RSI.mqh>



// Input parameters

input string EA_Name = "SMR HFT";

input double Lot_Size = 5.0;

input double Lot_Multiply = 1.3;

input bool Stop_Trading_At_Profit_Target = true;

input double Daily_Profit_Target = 10000.0;

input double Close_Money = 1000.0;

input double Nearby = 1000.0;

input int Pips_To_Raise = 200;

input int Time_To_Wait = 5;

input int Max_Order = 5;

input bool Cut_Loss = true;

input double Cut_Loss_Percentage = 3.0;

input double Stop_Loss = 2000.0; // In Points

input double Trail_And_Stop = 2000.0; // In Points

input double Trail_And_Step = 1.0;

input double Take_Profit = 6000.0; // In Points

input int RSI_Period = 14; // RSI Period

input int RSI_Buy = 70; // RSI value to trigger buy trade

input int RSI_Sell = 30; // RSI value to trigger sell trade

input double Money_Secure = 1000.0;

input double Money_Step = 1.0;

input double Start_When_Profit_Above = 200.0;

input int Magic_Number = 6403;



// Define RSI variables

double rsi_buffer[];



// Define the Close array to store historical prices

double Close[];



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

//| Expert initialization function                                   |

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

int OnInit()

{

    // Initialization code

    Print("EA Initialized: ", EA_Name);

    

    ArrayResize(rsi_buffer, 0);



    // Populate the Close array with historical prices

    int totalBars = Bars(Symbol(), Period());

    ArrayResize(Close, totalBars);

    CopyClose(Symbol(), Period(), 0, totalBars, Close);



    return(INIT_SUCCEEDED);

}



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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

{

    // Deinitialization code

    Print("EA Deinitialized: ", EA_Name);

}



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

//| Expert tick function                                             |

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

void OnTick()

{

    // Calculate RSI value

    double rsi_value = CalculateRSI(RSI_Period);



    // Check RSI conditions and open trades accordingly

    if (rsi_value < RSI_Sell)

    {

        // Open Buy trade

        double stop_loss = NormalizeDouble(SymbolInfoDouble(Symbol(), SYMBOL_ASK) - Stop_Loss * _Point, _Digits);

        double take_profit = NormalizeDouble(SymbolInfoDouble(Symbol(), SYMBOL_ASK) + Take_Profit * _Point, _Digits);



        MqlTradeRequest request = {10};

        request.action = TRADE_ACTION_DEAL;  // Specify the trade action

        request.type = ORDER_TYPE_BUY;

        request.volume = Lot_Size;

        request.price = SymbolInfoDouble(Symbol(), SYMBOL_ASK);

        request.sl = stop_loss;

        request.tp = take_profit;

        request.type_filling = ORDER_FILLING_FOK;



        MqlTradeResult result = {0};

        if (OrderSend(request, result)) // Check the return value

        {

            if (result.retcode != TRADE_RETCODE_DONE)

            {

                Print("Error opening Buy trade: ", result.retcode);

            }

        }

        else

        {

            Print("Error opening Buy trade: OrderSend failed");

        }

    }

    else if (rsi_value > RSI_Buy)

    {

        // Open Sell trade

        double stop_loss = NormalizeDouble(SymbolInfoDouble(Symbol(), SYMBOL_BID) + Stop_Loss * _Point, _Digits);

        double take_profit = NormalizeDouble(SymbolInfoDouble(Symbol(), SYMBOL_BID) - Take_Profit * _Point, _Digits);



        MqlTradeRequest request = {1};

        request.action = TRADE_ACTION_DEAL; // Specify the trade action

        request.type = ORDER_TYPE_SELL;

        request.volume = Lot_Size;

        request.price = SymbolInfoDouble(Symbol(), SYMBOL_BID);

        request.sl = stop_loss;

        request.tp = take_profit;

        request.type_filling = ORDER_FILLING_FOK;



        MqlTradeResult result = {0};

        if (OrderSend(request, result)) // Check the return value

        {

            if (result.retcode != TRADE_RETCODE_DONE)

            {

                Print("Error opening Sell trade: ", result.retcode);

            }

        }

        else

        {

            Print("Error opening Sell trade: OrderSend failed");

        }

    }

}



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

//| Calculate RSI function                                           |

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

double CalculateRSI(int period)

{

    double gain = 0, loss = 0;



    // Calculate price changes

    for (int i = period; i >= 0; i--)

    {

        double change = Close[i] - Close[i + 1];

        if (change > 0)

            gain += change;

        else

            loss += MathAbs(change);

    }



    // Calculate RS and RSI

    double rs = gain / loss;

    double rsi = 100 - 100 / (1 + rs);



    return rsi;

}


 
stevenr15:

Hi i have made an ea and it has no errors but fails to send orders, could someone help me find out why

It's better you learn it yourself!

Learn to use the debugger!

Code debugging:  https://www.metatrader5.com/en/metaeditor/help/development/debug
Error Handling and Logging in MQL5:  https://www.mql5.com/en/articles/2041
Tracing, Debugging and Structural Analysis of Source Code, scroll down to: "Launching and Debuggin": https://www.mql5.com/en/articles/272

Also, as a newbie, you shouldn't start with a blank page!

    Before you learn to program in MQL5, learn to search, because there is practically nothing that has not already been programmed for MT4/MT5!
    => Search in the articles: https://www.mql5.com/en/articles
    => Search in the codebase: https://www.mql5.com/en/code
    => Search in general: https://www.mql5.com/en/search or via Google with: "site:mql5.com .." (forgives typos and variants)
    Hint: If you place the cursor on a MQL function and press F1, you will see the reference directly, many with examples to copy and paste - the fastest form to code!

Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator
https://www.mql5.com/en/articles/35

and here are articles with about indicators, their interpretation and usage with code  to copy & paste:
https://www.mql5.com/en/articles/13406
https://www.mql5.com/en/articles/13244
https://www.mql5.com/en/articles/13277

Code debugging - Developing programs - MetaEditor Help
  • www.metatrader5.com
MetaEditor has a built-in debugger allowing you to check a program execution step by step (by individual functions). Place breakpoints in the code...
 

ive done that and it doesnt give me anything to go off, thats why i came here to ask experienced individuals for help. Id say its definitely better to learn from people who know how to do it

Carl Schreiber #:

It's better you learn it yourself!

Learn to use the debugger!

Code debugging:  https://www.metatrader5.com/en/metaeditor/help/development/debug
Error Handling and Logging in MQL5:  https://www.mql5.com/en/articles/2041
Tracing, Debugging and Structural Analysis of Source Code, scroll down to: "Launching and Debuggin": https://www.mql5.com/en/articles/272

Also, as a newbie, you shouldn't start with a blank page!

    Before you learn to program in MQL5, learn to search, because there is practically nothing that has not already been programmed for MT4/MT5!
    => Search in the articles: https://www.mql5.com/en/articles
    => Search in the codebase: https://www.mql5.com/en/code
    => Search in general: https://www.mql5.com/en/search or via Google with: "site:mql5.com .." (forgives typos and variants)
    Hint: If you place the cursor on a MQL function and press F1, you will see the reference directly, many with examples to copy and paste - the fastest form to code!

Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator
https://www.mql5.com/en/articles/35

and here are articles with about indicators, their interpretation and usage with code  to copy & paste:
https://www.mql5.com/en/articles/13406
https://www.mql5.com/en/articles/13244
https://www.mql5.com/en/articles/13277

 
stevenr15 #:

ive done that and it doesnt give me anything to go off, thats why i came here to ask experienced individuals for help. Id say its definitely better to learn from people who know how to do it

But this is not a forum where you find teachers that will coach you. So either learn by yourself or pay.
Trading applications for MetaTrader 5 to order
Trading applications for MetaTrader 5 to order
  • 2024.02.21
  • www.mql5.com
The largest freelance service with MQL5 application developers
 
Carl Schreiber #:
But this is not a forum where you find teachers that will coach you. So either learn by your self or pay.
not looking for coaching asking for advice, plenty people do the same thing I don't see why you have a problem with someone asking for help on a community forum
 
Carl Schreiber #:
But this is not a forum where you find teachers that will coach you. So either learn by yourself or pay.
you have posts doing the same thing aswell
 
stevenr15: Hi i have made an ea and it has no errors but fails to send orders, could someone help me find out why

  1. Just because it compiles do not mean "it has no errors".
  2.        double stop_loss = NormalizeDouble(SymbolInfoDouble(Symbol(), SYMBOL_BID) + Stop_Loss * _Point, _Digits);
           double take_profit = NormalizeDouble(SymbolInfoDouble(Symbol(), SYMBOL_BID) - Take_Profit * _Point, _Digits);
    

    You used NormalizeDouble, It's use is usually wrong, as it is in your case.

    1. Floating point has an infinite number of decimals, it's you were not understanding floating point and that some numbers can't be represented exactly. (like 1/10.)
                Double-precision floating-point format - Wikipedia, the free encyclopedia

      See also The == operand. - MQL4 programming forum (2013)

    2. Print out your values to the precision you want with DoubleToString - Conversion Functions - MQL4 Reference.

    3. SL/TP (stops) need to be normalized to tick size (not Point) — code fails on non-currencies.
                On 5Digit Broker Stops are only allowed to be placed on full pip values. How to find out in mql? - MQL4 programming forum #10 (2011)

      And abide by the limits Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial and that requires understanding floating point equality Can price != price ? - MQL4 programming forum (2012)

    4. Open price for pending orders need to be adjusted. On Currencies, Point == TickSize, so you will get the same answer, but it won't work on non-currencies. So do it right.
                Trailing Bar Entry EA - MQL4 programming forum (2013)
                Bid/Ask: (No Need) to use NormalizeDouble in OrderSend - MQL4 programming forum (2012)

    5. Lot size must also be adjusted to a multiple of LotStep and check against min and max. If that is not a power of 1/10 then NormalizeDouble is wrong. Do it right.
                (MT4 2013)) (MT5 2022))

    6. MathRound() and NormalizeDouble() are rounding in a different way. Make it explicit.
                MT4:NormalizeDouble - MQL5 programming forum (2017)
                How to Normalize - Expert Advisors and Automated Trading - MQL5 programming forum (2017)

    7. Prices you get from the terminal are already correct (normalized).

    8. PIP, Point, or Tick are all different in general.
                What is a TICK? - MQL4 programming forum (2014)

  3.        double stop_loss = NormalizeDouble(SymbolInfoDouble(Symbol(), SYMBOL_ASK) - Stop_Loss * _Point, _Digits);
    
            double take_profit = NormalizeDouble(SymbolInfoDouble(Symbol(), SYMBOL_ASK) + Take_Profit * _Point, _Digits);
    
    
    
            MqlTradeRequest request = {10};
    
            request.action = TRADE_ACTION_DEAL;  // Specify the trade action
    
            request.type = ORDER_TYPE_BUY;

    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)

 

thanks, it worked now just need to adjust some settings so it works better

William Roeder #:
  1. Just because it compiles do not mean "it has no errors".
  2. You used NormalizeDouble, It's use is usually wrong, as it is in your case.

    1. Floating point has an infinite number of decimals, it's you were not understanding floating point and that some numbers can't be represented exactly. (like 1/10.)
                Double-precision floating-point format - Wikipedia, the free encyclopedia

      See also The == operand. - MQL4 programming forum (2013)

    2. Print out your values to the precision you want with DoubleToString - Conversion Functions - MQL4 Reference.

    3. SL/TP (stops) need to be normalized to tick size (not Point) — code fails on non-currencies.
                On 5Digit Broker Stops are only allowed to be placed on full pip values. How to find out in mql? - MQL4 programming forum #10 (2011)

      And abide by the limits Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial and that requires understanding floating point equality Can price != price ? - MQL4 programming forum (2012)

    4. Open price for pending orders need to be adjusted. On Currencies, Point == TickSize, so you will get the same answer, but it won't work on non-currencies. So do it right.
                Trailing Bar Entry EA - MQL4 programming forum (2013)
                Bid/Ask: (No Need) to use NormalizeDouble in OrderSend - MQL4 programming forum (2012)

    5. Lot size must also be adjusted to a multiple of LotStep and check against min and max. If that is not a power of 1/10 then NormalizeDouble is wrong. Do it right.
                (MT4 2013)) (MT5 2022))

    6. MathRound() and NormalizeDouble() are rounding in a different way. Make it explicit.
                MT4:NormalizeDouble - MQL5 programming forum (2017)
                How to Normalize - Expert Advisors and Automated Trading - MQL5 programming forum (2017)

    7. Prices you get from the terminal are already correct (normalized).

    8. PIP, Point, or Tick are all different in general.
                What is a TICK? - MQL4 programming forum (2014)

  3. 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)