Expert Advisor eats space like crazy

 

Hi guys if I try to do a backtest on my custom Expert Advisor it starts eating diskspace like crazy within 5minutes easily 100GB, I have no idea why maybe the loops inside my code?


#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

//extern Settings
extern double maxRiskPerTrade = 1;
extern int risk =10;
extern int reward = 20;
extern int MaximumAmountTrades = 1;

//intern Settings
int NeutralMagicNumber = 123456;
int SafetyBarCheck = 0;
int StopLoss  = risk;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
  
   int counted_bars, limit;
   counted_bars=IndicatorCounted();
   limit=Bars-counted_bars;
   Print("start");

      //Indicator (EMA_M15_50)
      double ema_h1_50 = iCustom(NULL, PERIOD_CURRENT, "EMA_H1_50", 50, 0, MODE_EMA, PRICE_CLOSE, 0, 1);
      double ema_m15_50 = iCustom(NULL, PERIOD_CURRENT, "EMA_M15_50", 50, 0, MODE_EMA, PRICE_CLOSE, 0, 1);
      
      //Indicator (Traditional Macd Histogram Divergence "Moving Average")
      double tmhdiDodgerBlue = iCustom(NULL, PERIOD_CURRENT, "Traditional Macd Histogram Divergence", "*** MACD Settings ***", 12, 26, 9, "*** Indicator Settings ***", true, true, false, 1, 1);
      double tmhdiSignalLine = iCustom(NULL, PERIOD_CURRENT, "Traditional Macd Histogram Divergence", "*** MACD Settings ***", 12, 26, 9, "*** Indicator Settings ***", true, true, false, 2, 1);
      
      //Indicator (Traditional Macd Histogram Divergence "Histogram")
      double tmhdiHistogram = iCustom(NULL, PERIOD_CURRENT, "Traditional Macd Histogram Divergence", "*** MACD Settings ***", 12, 26, 9, "*** Indicator Settings ***", true, true, false, 0, 1);
           
      //Algorithm Long
      if(Close[1]>ema_m15_50 && ema_m15_50>ema_h1_50 && tmhdiDodgerBlue>tmhdiSignalLine && tmhdiDodgerBlue<0 && tmhdiSignalLine<0 && tmhdiHistogram>0){
      Print("Long Algo 1");
      
         //intern Loop (for)
         for(int i=0;i<30;i++){
            //Indicator (Traditional Macd Histogram Divergence "Histogram")
            double tmhdiHistogram = iCustom(NULL, PERIOD_CURRENT, "Traditional Macd Histogram Divergence", "*** MACD Settings ***", 12, 26, 9, "*** Indicator Settings ***", true, true, false, 0, i);
            if(tmhdiHistogram<0){
            Print("Long Algo 2");
               int j=i;
               //Intern Loop (for)
               for(j;j<30;j++){
               Print("Long Loop 2");
                  //Indicator (Traditional Macd Histogram Divergence "Histogram")
                  double tmhdiHistogram = iCustom(NULL, PERIOD_CURRENT, "Traditional Macd Histogram Divergence", "*** MACD Settings ***", 12, 26, 9, "*** Indicator Settings ***", true, true, false, 0, j);
                  if(tmhdiHistogram>0){
                  Print("Long Algo 3");
                     for(int z=0;z<30;z++){
                     Print("Long Loop 3");
                        //Indicator (Traditional Macd Histogram Divergence "Moving Average")
                        double tmhdiDodgerBlue = iCustom(NULL, PERIOD_CURRENT, "Traditional Macd Histogram Divergence", "*** MACD Settings ***", 12, 26, 9, "*** Indicator Settings ***", true, true, false, 1, z);
                        double tmhdiSignalLine = iCustom(NULL, PERIOD_CURRENT, "Traditional Macd Histogram Divergence", "*** MACD Settings ***", 12, 26, 9, "*** Indicator Settings ***", true, true, false, 2, z);
                        if(tmhdiDodgerBlue<0 && tmhdiSignalLine<0){
                        Print("Long Alog 4");
                        
                           //Intern Loop (for)
                           for(int k=0;k<5;k++){
                           Print("Long Loop 4");
                              //Indicator (Traditional Macd Histogram Divergence "Divergence")
                              double tmhdiDVUP = iCustom(NULL, PERIOD_CURRENT, "Traditional Macd Histogram Divergence", "*** MACD Settings ***", 12, 26, 9, "*** Indicator Settings ***", true, true, false, 3, k);
                              if(tmhdiDVUP != EMPTY_VALUE){
                              Print("Long Algo 5");
                                 //TotalOpenOrders
                                 if(TotalOpenOrders()<MaximumAmountTrades){
                                    //LastTicket
                                    //if(OrderSelect(LastTradeTicket(),SELECT_BY_TICKET,MODE_HISTORY)){
                                       //OrderProfit
                                       //if(OrderProfit()<0){
                                         //CalculateLotSize
                                         //if(CalculateLotSize(StopLoss)){
                                            //SafetyBarCheck
                                            if(Bars>SafetyBarCheck){
                                              //CalculateLotSize
                                              double LotSize = 0.00;
                                              LotSize = CalculateLotSize(StopLoss);
                                              //OrderSend
                                              int longTicket=OrderSend(Symbol(),OP_BUY,LotSize,Ask,100,0,0,"Long",NeutralMagicNumber,0,clrGreen);
                                            }//SafetyBarCheck
                                             SafetyBarCheck = Bars;
                                         //}//CalculateLotSize               
                                       //}//OrderProfit
                                    //}//LastTicket
                                 }//TotalOpenOrders            
                              }//Algorithm
                           }//Intern Loop (for)                        
                        }//Algorithm
                     }//Intern Loop (for)                  
                  }//Algorithm
               }//Intern Loop (for)
            }//Algorithm
         }//Intern Loop (for)
      }//Algorithm

      //Algorithm Short
      if(Close[1]<ema_m15_50 && ema_m15_50<ema_h1_50 && tmhdiDodgerBlue<tmhdiSignalLine && tmhdiDodgerBlue>0 && tmhdiSignalLine>0 && tmhdiHistogram<0){
      Print("Short Algo 1");
         //intern Loop (for)
         for(int i=0;i<30;i++){
         Print("Short Loop 1");
            //Indicator (Traditional Macd Histogram Divergence "Histogram")
            double tmhdiHistogram = iCustom(NULL, PERIOD_CURRENT, "Traditional Macd Histogram Divergence", "*** MACD Settings ***", 12, 26, 9, "*** Indicator Settings ***", true, true, false, 0, i);
            if(tmhdiHistogram>0){
            Print("Short Algo 2");
               int j=i;
               //Intern Loop (for)
               for(j;j<30;j++){
               Print("Short Loop 2");
                  //Indicator (Traditional Macd Histogram Divergence "Histogram")
                  double tmhdiHistogram = iCustom(NULL, PERIOD_CURRENT, "Traditional Macd Histogram Divergence", "*** MACD Settings ***", 12, 26, 9, "*** Indicator Settings ***", true, true, false, 0, j);
                  if(tmhdiHistogram<0){
                  Print("Short Algo 3");
                     for(int z=0;z<30;z++){
                     Print("Short Loop 3");
                        //Indicator (Traditional Macd Histogram Divergence "Moving Average")
                        double tmhdiDodgerBlue = iCustom(NULL, PERIOD_CURRENT, "Traditional Macd Histogram Divergence", "*** MACD Settings ***", 12, 26, 9, "*** Indicator Settings ***", true, true, false, 1, z);
                        double tmhdiSignalLine = iCustom(NULL, PERIOD_CURRENT, "Traditional Macd Histogram Divergence", "*** MACD Settings ***", 12, 26, 9, "*** Indicator Settings ***", true, true, false, 2, z);
                        if(tmhdiDodgerBlue>0 && tmhdiSignalLine>0){
                        Print("Short Algo 4");
                           //Intern Loop (for)
                           for(int k=0;k<5;k++){
                           Print("Short Loop 4");
                           //Indicator (Traditional Macd Histogram Divergence "Divergence")
                           double tmhdiDVDOWN = iCustom(NULL, PERIOD_CURRENT, "Traditional Macd Histogram Divergence", "*** MACD Settings ***", 12, 26, 9, "*** Indicator Settings ***", true, true, false, 4, k);
                              if(tmhdiDVDOWN != EMPTY_VALUE){
                              Print("Short Algo 5");
                                 //TotalOpenOrders
                                 if(TotalOpenOrders()<MaximumAmountTrades){
                                    //LastTicket
                                    //if(OrderSelect(LastTradeTicket(),SELECT_BY_TICKET,MODE_HISTORY)){
                                       //OrderProfit
                                       //if(OrderProfit()<0){
                                         //CalculateLotSize
                                         //if(CalculateLotSize(StopLoss)){
                                            //SafetyBarCheck
                                            if(Bars>SafetyBarCheck){
                                              //CalculateLotSize
                                              double LotSize = 0.00;
                                              LotSize = CalculateLotSize(StopLoss);
                                              //OrderSend
                                              int longTicket=OrderSend(Symbol(),OP_SELL,LotSize,Bid,100,0,0,"Long",NeutralMagicNumber,0,clrRed);
                                            }//SafetyBarCheck
                                             SafetyBarCheck = Bars;
                                         //}//CalculateLotSize               
                                       //}//OrderProfit
                                    //}//LastTicket
                                 }//TotalOpenOrders            
                              }//Algorithm
                           }//Intern Loop (for)                        
                        }//Algorithm
                     }//Intern Loop (for)                  
                  }//Algorithm
               }//Intern Loop (for)
            }//Algorithm
         }//Intern Loop (for)
      }//Algorithm
      
    //Stopmanagement
    StealthControl();
  }
