Issues with arrow plotting

 

Hi, 

I tried to import entry points (buy/sell) calculated externally from a csv file and plot on a chart. However the data reading (for newly updated data in CSV) from the CSV/ arrow plotting only works whenever I refresh the chart/ switch to different timeframe manually. 

I have been trying to solve this issues for quite sometime. Really appreciate if anybody could give me a hand on this. 

Thanks very much!

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
//---- Plot Buy Arrow
#property indicator_label1  "SB4H"
#property indicator_type1   DRAW_ARROW
#property indicator_color1  clrLime
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2
//---- Plot Sell Arrow
#property indicator_label2  "SS4H"
#property indicator_type2   DRAW_ARROW
#property indicator_color2  clrDarkOrange
#property indicator_style2  STYLE_SOLID
#property indicator_width2  2
//---- Parameters for Data Reading
input string InpFileName="H4_Rev12.csv";  // file name
input string InpDirectoryName="Data"; // directory name
 
//---- Global Variables
int      ind=0;         // index
double   buybuff[];     // indicator buffers of up arrows
double   sellbuff[];    // indicator buffer of down arrows
bool     signal_buff[]; // signal array (true - buy, false - sell)
datetime time_buff[];   // array of signals' arrival time
int      size=0;        // size of signal arrays

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
  //--- Extracting information from file
   ResetLastError();
   int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_ANSI|FILE_CSV,';');
   if(file_handle!=INVALID_HANDLE)
     {
      //--- Extract file size information
      size=(int)FileReadNumber(file_handle);

      //--- Allocate memory for the arrays
      ArrayResize(signal_buff,size);
      ArrayResize(time_buff,size); 
      //--- Read time and signal data from the file
      for(int i=0;i<size;i++)
         {

         //--- Signal time array
         time_buff[i]=FileReadDatetime(file_handle);
         //--- Signal value array
         signal_buff[i]=FileReadBool(file_handle);
         //Alert(time_buff[i]," ",signal_buff[i]); // Print array values
         }
         
      //--- Close the file
      FileClose(file_handle);
     }
   else
     {
      PrintFormat("Failed to open %s file, Error code = %d",InpFileName,GetLastError());
      return(INIT_FAILED);
     }
      


//--- Indicator Buffers Mapping
   //--- binding the arrays
   SetIndexBuffer(0,buybuff,INDICATOR_DATA);
   SetIndexBuffer(1,sellbuff,INDICATOR_DATA);
   //SetIndexBuffer(2,sellbuff,INDICATOR_DATA);
//--- Set the symbol code for drawing in PLOT_ARROW
   PlotIndexSetInteger(0,PLOT_ARROW,233);
   PlotIndexSetInteger(1,PLOT_ARROW,234);
//--- Set the vertical shift of arrows in pixels
   PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,20);
   PlotIndexSetInteger(1,PLOT_ARROW_SHIFT,-20);
//--- Set the indicator values that will not be seen on the chart
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);
   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0);
//---- Create label to display in DataWindow
   PlotIndexSetString(0,PLOT_LABEL,"SB4H");
   PlotIndexSetString(1,PLOT_LABEL,"SS4H");
//---
   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[])
  {
      ArraySetAsSeries(time,false);
      ArraySetAsSeries(low,false);
      ArraySetAsSeries(high,false);
      ArraySetAsSeries(close,false);
//--- the loop for the bars that have not been handled yet

     for(int i=prev_calculated;i<rates_total;i++)
     {
      //--- 0 by default
      buybuff[i]=0;
      //Alert(time_buff[i]," ",buybuff[i]);
      sellbuff[i]=0;
      //Alert(time_buff[i]," ",sellbuff[i]);
      //--- check if any data is still present
      if(ind<size)
        {
         for(int j=ind;j<size;j++)
           {
            //--- if dates coincide, use the value from the file
            if(time[i]==time_buff[j])
              {
               //--- draw the arrow according to the signal
               
               if(signal_buff[j])
                  buybuff[i]=low[i];
                  else
                  sellbuff[i]=high[i];
                 
               ind=j+1;
               Print("ind",ind);
               break;
              }
           }
        }
     }
   Print("PC",prev_calculated,"RT",rates_total);
   return(rates_total);
  }
 
Use ChartRedraw(0) after plotting loop
edit: For an indicator loop, use it when rates_total == prev_calculated. You should however use this hack sparingly, as it adds extra overhead to your execution
Reason: