Problem accessing particular buffer information

 

Ok, so this is the latest in my ongoing endeavor to learn MQL and to write my indicator.

I have indeed more or less learned how prev_calulated and rates_total and buffers work. More or less. A big thank you to Vlad for the time he took to give me some code to analyze. It really helped me understand how an indicator works and saved me a very long time of trial and error.

The problem:

I'm trying to access the buffer index that controls the buy and sell symbols on the "Kolier Super Trend." It seems so simple when I look at the data window. The square for a buy signal should be index 2 and for sell it should be index 3. When I mouse over the bars, it looks clear that those are the right indexes. But when I print them out I get inconsistent results.

I've been mulling over this particular problem for about 3 days now and I have run through the debugging gambit. I've printed out variables at every place I can think of and rewritten the code several times. I've found errors in my code that I thought was the issue and ended up not helping in the least.

Finally I wrote a test indicator which had ONLY that indicator in it so I could just get rid of all of the extra code and look at it in isolation. . . Same results.

I have it set to print out the KST index for the last bar and the information seems completely inconsistent when I mouse over it in the data window. I watched it go through 3 trend changes and in all that time the printed information said that the first index was the only one that wasn't 0.0. Then sometimes the print out would say one thing about a bar and change if I recompiled even though I changed nothing.

Errors:

None, not even a warning

The Code:

#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window

#property indicator_buffers 6
#property indicator_plots   2

//--- Plot Up
#property indicator_label1  "Up"
#property indicator_type1   DRAW_ARROW
#property indicator_color1  clrRoyalBlue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  3

//--- Plot Down
#property indicator_label2  "Down"
#property indicator_type2   DRAW_ARROW
#property indicator_color2  clrCrimson
#property indicator_style2  STYLE_SOLID
#property indicator_width2  3

input int
   stPeriod = 14;

input double
   stMult = 1.25;
   
//--- Indicator Buffers
double
   upBuffer[],
   downBuffer[],
   stBuffer0[],
   stBuffer1[],
   stBuffer2[],
   stBuffer3[];   
   
//--- Handle

int
   stHandle;
   
bool
   initError = false;

int
   barsCalculated = 0,
   tradesLong = 0,
   tradesShort = 0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0, upBuffer, INDICATOR_DATA);
   SetIndexBuffer(1, downBuffer, INDICATOR_DATA);
   SetIndexBuffer(2, stBuffer0, INDICATOR_CALCULATIONS);
   SetIndexBuffer(3, stBuffer1, INDICATOR_CALCULATIONS);
   SetIndexBuffer(4, stBuffer2, INDICATOR_CALCULATIONS);
   SetIndexBuffer(5, stBuffer3, INDICATOR_CALCULATIONS);   
   
   PlotIndexSetInteger(0, PLOT_ARROW, 159);
   PlotIndexSetInteger(1, PLOT_ARROW, 159);
   
   PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, 0.0);
   PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, 0.0);
   
   stHandle = iCustom(Symbol(), Period(), "Kolier Super Trend",
                              stPeriod,
                              stMult);
                              
   if( stHandle == INVALID_HANDLE ){

      PrintFormat( "Failed to create handle of the iCustom indicator for the symbol %s/%s, error code %d",
                  Symbol(),
                  EnumToString(Period()),
                  GetLastError());
      
      initError = true;
      return(INIT_SUCCEEDED);
     }
   
//---
   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(initError || rates_total<3)
      return(0);
      
   //---
   int valuesToCopy;

   int calculated = BarsCalculated(stHandle);
   
   if(calculated <= 0){
      PrintFormat("BarsCalculated returned %d, error code %d", calculated, GetLastError());
      return(0);
   }

   if(prev_calculated == 0 || calculated != barsCalculated || rates_total > prev_calculated + 1){

      if(calculated > rates_total)   
         valuesToCopy = rates_total;      
      else      
         valuesToCopy = calculated;
         
     } else {

      valuesToCopy = (rates_total - prev_calculated) + 1;
      
     }

   if(!fillStArrayFromBuffer(stBuffer0, stBuffer1, stBuffer2, stBuffer3, stHandle, valuesToCopy))
      return(0);

   barsCalculated = calculated;   
   
   //--- main loop
   int limit = prev_calculated - 1;
   
   if(prev_calculated == 0){
      upBuffer[0] = 0.0;
      downBuffer[0] = 0.0;
      limit = 1;
   }  
   
   for(int i = limit; i < rates_total; i++){

      upBuffer[i] = 0.0;
      downBuffer[i] = 0.0;
      int in = 2;

      downBuffer[rates_total - (in - 1)] = 0.0;
      downBuffer[rates_total - in] = low[rates_total - in];
      Print("Buffer 0: ", stBuffer0[rates_total - in], " || Buffer 1: ", stBuffer1[rates_total - in], " || Buffer 2: ", stBuffer2[rates_total - in], " || Buffer 3: ", stBuffer3[rates_total - in]);
      
      if(stBuffer3[rates_total - in] > 0){
         //Alert("Fired");
      }
      
      if(stBuffer2[rates_total - in] > 0){
         //Alert("Buy Fired");
      }
   }   

   return(rates_total);
  }
//+------------------------------------------------------------------+

bool fillStArrayFromBuffer(double &buffer_0[], double &buffer_1[], double &buffer_2[], double &buffer_3[], int mSTHandle, int amount){
   ResetLastError();   
      
   if(CopyBuffer(mSTHandle, 0, 0, amount, buffer_0) != amount){
      PrintFormat("Failed to copy data from the ST indicator (Buy), error code %d",GetLastError());
      return(false);
   }
   
   if(CopyBuffer(mSTHandle, 1, 0, amount, buffer_1) != amount){
      PrintFormat("Failed to copy data from the ST indicator (Sell), error code %d",GetLastError());
      return(false);
   }
   
   if(CopyBuffer(mSTHandle, 2, 0, amount, buffer_2) != amount){
      PrintFormat("Failed to copy data from the ST indicator (Buy), error code %d",GetLastError());
      return(false);
   }
   
   if(CopyBuffer(mSTHandle, 3, 0, amount, buffer_3) != amount){
      PrintFormat("Failed to copy data from the ST indicator (Sell), error code %d",GetLastError());
      return(false);
   }
   
   return(true);
}

The Indicator:

***

I greatly appreciate anyone who is willing to give me some advice. As always, I'm willing to do the leg and head work. Not looking for handouts, just a helping hand.

 
Tristen Shaw: It seems so simple when I look at the data window. The square for a buy signal should be index 2 and for sell it should be index 3.
#property indicator_label1  "Up"#property indicator_label2  "Down"

Up is zero and down is one.

 
William Roeder #:

Up is zero and down is one.

When I change the labels like that it gives an error:

'indicator_label0' - invalid property index     Super Trend Test.mq5    15      11

The way they are at the moment is how they were labeled in Vlads example.

Unless you are saying that those are the indexes that I am looking for. In which case, those are the buffers that I am using to draw my dots when I get all the needed signals, not the buffers of the Kolier Super Trend which I'm accessing through iCustom.
 
Tristen Shaw #: When I change the labels like that it gives an error:
You don't change the labels, you change the index used with your iCustom/copyX calls.
Tristen Shaw: I'm trying to access the buffer index
 
William Roeder #:
You don't change the labels, you change the index used with your iCustom/copyX calls.

My up and down buffers are not connected to the iCustom calls. I write to those buffers to make a buy or sell signal based on multiple indicators, of which KST is only one of.

It's these that I want to access:

Which is accessed here:

bool fillStArrayFromBuffer(double &buffer_0[], double &buffer_1[], double &buffer_2[], double &buffer_3[], int mSTHandle, int amount){
   ResetLastError();   
      
   if(CopyBuffer(mSTHandle, 0, 0, amount, buffer_0) != amount){
      PrintFormat("Failed to copy data from the ST indicator (Buy), error code %d",GetLastError());
      return(false);
   }
   
   if(CopyBuffer(mSTHandle, 1, 0, amount, buffer_1) != amount){
      PrintFormat("Failed to copy data from the ST indicator (Sell), error code %d",GetLastError());
      return(false);
   }
   
   if(CopyBuffer(mSTHandle, 2, 0, amount, buffer_2) != amount){
      PrintFormat("Failed to copy data from the ST indicator (Buy), error code %d",GetLastError());
      return(false);
   }
   
   if(CopyBuffer(mSTHandle, 3, 0, amount, buffer_3) != amount){
      PrintFormat("Failed to copy data from the ST indicator (Sell), error code %d",GetLastError());
      return(false);
   }
   
   return(true);
}

Loading the values of KST into the 4 different stBuffer arrays. I then print out those buffers in my for loop for specific bars to try and see which ones I should be using. And that is where I run into my afore mentioned problems.

 

I have continued to work on the issue. Fixed a few minor problems and still have not yet come up with a solution.

I have it coded so that, in theory it should only write a dot to my up and down buffers when a buy or sell signal comes up on the KST. But I get stuff like this. And I have tried using all 4 buffers. None of them seem to solely occur on the square signals. Even though from mousing over the signals, the "Buy SuperTrend" and "Sell SuperTrend" buffers should work.