//+------------------------------------------------------------------+
//| CalculateLotSize function                                        |
//+------------------------------------------------------------------+
double CalculateLotSize(double SL){
   double LotSize = 0;
   double nTickValue = MarketInfo(Symbol(), MODE_TICKVALUE);
   if ((Digits == 3) || (Digits == 5)){
      nTickValue = nTickValue * 10;
   }
   LotSize = (AccountBalance() * maxRiskPerTrade / 100) / (SL * nTickValue);
   return LotSize;
}
//+------------------------------------------------------------------+
//| TotalOpenOrder function                                          |
//+------------------------------------------------------------------+
int TotalOpenOrders(){
   int total = 0;
   for(int i=OrdersTotal();i>=0;i--){
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
            if(OrderSymbol()==Symbol()){
               total++;
         }//OrderSelect
      }//for Loop
   }//function
   return(total);
}
//+------------------------------------------------------------------+
//| LastTradeTicket function                                         |
//+------------------------------------------------------------------+
int LastTradeTicket(){
   int ticket = -1;
   for(int i=OrdersHistoryTotal()-1;i>0;i--){
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)){
         if(OrderMagicNumber()==NeutralMagicNumber){
            if(OrderSymbol()==Symbol()){
               ticket = OrderTicket();
               break;
            }//OrderSymbol
         }//OrderMagicNumber
      }//OrderSelect
   }//Loop
   return(ticket);
}
//+------------------------------------------------------------------+
//| StealthControl function                                          |
//+------------------------------------------------------------------+
void StealthControl(){
   int totalorders = OrdersTotal();
   for(int i=totalorders-1;i>=0;i--){
   
    //Digits calculation
    double myPoint = Point();
    if(Digits()==5 || Digits()==3){
      myPoint *= 10;
    }
    
    OrderSelect(i, SELECT_BY_POS);
    bool result = false;
    
      //Long Stopmanagement Trigger
      if (OrderSymbol()==Symbol()){
         if(OrderMagicNumber() == NeutralMagicNumber){
           if(OrderType() == OP_BUY){        
            if(Bid<OrderOpenPrice()-risk*myPoint || Bid>OrderOpenPrice()+reward*myPoint){
               result = OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID),10, Red);
            }//OrderClose
           }//OrderType
         }//OrderMagicNumber
       }//OrderSymbol
   
       //Short Stopmanagement Trigger
       if(OrderMagicNumber() == NeutralMagicNumber){
         if(OrderType() == OP_SELL){
               if(Bid>OrderOpenPrice()+risk*myPoint || Bid<OrderOpenPrice()-reward*myPoint){
                  result = OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK),10, Red);
               }//OrderClose
            }//OrderType
         }//OrderMagicNumber
      }//Loop end
  return;
}
//+------------------------------------------------------------------+
 

well....I am not sure what exactly your idea is... I looked at your code briefly....

However these loops with iCustom calls are not best idea.For fast speed, call iCustom only in a necessary moment. In general you refer to finished candles...but in loops you also use unfinished candle value since you start loops with 0 and not 1.

If I was suppose to make your code faster, I would definitelly get rid of unnecessary iCustom calls for finished candles on every tick.

I would make for example:


int current_bar;

void OnInit()
 {
   current_bar=Bars-1;
 }
 
void OnTick()
 {
   if(Bars>current_bar)
    {
      current_bar=Bars;
      //and here all calculations you need
    }
}

And a better option for really perfect and never ending operation till the end of the world (from William Roeder)

datetime bar_open_time;

void OnInit()
 {
    bar_open_time=Time[0]-1;
 }

void OnTick()
  {
     if(Time[0]>bar_open_time)
      {
        bar_open_time=Time[0];
        //and here all calculations you need
      }
  }

I would also change loops start to int=1 instead of 0 if it does not influence your strategy, tactic or however you call it.

I would also not create variables on every tick.

I would declare them first to allocate memory space for them and then use them....

if you need to reset them...just put a zero value there....


I would also get rid of Print functions.

Of course it may be necessary for you to check some part of code....but after checking I would remove it, to get better speed.


Wishing you luck:)

 

Piotr Latoszynski:

   if(Bars>current_bar)

  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2019.05.06)
              Messages Editor

  2. For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart), volume is unreliable (miss ticks), Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
              New candle - MQL4 programming forum #3 (2014.04.04)

    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              Running EA once at the start of each bar - MQL4 programming forum (2011.05.06)

 
i had this problem before, it's because you have print () in the code, it will print every tick and store it in log files
search *.log to remove log files and get rid of print
 
William Roeder:
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2019.05.06)
              Messages Editor

  2. For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart), volume is unreliable (miss ticks), Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
              New candle - MQL4 programming forum #3 (2014.04.04)

    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              Running EA once at the start of each bar - MQL4 programming forum (2011.05.06)

1.Done:)

2. Bars is unreliable (a refresh/reconnect can change number of bars on chart)...Well...same rule applies to time...if you change timeframe...Time{0] also may change unfavourably.

The protective code during OnInit() function does the job I believe in both cases to execute the code only once per bar. ( I skip the issue of the first "initial" bar, because it depends on what we need to do )

I agree with you, that bars will eventually reach the limit of Bars in MT4, so if we talk about a perfect solution, it must be time:)


I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
          Running EA once at the start of each bar - MQL4 programming forum (2011.05.06) 


...Here I do not see the relation to the issue:)New bar condition ( not function ) is to ensure executing the code only once - at the start of a new bar. Nothing more nothing less.

Running EA once at the start of each bar
Running EA once at the start of each bar
  • 2011.05.06
  • www.mql5.com
Hi all hero traders and tradettes. I want my EA to run once per bar...
Reason: