//+------------------------------------------------------------------+
//|                                                RVI Thread BT.mq5 |
//|                             Copyright © 2004-2026, Forex-Station |
//|                                    https://www.forex-station.com |
//+------------------------------------------------------------------+
#property copyright   "Copyright © 2004-2026, Forex-Station"
#property link        "https://forex-station.com/post1295586926.html#p1295586926"
#property version     "1.00"
#property description "RVI Thread BT with On/Off Toggle Button"

#property indicator_separate_window
#property indicator_buffers 13
#property indicator_plots   5

//--- Plot 1: RVI 11 Signal
#property indicator_label1  "RVI (11) Signal"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrDodgerBlue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

//--- Plot 2: RVI 12 Signal
#property indicator_label2  "RVI (12) Signal"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrDodgerBlue
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1

//--- Plot 3: RVI 13 Signal
#property indicator_label3  "RVI (13) Signal"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrDodgerBlue
#property indicator_style3  STYLE_SOLID
#property indicator_width3  1

//--- Plot 4: RVI 10 Signal
#property indicator_label4  "RVI (10) Signal"
#property indicator_type4   DRAW_LINE
#property indicator_color4  clrDodgerBlue
#property indicator_style4  STYLE_SOLID
#property indicator_width4  1

//--- Plot 5: RVI 10 Main
#property indicator_label5  "RVI (10) Main"
#property indicator_type5   DRAW_LINE
#property indicator_color5  clrGold
#property indicator_style5  STYLE_SOLID
#property indicator_width5  3

#property indicator_level1     0.0
#property indicator_levelcolor clrSilver
#property indicator_levelstyle STYLE_DOT

//--- input parameters
input group "=== RVI Thread Settings ==="
input int                InpRVIPeriod10        = 10;                 // Base RVI Period
input int                InpRVIPeriod11        = 11;                 // Thread RVI Period 1
input int                InpRVIPeriod12        = 12;                 // Thread RVI Period 2
input int                InpRVIPeriod13        = 13;                 // Thread RVI Period 3
input color              InpColorThread        = clrDodgerBlue;      // Thread Lines Color
input color              InpColorMain          = clrGold;            // Base Main Line Color
input int                InpWidthMain          = 3;                  // Base Main Line Width

//Forex-Station copy & paste code; Button code start 11
input group "=== Toggle Button Settings ==="
input string             button_note1          = "------------------------------"; // ------------------------------
input int                btn_Subwindow         = 0;                  // What window to put the button on (<0 = subwindow)
input ENUM_BASE_CORNER   btn_corner            = CORNER_LEFT_UPPER;  // chart btn_corner for anchoring
input string             btn_text              = "RVI: ON";          // Display label
input string             btn_Font              = "Arial";            // button font name
input int                btn_FontSize          = 9;                  // btn__font size
input color              btn_text_ON_color     = clrLime;            // ON color when the button is turned on
input color              btn_text_OFF_color    = clrRed;             // OFF color when the button is turned off
input color              btn_background_color  = clrDimGray;         // background color of the button
input color              btn_border_color      = clrBlack;           // border color the button
input int                button_x              = 20;                 // Horizontal location
input int                button_y              = 80;                 // Vertical location
input int                btn_Width             = 80;                 // btn__width
input int                btn_Height            = 20;                 // btn__height
input string             UniqueButtonID        = "RVIThreadBT";      // Unique Button ID                      
input string             button_note2          = "------------------------------"; // ------------------------------
//Forex-Station copy & paste code; Button code end 11

//+------------------------------------------------------------------+
//| Button State Tracking Variables                                  |
//+------------------------------------------------------------------+
bool    IndicatorEnabled = true;
bool    IndicatorHidden  = false; // True when indicator is turned OFF via toggle button
string  ToggleButtonName;
string  g_gvHidden;
string  g_prefix;
string  IndicatorName, IndicatorObjPrefix, buttonId;

//--- indicator display buffers
double  ExtRVI11Signal[];
double  ExtRVI12Signal[];
double  ExtRVI13Signal[];
double  ExtRVI10Signal[];
double  ExtRVI10Main[];

//--- internal calculation buffers
double  ExtRVI10MainCalc[];
double  ExtRVI10SigCalc[];
double  ExtRVI11MainCalc[];
double  ExtRVI11SigCalc[];
double  ExtRVI12MainCalc[];
double  ExtRVI12SigCalc[];
double  ExtRVI13MainCalc[];
double  ExtRVI13SigCalc[];

