questo è il mio codice mq4 per un EA
//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2018, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property strict

enum TimeFrameMode
  {
   USE_H1,
   USE_H4,
   USE_BOTH
  };

// Input variables
input int MagicNumber = 12345;
input double Lots = 0.1;
input int StopLoss = 50; // Stop loss in pips
input int TakeProfit = 50; // Take profit in pips
input int sma1 = 200;
input int sma2 = 30;
input int wpr = 50;
input int TRAILING_STEP = 10; // Trailing step in pips

input TimeFrameMode Mode = USE_BOTH; // Modalità di utilizzo dei livelli


// Global Variables
double D1_low_previous, D1_high_previous;
double D1_low_today, D1_high_today;
double H4_lower_open, H4_higher_open;
double H1_lower_open, H1_higher_open;
double H4_prev_lower_open, H4_prev_higher_open;
double H1_prev_lower_open, H1_prev_higher_open;

//±-----------------------------------------------------------------+
//| Expert initialization function                                   |
//±-----------------------------------------------------------------+
int OnInit()
  {
   RefreshLevels(); // Initialize price levels
   DrawLevelLines(); // Draw the initial lines
   return(INIT_SUCCEEDED);
  }

//±-----------------------------------------------------------------+
//| Expert tick function                                             |
//±-----------------------------------------------------------------+
void OnTick()
  {
   RefreshLevels(); // Update price levels every tick
   DrawLevelLines(); // Refresh the lines

   double current_bid_price = Bid;
   double current_ask_price = Ask;

// Calcola la SMA a 30 periodi
   double SMA1 = iMA(NULL, 0, sma1, 0, MODE_SMA, PRICE_CLOSE, 0);
   double SMA2 = iMA(NULL, 0, sma2, 0, MODE_SMA, PRICE_CLOSE, 0);
   double WPR = iWPR(NULL,0,wpr,0);
   double SAR = iSAR(NULL,0,0.02,0.2,0);

// Check for Buy conditions
   bool buyCondition = false;
   if(Mode == USE_H1 || Mode == USE_BOTH)
     {
      if(Open[0] > D1_low_today 
      && Open[0] < H1_lower_open 
      && current_ask_price > H1_lower_open 
      && current_ask_price > SMA1 
    //  && SMA1 > SMA2 
      && D1_low_today > D1_low_previous
//      && WPR < -80
      && SAR < Open[0]
      && D1_high_today - D1_low_today >= 250
      )
         buyCondition = true;
     }
   if(Mode == USE_H4 || Mode == USE_BOTH)
     {
      if(Open[0] > D1_low_today 
      && Open[0] < H4_lower_open 
      && current_ask_price > H4_lower_open  
      && current_ask_price > SMA1 
 //     && SMA1 > SMA2 
      && D1_low_today > D1_low_previous
 //     && WPR < -80
      && SAR < Open[0]
      && D1_high_today - D1_low_today >= 250
      )
         buyCondition = true;
     }

   if(buyCondition && !CheckIfOrderExists(OP_BUY))
     {
      OpenBuyOrder();
     }

// Check for Sell conditions
   bool sellCondition = false;
   if(Mode == USE_H1 || Mode == USE_BOTH)
     {
      if(Open[0] < D1_high_today 
      && Open[0] > H1_higher_open 
      && current_bid_price < H1_higher_open 
      && current_bid_price < SMA1 
//     && SMA1 < SMA2 
      && D1_high_today  < D1_high_previous
  //    && WPR > -20
      && SAR > Open[0]
      && D1_high_today - D1_low_today >= 250
      )
         sellCondition = true;
     }
   if(Mode == USE_H4 || Mode == USE_BOTH)
     {
      if(Open[0] < D1_high_today 
      && Open[0] > H4_higher_open 
      && current_bid_price < H4_higher_open 
      && current_bid_price < SMA1 
//      && SMA1 < SMA2 
      && D1_high_today < D1_high_previous
  //    && WPR > -20
      && SAR > Open[0]
      && D1_high_today - D1_low_today >= 250
      )
         sellCondition = true;
     }

   if(sellCondition && !CheckIfOrderExists(OP_SELL))
     {
      OpenSellOrder();
     }
     
     // Esegui trailing stop se le condizioni sono soddisfatte
   TrailingStop();

     
  }


//±-----------------------------------------------------------------+
//| Trailing Stop Function                                           |
//±-----------------------------------------------------------------+
//±-----------------------------------------------------------------+
//| Trailing Stop Function                                           |
//±-----------------------------------------------------------------+
void TrailingStop()
{
    for (int i = OrdersTotal() - 1; i >= 0; i--)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
            if (OrderMagicNumber() == MagicNumber)
            {
                double trailingDistance = TRAILING_STEP * Point;
                
                if (OrderType() == OP_BUY)
                {
                    double currentStopLoss = OrderStopLoss();
                    double potentialNewStopLoss = Bid - trailingDistance;

                    // Solo aggiorna se il nuovo stop loss è più alto dell’attuale ed è a TRAILING_STEP o più dal prezzo di aprertura
                    if (potentialNewStopLoss > currentStopLoss && potentialNewStopLoss > OrderOpenPrice() + trailingDistance)
                    {
                        OrderModify(OrderTicket(), OrderOpenPrice(), potentialNewStopLoss, OrderTakeProfit(), 0, clrGreen);
                    }
                }

                if (OrderType() == OP_SELL)
                {
                    double currentStopLoss = OrderStopLoss();
                    double potentialNewStopLoss = Ask + trailingDistance;

                    // Solo aggiorna se il nuovo stop loss è più basso dell’attuale ed è a TRAILING_STEP o più dal prezzo di aprertura
                    if (potentialNewStopLoss < currentStopLoss && potentialNewStopLoss < OrderOpenPrice() - trailingDistance)
                    {
                        OrderModify(OrderTicket(), OrderOpenPrice(), potentialNewStopLoss, OrderTakeProfit(), 0, clrRed);
                    }
                }
            }
        }
    }
}


/*
void OnTick()
  {
   RefreshLevels(); // Update price levels every tick
   DrawLevelLines(); // Refresh the lines

   double SMA = iMA(NULL,0,SMA_Period,0,MODE_SMA,PRICE_CLOSE,0);

   double current_bid_price = Bid; // Prezzo al quale puoi vendere
   double current_ask_price = Ask; // Prezzo al quale puoi acquistare

// Check for Buy conditions
   bool buyCondition = false;
   if(Mode == USE_H1 || Mode == USE_BOTH)
     {
      if(Open[0] > D1_low_today && Open[0] < H1_lower_open && current_ask_price > H1_lower_open && current_ask_price < SMA)
         buyCondition = true;
     }
   if(Mode == USE_H4 || Mode == USE_BOTH)
     {
      if(Open[0] > D1_low_today && Open[0] < H4_lower_open && current_ask_price > H4_lower_open  && current_ask_price < SMA)
         buyCondition = true;
     }

   if(buyCondition && !CheckIfOrderExists(OP_BUY))
     {
      OpenBuyOrder();
     }

// Check for Sell conditions
   bool sellCondition = false;
   if(Mode == USE_H1 || Mode == USE_BOTH)
     {
      if(Open[0] < D1_high_today && Open[0] > H1_higher_open && current_bid_price < H1_higher_open && current_bid_price > SMA)
         sellCondition = true;
     }
   if(Mode == USE_H4 || Mode == USE_BOTH)
     {
      if(Open[0] < D1_high_today && Open[0] > H4_higher_open && current_bid_price < H4_higher_open && current_bid_price > SMA)
         sellCondition = true;
     }

   if(sellCondition && !CheckIfOrderExists(OP_SELL))
     {
      OpenSellOrder();
     }
  }
*/
//±-----------------------------------------------------------------+
//| Refresh price levels                                             |
//±-----------------------------------------------------------------+
void RefreshLevels()
  {
   datetime day_start = iTime(NULL, PERIOD_D1, 0); // Inizio del giorno corrente

// Daily levels
   D1_low_previous = iLow(NULL, PERIOD_D1, 1);
   D1_high_previous = iHigh(NULL, PERIOD_D1, 1);
   D1_low_today = iLow(NULL, PERIOD_D1, 0);
   D1_high_today = iHigh(NULL, PERIOD_D1, 0);

// Se è l’inizio di un nuovo giorno, usa i valori del giorno precedente per H1 e H4
   if(iTime(NULL, PERIOD_H1, 0) >= day_start)
     {
      H1_prev_lower_open = H1_lower_open;
      H1_prev_higher_open = H1_higher_open;
      H1_lower_open = FindLowestOpenToday(PERIOD_H1);
      H1_higher_open = FindHighestOpenToday(PERIOD_H1);
     }

   if(iTime(NULL, PERIOD_H4, 0) >= day_start)
     {
      H4_prev_lower_open = H4_lower_open;
      H4_prev_higher_open = H4_higher_open;
      H4_lower_open = FindLowestOpenToday(PERIOD_H4);
      H4_higher_open = FindHighestOpenToday(PERIOD_H4);
     }

// Se i valori di apertura corrente non sono ancora aggiornati usa quelli del giorno precedente
   if(H1_lower_open == DBL_MAX)
      H1_lower_open = H1_prev_lower_open;
   if(H1_higher_open == -DBL_MAX)
      H1_higher_open = H1_prev_higher_open;
   if(H4_lower_open == DBL_MAX)
      H4_lower_open = H4_prev_lower_open;
   if(H4_higher_open == -DBL_MAX)
      H4_higher_open = H4_prev_higher_open;
  }

//±-----------------------------------------------------------------+
//| Draw horizontal lines for levels                                 |
//±-----------------------------------------------------------------+
void DrawLevelLines()
  {
   DrawHorizontalLine("H1_Lower_Open", H1_lower_open, clrBlue);
   DrawHorizontalLine("H1_Higher_Open", H1_higher_open, clrBlue);
   DrawHorizontalLine("H4_Lower_Open", H4_lower_open, clrGreen);
   DrawHorizontalLine("H4_Higher_Open", H4_higher_open, clrGreen);
   DrawHorizontalLine("D1_Low_Today", D1_low_today, clrRed);
   DrawHorizontalLine("D1_High_Today", D1_high_today, clrRed);
   DrawHorizontalLine("D1_Low_Yesterday", D1_low_previous, clrWhite);
   DrawHorizontalLine("D1_High_Yesterday", D1_high_previous, clrWhite);

  }

//±-----------------------------------------------------------------+
//| Draw a horizontal line                                           |
//±-----------------------------------------------------------------+
void DrawHorizontalLine(string name, double price, color lineColor)
  {
   if(ObjectFind(0, name) == -1)
     {
      ObjectCreate(0, name, OBJ_HLINE, 0, 0, price);
     }
   ObjectSetInteger(0, name, OBJPROP_COLOR, lineColor);
   ObjectSetInteger(0, name, OBJPROP_WIDTH, 1);
   ObjectSetDouble(0, name, OBJPROP_PRICE1, price);
  }

//±-----------------------------------------------------------------+
//| Find lowest open of the day for a given period                   |
//±-----------------------------------------------------------------+
double FindLowestOpenToday(int timeframe)
  {
   double lowest_open = DBL_MAX;
   datetime day_start = iTime(NULL, PERIOD_D1, 0);

   for(int i = 0; i < iBars(NULL, timeframe); i++)
     {
      datetime bar_time = iTime(NULL, timeframe, i);
      if(bar_time < day_start)
         break;

      double open = iOpen(NULL, timeframe, i);
      if(open < lowest_open)
         lowest_open = open;
     }
   return lowest_open;
  }

//±-----------------------------------------------------------------+
//| Find highest open of the day for a given period                  |
//±-----------------------------------------------------------------+
double FindHighestOpenToday(int timeframe)
  {
   double highest_open = -DBL_MAX;
   datetime day_start = iTime(NULL, PERIOD_D1, 0);

   for(int i = 0; i < iBars(NULL, timeframe); i++)
     {
      datetime bar_time = iTime(NULL, timeframe, i);
      if(bar_time < day_start)
         break;

      double open = iOpen(NULL, timeframe, i);
      if(open > highest_open)
         highest_open = open;
     }
   return highest_open;
  }

//±-----------------------------------------------------------------+
//| Open Buy Order                                                   |
//±-----------------------------------------------------------------+
void OpenBuyOrder()
  {
   double sl = Bid - StopLoss * Point; //D1_low_today;
   double tp = Bid + TakeProfit * Point;
   OrderSend(Symbol(), OP_BUY, Lots, Ask, 3, sl, tp, "", MagicNumber, 0, clrGreen);
  }

//±-----------------------------------------------------------------+
//| Open Sell Order                                                  |
//±-----------------------------------------------------------------+
void OpenSellOrder()
  {
   double sl = Ask + StopLoss * Point; //D1_high_today;
   double tp = Ask - TakeProfit * Point;
   OrderSend(Symbol(), OP_SELL, Lots, Bid, 3, sl, tp, "", MagicNumber, 0, clrRed);
  }

//±-----------------------------------------------------------------+
//| Check if order exists                                            |
//±-----------------------------------------------------------------+
bool CheckIfOrderExists(int orderType)
  {
   for(int i = OrdersTotal() - 1; i >= 0; i--)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
         if(OrderMagicNumber() == MagicNumber) //&& OrderType() == orderType)
            return true;
        }
     }
   return false;
  }
//+------------------------------------------------------------------+


ho bisogno che le operazioni vengano aperte solmanete se la distanza tra D1_high_today e D1_low_today >= 250. dimmi come aggiungerla, la mia versione non è corretta