Ea not opening trades

 

Hello guys just coded an EA but its not opening any trades in backtesting, so wondering what I did wrong here is the code below:


#property strict

// Input parameters
input double TakeProfitPercent = 2.0; // Take profit as a percentage of account balance
input double StopLossPercent = 1.0;   // Stop loss as a percentage of account balance
input int    RSI_Period = 14;         // Period for RSI
input int    MFI_Period = 14;         // Period for MFI

// Global variables
bool isTradeOpen = false;

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

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
    // Daily candle color
    bool dailyGreen = Close[1] > Open[1];
    bool dailyRed = Close[1] < Open[1];

    // RSI and MFI
    double rsi = iRSI(NULL, PERIOD_D1, RSI_Period, PRICE_CLOSE, 0);
    double mfi = iMFI(NULL, PERIOD_D1, MFI_Period, 0);

    // EMA values
    double ema7 = iMA(NULL, PERIOD_D1, 7, 0, MODE_EMA, PRICE_CLOSE, 0);
    double ema20 = iMA(NULL, PERIOD_D1, 20, 0, MODE_EMA, PRICE_CLOSE, 0);

    // Check for Buy conditions
    if (!isTradeOpen && dailyGreen && rsi > 50 && mfi > 50 && ema7 > ema20 && Low[0] > ema7 && High[0] < ema20)
    {
        ExecuteTrade(OP_BUY);
    }
    // Check for Sell conditions
    else if (!isTradeOpen && dailyRed && rsi < 50 && mfi < 50 && ema7 < ema20 && High[0] < ema7 && Low[0] > ema20)
    {
        ExecuteTrade(OP_SELL);
    }
  }

//+------------------------------------------------------------------+
//| Execute a trade                                                  |
//+------------------------------------------------------------------+
void ExecuteTrade(int type)
  {
    double accountBalance = AccountBalance();
    double stopLossPips = (StopLossPercent / 100.0) * accountBalance / (MarketInfo(Symbol(), MODE_TICKVALUE));
    double takeProfitPips = (TakeProfitPercent / 100.0) * accountBalance / (MarketInfo(Symbol(), MODE_TICKVALUE));

    double entryPrice = (type == OP_BUY) ? Ask : Bid;
    double stopLoss = (type == OP_BUY) ? (entryPrice - stopLossPips * Point) : (entryPrice + stopLossPips * Point);
    double takeProfit = (type == OP_BUY) ? (entryPrice + takeProfitPips * Point) : (entryPrice - takeProfitPips * Point);

    int ticket = OrderSend(Symbol(), type, 0.1, entryPrice, 3, stopLoss, takeProfit, "Trend Trade", 0, 0, (type == OP_BUY) ? clrBlue : clrRed);
    if(ticket > -1)
        isTradeOpen = true;
  }

//+------------------------------------------------------------------+
 
Your topic has been moved to the section: MQL4 and MetaTrader 4
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
  1. David Smith: t its not opening any trades in backtesting, so wondering what I did wrong here is the code below:

    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)

  2.     double stopLoss = (type == OP_BUY) ? (entryPrice - stopLossPips * Point) : (entryPrice + stopLossPips * Point);
        double takeProfit = (type == OP_BUY) ? (entryPrice + takeProfitPips * Point) : (entryPrice - takeProfitPips * 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. Once you open an order, when will you ever open another?
        if(ticket > -1)
            isTradeOpen = true;
    Simplified.
        isTradeOpen = ticket > -1;

 
William Roeder #:
  1. 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)

  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. Once you open an order, when will you ever open another?
    Simplified.

@William roader Thank you for the above response with the information attached, much appreciated

 
This is not taking trades because you have too many conditions out there to be checked. All of them being true is a once in a decade event.
 
Yashar Seyyedin #:
This is not taking trades because you have too many conditions out there to be checked. All of them being true is a once in a decade event.

Much appreciated @Yashar Seyyedin