iTriX on custom symbol

 

I try to write indicator - iTriX on custom symbol

The problem is - each time I switch time frames iTriX chenges charts, doesn't seem to work - sam time working, other times not working!..

Image of two trix (working, not working)

//--- input parameters
input int      Period=8;
//--- indicator buffers
double         TRIX2[];

string SName="A001";
bool RunOnce=false;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,TRIX2,INDICATOR_DATA);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 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[])
  {
//---

   if(RunOnce)
      return(-1);
   RunOnce=true;


   CustomSymbolDelete(SName);

   CustomSymbolCreate(SName,"\\Test\\");

   MqlRates rates[];
   ArraySetAsSeries(rates,true);

   int copied=CopyRates(Symbol(),0,0,Bars(_Symbol,_Period),rates);

   int check_int = CustomRatesUpdate(SName,rates);

   int handle=iTriX(SName,0,Period,PRICE_MEDIAN);

   CopyBuffer(handle,0,0,Bars(_Symbol,_Period),TRIX2);

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

Updated code (the same code only with property) :

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots   1
//--- plot TRIX22
#property indicator_label1  "TRIX22"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrDarkKhaki
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- input parameters
input int      InpPeriod=8;
//--- indicator buffers
double         TRIX22_Buffer[];

string         SName="A001";
bool           RunOnce=false;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,TRIX22_Buffer,INDICATOR_DATA);

   CustomSymbolDelete(SName);

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 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[])
  {
//---

   if(RunOnce)
      return(0);
   RunOnce=true;

   CustomSymbolDelete(SName);

   CustomSymbolCreate(SName,"\\Test\\");


   MqlRates rates[];
   ArraySetAsSeries(rates,true);

   int copied=CopyRates(Symbol(),0,0,Bars(_Symbol,_Period),rates);

   int check_int = CustomRatesUpdate(SName,rates);

   int handle=iTriX(SName,0,InpPeriod,PRICE_MEDIAN);

   CopyBuffer(handle,0,0,Bars(_Symbol,_Period),TRIX22_Buffer);

//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
   int handle=iTriX(SName,0,InpPeriod,PRICE_MEDIAN);

   CopyBuffer(handle,0,0,Bars(_Symbol,_Period),TRIX22_Buffer);

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)

 
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.

Thankyou for answering, I move handle to OnInit and clean the code, but it didnt solve the problem, indicator is emty, not working - now I getting 4806 error on a custom symbol:

2023.06.21 20:35:48.960 TRIX22 (test7Symbol,M15)        handle1=iTriX(SName,0,InpPeriod,PRICE_MEDIAN); >> 5304
2023.06.21 20:35:48.960 TRIX22 (test7Symbol,M15)        CopyBuffer(handle1,0,0,Bars(_Symbol,_Period),TRIX22_Buffer); >> 4806

ERR_INDICATOR_DATA_NOT_FOUND

4806

Requested data not found


On  handle2 - curent Symbol() ther is no error, error hapens only on a custom symbol  -  on handle1 :

2023.06.21 20:19:20.042 TRIX22 (test7Symbol,M15)        handle2=iTriX(_Symbol,0,InpPeriod,PRICE_MEDIAN); >> 5304
2023.06.21 20:19:20.042 TRIX22 (test7Symbol,M15)        CopyBuffer(handle2,0,0,Bars(_Symbol,_Period),TRIX22_Buffer); >> 5304


Updated code:

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots   1
//--- plot TRIX22
#property indicator_label1  "TRIX22"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrDarkKhaki
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- input parameters
input int      InpPeriod=8;
//--- indicator buffers
double         TRIX22_Buffer[];

string         SName="A001";
bool           RunOnce=false;
int            handle1,handle2;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,TRIX22_Buffer,INDICATOR_DATA);

//--- sets drawing line to empty value
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);

   CustomSymbolDelete(SName);

   CustomSymbolCreate(SName,"\\Test\\");

   MqlRates rates[];
   ArraySetAsSeries(rates,true);

   int copied=CopyRates(Symbol(),0,0,Bars(_Symbol,_Period),rates);

   int check_int = CustomRatesUpdate(SName,rates);


   Print("==================== "+TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS)+" ====================");

   handle1=iTriX(SName,0,InpPeriod,PRICE_MEDIAN);
   Print("handle1=iTriX(SName,0,InpPeriod,PRICE_MEDIAN); >> ",GetLastError());

   handle2=iTriX(_Symbol,0,InpPeriod,PRICE_MEDIAN);
   Print("handle2=iTriX(_Symbol,0,InpPeriod,PRICE_MEDIAN); >> ",GetLastError());

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 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[])
  {
//---

   if(!RunOnce)
     {
      RunOnce=true;
      //ArrayInitialize(TRIX22_Buffer,EMPTY_VALUE);
      ArrayInitialize(TRIX22_Buffer,0);
     }

   CopyBuffer(handle1,0,0,Bars(_Symbol,_Period),TRIX22_Buffer);
   Print("CopyBuffer(handle1,0,0,Bars(_Symbol,_Period),TRIX22_Buffer); >> ",GetLastError());

//CopyBuffer(handle2,0,0,Bars(_Symbol,_Period),TRIX22_Buffer);
//Print("CopyBuffer(handle2,0,0,Bars(_Symbol,_Period),TRIX22_Buffer); >> ",GetLastError());

   ArraySetAsSeries(TRIX22_Buffer,true);
   Print("ArraySetAsSeries(TRIX22_Buffer,true); >> ",GetLastError());

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


=============================

Update:

It start working after I presed refresh button!..