//+------------------------------------------------------------------+
//| Helper: Get timeframe as string                                  |
//+------------------------------------------------------------------+
string GetStringTimeframe(ENUM_TIMEFRAMES timeframe)
{
   return(StringSubstr(EnumToString(timeframe), 7));
}

//+------------------------------------------------------------------+
//| Helper to resolve button text based on state                     |
//+------------------------------------------------------------------+
string GetButtonText(bool isEnabled)
{
   string t = btn_text;
   if(isEnabled)
   {
      if(StringFind(t, "OFF") >= 0) StringReplace(t, "OFF", "ON");
      else if(StringFind(t, "Off") >= 0) StringReplace(t, "Off", "On");
      else if(StringFind(t, "off") >= 0) StringReplace(t, "off", "on");
   }
   else
   {
      if(StringFind(t, "ON") >= 0) StringReplace(t, "ON", "OFF");
      else if(StringFind(t, "On") >= 0) StringReplace(t, "On", "Off");
      else if(StringFind(t, "on") >= 0) StringReplace(t, "on", "off");
   }
   return t;
}

//+------------------------------------------------------------------+
//| Create Toggle Button                                             |
//+------------------------------------------------------------------+
void CreateToggleButton()
{
   int target_window = btn_Subwindow;
   if(target_window < 0)
   {
      target_window = ChartWindowFind(0, IndicatorName);
      if(target_window < 0) target_window = 0;
   }

   if(ObjectFind(0, buttonId) >= 0)
   {
      if(ObjectFind(0, buttonId) != target_window && target_window > 0)
         ObjectDelete(0, buttonId);
   }

   if(ObjectFind(0, buttonId) < 0)
   {
      int y_pos = button_y;
      int obj_total = ObjectsTotal(0, -1, OBJ_BUTTON);
      bool collision = true;
      while(collision)
      {
         collision = false;
         for(int i = 0; i < obj_total; i++)
         {
            string name = ObjectName(0, i, -1, OBJ_BUTTON);
            if(name == buttonId) continue;
            if((ENUM_BASE_CORNER)ObjectGetInteger(0, name, OBJPROP_CORNER) != btn_corner) continue;
            if(ObjectGetInteger(0, name, OBJPROP_XDISTANCE) == button_x &&
               ObjectGetInteger(0, name, OBJPROP_YDISTANCE) == y_pos)
            {
               y_pos += btn_Height + 4;
               collision = true;
               break;
            }
         }
      }

      ObjectCreate(0, buttonId, OBJ_BUTTON, target_window, 0, 0);

      ObjectSetInteger(0, buttonId, OBJPROP_CORNER,       btn_corner);
      ObjectSetInteger(0, buttonId, OBJPROP_XDISTANCE,    button_x);
      ObjectSetInteger(0, buttonId, OBJPROP_YDISTANCE,    y_pos);
      ObjectSetInteger(0, buttonId, OBJPROP_XSIZE,        btn_Width);
      ObjectSetInteger(0, buttonId, OBJPROP_YSIZE,        btn_Height);
      ObjectSetString (0, buttonId, OBJPROP_FONT,         btn_Font);
      ObjectSetInteger(0, buttonId, OBJPROP_FONTSIZE,     btn_FontSize);
      ObjectSetInteger(0, buttonId, OBJPROP_BGCOLOR,      btn_background_color);
      ObjectSetInteger(0, buttonId, OBJPROP_BORDER_COLOR, btn_border_color);
      ObjectSetInteger(0, buttonId, OBJPROP_BORDER_TYPE,  BORDER_RAISED);
      ObjectSetInteger(0, buttonId, OBJPROP_SELECTABLE,   false);
      ObjectSetInteger(0, buttonId, OBJPROP_HIDDEN,       true);
      ObjectSetInteger(0, buttonId, OBJPROP_TIMEFRAMES,   OBJ_ALL_PERIODS);
      ObjectSetInteger(0, buttonId, OBJPROP_ZORDER,       100);
   }

   UpdateToggleButton();
}

