//+------------------------------------------------------------------+
//|                                              IndicatorInputs.mqh |
//|                                                       Ali Rajput |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Ali Rajput"
#property link      "https://www.mql5.com"
//+------------------------------------------------------------------+
//| Defines                                                          |
//+------------------------------------------------------------------+
#define II_GROUP_PREFIX "@group '"                // how IndicatorParameters() showed a group on builds 6204 and 6230
#define II_PROBE_TEXT   "#ii probe#"              // probe value for string slots in Load()
#define II_PROBE_REAL   0.1                       // probe value for double slots, a float slot rounds it
#define II_MAX_STRING   245                       // longer strings were cut to 245 chars on build 6230
//+------------------------------------------------------------------+
//| Builds the parameter list of a custom indicator by input number  |
//| and inserts a placeholder for every input group line             |
//+------------------------------------------------------------------+
class CIndicatorInputs
  {
private:
   string            m_path;                      // indicator path as given to iCustom()
   MqlParam          m_slots[];                   // every slot after the name, with its default
   bool              m_group[];                   // true when the slot is an input group line
   bool              m_float[];                   // a DOUBLE slot that rounds to float precision
   int               m_input_slot[];              // input number -> slot number
   bool              m_price_slot;                // the last slot is the applied price
   bool              m_verified;                  // the last handle was read back and matched
   int               m_mismatch;                  // slots where the group text and the probe disagreed
   string            m_error;                     // reason for the last failure

   static bool       IsGroup(const MqlParam &p);
   static bool       IsInteger(const ENUM_DATATYPE type);
   static bool       Range(const ENUM_DATATYPE type,long &lo,long &hi);
   static string     Number(const double value);
   static string     Quoted(const string value);
   void              Index(void);
   bool              Probe(void);
   bool              CheckInput(const int input_no,const bool is_string);
   bool              CheckType(const int input_no,const ENUM_DATATYPE type,const ENUM_DATATYPE reported);
   bool              Store(const int input_no,const long value);
   bool              Same(const MqlParam &a,const MqlParam &b) const;

public:
   //--- constructor: no layout yet
                     CIndicatorInputs(void) : m_price_slot(false), m_verified(false), m_mismatch(0) {}
   //--- the layout: from a live handle, or from a file for the tester
   bool              Load(const string path,const bool price_slot=false,const bool probe=true);
   bool              SaveLayout(const string file_name) const;
   bool              LoadLayout(const string file_name);
   //--- true when both layouts have the same slots, types, groups and defaults
   bool              SameLayout(const CIndicatorInputs &other);
   //--- slots where the "@group '" text and the probe in Load() disagreed
   int               PrefixMismatches(void) const { return(m_mismatch); }
   //--- number of inputs, not counting group lines or the price slot
   int               Inputs(void) const { return(ArraySize(m_input_slot)); }
   //--- number of slots after the indicator name
   int               Slots(void)  const { return(ArraySize(m_slots)); }
   int               Groups(void) const;
   int               InputOf(const string group,const int position) const;
   string            Describe(void) const;
   //--- the iCustom() call for the current values, with "" in every group slot
   string            CallText(void) const;
   //--- reason for the last failure, empty after a success
   string            LastError(void) const { return(m_error); }
   //--- true when Create() read the handle back and every input matched
   bool              Verified(void)  const { return(m_verified); }
   //--- values by input number
   bool              Set(const int input_no,const long value);
   bool              Set(const int input_no,const double value);
   bool              Set(const int input_no,const string value);
   bool              SetBool(const int input_no,const bool value);
   bool              SetTime(const int input_no,const datetime value);
   bool              SetColor(const int input_no,const color value);
   bool              SetEnum(const int input_no,const int value);
   bool              SetPrice(const ENUM_APPLIED_PRICE price);
   //--- the parameter array and the handle
   int               Build(MqlParam &params[]) const;
   int               Create(const string symbol,const ENUM_TIMEFRAMES period);
   bool              Verify(const int handle);
  };
//+------------------------------------------------------------------+
//| A slot whose text looks like an input group line                 |
//+------------------------------------------------------------------+
bool CIndicatorInputs::IsGroup(const MqlParam &p)
  {
   return(p.type==TYPE_STRING && StringFind(p.string_value,II_GROUP_PREFIX)==0);
  }
//+------------------------------------------------------------------+
//| Numeric types that drop the fraction of a double                 |
//+------------------------------------------------------------------+
bool CIndicatorInputs::IsInteger(const ENUM_DATATYPE type)
  {
   return(type!=TYPE_STRING && type!=TYPE_DOUBLE && type!=TYPE_FLOAT);
  }
//+------------------------------------------------------------------+
//| Values an integer slot keeps without wrapping, false = no limit  |
//+------------------------------------------------------------------+
bool CIndicatorInputs::Range(const ENUM_DATATYPE type,long &lo,long &hi)
  {
   switch(type)
     {
      case TYPE_BOOL:   lo=0;         hi=1;          return(true);
      case TYPE_CHAR:   lo=CHAR_MIN;  hi=CHAR_MAX;   return(true);
      case TYPE_UCHAR:  lo=0;         hi=UCHAR_MAX;  return(true);
      case TYPE_SHORT:  lo=SHORT_MIN; hi=SHORT_MAX;  return(true);
      case TYPE_USHORT: lo=0;         hi=USHORT_MAX; return(true);
      case TYPE_INT:    lo=INT_MIN;   hi=INT_MAX;    return(true);
      case TYPE_UINT:
      case TYPE_COLOR:  lo=0;         hi=UINT_MAX;   return(true);
     }
   return(false);
  }
//+------------------------------------------------------------------+
//| Number the inputs, skipping group slots and the price slot       |
//+------------------------------------------------------------------+
void CIndicatorInputs::Index(void)
  {
   int slots=ArraySize(m_slots);
   ArrayFree(m_input_slot);
   int last_input=(m_price_slot ? slots-1 : slots);
   for(int s=0; s<last_input; s++)
     {
      if(m_group[s])
         continue;
      int k=ArraySize(m_input_slot);
      ArrayResize(m_input_slot,k+1);
      m_input_slot[k]=s;
     }
  }
//+------------------------------------------------------------------+
//| One handle with a probe value in every string and double slot.   |
//| A group slot keeps its own text, a string input takes the probe. |
//| A float input is reported as DOUBLE and rounds 0.1 to float.     |
//+------------------------------------------------------------------+
bool CIndicatorInputs::Probe(void)
  {
   MqlParam params[];
   int n=Build(params);
   int last_input=(m_price_slot ? Slots()-1 : Slots());
   for(int s=0; s<last_input; s++)
     {
      if(m_slots[s].type==TYPE_STRING)
         params[s+1].string_value=II_PROBE_TEXT;
      if(m_slots[s].type==TYPE_DOUBLE)
         params[s+1].double_value=II_PROBE_REAL;
     }
   ResetLastError();
   int handle=IndicatorCreate(_Symbol,PERIOD_CURRENT,IND_CUSTOM,n,params);
   if(handle==INVALID_HANDLE)
     {
      m_error=StringFormat("the probe handle failed, error %d",GetLastError());
      return(false);
     }
   MqlParam got[];
   ENUM_INDICATOR type;
   int count=IndicatorParameters(handle,type,got);
   IndicatorRelease(handle);
   if(count!=n)
     {
      m_error=StringFormat("the probe handle reports %d slots, expected %d",count-1,n-1);
      return(false);
     }
   for(int s=0; s<last_input; s++)
     {
      if(m_slots[s].type==TYPE_STRING)
        {
         bool group=(got[s+1].string_value!=II_PROBE_TEXT);
         if(group!=m_group[s])
            m_mismatch++;
         m_group[s]=group;
        }
      if(m_slots[s].type==TYPE_DOUBLE)
         m_float[s]=(got[s+1].double_value!=II_PROBE_REAL);
     }
   return(true);
  }
//+------------------------------------------------------------------+
//| Read the slot layout from a handle created with the defaults     |
//+------------------------------------------------------------------+
bool CIndicatorInputs::Load(const string path,const bool price_slot=false,const bool probe=true)
  {
   m_path      =path;
   m_price_slot=price_slot;
   m_mismatch  =0;
   m_error     ="";
   ArrayFree(m_slots);
   ArrayFree(m_group);
   ArrayFree(m_float);
   ArrayFree(m_input_slot);
//--- a handle with no inputs uses the defaults and reports every slot
   MqlParam name[1];
   name[0].type        =TYPE_STRING;
   name[0].string_value=path;
   ResetLastError();
   int handle=IndicatorCreate(_Symbol,PERIOD_CURRENT,IND_CUSTOM,1,name);
   if(handle==INVALID_HANDLE)
     {
      m_error=StringFormat("cannot load %s, error %d",path,GetLastError());
      return(false);
     }
   MqlParam got[];
   ENUM_INDICATOR type;
   ResetLastError();
   int n=IndicatorParameters(handle,type,got);
   int err=GetLastError();
   IndicatorRelease(handle);
//--- the Strategy Tester answers 4014 here, a saved layout is needed there
   if(n<1)
     {
      m_error=StringFormat("IndicatorParameters() returned %d for %s, error %d",n,path,err);
      return(false);
     }
//--- slot 0 of the reply is the indicator name, the inputs follow
   ArrayResize(m_slots,n-1);
   ArrayResize(m_group,n-1);
   ArrayResize(m_float,n-1);
   for(int s=0; s<n-1; s++)
     {
      m_slots[s]=got[s+1];
      m_group[s]=IsGroup(m_slots[s]);
      m_float[s]=false;
     }
//--- the text is a first guess, the probe decides
   if(probe && !Probe())
     {
      ArrayFree(m_slots);
      ArrayFree(m_group);
      ArrayFree(m_float);
      return(false);
     }
   Index();
   return(true);
  }
//+------------------------------------------------------------------+
//| Write the layout to MQL5\Files, one tab-separated line per slot  |
//+------------------------------------------------------------------+
bool CIndicatorInputs::SaveLayout(const string file_name) const
  {
   int file=FileOpen(file_name,FILE_WRITE|FILE_TXT|FILE_ANSI);
   if(file==INVALID_HANDLE)
      return(false);
   FileWriteString(file,StringFormat("%s\t%d\t%d\r\n",m_path,(int)m_price_slot,Slots()));
   for(int s=0; s<Slots(); s++)
     {
      //--- a numeric slot carries a NULL string, which StringFormat() prints as "(null)"
      string text=(m_slots[s].string_value==NULL ? "" : m_slots[s].string_value);
      //--- flags: 1 = group line, 2 = float input
      int flags=(m_group[s] ? 1 : 0)+(m_float[s] ? 2 : 0);
      FileWriteString(file,StringFormat("%d\t%I64d\t%.17g\t%d\t%s\r\n",(int)m_slots[s].type,
                                        m_slots[s].integer_value,m_slots[s].double_value,flags,text));
     }
   FileClose(file);
   return(true);
  }
//+------------------------------------------------------------------+
//| Read a layout written by SaveLayout()                            |
//+------------------------------------------------------------------+
bool CIndicatorInputs::LoadLayout(const string file_name)
  {
   m_error   ="";
   m_mismatch=0;
   int file=FileOpen(file_name,FILE_READ|FILE_TXT|FILE_ANSI);
   if(file==INVALID_HANDLE)
     {
      m_error=StringFormat("cannot open %s, error %d",file_name,GetLastError());
      return(false);
     }
   string head[];
   int    slots=-1;
   if(StringSplit(FileReadString(file),'\t',head)==3)
     {
      m_path      =head[0];
      m_price_slot=(head[1]=="1");
      slots       =(int)StringToInteger(head[2]);
     }
   ArrayResize(m_slots,MathMax(slots,0));
   ArrayResize(m_group,MathMax(slots,0));
   ArrayResize(m_float,MathMax(slots,0));
   int read=0;
   while(read<slots && !FileIsEnding(file))
     {
      string f[];
      if(StringSplit(FileReadString(file),'\t',f)<4)
         break;
      ZeroMemory(m_slots[read]);
      m_slots[read].type         =(ENUM_DATATYPE)StringToInteger(f[0]);
      m_slots[read].integer_value=StringToInteger(f[1]);
      m_slots[read].double_value =StringToDouble(f[2]);
      int flags                  =(int)StringToInteger(f[3]);
      m_group[read]              =((flags&1)!=0);
      m_float[read]              =((flags&2)!=0);
      m_slots[read].string_value =(ArraySize(f)>4 ? f[4] : "");
      read++;
     }
   FileClose(file);
   if(slots<0 || read!=slots)
     {
      m_error=StringFormat("%s is not a layout file (%d of %d slots read)",file_name,read,slots);
      ArrayFree(m_slots);
      ArrayFree(m_group);
      ArrayFree(m_float);
      return(false);
     }
   Index();
   return(true);
  }
//+------------------------------------------------------------------+
//| Compare two layouts slot by slot, the reason goes to LastError() |
//+------------------------------------------------------------------+
bool CIndicatorInputs::SameLayout(const CIndicatorInputs &other)
  {
   m_error="";
   if(other.Slots()!=Slots() || other.m_price_slot!=m_price_slot)
     {
      m_error=StringFormat("%d slots against %d",Slots(),other.Slots());
      return(false);
     }
   for(int s=0; s<Slots(); s++)
     {
      if(m_slots[s].type!=other.m_slots[s].type || m_group[s]!=other.m_group[s] || m_float[s]!=other.m_float[s])
        {
         m_error=StringFormat("slot %d: %s%s against %s%s",s,StringSubstr(EnumToString(m_slots[s].type),5),
                              (m_group[s] ? " group" : ""),StringSubstr(EnumToString(other.m_slots[s].type),5),
                              (other.m_group[s] ? " group" : ""));
         return(false);
        }
      if(!Same(m_slots[s],other.m_slots[s]))
        {
         m_error=StringFormat("slot %d: the default changed",s);
         return(false);
        }
     }
   return(true);
  }
//+------------------------------------------------------------------+
//| Number of input group lines                                      |
//+------------------------------------------------------------------+
int CIndicatorInputs::Groups(void) const
  {
   int count=0;
   for(int s=0; s<ArraySize(m_group); s++)
      if(m_group[s])
         count++;
   return(count);
  }
//+------------------------------------------------------------------+
//| Input number of the n-th input inside a named group              |
//+------------------------------------------------------------------+
int CIndicatorInputs::InputOf(const string group,const int position) const
  {
   string header=II_GROUP_PREFIX+group+"'";
   int    found =-1;
   for(int i=0; i<ArraySize(m_input_slot); i++)
     {
      //--- the group of an input is the nearest group line above it
      string owner="";
      for(int s=m_input_slot[i]-1; s>=0; s--)
         if(m_group[s])
           {
            owner=m_slots[s].string_value;
            break;
           }
      if(owner!=header)
         continue;
      found++;
      if(found==position)
         return(i);
     }
   return(-1);
  }
//+------------------------------------------------------------------+
//| One line per slot: input number, type and default                |
//+------------------------------------------------------------------+
string CIndicatorInputs::Describe(void) const
  {
   string text=StringFormat("%s: %d slot%s, %d input%s, %d group%s\n",m_path,
                            Slots(),Slots()==1 ? "" : "s",Inputs(),Inputs()==1 ? "" : "s",
                            Groups(),Groups()==1 ? "" : "s");
   int    input_no=0;
   for(int s=0; s<Slots(); s++)
     {
      string value;
      if(m_slots[s].type==TYPE_STRING)
         value="\""+m_slots[s].string_value+"\"";
      else
         if(!IsInteger(m_slots[s].type))
            value=DoubleToString(m_slots[s].double_value,4);
         else
            value=IntegerToString(m_slots[s].integer_value);
      string role;
      if(m_group[s])
         role="group";
      else
         if(m_price_slot && s==Slots()-1)
            role="price";
         else
            role=StringFormat("input %d",input_no++);
      string kind=(m_float[s] ? "FLOAT" : StringSubstr(EnumToString(m_slots[s].type),5));
      text+=StringFormat("  slot %2d  %-8s  %-13s %s\n",s,role,kind,value);
     }
   if(m_mismatch>0)
      text+=StringFormat("  %d slot(s) where the group text and the probe disagreed, the probe was used\n",m_mismatch);
   return(text);
  }
//+------------------------------------------------------------------+
//| A double as source text that keeps one digit after the point     |
//+------------------------------------------------------------------+
string CIndicatorInputs::Number(const double value)
  {
   string text=DoubleToString(value,8);
   int    len =StringLen(text);
   while(len>2 && StringGetCharacter(text,len-1)=='0' && StringGetCharacter(text,len-2)!='.')
      len--;
   return(StringSubstr(text,0,len));
  }
//+------------------------------------------------------------------+
//| A string as a quoted MQL5 literal                                |
//+------------------------------------------------------------------+
string CIndicatorInputs::Quoted(const string value)
  {
   string text=value;
   StringReplace(text,"\\","\\\\");
   StringReplace(text,"\"","\\\"");
   return("\""+text+"\"");
  }
//+------------------------------------------------------------------+
//| The iCustom() call for the current values, ready to paste        |
//+------------------------------------------------------------------+
string CIndicatorInputs::CallText(void) const
  {
   string text="iCustom(_Symbol,_Period,"+Quoted(m_path);
   for(int s=0; s<Slots(); s++)
     {
      string value;
      //--- a group slot takes the empty string, like in Build()
      if(m_group[s])
         value="\"\"";
      else
         if(m_slots[s].type==TYPE_STRING)
            value=Quoted(m_slots[s].string_value);
         else
            if(m_slots[s].type==TYPE_BOOL)
               value=(m_slots[s].integer_value!=0 ? "true" : "false");
            else
               if(!IsInteger(m_slots[s].type))
                  value=Number(m_slots[s].double_value);
               else
                  value=IntegerToString(m_slots[s].integer_value);
      text+=","+value;
     }
   return(text+")");
  }
//+------------------------------------------------------------------+
//| Check an input number and the kind of value against the slot     |
//+------------------------------------------------------------------+
bool CIndicatorInputs::CheckInput(const int input_no,const bool is_string)
  {
   m_error="";
   if(input_no<0 || input_no>=Inputs())
     {
      m_error=StringFormat("input %d does not exist, the indicator has %d",input_no,Inputs());
      return(false);
     }
   ENUM_DATATYPE type=m_slots[m_input_slot[input_no]].type;
   if(is_string!=(type==TYPE_STRING))
     {
      m_error=StringFormat("input %d is %s, the value is %s",input_no,
                           StringSubstr(EnumToString(type),5),(is_string ? "a string" : "a number"));
      return(false);
     }
   return(true);
  }
//+------------------------------------------------------------------+
//| Check the slot type. Build 6230 reported a datetime as LONG and  |
//| a color as UINT, so those reported types are accepted as well    |
//+------------------------------------------------------------------+
bool CIndicatorInputs::CheckType(const int input_no,const ENUM_DATATYPE type,const ENUM_DATATYPE reported)
  {
   if(!CheckInput(input_no,type==TYPE_STRING))
      return(false);
   ENUM_DATATYPE slot=m_slots[m_input_slot[input_no]].type;
   if(slot!=type && slot!=reported)
     {
      m_error=StringFormat("input %d is %s, not %s",input_no,StringSubstr(EnumToString(slot),5),
                           StringSubstr(EnumToString(type),5));
      return(false);
     }
   return(true);
  }
//+------------------------------------------------------------------+
//| Store a whole number if the slot keeps it without wrapping       |
//+------------------------------------------------------------------+
bool CIndicatorInputs::Store(const int input_no,const long value)
  {
   int  s=m_input_slot[input_no];
   long lo=0,hi=0;
   if(Range(m_slots[s].type,lo,hi) && (value<lo || value>hi))
     {
      m_error=StringFormat("input %d is %s, %I64d is outside %I64d..%I64d",input_no,
                           StringSubstr(EnumToString(m_slots[s].type),5),value,lo,hi);
      return(false);
     }
   m_slots[s].integer_value=value;
   m_slots[s].double_value =(double)value;
   return(true);
  }
//+------------------------------------------------------------------+
//| Set an integer, bool, enum, color or datetime input              |
//+------------------------------------------------------------------+
bool CIndicatorInputs::Set(const int input_no,const long value)
  {
   if(!CheckInput(input_no,false))
      return(false);
//--- a double or float input takes the number through the double overload
   if(!IsInteger(m_slots[m_input_slot[input_no]].type))
      return(Set(input_no,(double)value));
   return(Store(input_no,value));
  }
//+------------------------------------------------------------------+
//| Set a double input, or a whole number into an integer input      |
//+------------------------------------------------------------------+
bool CIndicatorInputs::Set(const int input_no,const double value)
  {
   if(!CheckInput(input_no,false))
      return(false);
   int s=m_input_slot[input_no];
//--- NaN and inf are stored, but no read-back can confirm them
   if(!MathIsValidNumber(value))
     {
      m_error=StringFormat("input %d: %.8g is not a finite number",input_no,value);
      return(false);
     }
//--- the terminal would drop the fraction without a word
   if(IsInteger(m_slots[s].type))
     {
      if(value!=MathFloor(value) || MathAbs(value)>9.0e18)
        {
         m_error=StringFormat("input %d is %s, %.8g is not a whole number it can hold",input_no,
                              StringSubstr(EnumToString(m_slots[s].type),5),value);
         return(false);
        }
      return(Store(input_no,(long)value));
     }
//--- a float input keeps float precision, and 1e40 would become inf
   if(m_float[s] || m_slots[s].type==TYPE_FLOAT)
     {
      if(MathAbs(value)>FLT_MAX)
        {
         m_error=StringFormat("input %d is float, %.8g is outside its range",input_no,value);
         return(false);
        }
      m_slots[s].double_value=(double)(float)value;
     }
   else
      m_slots[s].double_value=value;
   m_slots[s].integer_value=(long)value;
   return(true);
  }
//+------------------------------------------------------------------+
//| Set a string input                                               |
//+------------------------------------------------------------------+
bool CIndicatorInputs::Set(const int input_no,const string value)
  {
   if(!CheckInput(input_no,true))
      return(false);
//--- the terminal cut longer strings without a word
   if(StringLen(value)>II_MAX_STRING)
     {
      m_error=StringFormat("input %d: %d chars, the terminal keeps %d",input_no,StringLen(value),II_MAX_STRING);
      return(false);
     }
   m_slots[m_input_slot[input_no]].string_value=value;
   return(true);
  }
//+------------------------------------------------------------------+
//| Set a bool input, refused for any other slot type                |
//+------------------------------------------------------------------+
bool CIndicatorInputs::SetBool(const int input_no,const bool value)
  {
   return(CheckType(input_no,TYPE_BOOL,TYPE_BOOL) && Store(input_no,(value ? 1 : 0)));
  }
//+------------------------------------------------------------------+
//| Set a datetime input, refused unless the slot is DATETIME or LONG|
//+------------------------------------------------------------------+
bool CIndicatorInputs::SetTime(const int input_no,const datetime value)
  {
   return(CheckType(input_no,TYPE_DATETIME,TYPE_LONG) && Store(input_no,(long)value));
  }
//+------------------------------------------------------------------+
//| Set a color input, refused unless the slot is COLOR or UINT      |
//+------------------------------------------------------------------+
bool CIndicatorInputs::SetColor(const int input_no,const color value)
  {
   return(CheckType(input_no,TYPE_COLOR,TYPE_UINT) && Store(input_no,(long)(uint)value));
  }
//+------------------------------------------------------------------+
//| Set an enum input. The terminal reports an enum as INT, so the   |
//| enum type and its range can't be checked                         |
//+------------------------------------------------------------------+
bool CIndicatorInputs::SetEnum(const int input_no,const int value)
  {
   return(CheckType(input_no,TYPE_INT,TYPE_INT) && Store(input_no,value));
  }
//+------------------------------------------------------------------+
//| Set the applied price of a short-form indicator                  |
//+------------------------------------------------------------------+
bool CIndicatorInputs::SetPrice(const ENUM_APPLIED_PRICE price)
  {
   m_error="";
   if(!m_price_slot || Slots()==0)
     {
      m_error="the layout was loaded without a price slot";
      return(false);
     }
   m_slots[Slots()-1].integer_value=price;
   return(true);
  }
//+------------------------------------------------------------------+
//| Fill the IndicatorCreate() array: the name, then every slot      |
//+------------------------------------------------------------------+
int CIndicatorInputs::Build(MqlParam &params[]) const
  {
   int n=Slots()+1;
   ArrayResize(params,n);
   params[0].type        =TYPE_STRING;
   params[0].string_value=m_path;
   for(int s=0; s<Slots(); s++)
     {
      params[s+1]=m_slots[s];
      //--- a group slot takes any value, an empty string is the usual one
      if(m_group[s])
         params[s+1].string_value="";
     }
   return(n);
  }
//+------------------------------------------------------------------+
//| Create the handle and check that every input landed in place     |
//+------------------------------------------------------------------+
int CIndicatorInputs::Create(const string symbol,const ENUM_TIMEFRAMES period)
  {
   m_error   ="";
   m_verified=false;
   if(Slots()==0)
     {
      m_error="no layout, Load() has not succeeded";
      return(INVALID_HANDLE);
     }
   MqlParam params[];
   int n=Build(params);
   ResetLastError();
   int handle=IndicatorCreate(symbol,period,IND_CUSTOM,n,params);
   if(handle==INVALID_HANDLE)
     {
      m_error=StringFormat("IndicatorCreate() failed, error %d",GetLastError());
      return(INVALID_HANDLE);
     }
//--- IndicatorParameters() is not allowed in the Strategy Tester, the handle stays unverified
   if(MQLInfoInteger(MQL_TESTER) || MQLInfoInteger(MQL_OPTIMIZATION))
      return(handle);
   if(!Verify(handle))
     {
      IndicatorRelease(handle);
      return(INVALID_HANDLE);
     }
   m_verified=true;
   return(handle);
  }
//+------------------------------------------------------------------+
//| Compare two slots by the field their type uses                   |
//+------------------------------------------------------------------+
bool CIndicatorInputs::Same(const MqlParam &a,const MqlParam &b) const
  {
   if(a.type==TYPE_STRING || b.type==TYPE_STRING)
      return(a.type==b.type && a.string_value==b.string_value);
   if(!IsInteger(a.type) && (!MathIsValidNumber(a.double_value) || !MathIsValidNumber(b.double_value)))
      return(false);
   if(!IsInteger(a.type))
      return(MathAbs(a.double_value-b.double_value)<=1e-12*MathMax(1.0,MathAbs(a.double_value)));
   return(a.integer_value==b.integer_value);
  }
//+------------------------------------------------------------------+
//| Read the handle back and compare every input slot                |
//+------------------------------------------------------------------+
bool CIndicatorInputs::Verify(const int handle)
  {
   m_error="";
   MqlParam got[];
   ENUM_INDICATOR type;
   ResetLastError();
   int n=IndicatorParameters(handle,type,got);
   if(n!=Slots()+1)
     {
      m_error=StringFormat("the handle reports %d slots, the layout has %d (error %d)",n-1,Slots(),GetLastError());
      return(false);
     }
   for(int s=0; s<Slots(); s++)
     {
      if(m_group[s])
         continue;
      if(got[s+1].type!=m_slots[s].type || !Same(got[s+1],m_slots[s]))
        {
         m_error=StringFormat("slot %d does not hold the value that was set",s);
         return(false);
        }
     }
   return(true);
  }
//+------------------------------------------------------------------+
