Problem with IndicatorRelease

 

Hi everyone,

I'm using MQL5 for a quiet of time. I'm encountering a problem in which the iteration of Oncalculate block itself  never ends!

I call a custom indicator with 5 buffers then use the buffer to suggest if its good to open a trade position or not (on my market watch),

prior to the utilization of IndicatorRelease metatrader wasused to used huge amount of RAM, so I tried IndicatorRelease. and this is the code

can any one help on this?

#property version   "1.00"
#property indicator_chart_window
#property indicator_plots   0

input int         TableRows   =   22;                   //Table Rows
input double      risk        =   50;                   // optimise risk acception (%)
input string      buys        =  "yes";                 //show  buy signals?
input string      watchs      =  "yes";                 //show watch signals?
input string      keeps       =  "yes";                 //show keep signals?
input string      sells       =  "no";                  //show sell signals?
input string      risks       =  "no";                  //show risk signals?
input int         xpos1       =  0;                     //coulmns to skip
int               Mi          =   1;        //strategy 1 for short time 2 for midtime
int               NoF         =   144;     //Time Span in Days
int               fontsize    =   12;      //Font Size
int or=0;

string         marketWatchSymbolsList[]={....};//some symbols 
string         TradePosition[];
//---

//---
MqlRates       DailyBar[];
int currentSymbolsTotal=ArraySize(marketWatchSymbolsList);
//+------------------------------------------------------------------+
//| Indicator initialization function                                |
//+------------------------------------------------------------------+
int OnInit()
  {
  deleteScale(currentSymbolsTotal,1);
  ArraySetAsSeries(DailyBar,true);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Indicator deinitialization function                              |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- Delete only what is drawn by your code
   deleteScale(currentSymbolsTotal,1);
   ChartRedraw();
   Comment("");
  }
//+------------------------------------------------------------------+
//| Indicator iteration function                                     |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tickVolume[],
                const long &volume[],
                const int &spread[])
  {

      or=or+1;
      Comment(or); //this is to check how many time this block is repeated!!!
      double rsi1[];
      double rsi2[];
      double rsi3[];
      double rsi4[];
      double rsi5[];
      double rsi31[];
      double rsi41[];
           
      
int jj=0;
int jp=0;


   
      //--- resize arrays 
      
      ArrayResize(TradePosition,currentSymbolsTotal);

      for(int i=0;i<100;i++) 
      {
      int indcdef1=0;
      int indcdef2=0;

      indcdef1 = iCustom (marketWatchSymbolsList[i],_Period,"bks\\bks-efficiency","",Mi,risk,0,"no",PRICE_CLOSE);
      indcdef2 = iCustom (marketWatchSymbolsList[i],_Period,"bks\\bks-efficiency","",2*Mi,risk,0,"no",PRICE_CLOSE);
      

      CopyBuffer(indcdef1,0,0,1,rsi1);
      CopyBuffer(indcdef1,1,0,1,rsi2);
      CopyBuffer(indcdef1,2,0,1,rsi3);
      CopyBuffer(indcdef1,3,0,1,rsi4);
      CopyBuffer(indcdef1,4,0,5,rsi5);
      CopyBuffer(indcdef2,2,0,3,rsi31);
      CopyBuffer(indcdef2,3,0,3,rsi41);   
      
      TradePosition[i]="";
      
      if(market_closed(marketWatchSymbolsList[i],_Period)==false && CopyRates(marketWatchSymbolsList[i],_Period,0,NoF,DailyBar)==NoF )
       {       
        
        if((rsi5[0]==1 || rsi5[1]==1 || rsi5[2]==1 || rsi5[3]==1 || rsi5[4]==1)&& buys == "yes") {if (rsi41[0]!=1 && rsi31[0]!=1){TradePosition[i]="Buy-it";}}
        if((rsi5[0]==1 || rsi5[1]==1 || rsi5[2]==1 || rsi5[3]==1 || rsi5[4]==1)&& buys == "yes") {if (rsi41[0]==1 || rsi31[0]==1){TradePosition[i]="Not sure!";}}
        if(rsi1[0]==1 && watchs== "yes"){ if(rsi41[0]!=1 && rsi31[0]!=1 )  {TradePosition[i]="watch-it";}}
        
        if(rsi2[0]==1 && keeps==  "yes"){if(rsi41[0]!=1 && rsi31[0]!=1  )  {TradePosition[i]="keep-it";}}
        if(rsi3[0]==1&& sells=="yes")  {TradePosition[i]="Sell-it";}
        if(rsi4[0]==1&& risks=="yes")  {TradePosition[i]="Dont-risk";}
      
      }
              //Release the memory of indicator for the previous calculated symbol position
              IndicatorRelease(indcdef1);
              IndicatorRelease(indcdef2);

      }
      color posc = clrGray;
      for(int i=0;i<100;i++) // creates a grid win n rows and m cols 
      {
              
              if (TradePosition[i]=="watch-it"){posc = clrGreen;}
              if (TradePosition[i]=="keep-it"){posc = clrOrange;}
              if (TradePosition[i]=="Sell-it"){posc = clrRed;}
              if (TradePosition[i]=="Dont-risk"){posc = clrWhiteSmoke;}
              if (TradePosition[i]=="Buy-it"){posc = clrMediumSpringGreen;}
           
              int xpos=fontsize+1;
              xpos=xpos+fontsize*11*(jp+xpos1);
              if(TradePosition[i]!="")
              {
                // SetPanel("Panel "+IntegerToString(i),0,xpos,32+(jj-jp*TableRows)*32,50,30,StringToColor(posc),clrWhite,1);
                 //SetPanel(string name,int sub_window,x,y,width,height,bg_color,border_clr,int border_width)
                 SetText("Textbksv5 "+IntegerToString(i),StringSubstr(marketWatchSymbolsList[i],0,10),xpos+3,fontsize*2+(jj-jp*TableRows)*fontsize*2,posc,fontsize);
                 //SetText(name, text,int x,int y,color colour,int fontsize=12)
                 SetText("Variationbksv5 "+IntegerToString(i),TradePosition[i],xpos+fontsize*6,fontsize*2+(jj-jp*TableRows)*fontsize*2,posc,fontsize);
                 jj=jj+1;jp = (int)MathFloor(jj/TableRows);
              }
        
        }

   return(rates_total);   
  }