//+------------------------------------------------------------------+
//| Update Toggle Button Appearance                                  |
//+------------------------------------------------------------------+
void UpdateToggleButton()
{
   if(ObjectFind(0, buttonId) < 0) return;

   IndicatorEnabled = !IndicatorHidden;

   string labelText  = GetButtonText(IndicatorEnabled);
   color  textColor  = IndicatorEnabled ? btn_text_ON_color : btn_text_OFF_color;

   ObjectSetString (0, buttonId, OBJPROP_TEXT,         labelText);
   ObjectSetString (0, buttonId, OBJPROP_FONT,         btn_Font);
   ObjectSetInteger(0, buttonId, OBJPROP_FONTSIZE,     btn_FontSize);
   ObjectSetInteger(0, buttonId, OBJPROP_COLOR,        textColor);
   ObjectSetInteger(0, buttonId, OBJPROP_BGCOLOR,      btn_background_color);
   ObjectSetInteger(0, buttonId, OBJPROP_BORDER_COLOR, btn_border_color);
   ObjectSetInteger(0, buttonId, OBJPROP_CORNER,       btn_corner);
   ObjectSetInteger(0, buttonId, OBJPROP_XDISTANCE,    button_x);
   ObjectSetInteger(0, buttonId, OBJPROP_XSIZE,        btn_Width);
   ObjectSetInteger(0, buttonId, OBJPROP_YSIZE,        btn_Height);
   ObjectSetInteger(0, buttonId, OBJPROP_STATE,        IndicatorEnabled);
   ObjectSetInteger(0, buttonId, OBJPROP_ZORDER,       100);

   ChartRedraw(0);
}

//+------------------------------------------------------------------+
//| Ensure all buttons on chart have higher ZORDER                   |
//+------------------------------------------------------------------+
void BringButtonsToFront()
{
   int total = ObjectsTotal(0, -1, OBJ_BUTTON);
   for(int i = 0; i < total; i++)
   {
      string name = ObjectName(0, i, -1, OBJ_BUTTON);
      if(name != "")
      {
         if(ObjectGetInteger(0, name, OBJPROP_ZORDER) < 100)
            ObjectSetInteger(0, name, OBJPROP_ZORDER, 100);
      }
   }
}

//+------------------------------------------------------------------+
//| Apply Plot Styles and Buffers based on toggle state              |
//+------------------------------------------------------------------+
void ApplyPlotsStyle()
{
   bool show = !IndicatorHidden;

   if(show)
   {
      PlotIndexSetInteger(0, PLOT_DRAW_TYPE, DRAW_LINE);
      PlotIndexSetInteger(0, PLOT_LINE_COLOR, InpColorThread);
      PlotIndexSetInteger(0, PLOT_LINE_WIDTH, 1);
      PlotIndexSetInteger(0, PLOT_SHOW_DATA,  true);
      PlotIndexSetString (0, PLOT_LABEL,      "RVI (" + IntegerToString(InpRVIPeriod11) + ") Signal");

      PlotIndexSetInteger(1, PLOT_DRAW_TYPE, DRAW_LINE);
      PlotIndexSetInteger(1, PLOT_LINE_COLOR, InpColorThread);
      PlotIndexSetInteger(1, PLOT_LINE_WIDTH, 1);
      PlotIndexSetInteger(1, PLOT_SHOW_DATA,  true);
      PlotIndexSetString (1, PLOT_LABEL,      "RVI (" + IntegerToString(InpRVIPeriod12) + ") Signal");

      PlotIndexSetInteger(2, PLOT_DRAW_TYPE, DRAW_LINE);
      PlotIndexSetInteger(2, PLOT_LINE_COLOR, InpColorThread);
      PlotIndexSetInteger(2, PLOT_LINE_WIDTH, 1);
      PlotIndexSetInteger(2, PLOT_SHOW_DATA,  true);
      PlotIndexSetString (2, PLOT_LABEL,      "RVI (" + IntegerToString(InpRVIPeriod13) + ") Signal");

      PlotIndexSetInteger(3, PLOT_DRAW_TYPE, DRAW_LINE);
      PlotIndexSetInteger(3, PLOT_LINE_COLOR, InpColorThread);
      PlotIndexSetInteger(3, PLOT_LINE_WIDTH, 1);
      PlotIndexSetInteger(3, PLOT_SHOW_DATA,  true);
      PlotIndexSetString (3, PLOT_LABEL,      "RVI (" + IntegerToString(InpRVIPeriod10) + ") Signal");

      PlotIndexSetInteger(4, PLOT_DRAW_TYPE, DRAW_LINE);
      PlotIndexSetInteger(4, PLOT_LINE_COLOR, InpColorMain);
      PlotIndexSetInteger(4, PLOT_LINE_WIDTH, InpWidthMain);
      PlotIndexSetInteger(4, PLOT_SHOW_DATA,  true);
      PlotIndexSetString (4, PLOT_LABEL,      "RVI (" + IntegerToString(InpRVIPeriod10) + ") Main");
   }
   else
   {
      for(int p = 0; p < 5; p++)
      {
         PlotIndexSetInteger(p, PLOT_DRAW_TYPE, DRAW_NONE);
         PlotIndexSetInteger(p, PLOT_LINE_COLOR, clrNONE);
         PlotIndexSetInteger(p, PLOT_SHOW_DATA,  false);
         PlotIndexSetString (p, PLOT_LABEL,      "");
      }
   }

   int sz = ArraySize(ExtRVI10MainCalc);
   if(sz > 0)
   {
      if(show)
      {
         for(int i = 0; i < sz; i++)
         {
            ExtRVI11Signal[i] = ExtRVI11SigCalc[i];
            ExtRVI12Signal[i] = ExtRVI12SigCalc[i];
            ExtRVI13Signal[i] = ExtRVI13SigCalc[i];
            ExtRVI10Signal[i] = ExtRVI10SigCalc[i];
            ExtRVI10Main[i]   = ExtRVI10MainCalc[i];
         }
      }
      else
      {
         for(int i = 0; i < sz; i++)
         {
            ExtRVI11Signal[i] = EMPTY_VALUE;
            ExtRVI12Signal[i] = EMPTY_VALUE;
            ExtRVI13Signal[i] = EMPTY_VALUE;
            ExtRVI10Signal[i] = EMPTY_VALUE;
            ExtRVI10Main[i]   = EMPTY_VALUE;
         }
      }
   }
}

//+------------------------------------------------------------------+
//| Toggle Indicator Visibility                                      |
//+------------------------------------------------------------------+
void ToggleIndicatorVisibility()
{
   IndicatorHidden  = !IndicatorHidden;
   IndicatorEnabled = !IndicatorHidden;

   if(g_gvHidden != "")
      GlobalVariableSet(g_gvHidden, IndicatorHidden ? 1.0 : 0.0);

   ApplyPlotsStyle();
   UpdateToggleButton();
   BringButtonsToFront();
   ChartRedraw(0);
}

//+------------------------------------------------------------------+
//| Helper to calculate RVI Main and Signal for a given period       |
//+------------------------------------------------------------------+
void CalculateRVIPair(const int period,
                      const int rates_total,
                      const int prev_calculated,
                      const double &open[],
                      const double &high[],
                      const double &low[],
                      const double &close[],
                      double &main_buf[],
                      double &sig_buf[])
{
   if(period < 1 || rates_total <= period + 8)
      return;

   int start = period + 2;
   if(prev_calculated > period + 5)
      start = prev_calculated - 1;

   if(prev_calculated == 0)
   {
      for(int i = 0; i < period + 2; i++)
         main_buf[i] = EMPTY_VALUE;
      for(int i = 0; i < period + 5; i++)
         sig_buf[i] = EMPTY_VALUE;
   }

   // 1. Calculate Main RVI Line
   for(int i = start; i < rates_total && !IsStopped(); i++)
   {
      double sum_up   = 0.0;
      double sum_down = 0.0;
      for(int j = i; j > i - period; j--)
      {
         double value_up   = (close[j] - open[j]) + 2.0 * (close[j-1] - open[j-1]) + 2.0 * (close[j-2] - open[j-2]) + (close[j-3] - open[j-3]);
         double value_down = (high[j]  - low[j])  + 2.0 * (high[j-1]  - low[j-1])  + 2.0 * (high[j-2]  - low[j-2])  + (high[j-3]  - low[j-3]);
         sum_up   += value_up;
         sum_down += value_down;
      }
      if(sum_down != 0.0)
         main_buf[i] = sum_up / sum_down;
      else
         main_buf[i] = sum_up;
   }

   // 2. Calculate Signal Line
   int start_sig = period + 5;
   if(prev_calculated > period + 8)
      start_sig = prev_calculated - 1;

   for(int i = start_sig; i < rates_total && !IsStopped(); i++)
   {
      sig_buf[i] = (main_buf[i] + 2.0 * main_buf[i-1] + 2.0 * main_buf[i-2] + main_buf[i-3]) / 6.0;
   }
}

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   //--- indicator buffers mapping
   SetIndexBuffer(0, ExtRVI11Signal,   INDICATOR_DATA);
   SetIndexBuffer(1, ExtRVI12Signal,   INDICATOR_DATA);
   SetIndexBuffer(2, ExtRVI13Signal,   INDICATOR_DATA);
   SetIndexBuffer(3, ExtRVI10Signal,   INDICATOR_DATA);
   SetIndexBuffer(4, ExtRVI10Main,     INDICATOR_DATA);

   SetIndexBuffer(5,  ExtRVI10MainCalc, INDICATOR_CALCULATIONS);
   SetIndexBuffer(6,  ExtRVI10SigCalc,  INDICATOR_CALCULATIONS);
   SetIndexBuffer(7,  ExtRVI11MainCalc, INDICATOR_CALCULATIONS);
   SetIndexBuffer(8,  ExtRVI11SigCalc,  INDICATOR_CALCULATIONS);
   SetIndexBuffer(9,  ExtRVI12MainCalc, INDICATOR_CALCULATIONS);
   SetIndexBuffer(10, ExtRVI12SigCalc,  INDICATOR_CALCULATIONS);
   SetIndexBuffer(11, ExtRVI13MainCalc, INDICATOR_CALCULATIONS);
   SetIndexBuffer(12, ExtRVI13SigCalc,  INDICATOR_CALCULATIONS);

   IndicatorSetInteger(INDICATOR_DIGITS, 4);

   for(int p = 0; p < 5; p++)
      PlotIndexSetDouble(p, PLOT_EMPTY_VALUE, EMPTY_VALUE);

   PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, InpRVIPeriod11 + 5);
   PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, InpRVIPeriod12 + 5);
   PlotIndexSetInteger(2, PLOT_DRAW_BEGIN, InpRVIPeriod13 + 5);
   PlotIndexSetInteger(3, PLOT_DRAW_BEGIN, InpRVIPeriod10 + 5);
   PlotIndexSetInteger(4, PLOT_DRAW_BEGIN, InpRVIPeriod10 + 2);

   //--- Set up unique IDs and global variables for state tracking
   IndicatorObjPrefix = "__" + btn_text + "__";
   buttonId           = IndicatorObjPrefix + UniqueButtonID;
   ToggleButtonName   = buttonId;
   IndicatorName      = "RVI Thread (" + IntegerToString(InpRVIPeriod10) + ") to (" + IntegerToString(InpRVIPeriod13) + ")";
   g_prefix           = "RVIThread_BT_" + UniqueButtonID + "_";
   g_gvHidden         = g_prefix + IntegerToString(ChartID()) + "_Hidden";

   IndicatorSetString(INDICATOR_SHORTNAME, IndicatorName);

   //--- Clean up any old duplicate or phantom buttons
   int total_objs = ObjectsTotal(0, -1, OBJ_BUTTON);
   for(int i = total_objs - 1; i >= 0; i--)
   {
      string bName = ObjectName(0, i, -1, OBJ_BUTTON);
      if(StringFind(bName, UniqueButtonID) >= 0 || StringFind(bName, "RVIThread_BT") >= 0)
      {
         if(bName != buttonId)
            ObjectDelete(0, bName);
      }
   }

   //--- When parameters are changed by user in properties dialog, delete old global variable and respect inputs
   bool isParamChange = (UninitializeReason() == REASON_PARAMETERS);
   if(isParamChange && g_gvHidden != "")
   {
      GlobalVariableDel(g_gvHidden);
   }

   //--- Restore toggle state across timeframe/symbol changes
   bool hasSavedState = false;
   bool savedHidden   = false;

   if(!isParamChange)
   {
      if(ObjectFind(0, buttonId) >= 0)
      {
         string btnTxt = ObjectGetString(0, buttonId, OBJPROP_TEXT);
         long   btnSt  = ObjectGetInteger(0, buttonId, OBJPROP_STATE);

         if(StringFind(btnTxt, "OFF") >= 0 || StringFind(btnTxt, "Off") >= 0 || btnSt == 0)
            savedHidden = true;
         else if(StringFind(btnTxt, "ON") >= 0 || StringFind(btnTxt, "On") >= 0 || btnSt != 0)
            savedHidden = false;
         hasSavedState = true;
      }

      if(!hasSavedState && GlobalVariableCheck(g_gvHidden))
      {
         savedHidden   = (GlobalVariableGet(g_gvHidden) > 0.5);
         hasSavedState = true;
      }
   }

   if(hasSavedState)
      IndicatorHidden = savedHidden;
   else
      IndicatorHidden = false;

   IndicatorEnabled = !IndicatorHidden;

   if(g_gvHidden != "")
      GlobalVariableSet(g_gvHidden, IndicatorHidden ? 1.0 : 0.0);

   CreateToggleButton();
   BringButtonsToFront();
   ApplyPlotsStyle();

   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   if(reason == REASON_REMOVE || reason == REASON_CHARTCLOSE || reason == REASON_PARAMETERS)
   {
      ObjectDelete(0, buttonId);
      ObjectDelete(0, ToggleButtonName);
      if(g_gvHidden != "")
         GlobalVariableDel(g_gvHidden);
   }

   ChartRedraw(0);
}

//+------------------------------------------------------------------+
//| Custom indicator chart's event handler                           |
//+------------------------------------------------------------------+
void OnChartEvent(const int id, const long& lparam, const double& dparam, const string& sparam)
{
   if(id == CHARTEVENT_OBJECT_CLICK)
   {
      string altId = "_" + UniqueButtonID + IndicatorObjPrefix + "_BT_";
      if(sparam == buttonId || sparam == ToggleButtonName || sparam == altId)
      {
         ToggleIndicatorVisibility();
         return;
      }
   }

   if(id >= CHARTEVENT_CUSTOM)
      return;

   if(CHARTEVENT_CHART_CHANGE == id)
   {
      BringButtonsToFront();
   }
}

//+------------------------------------------------------------------+
//| Relative Vigor Index Thread calculation                          |
//+------------------------------------------------------------------+
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(prev_calculated <= 0)
   {
      CreateToggleButton();
      BringButtonsToFront();
      ApplyPlotsStyle();
   }

   int max_period = MathMax(InpRVIPeriod10, MathMax(InpRVIPeriod11, MathMax(InpRVIPeriod12, InpRVIPeriod13)));
   if(rates_total <= max_period + 8)
      return(0);

   // 1. Calculate all 4 RVI periods into calculation buffers
   CalculateRVIPair(InpRVIPeriod10, rates_total, prev_calculated, open, high, low, close, ExtRVI10MainCalc, ExtRVI10SigCalc);
   CalculateRVIPair(InpRVIPeriod11, rates_total, prev_calculated, open, high, low, close, ExtRVI11MainCalc, ExtRVI11SigCalc);
   CalculateRVIPair(InpRVIPeriod12, rates_total, prev_calculated, open, high, low, close, ExtRVI12MainCalc, ExtRVI12SigCalc);
   CalculateRVIPair(InpRVIPeriod13, rates_total, prev_calculated, open, high, low, close, ExtRVI13MainCalc, ExtRVI13SigCalc);

   // 2. Transfer to plot buffers according to visibility state
   int start = (prev_calculated > 0) ? prev_calculated - 1 : 0;
   if(!IndicatorHidden)
   {
      for(int i = start; i < rates_total && !IsStopped(); i++)
      {
         ExtRVI11Signal[i] = ExtRVI11SigCalc[i];
         ExtRVI12Signal[i] = ExtRVI12SigCalc[i];
         ExtRVI13Signal[i] = ExtRVI13SigCalc[i];
         ExtRVI10Signal[i] = ExtRVI10SigCalc[i];
         ExtRVI10Main[i]   = ExtRVI10MainCalc[i];
      }
   }
   else
   {
      for(int i = start; i < rates_total && !IsStopped(); i++)
      {
         ExtRVI11Signal[i] = EMPTY_VALUE;
         ExtRVI12Signal[i] = EMPTY_VALUE;
         ExtRVI13Signal[i] = EMPTY_VALUE;
         ExtRVI10Signal[i] = EMPTY_VALUE;
         ExtRVI10Main[i]   = EMPTY_VALUE;
      }
   }

   return(rates_total);
}
//+------------------------------------------------------------------+
