Библиотеки: Input_Struct - страница 7

 

Author, how can I dynamically get parameter values? Not the default values. I wrote a function, but I ran into a problem when getting bool values. It won't compile because a string can't be converted to a bool. Although I can solve this problem with other tricks, I still want to ask you, is there a better way to get parameter values?

  template <typename T>
  T GetValue(const string Name)
  {
     #define MACROS(A, B, C) if (Name == #A) { return (T)this.A; }
     MACROS_MULTI
     #undef MACROS
     return NULL; // Not Found Name
  }

INPUT(CustomLot,                string,               "0.01-0.02")
INPUT(OpenLong,                 bool,                 false)

string inputName = "OpenLong";
//ok, 1.0 -> true,0.0 -> false
bool b = Inputs.GetValue<double>(inputName); //Inputs.GetValue<bool>(inputName);  --> cannot convert type 'string' to type 'bool' Input_Struct.mqh

 
hini #:

Author, how can I dynamically get parameter values? Not the default values. I wrote a function, but I ran into a problem when getting bool values. It won't compile because a string can't be converted to a bool. Although I can solve this problem with other tricks, I still want to ask you, is there a better way to get parameter values?

Не представляю сценарий использования такого функционала.

 

Suppose there are two sets of parameters corresponding to long and short order settings. The simplest example is whether to enable long order opening or short order opening, which are OpenLongOrder and OpenShortOrder respectively. There is a function that obtains the corresponding parameter value based on the isLong parameter:

string prefix = isLong ? "OpenLong" : "OpenShort";
bool value = Inputs.GetInputValue(prefix + "Order");
if (value) {
  
}

Not just these two parameters, but there is a series of parameters related to long and short orders, all starting with OpenLong and OpenShort respectively. In essence, there's a function that sets the same functionality based on long and short order settings. If not obtained this way, two separate functions would need to be written corresponding to long and short order settings. I hope this explanation is clear?

 
Почему не делать так?
bool value = isLong ? Inputs.OpenLongOrder : Inputs.OpenShortOrder;
if (value) {
  
}
 
fxsaber #:
Почему не делать так?
  INPUT(OpenLongMinLots,           double,               0.01)\
  INPUT(OpenLongMaxLots,           double,               999)\
  INPUT(OpenLongMinOrders,         int,                  2)\
  INPUT(OpenLongMaxOrders,         int,                  999)\
  INPUT(OpenLongTP,                double,               300)\
  INPUT(OpenLongSL,                double,               200)\
  //......
  INPUT(OpenShortMinLots,           double,               0.08)\
  INPUT(OpenShortMaxLots,           double,               999)\
  INPUT(OpenShortMinOrders,         int,                  2)\
  INPUT(OpenShortMaxOrders,         int,                  999)\
  INPUT(OpenShortTP,                double,               500)\
  INPUT(OpenShortSL,                double,               300)\
  //......

  Because there is more than one parameter, there are even more than ten or twenty parameters

hini #:
Not just these two parameters, but there is a series of parameters related to long and short orders, all starting with OpenLong and OpenShort respectively
    //example
    double amout = GetProfitAmount(GetInputValue<double>(prefix + "UseProfitAmout"),
                                    GetInputValue<double>(prefix + "ProfitAmout"));
    double pip = GetProfitPip(GetInputValue<double>(prefix + "UseProfitPip"),
                              GetInputValue<double>(prefix + "ProfitPip"));
 
hini #:

  Because there is more than one parameter, there are even more than ten or twenty parameters

А такой вариант?

#define GETVALUE(A) (isLong ? Inputs.OpenLong##A : Inputs.OpenShort##A)

double amout = GetProfitAmount(GETVALUE(UseProfitAmout), GETVALUE(ProfitAmout));
double pip = GetProfitPip(GETVALUE(UseProfitPip), GETVALUE(ProfitPip));
 
fxsaber #:

А такой вариант?

That's ok, thanks :)

 
hini #:

a string can't be converted to a bool.

Thanks, fixed.

INPUT(Flag, bool, "true")  // OK
 
fxsaber #:

Thanks, fixed.

  template <typename T>
  T GetValue(const string Name)
  {
     #define MACROS(A, B, C) if (Name == #A) { \
         T value;                              \
         Set_INPUT_STRUCT(value, this.A);      \
        return value; \
     }
     MACROS_MULTI
     #undef MACROS
     return NULL; // Not Found Name
  }

Great! , now it works.   :)

bool b = Inputs.GetValue<bool>(prefix + "UseProfitAmout")