Help with MQL5 EA code not taking any positions. it doesn't have any errors

 
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"


#include <Trade/Trade.mqh>

CTrade            trade;
CPositionInfo     pos;
COrderInfo        ord;

input group "=== Trading inputs ==="

     input double   RiskPercent           = 3;    // Risk as % of Trading Capital
     input int      Tppoints              = 200; //Take profit in points (10 points=1 pip)
     input int      Slpoints              = 200; //Stop loss in points (10 points=1 pip)
     input int      TslTriggerpints       = 15; // points in profit before Trailing SL is activated (10 points=1 pip)
     input int      Tslpoints             = 10; //Trailing stop loss (10 points = 1 pip)
     input ENUM_TIMEFRAMES   Timeframe    =PERIOD_CURRENT; //Time Frame to run
     input int  InpMagic   = 298348;    //EA Identification number
     input string     Tradecomment = "scalping robot";
     
     
     enum  StartHour      {Inactive=0, _0100=1, _0200=2, _0300=3, _0400=4, _0500=5, _0600=6, _0700=7, _0800=8, _0900=9, _1000=10, _1100=11, _1200=12, _1300=13, _1400=14, _1500=15, _1600=16, _1700=17, _1800=18, _1900=19, _2000=20, _2100=21, _2200=22, _2300=23};
     input StartHour  SHInput=0; //Start Hour
     
     enum EndHour  {Inactive=0, _0100=1, _0200=2, _0300=3, _0400=4, _0500=5, _0600=6, _0700=7, _0800=8, _0900=9, _1000=10, _1100=11, _1200=12, _1300=13, _1400=14, _1500=15, _1600=16, _1700=17, _1800=18, _1900=19, _2000=20, _2100=21, _2200=22, _2300=23};
     input EndHour  EHInput=0; //End Hour
     
     
     int SHChoice;
     int EHChoice;
     
     
     int       BarsN  = 5;
     int       ExpirationBars = 100;
     int       OrderDistPoints = 100;
     
     
     




//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit() {

 trade.SetExpertMagicNumber(InpMagic);
    ChartSetInteger(0,CHART_SHOW_GRID,false);
   
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick() {

    TrailStop();
    
    if(!IsNewBar())  return;
    
    MqlDateTime   time;
    TimeToStruct(TimeCurrent(), time);
    int  Hournow  = time.hour;
    
    SHChoice = SHInput;
    EHChoice = EHInput;
    
    if(Hournow<SHChoice){CloseAllOrders(); return;}
    if(Hournow>=EHChoice && EHChoice !=0){CloseAllOrders(); return;}
    
    
    int BuyTotal = 0;
    int SellTotal = 0;
    
   for (int i=PositionsTotal()-1; i>=0; i--){
    pos.SelectByIndex(i);
    if(pos.PositionType()==POSITION_TYPE_BUY && pos.Symbol()==_Symbol && pos.Magic()==InpMagic) BuyTotal++;
    if(pos.PositionType()==POSITION_TYPE_SELL && pos.Symbol()==_Symbol && pos.Magic()==InpMagic) SellTotal++;
}
   
 for (int i=OrdersTotal()-1; i>=0; i--){
    ord.SelectByIndex(i);
    if(ord.OrderType()==ORDER_TYPE_BUY_STOP && ord.Symbol()==_Symbol && ord.Magic()==InpMagic) BuyTotal++;
    if(ord.OrderType()==ORDER_TYPE_SELL_STOP && ord.Symbol()==_Symbol && ord.Magic()==InpMagic) SellTotal++;
}  
   
   if(BuyTotal<=0){
     double high = findHigh();
     if(high>0){
        SendBuyOrder(high);
     }
   
   }
   
   if(SellTotal<=0){
      double low = findLow();
       if(low>0){
         SendSellOrder(low);
       }
   
   }
   
     
   
  }
//+------------------------------------------------------------------+
                   //Finding High//

double findHigh(){
  double highestHigh = 0;
  for(int i=0; i < 200; i++){
    double high = iHigh(_Symbol, Timeframe,i);
    if(i > BarsN && iHighest(_Symbol, Timeframe, MODE_HIGH, BarsN*2+1, i-BarsN) == 1){
      if(high > highestHigh) {
        return high;
      }
    }
    highestHigh = MathMax(high, highestHigh);
  }
  return -1;
}


              // findind LOW// 
  
double findLow(){
  double lowestLow = DBL_MAX;
  for(int i=0; i < 200; i++){
    double low = iLow(_Symbol, Timeframe, i);
    if(i > BarsN && iLowest(_Symbol, Timeframe, MODE_LOW, BarsN*2+1, i-BarsN) == 1){
      if(low < lowestLow) {
        return low;
      }
    }
    lowestLow = MathMin(low, lowestLow);
  }
  return -1;
}

         //Finding New Bar//

bool IsNewBar(){
  static datetime previousTime = 0;
  datetime currentTime = iTime(_Symbol, Timeframe, 0);
  if(previousTime != currentTime) {
    previousTime = currentTime;
    return true;
  }
  return false;
}
         //sending Buy order//
    
void SendBuyOrder(double entry) {

  double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
  
  if(ask > entry - OrderDistPoints * _Point) return;
  
  double tp = entry + Tppoints * _Point;
  double sl = entry - Slpoints * _Point;
  
  double lots = 0.01;
  if(RiskPercent > 0) lots = calclots(entry-sl);
  datetime expiration = iTime(_Symbol,Timeframe,0) + ExpirationBars * PeriodSeconds(Timeframe);
      trade.BuyStop(lots,entry,_Symbol,sl,tp,ORDER_TIME_SPECIFIED,expiration);
}

        //    Sending sell orders//
        
 void SendSellOrder(double entry) {
  double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
  if (bid < entry + OrderDistPoints * _Point) return;

  double tp = entry - Tppoints * _Point;
  double sl = entry + Slpoints * _Point;

  double lots = 0.01;
  if (RiskPercent > 0) lots = calclots(sl - entry);

  datetime expiration = iTime(_Symbol, Timeframe, 0) + ExpirationBars * PeriodSeconds(Timeframe);

  trade.SellStop(lots, entry, _Symbol, sl, tp, ORDER_TIME_SPECIFIED, expiration);
}       
        


             //lot size//
    
double calclots(double slPoints) {
  double risk = AccountInfoDouble(ACCOUNT_BALANCE) * RiskPercent / 100;
  double ticksize = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
  double tickvalue = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE);
  double lotstep = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP);
  double minvolume = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);
  double maxvolume = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX);
  double volumelimit = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_LIMIT);

  double moneyPerLotstep = slPoints / ticksize * tickvalue * lotstep;
  double lots = MathFloor(risk / moneyPerLotstep) * lotstep;

  if (volumelimit!= 0) lots = MathMin(lots, volumelimit);
  if (maxvolume!= 0) lots = MathMin(lots, SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX));
  if (minvolume!= 0) lots = MathMax(lots, SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN));

  lots = NormalizeDouble(lots, 2);

  return lots;
}

void CloseAllOrders(){
    for(int i=OrdersTotal()-1;i>=0;i--){
        ord.SelectByIndex(i);
        ulong ticket = ord.Ticket();
        if(ord.Symbol()==_Symbol && ord.Magic()==InpMagic){
            trade.OrderDelete(ticket);
        }
    }
}


void TrailStop(){

  double sl = 0;
  double tp = 0;
  double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
  double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
   
       for(int i=PositionsTotal()-1; i>=0; i--){
       
          if(pos.SelectByIndex(i)){
              ulong  ticket = pos.Ticket();
              
              if(pos.Magic()==InpMagic && pos.Symbol()==_Symbol){
    if(pos.PositionType()==POSITION_TYPE_BUY){
        if(bid-pos.PriceOpen()>TslTriggerpints*_Point){
            tp = pos.TakeProfit();
            sl = bid - (Tslpoints * _Point);
        }
        if(sl > pos.StopLoss() && sl!=0){
            trade.PositionModify(ticket,sl,tp);
        }
    }
}
      else if(pos.PositionType()==POSITION_TYPE_SELL){
         if(ask+(TslTriggerpints*_Point)<pos.PriceOpen()){
            tp = pos.TakeProfit();
            sl = ask + (Tslpoints * _Point);
           if(sl < pos.StopLoss() && sl!=0){
              trade.PositionModify(ticket,sl,tp);
        }
    }
}
          }
       
       }



}
 
i am guessing that this is created by ChatGPT or other AI? These codes are often removed by moderators because theses generators create crap code. I only read first quarter of the code and i see 5 lines that are wrong or missing stuff. There is NO fixing this code. Too many issues.
 
Michael Charles Schefe #:
i am guessing that this is created by ChatGPT or other AI? These codes are often removed by moderators because theses generators create crap code. I only read first quarter of the code and i see 5 lines that are wrong or missing stuff. There is NO fixing this code. Too many issues.

It is not Generated by AI. 
There is no warning or issues with code.

it just doesn't take any positions while backtesting. 

you don't have to say it to be deleted if you don't know to how ot fix it

 

Сoders are working for free:

  • if it is interesting for them personally, or
  • if it is interesting for many members on this forum.

Freelance section of the forum should be used in most of the cases.

NOTE: some parts in your code are common for AI-generated, so it can be hardly helped (would need considerable human review/corrections before being usable in a live trading environment).

Trading applications for MetaTrader 5 to order
Trading applications for MetaTrader 5 to order
  • 2025.02.18
  • www.mql5.com
The largest freelance service with MQL5 application developers
 
Hossein Rahimi #: There is no warning or issues with code.
  1. If there is no issue with the code, why did you post?

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

 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893