How to save iCustom calls

 

I have thought a lot of times that it is not optimal the way we have to get the output parameters of Custom Indicators in our EAs. I explain myself. 

If you have an indicator with only one output -I mean output buffers-, then there's no problem at all, you can get the value of that output with one simple call to iCustom function.

But if you have an indicator with more than one output, then you'll have to call iCustom for every single output, even if the indicator is the same. 

I know that solving this inefficiency is probably difficult, or maybe even impossible, because of the way functions work in mql4: they can only return one value, and not many.

In other languages, like R, you may do the following trick: you can program your function to return a list of values, being each of the items of that list the variables you want the function to give back. You can also have (in mql4) the function with parameters passed by reference instead of by value, and this does also the effect of a function returning several variables instead of only one.

 The problem is when you have a function that you cannot modify, like iCustom. Could anyone in MetaQuotes give a think to this issue, and  try to improve the language by allowing these helpful behaviors?

 And you, guys developers in the forum, what do you think of this idea? 

 

Mt4 is as it is and as mt5 and the android version are heavily developed I hardly can imagine that such a change in the basic structure (my guess) of mt4 will be taken under consideration.

Beside that the first call of an indicator will calculate all buffer values, so the second call is just a memory access and does not start (again) the calc. process - imho.

Finally this way is less error prone ion case of changes.

 
gooly:

Mt4 is as it is and as mt5 and the android version are heavily developed I hardly can imagine that such a change in the basic structure (my guess) of mt4 will be taken under consideration.

Beside that the first call of an indicator will calculate all buffer values, so the second call is just a memory access and does not start (again) the calc. process - imho.

Finally this way is less error prone ion case of changes.

Are you sure of that? If it  is true, then great... But my oppinion is that every single call to iCustom will calculate the same values over and over again. 

In the EA I am developing now, I want to get 6 values of the same indicator, and I want values of candles 1 and 2 (latest 2 close candles).

In total: 12 iCustom calls to the same indicator, when I actually would need 2 calculations (one per candle). It is a heavy indicator, so if I don't optimize its code and if I let it run in every tick in stead of at the beginning tick of every candle, the EA becomes unusable.

I know that what I am asking is possibly weird and not the usual trader will use it, but for every EAs developer will be very helpful and powerful at some time. 

Thanks for your opinion 

 
WHRoeder:

Yes we are.

The iCustom checks if the requested chart exists (opens it if not) and if an indicator with that name and parameter(s) is running on the chart (and starts it if not.) Then it reads from the indicator's buffer.

The buffers have already been calculated before the EA get's its OnTick.


That's great! Your answer now makes sense to what I was getting from my indicator, which is sensible to the number of visible candles in the chart: it is giving me the same outputs in the indicator's window than iCustom's calls to the indicator, even after modifying number of visible candles on chart... All is running in the same window.

 

You see? Nothing like asking in the forum to get precise and useful answers. 

 Thank you! 

 
yokinfx: Are you sure of that?

Yes we are.

What use to happen:

The iCustom checks if the requested chart exists (opens it if not) and if an indicator with that name and parameter(s) is running on the chart (and starts it if not.) Then it reads from the indicator's buffer.

After that the buffers have already been calculated before the EA gets its OnTick.

What happens now is the buffers are updated on the first iCustom call. Which you can see by adding a Print(counted+" "+WindowExpertName()); to the indicator's OnCalculate and running this EA:
void OnTick(){
  Print("^"+WindowExpertName());
  Sleep(100);
  double v = iCustom(NULL,0, "MMAPC", 20,10,false,0,2);
         v = iCustom(NULL,0, "MMAPC", 20,10,false,0,1);
  Print("v"+WindowExpertName());
  Sleep(10000);
  Print("^^"+WindowExpertName());
  Sleep(100);
    v = iCustom(NULL,0, "MMAPC", 20,10,false,0,2);
         v = iCustom(NULL,0, "MMAPC", 20,10,false,0,1);
  Print("vv"+WindowExpertName());
  Sleep(10000);
2016.06.01 10:13:39.129    testea EURUSD,H4: vvtestea
2016.06.01 10:13:39.028    testea EURUSD,H4: ^^testea
2016.06.01 10:13:29.000    testea EURUSD,H4: vtestea
2016.06.01 10:13:28.935    MMAPC EURUSD,H4: 0 MMAPC
2016.06.01 10:13:28.816    testea EURUSD,H4: ^testea
Note, there was a post a few months back that showed if the indicator is visually on the chart and called by iCustom, there are actually two running.
 
WHRoeder:

Yes we are.

What use to happen:

The iCustom checks if the requested chart exists (opens it ifnot) and if an indicator with that name and parameter(s) is running onthe chart (and starts it if not.) Then it reads from the indicator'sbuffer.

After that the buffers have already been calculated before the EA gets its OnTick.

What happens now is the buffers are updated on the first iCustom call. Which you can see by adding a Print(counted+" "+WindowExpertName()); to the indicator's OnCalculate and running this EA:
2016.06.01 10:13:39.129    testea EURUSD,H4: vvtestea
2016.06.01 10:13:39.028    testea EURUSD,H4: ^^testea
2016.06.01 10:13:29.000    testea EURUSD,H4: vtestea
2016.06.01 10:13:28.935    MMAPC EURUSD,H4: 0 MMAPC
2016.06.01 10:13:28.816    testea EURUSD,H4: ^testea
Note, there was a post a few months back that showed if the indicator is visually on the chart and called by iCustom, there are actually two running.

Is there a way here to vote you up? Can I give 5 stars to your answer?

:-)  

Reason: