IndicatorCounted() emulation

1
Hello,
I have a simple expert to manage orders (like move to break-even) with a simple GUI. I would like to extend the basic code with a plugin infrastructure where I will add the functionality of some indicators (like spread monitor, time of day etc). Indicators that don't use buffers but write text on screen and then I could make small gui option panels to modify parameters on the fly. Standard indicator OnCalculate code:

Code: Select all

//+------------------------------------------------------------------+
//| Custom 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 &tick_volume[],
                const long &volume[],
                const int &spread[]){
   
   int counted_bars=IndicatorCounted();
   int limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
           limit=MathMin(Bars-1,Bars-counted_bars);

   for (int i=limit; i>=0; i--)
   {
	//Do work
   }
   return(rates_total);
}
I am not very familiar with the inner workings of IndicatorCounted(). In the expert I have the OnTick() method where I could call the OnCalculate function. Any advice on how I should go about emulating the functionality of IndicatorCounted() so I can use the existing indicator code without much changes?
Thank you in advance


Re: IndicatorCounted() emulation

3
Thank you for your prompt reply. I'm still on mt4. How does this idea in pseudocode look to you to achieve InidcatorCounted():

Code: Select all

global int MyBarsCalculated=0;
OnTick(){
	if(Bars>=MyBarsCalculated){
		//if I had 10 bars and one more has come in so Bars is now 11 need to recalc bar with index 1 and 0 (currently forming)
		Loop from (Bars-MyBarsCalculated) to >=0 and do work;
		MyBarsCalculated=Bars	
	} 
}
I suppose there will be an overflow problems if Bars=max bars on chart...

Re: IndicatorCounted() emulation

4
jerrysb wrote: Wed Jul 26, 2017 2:39 am Thank you for your prompt reply. I'm still on mt4. How does this idea in pseudocode look to you to achieve InidcatorCounted():

Code: Select all

global int MyBarsCalculated=0;
OnTick(){
	if(Bars>=MyBarsCalculated){
		//if I had 10 bars and one more has come in so Bars is now 11 need to recalc bar with index 1 and 0 (currently forming)
		Loop from (Bars-MyBarsCalculated) to >=0 and do work;
		MyBarsCalculated=Bars	
	} 
}
I suppose there will be an overflow problems if Bars=max bars on chart...
Try something like this

Code: Select all

void OnTick()
{
   static int _countedBars=0;
      int limit = MathMin(MathMax(Bars-_countedBars,0),Bars-1);
		//if I had 10 bars and one more has come in so Bars is now 11 need to recalc bar with index 1 and 0 (currently forming)
		Loop from (limit) to >=0 and do work;
		
		_countedBars=Bars	
}




Who is online

Users browsing this forum: No registered users and 3 guests