//+------------------------------------------------------------------+
//| 64_Andy_Kumo_Shifted_v1_01.mq5                                 |
//| Draws a shifted two-color cloud with visible boundary lines.     |
//+------------------------------------------------------------------+
#property copyright "FXおもしろラボ"
#property version   "1.01"
#property strict
#property description "Shifted two-color Andy Kumo cloud."

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_plots   3

#property indicator_label1 "Andy Kumo;Andy Kumo"
#property indicator_type1  DRAW_FILLING
#property indicator_color1 clrDarkGreen,clrDarkOrchid

#property indicator_label2 "Span A"
#property indicator_type2  DRAW_LINE
#property indicator_color2 clrMediumSeaGreen
#property indicator_width2 2

#property indicator_label3 "Span B"
#property indicator_type3  DRAW_LINE
#property indicator_color3 clrOrchid
#property indicator_width3 2

input int InpTenkan    = 9;  // 転換線の期間
input int InpKijun     = 26; // 基準線・先行スパンBの期間
input int InpShiftBars = 26; // 右へ移動するバー数

double g_cloud_span_a[];
double g_cloud_span_b[];
double g_line_span_a[];
double g_line_span_b[];

int OnInit()
{
   if(InpTenkan < 1 || InpKijun < 1 || InpShiftBars < 0)
      return INIT_PARAMETERS_INCORRECT;

   SetIndexBuffer(0, g_cloud_span_a, INDICATOR_DATA);
   SetIndexBuffer(1, g_cloud_span_b, INDICATOR_DATA);
   SetIndexBuffer(2, g_line_span_a, INDICATOR_DATA);
   SetIndexBuffer(3, g_line_span_b, INDICATOR_DATA);

   ArraySetAsSeries(g_cloud_span_a, true);
   ArraySetAsSeries(g_cloud_span_b, true);
   ArraySetAsSeries(g_line_span_a, true);
   ArraySetAsSeries(g_line_span_b, true);

   const int first_valid_bar = MathMax(InpTenkan, InpKijun) - 1;
   for(int plot = 0; plot < 3; ++plot)
   {
      PlotIndexSetInteger(plot, PLOT_SHIFT, InpShiftBars);
      PlotIndexSetInteger(plot, PLOT_DRAW_BEGIN, first_valid_bar);
      PlotIndexSetDouble(plot, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   }

   IndicatorSetInteger(INDICATOR_DIGITS, _Digits);
   IndicatorSetString(
      INDICATOR_SHORTNAME,
      StringFormat("Andy Kumo shifted cloud v1.01 (%d,%d,+%d)",
                   InpTenkan,
                   InpKijun,
                   InpShiftBars));
   return INIT_SUCCEEDED;
}

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[])
{
   const int required_bars = MathMax(InpTenkan, InpKijun);
   if(rates_total < required_bars)
      return 0;

   ArraySetAsSeries(high, true);
   ArraySetAsSeries(low, true);

   if(prev_calculated == 0)
   {
      ArrayInitialize(g_cloud_span_a, EMPTY_VALUE);
      ArrayInitialize(g_cloud_span_b, EMPTY_VALUE);
      ArrayInitialize(g_line_span_a, EMPTY_VALUE);
      ArrayInitialize(g_line_span_b, EMPTY_VALUE);
   }

   int limit = rates_total - prev_calculated;
   if(prev_calculated == 0)
      limit = rates_total - required_bars;
   else
      limit = MathMin(limit, rates_total - required_bars);

   for(int i = limit; i >= 0; --i)
   {
      double tenkan_high = high[i];
      double tenkan_low  = low[i];
      double kijun_high  = high[i];
      double kijun_low   = low[i];

      for(int j = 1; j < InpTenkan; ++j)
      {
         tenkan_high = MathMax(tenkan_high, high[i + j]);
         tenkan_low  = MathMin(tenkan_low, low[i + j]);
      }
      for(int j = 1; j < InpKijun; ++j)
      {
         kijun_high = MathMax(kijun_high, high[i + j]);
         kijun_low  = MathMin(kijun_low, low[i + j]);
      }

      const double tenkan = (tenkan_high + tenkan_low) * 0.5;
      const double kijun  = (kijun_high + kijun_low) * 0.5;
      const double span_a = (tenkan + kijun) * 0.5;
      const double span_b = (kijun_high + kijun_low) * 0.5;

      g_cloud_span_a[i] = span_a;
      g_cloud_span_b[i] = span_b;
      g_line_span_a[i]  = span_a;
      g_line_span_b[i]  = span_b;
   }

   return rates_total;
}
//+------------------------------------------------------------------+
