the code seems all fine, yet doesn't give me any result.

 
it starts and ends without any result. what did i miss?
int slippage;
double stopLoss;
double takeProfit;

bool isWeekday = (TimeDayOfWeek(TimeLocal()) >= 1 && TimeDayOfWeek(TimeLocal()) <= 5);
bool isTradingHour = (TimeHour(TimeLocal()) >= 0 && TimeHour(TimeLocal()) <= 23);



           

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+


extern int MaxPositions = 1; // Maximum number of positions allowed
extern double MinTimeBetweenTrades = 30; // Minimum time in minutes between trades

datetime lastTradeTime = 0;


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
   static bool tradeOpen = false; // flag to indicate whether a trade is open or not


   double ema1_last_5[5]; // array to store the last 5 EMA(1) values
   ArrayInitialize(ema1_last_5, 0.0);
   int start_index = IndicatorCounted(); // get the index of the earliest uncalculated bar
  
   double ema100_handle = iMA(_Symbol, PERIOD_CURRENT, 100, 0, MODE_EMA, PRICE_CLOSE,0); // handle for EMA(100)
   
   
   


  


// --------------------------------------------------------------------------



//----------------------------------------------------------------------------

   int totalOrders = OrdersTotal();
   int totalPositions = 0;
   for(int i = 0; i < totalOrders; i++)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
         if(OrderSymbol() == Symbol() && (OrderType() == OP_BUY || OrderType() == OP_SELL))
           {
            totalPositions++;
           }
        }
     }


   double initialLotSize = 0.01; 
   double lotSize = initialLotSize; 
   
   int ticket1 = 0;

   if(totalPositions < MaxPositions && (TimeCurrent() - lastTradeTime) >= MinTimeBetweenTrades * 60)
     {
      if(!isWeekday || !isTradingHour)
         return;



      // loop through the last 5 bars and store the EMA(1) values in the array
      bool tradeOpened = false;

      for(int i = 0; i < 5; i++)
        {
         ema1_last_5[i] = iMA(NULL, 0, 1, 0, MODE_EMA, PRICE_CLOSE, start_index + i);
        }

      // check if the EMA(1) values have not crossed below the EMA(100)
      bool ema1_below_ema100 = true;
      for(int i = 0; i < 5; i++)
        {
         double ema1 = ema1_last_5[i];
         double ema100 = iMA(NULL, 0, 100, 0, MODE_EMA, PRICE_CLOSE, start_index + i);
         if(ema1 >= ema100)
           {
            ema1_below_ema100 = false;
            break;
           }
        }

      // check if the RSI(14) is above 50 and the Stochastic(14,3,3) signal line crossed above the 20 level
      bool rsi_above_50 = iRSI(NULL, 0, 14, PRICE_CLOSE, 0) > 50;
      bool stoch_signal_crossed_above_20 = iStochastic(NULL, PERIOD_CURRENT, 14, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 0) < 20 &&
                                           iStochastic(NULL, PERIOD_CURRENT, 14, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 1) >= 20;

      if(ema1_below_ema100 && rsi_above_50 && stoch_signal_crossed_above_20)
        {
         // do something if all the conditions are met (e.g. enter a buy trade)
         stopLoss = ema100_handle;
         takeProfit = stopLoss + (stopLoss - Ask);

         ticket1 = OrderSend(Symbol(), OP_BUY, 5, Ask, slippage, stopLoss, takeProfit, "My buy order", 0, 0, clrGreen);
         if(ticket1 > 0)
           {
            Print("Trade opened successfully with ticket #", ticket1);
            Print("The opening price of the order is ", OrderOpenPrice());
            lastTradeTime = TimeCurrent();
            tradeOpened = true;
           }
        }

      
     









     }
  }
 
Ricca97:
it starts and ends without any result. what did i miss?

What do you mean by "it starts and ends" exactly ?

What results are you expecting ?

 
Alain Verleyen #:

What do you mean by "it starts and ends" exactly ?

What results are you expecting ?

when i backtest it, it starts and ends without any result. usualy when there is something wrong, either it freezes, or give me error message in the journal. but this time, it doesnt. and also i can see on the chart  the criteria are met, but the code doesn't identify it. 
 
  1. bool isWeekday = (TimeDayOfWeek(TimeLocal()) >= 1 && TimeDayOfWeek(TimeLocal()) <= 5);
    bool isTradingHour = (TimeHour(TimeLocal()) >= 0 && TimeHour(TimeLocal()) <= 23);

    Those are not assignments; they are initialization of a common (globally declared), or static variable(s) with a constant. They work exactly the same way in MT4/MT5/C/C++.

    1. They are initialized once on program load.

    2. They don't update unless you assign to them.

    3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use).

      MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and Don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), 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. A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

    4. Unlike indicators, EAs are not reloaded on chart change, so you must reinitialize them, if necessary.
                external static variable - MQL4 programming forum #2 (2013)

  2.    double ema100_handle = iMA(_Symbol, PERIOD_CURRENT, 100, 0, MODE_EMA, PRICE_CLOSE,0); // handle for EMA(100)
    MT4 does not return a handle. Stop using ChatGPT.

              Help needed to debug and fix an AI EA - Trading Systems - MQL5 programming forum (2023)

Reason: