//+------------------------------------------------------------------+
//|                                             TickDataExporter.mq5 |
//+------------------------------------------------------------------+

#property script_show_inputs
//+------------------------------------------------------------------+
//| Includes                                                         |
//+------------------------------------------------------------------+
#include <Tick_Exporter/TickRecord.mqh>
#include <Tick_Exporter/TickFileHeader.mqh>
#include <Tick_Exporter/TickExporter.mqh>

//--- Inputs                                                           
input string   InpSymbol   = "";          // Symbol (empty = current chart symbol)
input datetime InpFrom     = 0;           // Start date and time
input datetime InpTo       = 0;           // End date and time (0 = now)
input string   InpFilename = "ticks.bin"; // Output filename in MQL5/Files/
input int      InpMaxTicks = 0;           // Max ticks to export (0 = no limit)
//+------------------------------------------------------------------+
//| Script entry point: validate inputs, export, and report.         |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- resolve symbol default to the chart symbol
   string symbol = (InpSymbol == "") ? _Symbol : InpSymbol;
//--- resolve end-time default to the current server time
   datetime from = InpFrom;
   datetime to   = (InpTo == 0) ? TimeCurrent() : InpTo;
//--- validate the date range before making any API calls
   if(from >= to)
     {
      ::PrintFormat("TickDataExporter: invalid range - from (%s) must be before to (%s).",
                    ::TimeToString(from), ::TimeToString(to));
      return;
     }
//--- confirm the symbol exists and its tick history is accessible
   if(!::SymbolSelect(symbol, true))
     {
      ::PrintFormat("TickDataExporter: symbol '%s' not found or cannot be selected.",
                    symbol);
      return;
     }

   uint t_start = GetTickCount(); // record start time for elapsed logging

   CTickExporter exporter;
   exporter.Init(symbol, from, to, InpFilename, InpMaxTicks);

   if(!exporter.Export())
     {
      ::Print("TickDataExporter: export failed. Check the journal for details.");
      return;
     }

   uint elapsed = GetTickCount() - t_start; // compute elapsed time in ms
   ::PrintFormat("TickDataExporter: exported %d ticks to MQL5/Files/%s in %d ms.",
                 exporter.GetCount(), exporter.GetFilename(), elapsed);
  }
//+------------------------------------------------------------------+