Distribute slow indicator calculation over mutliple frames

 

Hi guys,

I‌ have an indicator which takes quite a long time to calculate. This is a problem when I try to switch quickly between periods as the chart is stuck as long as the calculation is ongoing. Now i want to spread out this slow calculation over multiple frames, so that i can still work with the chart in the meantime whilst i watching the indicator take shape in real time. Is this somehow possible?

Thank you for your answer :) 

 
RichPiano:

Hi guys,

I‌ have an indicator which takes quite a long time to calculate. This is a problem when I try to switch quickly between periods as the chart is stuck as long as the calculation is ongoing. Now i want to spread out this slow calculation over multiple frames, so that i can still work with the chart in the meantime whilst i watching the indicator take shape in real time. Is this somehow possible?

Thank you for your answer :) 

Optimize your code.
 
RichPiano:
I‌ have an indicator which takes quite a long time to calculate.
This is a problem when I try to switch quickly between periods as the chart is stuck as long as the calculation is ongoing.
Now i want to spread out this slow calculation over multiple frames, so that i can still work with the chart in the meantime whilst i watching the indicator take shape in real time.
  1. It shouldn't.
  2. Check the _StopFlag and exit your loop(s)
  3. See How to do your lookbacks correctly.
 
Alain Verleyen:
Optimize your code.


I have... the thing is, it's an indicator showing the upper and lower boundaries of the value area of the market profile. That means i have to calculate the market profile for each bar which takes some time, but I can't see how I could possibly speed that up without loosing accuracy.

whroeder1:
  1. It shouldn't.
  2. Check the _StopFlag and exit your loop(s)
  3. See How to do your lookbacks correctly.

I‌ do exit my loops, the indicator isn't stopped by the terminal. To be precise, it takes about 2-3 seconds to load the indicator, which for some might not be over the top. But it's annoying, that's what I'm saying ;)

I always do my lookbacks like this:

   ...
   // leftmost bar has index 0
   ArraySetAsSeries(open,false);
   ArraySetAsSeries(high,false);
   ArraySetAsSeries(low,false);
   ArraySetAsSeries(close,false);
   ArraySetAsSeries(tick_volume,false);

   // lookback period
   int begin = MathMin(prev_calculated, IndicatorCounted());

   // walk through each candle from last to most current
   for (int i=begin; i < rates_total; i++)
      {
      ...
      }
   ...

It does the job perfectly well as far as I can see. The indicator also doesn't slow down the terminal once it's loaded, it's just at the beginning.

So I'm guessing there's no way to do it as i intended, or is there? Thanks anyway :)

 
RichPiano:


I have... the thing is, it's an indicator showing the upper and lower boundaries of the value area of the market profile. That means i have to calculate the market profile for each bar which takes some time, but I can't see how I could possibly speed that up without loosing accuracy.

I‌ do exit my loops, the indicator isn't stopped by the terminal. To be precise, it takes about 2-3 seconds to load the indicator, which for some might not be over the top. But it's annoying, that's what I'm saying ;)

I always do my lookbacks like this:

It does the job perfectly well as far as I can see. The indicator also doesn't slow down the terminal once it's loaded, it's just at the beginning.

So I'm guessing there's no way to do it as i intended, or is there? Thanks anyway :)

Why don't you post the whole code?

From a few lines of code it is impossible to see where are you going wrong (and you are or else you would not have that issue)

 

Your source code does not contain IsStopped check within the main loop as it was suggested, but you said you have it. Did you make a mistake?

A‌s for calculating indicator by frames, you can do it by hands - MQL does not provide such things. For example, you can calculate profile of only one bar on every new tick, starting from the latest bar and going to the past. That is, after you place the indicator on a chart, you'll see one bar calculated, then second, and so on, and every moment the chart will remain responsive to timeframe changes and other user actions. To do so, you need to save indicator state internally in your code, that is invent more sophisticated sheme than just a simple standard IndicatorCounted. Of course, you can use frames of larger size, not a single bar.

 
RichPiano: I‌ do exit my loops, the indicator isn't stopped by the terminal. ‌
That's not what I was saying. You said "problem when I try to switch quickly between periods as the chart is stuck as long as the calculation is ongoing." So check the _StopFlag and exit early.


RichPiano: So I'm guessing there's no way to do it as i intended, or is there?
I gave you a link, that shows how to do exactly that.
 
whroeder1:
That's not what I was saying. You said "problem when I try to switch quickly between periods as the chart is stuck as long as the calculation is ongoing." So check the _StopFlag and exit early.


I gave you a link, that shows how to do exactly that.

T‌hanks! :)

1) I'm not really sure what you mean by that: According to the wiki:

"The _StopFlag variable contains the flag of the mql4-program stop. When the client terminal is trying to stop the program, it sets the _StopFlag variable to true.‌"

Can you explain to me a little more detailed what you mean by "check the stop flag?" How and why should I do that exactly? Thanks :)

2‌)‌ The links shows me how to optimize my lookback period? But I have already done that. There's no need for me to alter it as far as i can tell?

Stanislav Korotky:

Your source code does not contain IsStopped check within the main loop as it was suggested, but you said you have it. Did you make a mistake?

A‌s for calculating indicator by frames, you can do it by hands - MQL does not provide such things. For example, you can calculate profile of only one bar on every new tick, starting from the latest bar and going to the past. That is, after you place the indicator on a chart, you'll see one bar calculated, then second, and so on, and every moment the chart will remain responsive to timeframe changes and other user actions. To do so, you need to save indicator state internally in your code, that is invent more sophisticated sheme than just a simple standard IndicatorCounted. Of course, you can use frames of larger size, not a single bar.


T‌hanks, this could in fact be a solution. However, one problem i see is that in situations with less ticks per minute, the indicator will load very slowly.

 
RichPiano:

1)"The _StopFlag variable contains the flag of the mql4-program stop. When the client terminal is trying to stop the program, it sets the _StopFlag variable to true.‌"

Can you explain to me a little more detailed what you mean by "check the stop flag?" How and why should I do that exactly? Thanks :)

2‌)‌ The links shows me how to optimize my lookback period? But I have already done that. There's no need for me to alter it as far as i can tell?

  1.    int begin = MathMin(prev_calculated, IndicatorCounted());
    
       // walk through each candle from last to most current
       for (int i=begin; i < rates_total; i++)
          {
    if(_StopFlag) return prev_calculated; // Exit the loop early and allow the chart to change.
          ...
          }
    Was that so hard?
  2. Did you bother reading the thread to #12?
    What part of "do in groups" was unclear?
    int      iLast   = MathMax(LAST, iMA - 2000);        // Do in groups

 
RichPiano:

T‌hanks, this could in fact be a solution. However, one problem i see is that in situations with less ticks per minute, the indicator will load very slowly.


You can do it not only on ticks but on timer events.
 
whroeder1:
  1. Was that so hard?
  2. Did you bother reading the thread to #12?
    What part of "do in groups" was unclear?

I think you should consider re-implementing this example. It's really not that clear how you achieve the "chunking" of work between calls.

Usually your examples are really good, but this one is not quite up to your normal high standards

Reason: