Reference to an Indicator - page 2

 
Asa Sh #:

It doesn't work unfortunately.

:), I know but, I need the data in a lots of functions so I can't write iCustom and copybuffer every time.

I have attached 3 files, 2 indicators and one C.mqh

put B.mq5 in D1 chart, it should print the data of A.mq5 in candle 10 MN (Print(Function0(ChartID(), ChartSymbol(), 7, 0, 10))) but it refers error.

You haven't done what I said about resources.   

This does not reference the resource, it simply calls the indicator in the standard way from the directory.

Handle = iCustom(A_MySymbol, PERIOD_MN1, "\\Indicators\\A.ex5");   

This references the resource

Handle = iCustom(A_MySymbol, PERIOD_MN1, "::Indicators\\A");

Once you fix that it will still fail because you are creating the handle and trying to access the data from OnInit()

Create the handle in OnInit()  but use it from OnCalculate()  otherwise the data will not be available.

Needing the data in lots of functions doesn't mean you need an include file...


these log entries show the difference between calling the indicator when it is ready and when it is not....


 
Paul Anscombe #:
You haven't done what I said about resources.   

I did, but it didn't work.

Paul Anscombe #:
Needing the data in lots of functions doesn't mean you need an include file...

look, I need A higher timeframe date in lower timeframe so i can't work with OnCalculate() parts because bar numbers is different and lots of other data. I use include to get data from HTF then use them in LTF, I have some parameters that changed so after them functions decide which HTF should use for handle and so on... My Indicator A has 14 buffers and I need them and then there is more than 10 indicator to needs the data of A and it's buffers and also there more than 500 functions that use A data to calculate some other staff ... so I can't call A in every other indicator and write all functions in every indicator and...

 
Asa Sh #:

I did, but it didn't work.

look, I need A higher timeframe date in lower timeframe so i can't work with OnCalculate() parts because bar numbers is different and lots of other data. I use include to get data from HTF then use them in LTF, I have some parameters that changed so after them functions decide which HTF should use for handle and so on... My Indicator A has 14 buffers and I need them and then there is more than 10 indicator to needs the data of A and it's buffers and also there more than 500 functions that use A data to calculate some other staff ... so I can't call A in every other indicator and write all functions in every indicator and...

I've shown you with the log output above that it does work if you put the correct references in and call it from the correct place.

You will end up with a lot of issues and an unreliable solution if you try getting data during OnInit() lots of indicators and EAs use MTF data they don't get it from OnInit(). More importantly the fact you are getting the data during OnInit means it will only be accessed on loading the indicator, after that OnInit is not run again.

 
Paul Anscombe #:
I've shown you with the log output above that it does work if you put the correct references in and call it from the correct place

would you pls send the code you have made as attached file

 
Asa Sh #:

would you pls send the code you have made. because "::" always send me error 4802

Below with changes highlighted.

Note you will always be getting the 4802 error because you are running it from OnInit() as already stated. I moved your call to OnCalculate() therefore it will run every tick.


File B

#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_plots   0
#include "C.mqh"    // removed <> as I located all 3 files in Indicators folder

bool  DoOnce = true;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
//   Print(Function0(ChartID(), ChartSymbol(), 7, 0, 10));   // Moved out of OnInit()
//---
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
{
  
      Print(Function0(ChartID(), ChartSymbol(), 7, 0, 10));
   

//---
//--- return value of prev_calculated for next call
   return(rates_total);
}

File C

#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#resource "\\Indicators\\A.ex5"
//#property tester_indicator "A.ex5"

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double Function0(long A_ChID,
                 string A_MySymbol,
                 int t,
                 int d,
                 int i) //t:time,d:buffers,i:Candle
{
   
   int Handle = 0;
   if(t == 7)
      Handle = iCustom(A_MySymbol, PERIOD_MN1, "::Indicators\\A");
   else if(t == 6)
      Handle = iCustom(A_MySymbol, PERIOD_W1, "::Indicators\\A");
   else
      Handle = iCustom(A_MySymbol, PERIOD_D1, "::Indicators\\A");
    
   int err0 = GetLastError();

   double p = 0;
   double MyLegsIndicator[];
   int copy = 0;
   copy = CopyBuffer(Handle, d, i, 1, MyLegsIndicator); //iBars(A_MySymbol, MyTimeFramesMQL4(t)) - 1
   int err = GetLastError();
   if(copy == -1)// && err == 4806)
   {
      if(err == 4806)
      {
         //--- wait till the data is uploaded
         for(int j = 0; j < 10000; ++j)
            if(BarsCalculated(Handle) > 0)
               break;
         copy = CopyBuffer(Handle, d, i, 1, MyLegsIndicator);
         err = GetLastError();
      }
   }
//--- check coying result
   if(copy == -1)
   {

      Print("Error when trying to get --A0_Legs_0-- values, last error is ", err0, "_", err, " bars ", i, " buffurs ", d, " TF ", t);
      return(0);
   }
   p = MyLegsIndicator[0];
   return(p);
}
 
  1. Paul Anscombe #:      Handle = iCustom(A_MySymbol, PERIOD_D1, "::Indicators\\A");   

    Paul is correct

    #resource "\\Indicators… iCustom("::Indicators…
              Use the publicly released code - MQL5 programming forum (2017)
              Resources - MQL4 Reference

    Be aware that using resources is 40x times slower than using CIs directly.
              A custom indicator as a resource - MQL4 programming forum (2019)

    Also make use there are no spaces in the path.
              Getting error 4802 when loading custom indicator that loads another custom indicator with iCustom - Technical Indicators - MQL5 programming forum. (2020)

    Also Use of Resources in MQL5 - MQL5 Articles (2011)

  2. Second problem.

          Handle = iCustom(A_MySymbol, PERIOD_D1, "::Indicators\\A");
        ⋮
       int copy = CopyBuffer(Handle, …

    Perhaps you should read the manual, especially the examples.
       How To Ask Questions The Smart Way. (2004)
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

    They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
              Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
              Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
              How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020)
              How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020)
              MQL5 for Newbies: Guide to Using Technical Indicators in Expert Advisors - MQL5 Articles (2010)
              How to call indicators in MQL5 - MQL5 Articles (2010)

 
Paul Anscombe #:

Below with changes highlighted.

Note you will always be getting the 4802 error because you are running it from OnInit() as already stated. I moved your call to OnCalculate() therefore it will run every tick.


File B

File C

That was good and worked fine, now Can you say if I need a data of A.ex5 in OnInit() part of B.ex5 what should I do?!

I have Global parameter that I don't want to calculate it on every ticket I just need to calculate it once in OnInnit(), and it's value effects on OnCalculate() part, so I have to calculate it first in  OnInnit() then use it on  OnCalculate() part
 
William Roeder #:
They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.

if I need a data of A.ex5 in OnInit() part of B.ex5 what should I do?!

I have Global parameter that I don't want to calculate it on every ticket I just need to calculate it once in OnInnit(), and it's value effects on OnCalculate() part, so I have to calculate it first in  OnInnit() then use it on  OnCalculate() part


 
Asa Sh #:

That was good and worked fine, now Can you say if I need a data of A.ex5 in OnInit() part of B.ex5 what should I do?!

I have Global parameter that I don't want to calculate it on every ticket I just need to calculate it once in OnInnit(), and it's value effects on OnCalculate() part, so I have to calculate it first in  OnInnit() then use it on  OnCalculate() part

I'll assume you meant thanks!

Nobody can tell you what to do with your data requests because we do not know your overall design.... and the problem is nor do you, and you are insisting on complicating it with includes, and resources, and data access from OnInit when your knowledge and coding skills are not at that level. It has taken 18 posts to get you to accept what I told you in post 6. and the level of complexity you think you need requires somebody that knows how to design and code.

 
Paul Anscombe #:

I'll assume you meant thanks!

Nobody can tell you what to do with your data requests because we do not know your overall design.... and the problem is nor do you, and you are insisting on complicating it with includes, and resources, and data access from OnInit when your knowledge and coding skills are not at that level. It has taken 18 posts to get you to accept what I told you in post 6. and the level of complexity you think you need requires somebody that knows how to design and code.

oh god, I'm really sorry if I bother you, I really don't mean that, I'm so sorry and I'm really thankful to you, I mean it.

look, I have wrote this codes in MT4 and it works fine, but it doesn't same in MT5, I don't know MT5 a lot I want to have least change on my code.

again I don't mean what you felt.