//+------------------------------------------------------------------+
//| Remove unneeded objects from main chart                          |
//+------------------------------------------------------------------+
void deleteScale(int from,int to=1)
  {
        //some code
  }
//+------------------------------------------------------------------+
//| Draw data about a symbol in a panel                              |
//+------------------------------------------------------------------+
void SetText(string name,string text,int x,int y,color colour,int fontsize2=12)
  {
        //some code
  }
//+------------------------------------------------------------------+
//| Draw a panel with given color for a symbol                       |
//+------------------------------------------------------------------+
void SetPanel(string name,int sub_window,int x,int y,int width,int height,color bg_color,color border_clr,int border_width)
  {
        //some code
  }
//+------------------------------------------------------------------+
//| check's if the symbol is available to trade                      |
//+------------------------------------------------------------------+
bool market_closed(string symbol,ENUM_TIMEFRAMES period)
{
        //some code to check if the symbol is available to trade
}
//+------------------------------------------------------------------+
 
hhr_rahimi:

Hi everyone,

I'm using MQL5 for a quiet of time. I'm encountering a problem in which the iteration of Oncalculate block itself  never ends!

I call a custom indicator with 5 buffers then use the buffer to suggest if its good to open a trade position or not (on my market watch),

prior to the utilization of IndicatorRelease metatrader wasused to used huge amount of RAM, so I tried IndicatorRelease. and this is the code

can any one help on this?

Try moving these lines to global scope:

      int indcdef1=0[];
      int indcdef2=0[];

Move these lines to OnInit():

      ArrayResize(indcdef1, ArraySize(marketWatchSymbolsList));
      ArrayResize(indcdef2, ArraySize(marketWatchSymbolsList));

      for(int i=0;i<100;i++)
      {
         indcdef1[i] = iCustom (marketWatchSymbolsList[i],_Period,"bks\\bks-efficiency","",Mi,risk,0,"no",PRICE_CLOSE);
         indcdef2[i] = iCustom (marketWatchSymbolsList[i],_Period,"bks\\bks-efficiency","",2*Mi,risk,0,"no",PRICE_CLOSE);
      }
      CopyBuffer(indcdef1[i],0,0,1,rsi1);
      CopyBuffer(indcdef1[i],1,0,1,rsi2);
      CopyBuffer(indcdef1[i],2,0,1,rsi3);
      CopyBuffer(indcdef1[i],3,0,1,rsi4);
      CopyBuffer(indcdef1[i],4,0,5,rsi5);
      CopyBuffer(indcdef2[i],2,0,3,rsi31);
      CopyBuffer(indcdef2[i],3,0,3,rsi41);   

(highlighted parts are new additions)

And remove IndicatorRelease(). Lastly, note that you're computing every tick. .. that wastes time - check out https://www.mql5.com/en/articles/159 as an example on how to do things every bar, rather than tick, if possible.

Speed should improve, but check whether your memory consumption is acceptable.

See what happens to your memory consumption.

The "New Bar" Event Handler
The "New Bar" Event Handler
  • www.mql5.com
Authors of indicators and experts have always been interested in writing the compact code in terms of execution time. You can approach to this problem from different angles. From this broad topic in this article we will cover the problem, that is seemingly already have been solved: check for a new bar. This is quite a popular way to limit the...
 
Seng Joo Thio:

Try moving these lines to global scope:

Move these lines to OnInit():


(highlighted parts are new additions)

And remove IndicatorRelease(). Lastly, note that you're computing every tick. .. that wastes time - check out https://www.mql5.com/en/articles/159 as an example on how to do things every bar, rather than tick, if possible.

Speed should improve, but check whether your memory consumption is acceptable.

See what happens to your memory consumption.

WOW!

that is a good design, thanks,

It's not clear for me why your method solve the iteration of Oncalculate block!

by the way, the Memory issue is still there and i had to use IndicatorRelease() which really helps to reduce memory usage,

Regards from Iran.

 
hhr_rahimi:

WOW!

that is a good design, thanks,

It's not clear for me why your method solve the iteration of Oncalculate block!

by the way, the Memory issue is still there and i had to use IndicatorRelease() which really helps to reduce memory usage,

Regards from Iran.

When you have iCustom() and ReleaseIndicator() within OnCalculate(), the indicators are being created and released repeatedly every tick, which consumes time. That likely "blocked" your OnCalculate().

IndicatorRelease() should not be used for the sole purpose of reducing memory usage unless you don't need the indicator in all future ticks (which is not your case). So if memory is really an issue, try reducing [Tools->Options->Max bars in chart] instead, or check that your custom indicator is not wasting memory unnecessarily.

The Fundamentals of Testing in MetaTrader 5
The Fundamentals of Testing in MetaTrader 5
  • www.mql5.com
The idea of ​​automated trading is appealing by the fact that the trading robot can work non-stop for 24 hours a day, seven days a week. The robot does not get tired, doubtful or scared, it's is totally free from any psychological problems. It is sufficient enough to clearly formalize the trading rules and implement them in the algorithms, and...
Reason: