//+------------------------------------------------------------------+
//|                                       IndicatorBufferExporter.mq5|
//+------------------------------------------------------------------+

#property script_show_inputs

//--- Includes
#include <IndicatorExporter/IndicatorRow.mqh>
#include <IndicatorExporter/IndicatorExporter.mqh>

//--- Inputs
input string InpSymbol             = "";                    // Symbol (empty = current chart symbol)
input ENUM_TIMEFRAMES InpTimeframe = PERIOD_CURRENT;         // Timeframe
input string InpIndicatorName      = "IndicatorExporter\\SimpleBandsDemo";      // Indicator name in MQL5/Indicators
input double InpParam1             = 20;                     // Numeric parameter 1
input double InpParam2             = 2.0;                    // Numeric parameter 2
input double InpParam3             = 0;                      // Numeric parameter 3 (unused if 0 count)
input double InpParam4             = 0;                      // Numeric parameter 4 (unused if 0 count)
input int    InpParamCount         = 2;                      // How many of the above to pass
input string InpBufferIndices      = "0,1,2";                // Comma-separated buffer indices
input string InpBufferNames        = "Upper,Mid,Lower";      // Comma-separated CSV column names
input datetime InpFrom             = 0;                      // Start date
input datetime InpTo               = 0;                      // End date (0 = now)
input string InpOutputFilename     = "indicator_export.csv"; // Output filename in MQL5/Files/
//+------------------------------------------------------------------+
//| Splits a comma-separated string into an array of integers.       |
//+------------------------------------------------------------------+
int ParseIntList(const string &csv, int &out[])
  {
   string parts[];
   int count = ::StringSplit(csv, ',', parts);
   ::ArrayResize(out, count);
   for(int i = 0; i < count; i++)
      out[i] = (int)::StringToInteger(parts[i]);
   return(count);
  }
//+------------------------------------------------------------------+
//| Splits a comma-separated string into an array of strings.        |
//+------------------------------------------------------------------+
int ParseStringList(const string &csv, string &out[])
  {
   return(::StringSplit(csv, ',', out));
  }
//+------------------------------------------------------------------+
//| Script entry point: resolve inputs, run the export, and report.  |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- resolve defaults
   string symbol = (InpSymbol == "") ? _Symbol : InpSymbol;
   ENUM_TIMEFRAMES tf = (InpTimeframe == PERIOD_CURRENT) ? _Period : InpTimeframe;
   datetime from = InpFrom;
   datetime to   = (InpTo == 0) ? ::TimeCurrent() : InpTo;

   if(from >= to)
     {
      ::PrintFormat("IndicatorBufferExporter: invalid range - from (%s) must be before to (%s).",
                    ::TimeToString(from), ::TimeToString(to));
      return;
     }

//--- parse the buffer index and name lists
   int    buffer_indices[];
   string buffer_names[];
   int idx_count  = ParseIntList(InpBufferIndices, buffer_indices);
   int name_count = ParseStringList(InpBufferNames, buffer_names);

   if(idx_count != name_count)
     {
      ::PrintFormat("IndicatorBufferExporter: buffer index count (%d) does not match "
                    "buffer name count (%d).", idx_count, name_count);
      return;
     }

//--- assemble the numeric parameter array from the four inputs
   double params[4];
   params[0] = InpParam1;
   params[1] = InpParam2;
   params[2] = InpParam3;
   params[3] = InpParam4;

   uint t_start = ::GetTickCount();

   CIndicatorExporter exporter;
   exporter.Init(symbol, tf, InpIndicatorName, params, InpParamCount,
                 buffer_indices, idx_count, from, to);

   if(!exporter.Fetch())
     {
      ::Print("IndicatorBufferExporter: fetch failed. Check the journal for details.");
      return;
     }

   if(!exporter.WriteCsv(InpOutputFilename, buffer_names))
     {
      ::Print("IndicatorBufferExporter: write failed. Check the journal for details.");
      return;
     }

   uint elapsed = ::GetTickCount() - t_start;
   ::PrintFormat("IndicatorBufferExporter: exported %d rows to MQL5/Files/%s in %d ms.",
                 exporter.GetRowCount(), InpOutputFilename, elapsed);
  }
//+------------------------------------------------------------------+