Home Forums MQL4 and MetaTrader 4 Recursive indicators adding on Expert Advisor

  • Recursive indicators adding on Expert Advisor

    Posted by angelica-heidenreich on July 27, 2024 at 2:29 pm

    Good afternoon guys,

    I am currently coding an Expert Advisor to automagically trade for me. I already got the indicators, their respective buffers, the names and all of them are on the correct folder. I am using the iCustom() function to read out the buffer values and perform calculations. So far so good.

    For a particular reason which I do not understand, whenever I run the Expert Advisor on the Strategy tester from MT4, no matter how many days/months/hours, it inputs an ABSURD amount of indicators on the chart. For instance, if 20 minutes have passed, there will be at least 20 indicators and that crashes my computer after letting it run for a few minutes.

    Is there a way to fix this in my code?

    I share the Expert Advisor code so you can check on it!

    Please advise!!

    //+------------------------------------------------------------------+
    #property version   "1.00"
    #property strict
    
    double myPoint;
    
    /////////Indicator Inputs//////////////
    input int InitialHour = 09;
    input int InitialMinute = 00;
    input int FinalHour = 17;
    input int FinalMinute = 00;
    
    //ATR Values//
    input int ATRValue = 14;
    
    //BH Ergodic  Inputs//
    input int FEMA = 2;
    input int SEMA = 10;
    input int TEMA = 5;
    input int SignalEMA = 3;
    
    //Absolute Strength Histogram Inputs//
    input int Length = 7;
    input int Smooth = 2;
    input int Signal = 2;
    
    //KAMA//
    input int AMAPeriod = 10;
    input int NFast = 2;
    input int NSlow = 30;
    input double GCoeff = 2.0;
    input int PriceFilter = 5;
    
    //WAE
    input int Sensetive = 90;
    input double DeadZonePip = 3.5;
    input int ExplosionPower = 15;
    input int TrendPower = 15;
    input int BBandPeriod = 20;
    input double BBandSD = 2.5;
    input int FEMAWAE = 11;
    input int SEMAWAE = 30;
    input int SignalMA = 9;
    const string WAEPRICE = "Close price";
    
    
    
    /////////Risk Management//////////////
    input double InpOrderSize = 1.0;
    input string InpTradeComment = __FILE__;
    input int InpMagicNumber = 2000001;
    
    //Indicator Name and Buffers
    const string ATRName = "ATR";
    const int BufferATR = 0;
    
    const string BHErgodic = "BH - ergodic (mtf + arrows)";
    const int BHErgodicBuffer1 = 0;
    const int BHErgodicBuffer2 = 1;
    
    const string ASH = "Absolute Strength Histogram";
    const int ASHBufferBuy = 0;
    const int ASHBufferSell = 1;
    
    const string KAMA = "kaufman_ama_averages_filtered_ATR bands";
    const int KAMABuffer = 0;
    const int KAMAUpperBandBuffer = 4;
    const int KAMALowerBandBuffer = 5;
    
    const string WAE = "WAE_Ext";
    const int WAEBufferBuy = 0;
    const int WAEBufferSell = 1;
    const int WAEBufferLimit = 2;
    
    //Time Input Function//
    
    int TOD_From_Hour = InitialHour; //time of the day (from hour)
    int TOD_From_Min = InitialMinute; //time of the day (from min)
    int TOD_To_Hour = FinalHour; //time of the day (to hour)
    int TOD_To_Min = FinalMinute; //time of the day (to min)
    
    bool inTimeInterval(datetime t, int From_Hour, int From_Min, int To_Hour, int To_Min)
      {
       string TOD = TimeToString(t, TIME_MINUTES);
       string TOD_From = StringFormat("%02d", From_Hour)+":"+StringFormat("%02d", From_Min);
       string TOD_To = StringFormat("%02d", To_Hour)+":"+StringFormat("%02d", To_Min);
       return((StringCompare(TOD, TOD_From) >= 0 && StringCompare(TOD, TOD_To) <= 0)
         || (StringCompare(TOD_From, TOD_To) > 0
           && ((StringCompare(TOD, TOD_From) >= 0 && StringCompare(TOD, "23:59") <= 0)
             || (StringCompare(TOD, "00:00") >= 0 && StringCompare(TOD, TOD_To) <= 0))));
      }
    
    //End Time Input//
    
    //Alert Function//
    void myAlert(string type, string message)
      {
       if(type == "print")
          Print(message);
       else if(type == "error")
         {
          Print(type+" | Engulfing Candle Indicator @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
         }
       else if(type == "order")
         {
         }
       else if(type == "modify")
         {
         }
       else if(type == "indicator")
         {
          if(true) SendNotification(type+" | Test @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
         }
         RefreshRates();
      }
    
    
    //+------------------------------------------------------------------+
    //| Expert initialization function                                   |
    //+------------------------------------------------------------------+
    int OnInit()
      {
       //initialize myPoint
       myPoint = Point();
       if(Digits() == 5 || Digits() == 3)
         {
          myPoint *= 10;
         }
       return(INIT_SUCCEEDED);
      }
    //+------------------------------------------------------------------+
    //| Expert deinitialization function                                 |
    //+------------------------------------------------------------------+
    void OnDeinit(const int reason)
      {
    //---
       
      }
    //+------------------------------------------------------------------+
    //| Expert tick function                                             |
    //+------------------------------------------------------------------+
    void OnTick()
      {
    //---
       
       if(!NewBar()) return;
       
       
       //Perform any calculations and analysis here
       
       //BH Ergodic Buffers Values//C1 Indicator
       double BHErgodicSignalSlow = iCustom(Symbol(), Period(), BHErgodic, "current", FEMA, SEMA, TEMA, SignalEMA, "Close price", true, false, "erg Arrows1", 0.1, 0.1, "Blue", "Crimson", 116, 116, 2, 2, true, BHErgodicBuffer1,1);
       double BHErgodicSignalFast = iCustom(Symbol(), Period(), BHErgodic, "current", FEMA, SEMA, TEMA, SignalEMA, "Close price", true, false, "erg Arrows1", 0.1, 0.1, "Blue", "Crimson", 116, 116, 2, 2, true, BHErgodicBuffer2,1);
       
       double BHErgodicSignalSlow1 = iCustom(Symbol(), Period(), BHErgodic, "current", FEMA, SEMA, TEMA, SignalEMA, "Close price", true, false, "erg Arrows1", 0.1, 0.1, "Blue", "Crimson", 116, 116, 2, 2, true, BHErgodicBuffer1,2);
       double BHErgodicSignalFast1 = iCustom(Symbol(), Period(), BHErgodic, "current", FEMA, SEMA, TEMA, SignalEMA, "Close price", true, false, "erg Arrows1", 0.1, 0.1, "Blue", "Crimson", 116, 116, 2, 2, true, BHErgodicBuffer2,2);
       
       //Absolute Strength Histogram Buffer Values//C2 Indicator
       double ASHBuy = iCustom(Symbol(), Period(), ASH, Length, Smooth, Signal, "Linear weighted", 3, false, 5000, "Blue", "Red", "Dash", 1, "ASH line", "alert2", false, false, false, false, ASHBufferBuy,1);
       double ASHSell = iCustom(Symbol(), Period(), ASH, Length, Smooth, Signal, "Linear weighted", 3, false, 5000, "Blue", "Red", "Dash", 1, "ASH line", "alert2", false, false, false, false, ASHBufferSell,1);
       
       //KAMA Buffer Values//Baseline Indicator
       double KAMAValue = iCustom(Symbol(), Period(), KAMA, "current", AMAPeriod, "Close price", NFast, NSlow, GCoeff, PriceFilter, "smoother", true, KAMABuffer,1);
       double KAMAUpperBandValue = iCustom(Symbol(), Period(), KAMA, "current", AMAPeriod, "Close price", NFast, NSlow, GCoeff, PriceFilter, "smoother", true, KAMAUpperBandBuffer,1);
       double KAMALowerBandValue = iCustom(Symbol(), Period(), KAMA, "current", AMAPeriod, "Close price", NFast, NSlow, GCoeff, PriceFilter, "smoother", true, KAMALowerBandBuffer,1);
       
       //WAE Buffer Values//Volume Indicator
       double WAEBuyValue = iCustom(Symbol(), Period(), WAE, Sensetive, DeadZonePip, ExplosionPower, TrendPower, BBandPeriod, BBandSD, FEMAWAE, SEMAWAE, SignalMA, ATRValue, WAEPRICE, false, 5000, false, false, false, false, WAEBufferBuy, 1);
       double WAESellValue = iCustom(Symbol(), Period(), WAE, Sensetive, DeadZonePip, ExplosionPower, TrendPower, BBandPeriod, BBandSD, FEMAWAE, SEMAWAE, SignalMA, ATRValue, WAEPRICE, false, 5000, false, false, false, false, WAEBufferSell, 1);
       double WAELimitValue = iCustom(Symbol(), Period(), WAE, Sensetive, DeadZonePip, ExplosionPower, TrendPower, BBandPeriod, BBandSD, FEMAWAE, SEMAWAE, SignalMA, ATRValue, WAEPRICE, false, 5000, false, false, false, false, WAEBufferLimit, 1);
       
       //ATR Value
       double ATRCustom = iCustom(Symbol(), Period(), ATRName, ATRValue, BufferATR,1);
       
       //EXECUTE STRATEGY HERE!!//
       bool buyCondition = false;
       bool sellCondition = false;
       
       
       if(BHErgodicSignalSlow1 < BHErgodicSignalFast1 && BHErgodicSignalSlow > BHErgodicSignalFast && ASHBufferBuy != EMPTY_VALUE && Close[1] > KAMAValue && Close[1] < KAMAUpperBandBuffer && WAEBuyValue > WAELimitValue && inTimeInterval(Time[1], TOD_From_Hour, TOD_From_Min, TOD_To_Hour, TOD_To_Min)){
          
          buyCondition = true;
          
       }
       
       if(BHErgodicSignalSlow1 > BHErgodicSignalFast1 && BHErgodicSignalSlow < BHErgodicSignalFast && ASHBufferSell != EMPTY_VALUE && Close[1] < KAMAValue && Close[1] > KAMALowerBandBuffer && WAESellValue > WAELimitValue && inTimeInterval(Time[1], TOD_From_Hour, TOD_From_Min, TOD_To_Hour, TOD_To_Min)){
          
          sellCondition = true;
              
       }
      
       //SEND ORDERS HERE!//
       if(buyCondition){
       
          OrderOpen(ORDER_TYPE_BUY, 2*ATRCustom, 1.5*ATRCustom);
          OrderOpen(ORDER_TYPE_BUY, 2*ATRCustom, 2.0*ATRCustom);
          
          
       } else
       
       if(sellCondition){
       
          OrderOpen(ORDER_TYPE_SELL, 2*ATRCustom, 1.5*ATRCustom);
          OrderOpen(ORDER_TYPE_SELL, 2*ATRCustom, 2.0*ATRCustom);
          
       }
     
      }
    //+------------------------------------------------------------------+
    
    
    bool NewBar() {
    
       static datetime prevTime = 0;
       datetime currentTime = iTime(Symbol(), PERIOD_CURRENT, 0);
       if(currentTime != prevTime) {
          prevTime = currentTime;
          return(true);
       }
       return(false);
    
    }
    
    
    bool OrderOpen(ENUM_ORDER_TYPE orderType, double stopLoss, double takeProfit) {
    
       int ticket;
       double openPrice;
       double stopLossPrice;
       double takeProfitPrice;
       
       if(orderType == ORDER_TYPE_BUY){
          openPrice = SymbolInfoDouble(Symbol(), SYMBOL_ASK);
          stopLossPrice = openPrice - stopLoss;
          takeProfitPrice = openPrice + takeProfit;
       } else
       
       if(orderType == ORDER_TYPE_SELL){
          openPrice = SymbolInfoDouble(Symbol(), SYMBOL_BID);
          stopLossPrice = openPrice + stopLoss;
          takeProfitPrice = openPrice - takeProfit;     
          
       } else {
       
       return(false);
       }
       
       ticket = OrderSend(Symbol(), orderType, InpOrderSize, openPrice, 0, stopLossPrice, takeProfitPrice, InpTradeComment);
       return(ticket > 0);
    
    }
    

    angelica-heidenreich replied 7 months, 3 weeks ago 1 Member · 0 Replies
  • 0 Replies

Sorry, there were no replies found.

Log in to reply.