//+------------------------------------------------------------------+
//|                                    11_Data_Dump_Script_FIXED.mq4 |
//|                                  Copyright 2025, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.01"
#property strict
#property script_show_inputs

input string IndicatorName = "11";
input int    BarsToExport  = 5000;

// Indicator inputs - must match what you actually see on chart
input int    VelasBack           = 200;
input int    AcervidadeMinima    = 75;
input int    VelasEntreSinais    = 3;

// DIAGNOSTIC SWITCH: if true, pass params explicitly (order below).
// If false, calls iCustom with NO params -> indicator uses its own
// compiled defaults, same as the very first version of this script.
// Run once with true and once with false and compare signal counts.
input bool   PassParameters = true;

#define BUFS 15

bool IsEmpty(double v)
{
   return(v==EMPTY_VALUE || MathAbs(v)>1e20);
}

string BufValue(double v)
{
   if(IsEmpty(v))
      return("EMPTY");
   return(DoubleToString(v,10));
}

double GetBuf(int b,int shift)
{
   if(PassParameters)
      return(iCustom(NULL,0,IndicatorName,
                      VelasBack,AcervidadeMinima,VelasEntreSinais,
                      b,shift));
   else
      return(iCustom(NULL,0,IndicatorName,
                      b,shift));
}

void OnStart()
{
   int bars=MathMin(BarsToExport,Bars);

   string file=StringFormat("%s_%d_%s_%s.csv",
                            Symbol(),Period(),IndicatorName,
                            (PassParameters?"withparams":"defaults"));

   int h=FileOpen(file,FILE_CSV|FILE_WRITE,';');

   if(h==INVALID_HANDLE)
   {
      Print("Cannot create file.");
      return;
   }

   //================ HEADER (single line) =================

   string header="Shift;Time;Open;High;Low;Close;Volume;Spread_Current;Body;Upper;Lower;Range";

   for(int b=0;b<BUFS;b++)
   {
      header += StringFormat(";B%d",b);
      header += StringFormat(";B%d_1",b);
      header += StringFormat(";B%d_2",b);
      header += StringFormat(";E%d",b);
   }

   FileWriteString(h,header+"\r\n");

   //================ DATA (single line per candle) ===================

   int curSpread=(int)MarketInfo(Symbol(),MODE_SPREAD);

   for(int i=bars-1;i>=2;i--)
   {
      double body=MathAbs(Close[i]-Open[i]);
      double upper=High[i]-MathMax(Open[i],Close[i]);
      double lower=MathMin(Open[i],Close[i])-Low[i];
      double range=High[i]-Low[i];

      string line=StringFormat("%d;%s;%s;%s;%s;%s;%d;%d;%s;%s;%s;%s",
         i,
         TimeToString(Time[i],TIME_DATE|TIME_SECONDS),
         DoubleToString(Open[i],_Digits),
         DoubleToString(High[i],_Digits),
         DoubleToString(Low[i],_Digits),
         DoubleToString(Close[i],_Digits),
         (int)Volume[i],
         curSpread,
         DoubleToString(body,_Digits),
         DoubleToString(upper,_Digits),
         DoubleToString(lower,_Digits),
         DoubleToString(range,_Digits));

      for(int b=0;b<BUFS;b++)
      {
         double v0=GetBuf(b,i);
         double v1=GetBuf(b,i+1);
         double v2=GetBuf(b,i+2);

         line+=";"+BufValue(v0);
         line+=";"+BufValue(v1);
         line+=";"+BufValue(v2);
         line+=";"+IntegerToString(IsEmpty(v0));
      }

      FileWriteString(h,line+"\r\n");
   }

   FileClose(h);

   Print("Saved: ",file);
}