کدزیر رو بررسی کم ببین اشتباهی داره یا ن ؟؟
//+------------------------------------------------------------------+
//|                                                  CustomerBot.mq4 |
//|                         Copyright 2024, MetaQuotes Ltd. BitoByte |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Ltd. BitoByte"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property script_show_inputs

//--- user input ---
extern double area_down_level = 0 ;                   //Area Down Level
extern double area_up_level = 10 ;                    //Area Up Level
extern double area_step = 0.1 ;                       //Area Step
extern double risk_percent = 1 ;                      //Risk Percent
extern double entry_deviation = 1 ;                   //Entry Deviation
extern double entry_step = 0.01 ;                     //Entry Step
extern double stoploss_deviation = 1;                 //Stoploss Deviation
extern double profit_loss_ratio = 1;                  //Profit-Loss Ratio
extern double candle_divition = 0.5;                  //Candle Divition
extern int slippage = 2;                              //Slippage Order
extern int max_difference_size = 20;                  //Max Difference Entry and Stop Size
extern int lotSize = 100000;                          //Lot Size
extern int allow_position_number = 4;                 //Allow Position Number Any Day
extern int startHour1 = 15;                           //Start Hour1
extern int startMinute1 = 30;                         //Start Minute1
extern int endHour1 = 19;                             //End Hour1
extern int endMinute1 = 30;                           //End Minute1
extern int startHour2 = 10;                           //Start Hour2
extern int startMinute2 = 00;                         //Start Minute2
extern int endHour2 = 12;                             //End Hour2
extern int endMinute2 = 00;                           //End Minute2
extern int startHour3 = 01;                           //Start Hour3
extern int startMinute3 = 00;                         //Start Minute3
extern int endHour3 = 23;                             //End Hour3
extern int endMinute3 = 50;                           //End Minute3
extern int price_decimals_num = 5;                    //Price Decimals Num


double area_price[];
int allow_position_counter = 4;
datetime lastResetTime ;

//--- allow sessions for trade ---
bool isTradingTime() {
   int currentHour = Hour();
   int currentMinute = Minute();
   bool withinPeriod1 = (currentHour > startHour1 || (currentHour == startHour1 && currentMinute >= startMinute1)) && (currentHour < endHour1 || (currentHour == endHour1 && currentMinute < endMinute1));
   bool withinPeriod2 = (currentHour > startHour2 || (currentHour == startHour2 && currentMinute >= startMinute2)) && (currentHour < endHour2 || (currentHour == endHour2 && currentMinute < endMinute2));
   bool withinPeriod3 = (currentHour > startHour3 || (currentHour == startHour3 && currentMinute >= startMinute3)) && (currentHour < endHour3 || (currentHour == endHour3 && currentMinute < endMinute3));
   return (withinPeriod1 || withinPeriod2 || withinPeriod3);
}

//--- check not exist open position ---
bool noOpenOrders() {
    return (OrdersTotal() == 0);
}

//--- calculate pip between two price ---
double PipDifference(double price1, double price2) 
{
   int digits = SymbolInfoInteger(Symbol(), SYMBOL_DIGITS);;
   double diff = price2 - price1;
   double pipDiff;
   switch(digits) 
   {
      case 4:
      case 5:
      pipDiff = NormalizeDouble(MathAbs(diff) * 10000, 0);
      break;
      case 3:
      pipDiff = NormalizeDouble(MathAbs(diff) * 1000, 0);
      break;
      case 2:
      pipDiff = NormalizeDouble(MathAbs(diff) * 100, 0);
      break;
      default:
      pipDiff = NormalizeDouble(MathAbs(diff) * 10, 0);
      break;
   }
   return pipDiff;
}

//--- pip to dollar value ---
double PipValue() 
{
   double _lotSize = lotSize;
   int digits = SymbolInfoInteger(Symbol(), SYMBOL_DIGITS);
   double pipValue;
   switch(digits) 
   {
      case 5:
      case 4:
      pipValue = (0.0001 / MarketInfo(Symbol(), MODE_TICKVALUE)) * _lotSize;
      break;
      case 3:
      pipValue = (0.001 / MarketInfo(Symbol(), MODE_TICKVALUE)) * _lotSize;
      break;
      case 2:
      pipValue = (0.001 / MarketInfo(Symbol(), MODE_TICKVALUE)) * _lotSize;
      break;
      default:   
      pipValue = (0.1 / MarketInfo(Symbol(), MODE_TICKVALUE)) * _lotSize;  
      break;
   }

   return pipValue;

}

//--- reset allow open position number ---
void CheckAndReset() {
  int currentHour = Hour();
  int currentMinute = Minute();
  //Comment(currentHour, currentMinute, PERIOD_CURRENT);
  if (currentHour == 23 && currentMinute == 60 - Period())
   {
      allow_position_counter = 0;
   }
}


int OnInit()
  {
   Print("The expert started!");
   //--- create area level ---
   double base_price = area_down_level;
   int index = 0;
   while(base_price < area_up_level){
      base_price += area_step; 
      ArrayResize(area_price, index + 1);
      area_price[index] = base_price;
      index++;
   }
   return(INIT_SUCCEEDED);
  }
  
void OnTick()
  {
  
      CheckAndReset();
      if(noOpenOrders() && isTradingTime() && allow_position_counter<=allow_position_number)
      {
         //--- candle & account data ---
         double open1 = Open[1];
         double close1 = Close[1];
         double high1 = High[1];
         double low1 = Low[1];
         double open2 = Open[2];
         double close2 = Close[2];
         double high2 = High[2];
         double low2 = Low[2];
         double account_balance = AccountBalance();
         
         //--- signal params ---
         double entry_long_trigger = 0.0;
         double entry_short_trigger = 0.0;
         double stop_loss_price = 0.0;
         double take_profit_price = 0.0;
         string condition_type = "";
         //--- check signal ---
         double level_price;
         for(int i = 0; i<ArraySize(area_price)-1; i++)
         {
            level_price = area_price[i];
            if((high2>level_price && low2>level_price && high2<level_price+entry_step && low2<level_price+entry_step)&&(high1<high2 && low1>level_price)) // type1_long
            {
               entry_long_trigger = high1 + (entry_deviation * 10 * Point); 
               stop_loss_price = low1 - (stoploss_deviation * 10 * Point); 
               double distance_to_sl = entry_long_trigger - stop_loss_price; 
               take_profit_price = entry_long_trigger + (profit_loss_ratio * distance_to_sl);
               condition_type = "Long1"; // نوع پوزیشن
               int digits = (int)SymbolInfoInteger(_Symbol, SYMBOL_DIGITS); 
               double stopLossPips = PipDifference(entry_long_trigger, stop_loss_price); 
               double riskAmount = (account_balance * risk_percent) / 100.0; 
               double pipValue = PipValue(); 
               double entry_volume = riskAmount / (stopLossPips * pipValue);
               entry_volume = NormalizeDouble(entry_volume, 2);
               if (entry_volume > 100) {entry_volume = 100;}
               if (entry_volume < 0.01) {entry_volume = 0.01;}
               
               if (Ask >= entry_long_trigger) 
               {
                  if (stopLossPips<max_difference_size) 
                  {   
                     int ticketBuy = OrderSend(Symbol(), OP_BUY, entry_volume, Ask, slippage * 10 * Point, stop_loss_price, take_profit_price, condition_type, 0, 0, clrBlue);
                     if (ticketBuy <= 0)
                     {
                      Print("Error opening Buy order: ", GetLastError());
                     }
                     else
                     {
                      Print("Buy order opened successfully at ", Ask);
                      allow_position_counter += 1;
                     }
                  } 
               }
            }
            
            
            if((high2<level_price && low2<level_price && high2>level_price-entry_step && low2>level_price-entry_step)&&(low1>low2 && high1<level_price)) // type1_short
            {
               entry_short_trigger = low1 - (entry_deviation * 10 * Point); 
               stop_loss_price = high1 + (stoploss_deviation * 10 * Point); 
               double distance_to_sl = entry_long_trigger - stop_loss_price; 
               take_profit_price = entry_short_trigger + (profit_loss_ratio * distance_to_sl); 
               condition_type = "Short1";
               int digits = (int)SymbolInfoInteger(_Symbol, SYMBOL_DIGITS); 
               double stopLossPips = PipDifference(entry_short_trigger, stop_loss_price); 
               double riskAmount = (account_balance * risk_percent) / 100.0; 
               double pipValue = PipValue(); 
               double entry_volume = riskAmount / (stopLossPips * pipValue); 
               entry_volume = NormalizeDouble(entry_volume, 2);
               if (entry_volume > 100) {entry_volume = 100;}
               if (entry_volume < 0.01) {entry_volume = 0.01;}
               
               if (Bid <= entry_short_trigger)
               {
                  if (stopLossPips<max_difference_size)
                  {
                     int ticketBuy = OrderSend(Symbol(), OP_SELL, entry_volume, Bid, slippage * 10 * Point, stop_loss_price, take_profit_price, condition_type, 0, 0, clrRed);
                     if (ticketBuy <= 0)
                     {
                      Print("Error opening Sell order: ", GetLastError());
                     }
                     else
                     {
                      Print("Sell order opened successfully at ", Bid);
                      allow_position_counter += 1;
                     }
                  }
               }
            }
            
            if((high2>level_price && high2<level_price+entry_step && low2<level_price && low2>level_price-entry_step) && (high1>level_price && low1<level_price && high1<high2 && low1>low2)) // type2 only long
            {
               entry_long_trigger = high1 + (entry_deviation * 10 * Point);
               entry_short_trigger = low1 - (entry_deviation * 10 * Point);
               if (Ask >= entry_long_trigger)
               {
                  stop_loss_price = low1 - (stoploss_deviation * 10 * Point); 
                  double distance_to_sl = entry_long_trigger - stop_loss_price; 
                  take_profit_price = entry_long_trigger + (profit_loss_ratio * distance_to_sl); 
                  condition_type = "Long2"; 
                  int digits = (int)SymbolInfoInteger(_Symbol, SYMBOL_DIGITS); 
                  double stopLossPips = PipDifference(entry_long_trigger, stop_loss_price); 
                  double riskAmount = (account_balance * risk_percent) / 100.0; 
                  double pipValue = PipValue(); 
                  double entry_volume = riskAmount / (stopLossPips * pipValue); 
                  entry_volume = NormalizeDouble(entry_volume, 2);
                  if (entry_volume > 100) {entry_volume = 100;}
                  if (entry_volume < 0.01) {entry_volume = 0.01;}
                  
                  if (stopLossPips<max_difference_size)
                  {
                     int ticketBuy = OrderSend(Symbol(), OP_BUY, entry_volume, Ask, slippage * 10 * Point, stop_loss_price, take_profit_price, condition_type, 0, 0, clrBlue);
                     if (ticketBuy <= 0)
                     {
                      Print("Error opening Buy order: ", GetLastError());
                     }
                     else
                     {
                      Print("Buy order opened successfully at ", Ask);
                      allow_position_counter += 1;
                     }
                  }
               }
               if (Bid <= entry_short_trigger) 
               {
                  stop_loss_price = high1 + (stoploss_deviation * 10 * Point);
                  double distance_to_sl = entry_long_trigger - stop_loss_price; 
                  take_profit_price = entry_short_trigger + (profit_loss_ratio * distance_to_sl); 
                  condition_type = "Short1"; 
                  int digits = (int)SymbolInfoInteger(_Symbol, SYMBOL_DIGITS); 
                  double stopLossPips = PipDifference(entry_short_trigger, stop_loss_price); 
                  double riskAmount = (account_balance * risk_percent) / 100.0; 
                  double pipValue = PipValue();  
                  double entry_volume = riskAmount / (stopLossPips * pipValue); 
                  entry_volume = NormalizeDouble(entry_volume, 2);
                  if (entry_volume > 100) {entry_volume = 100;}
                  if (entry_volume < 0.01) {entry_volume = 0.01;}
                   
                  if (stopLossPips<max_difference_size)
                  {
                     int ticketBuy = OrderSend(Symbol(), OP_SELL, entry_volume, Bid, slippage * 10 * Point, stop_loss_price, take_profit_price, condition_type, 0, 0, clrRed);
                     if (ticketBuy <= 0)
                     {
                      Print("Error opening Sell order: ", GetLastError());
                     }
                     else
                     {
                      Print("Sell order opened successfully at ", Bid);
                      allow_position_counter += 1;
                     }
                  }
               }
            }
            
            
            
            if((high2>level_price && high2<level_price+entry_step && low2<level_price) && (high1>level_price && high1<high2 && low1<level_price && low1>level_price-entry_step && low1<low2)) // type3 only long
            {
               entry_long_trigger = high1 + (entry_deviation * 10 * Point); 
               stop_loss_price = low1 - (stoploss_deviation * 10 * Point); 
               double distance_to_sl = entry_long_trigger - stop_loss_price; 
               take_profit_price = entry_long_trigger + (profit_loss_ratio * distance_to_sl);
               condition_type = "Long3";
               int digits = (int)SymbolInfoInteger(_Symbol, SYMBOL_DIGITS); 
               double stopLossPips = PipDifference(entry_long_trigger, stop_loss_price); 
               double riskAmount = (account_balance * risk_percent) / 100.0; 
               double pipValue = PipValue(); 
               double entry_volume = riskAmount / (stopLossPips * pipValue);
               entry_volume = NormalizeDouble(entry_volume, 2);
               if (entry_volume > 100) {entry_volume = 100;}
               if (entry_volume < 0.01) {entry_volume = 0.01;}
               
               if (Ask >= entry_long_trigger) 
               {
                  if (stopLossPips<max_difference_size) 
                  {   
                     int ticketBuy = OrderSend(Symbol(), OP_BUY, entry_volume, Ask, slippage * 10 * Point, stop_loss_price, take_profit_price, condition_type, 0, 0, clrBlue);
                     if (ticketBuy <= 0)
                     {
                      Print("Error opening Buy order: ", GetLastError());
                     }
                     else
                     {
                      Print("Buy order opened successfully at ", Ask);
                      allow_position_counter += 1;
                     }
                  } 
               }
               
            } 
            
            
            
            if((high2>level_price && high2<level_price+entry_step && low2>level_price) && (high1>level_price && high1<high2 && low1<level_price && low1>level_price-entry_step && low1<low2)) // type4 only long
            {
               entry_long_trigger = high1 + (entry_deviation * 10 * Point); 
               stop_loss_price = low1 - (stoploss_deviation * 10 * Point); 
               double distance_to_sl = entry_long_trigger - stop_loss_price; 
               take_profit_price = entry_long_trigger + (profit_loss_ratio * distance_to_sl);
               condition_type = "Long4";
               int digits = (int)SymbolInfoInteger(_Symbol, SYMBOL_DIGITS); 
               double stopLossPips = PipDifference(entry_long_trigger, stop_loss_price); 
               double riskAmount = (account_balance * risk_percent) / 100.0; 
               double pipValue = PipValue(); 
               double entry_volume = riskAmount / (stopLossPips * pipValue);
               entry_volume = NormalizeDouble(entry_volume, 2);
               if (entry_volume > 100) {entry_volume = 100;}
               if (entry_volume < 0.01) {entry_volume = 0.01;}
               
               if (Ask >= entry_long_trigger) 
               {
                  if (stopLossPips<max_difference_size) 
                  {   
                     int ticketBuy = OrderSend(Symbol(), OP_BUY, entry_volume, Ask, slippage * 10 * Point, stop_loss_price, take_profit_price, condition_type, 0, 0, clrBlue);
                     if (ticketBuy <= 0)
                     {
                      Print("Error opening Buy order: ", GetLastError());
                     }
                     else
                     {
                      Print("Buy order opened successfully at ", Ask);
                      allow_position_counter += 1;
                     }
                  } 
               }
               
            }
            
            
            
            if((high2>level_price && high2<level_price+entry_step && low2<level_price && low2>level_price-entry_step) && (high1>level_price && high1<high2 && low1>level_price)) // type5 only long
            {
               entry_long_trigger = high1 + (entry_deviation * 10 * Point); 
               stop_loss_price = low1 - (stoploss_deviation * 10 * Point); 
               double distance_to_sl = entry_long_trigger - stop_loss_price; 
               take_profit_price = entry_long_trigger + (profit_loss_ratio * distance_to_sl);
               condition_type = "Long4";
               int digits = (int)SymbolInfoInteger(_Symbol, SYMBOL_DIGITS); 
               double stopLossPips = PipDifference(entry_long_trigger, stop_loss_price); 
               double riskAmount = (account_balance * risk_percent) / 100.0; 
               double pipValue = PipValue(); 
               double entry_volume = riskAmount / (stopLossPips * pipValue);
               entry_volume = NormalizeDouble(entry_volume, 2);
               if (entry_volume > 100) {entry_volume = 100;}
               if (entry_volume < 0.01) {entry_volume = 0.01;}
               
               if (Ask >= entry_long_trigger) 
               {
                  if (stopLossPips<max_difference_size) 
                  {   
                     int ticketBuy = OrderSend(Symbol(), OP_BUY, entry_volume, Ask, slippage * 10 * Point, stop_loss_price, take_profit_price, condition_type, 0, 0, clrBlue);
                     if (ticketBuy <= 0)
                     {
                      Print("Error opening Buy order: ", GetLastError());
                     }
                     else
                     {
                      Print("Buy order opened successfully at ", Ask);
                      allow_position_counter += 1;
                     }
                  } 
               }
            }
            
            
            
            
         }
                 
      }
      
   }