The ternary operator is not the best tool in this case, in my opinion
#property script_show_inputs input ENUM_TIMEFRAMES InpTradeTimeframe = PERIOD_M5; input double InpSLCoef = 1.0; void OnStart() { double param1 = 0.0, param2 = 0.0, param3 = 0.0; if(!chooseValuesDependingOnTf(param1, param2, param3, InpTradeTimeframe)) return; string text = generateTerxt(InpSLCoef, param1, param2, param3); Print(text); } string generateTerxt(double slCoeff, double param1, double param2, double param3) { return(StringFormat("InpSLCoef=%f||%f||%f||%f||Y\n", slCoeff, param1, param2, param3)); } bool chooseValuesDependingOnTf(double ¶m1, double ¶m2, double ¶m3, ENUM_TIMEFRAMES tf) { switch(tf) { case PERIOD_M5: param1 = 50.0; param2 = 15.0; param3 = 110.0; break; case PERIOD_M15: param1 = 300.0; param2 = 50.0; param3 = 500.0; break; default: Print(__FUNCTION__, " Unexpected timeframe: ", EnumToString(tf)); return(false); } return(true); }
This part return an error message
Ternary operator cannot return 3 values (only one).
https://www.mql5.com/en/docs/basis/operators/ternary
It looks like %lf is meaningless in MQL. But I'm not sure. See documentation:
using your method will create a lot of new variable since the variable that I want to change is more than one (there's still another 5 variable), it would create 18 new variable to store the value. Another method that I have is just creating new set file for different period, postpone it if there's another great alternatives.
The purpose of my example is to demonstrate the use of the switch statement. It's like pseudo code, you can implement it in any form you like
It looks like %lf is meaningless in MQL. But I'm not sure. See documentation:
yes, It should be %.lf, forgot to put full stop there.
Vladislav Boyko #:
Ternary operator cannot return 3 values (only one).
https://www.mql5.com/en/docs/basis/operators/ternary
Based on the value of "expression1", the operator must return one of the two values - either "expression2" or "expression3". There are several limitations to these expressions:
a lot of new variable
No variables (but still using switch):
#property script_show_inputs input ENUM_TIMEFRAMES InpTradeTimeframe = PERIOD_M5; class CParams { public: const double param1; const double param2; const double param3; const double param4; const double param5; const double param6; const double param7; const double param8; static string generateString(ENUM_TIMEFRAMES tf); static CParams* instantiateDependingOnTf(ENUM_TIMEFRAMES tf); static string toStr(const CParams& p); CParams(double p1, double p2, double p3, double p4, double p5, double p6, double p7, double p8) : param1(p1), param2(p2), param3(p3), param4(p4), param5(p5), param6(p6), param7(p7), param8(p8) {} }; string CParams::generateString(ENUM_TIMEFRAMES tf) { CParams* obj = CParams::instantiateDependingOnTf(tf); string text = CParams::toStr(obj); delete obj; return(text); } string CParams::toStr(const CParams &p) { return(StringFormat("||%f||%f||%f||%f||%f||%f||%f||%f||\n", p.param1, p.param2, p.param3, p.param4, p.param5, p.param6, p.param7, p.param8)); } CParams* CParams::instantiateDependingOnTf(ENUM_TIMEFRAMES tf) { switch(tf) { case PERIOD_M5: return(new CParams(10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0)); case PERIOD_M15: case PERIOD_M30: return(new CParams(18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0)); case PERIOD_H1: return(new CParams(28.0, 29.0, 30.0, 31.0, 32.0, 33.0, 34.0, 55.0)); default: return(new CParams(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)); } } void OnStart() { Print(CParams::generateString(InpTradeTimeframe)); }
Hi, I want to write a set file inside an EA. The condition is like this,
if time frame is PERIOD_M15 using this set value for this variable if it's PERIOD_M5 using this set value, in code would be like this
This part return an error message and I assume because format string can't referring to those value, is there a way to work with ternary operator when the value is more than one?
StringFormat("InpSLCoef=0||%lf||%lf||%lf||Y\n", (InpTradeTimeframe == PERIOD_M15) ? 300 : 50, (InpTradeTimeframe == PERIOD_M15) ? 50 : 15, (InpTradeTimeframe == PERIOD_M15) ? 500 : 110);
while your code working, I don't know how to make it works for other timeframe. This is the least error that I can do and it still return error.
StringFormat("InpSLCoef=0||%.lf||%.lf||%.lf||Y\n", (((InpTradeTimeframe == PERIOD_M15) ? 300 : (InpTradeTimeframe == PERIOD_M5) ? 50 : (InpTradeTimeframe == PERIOD_H1) ? 500 : 300), // first %.lf ((InpTradeTimeframe == PERIOD_M15) ? 50 : (InpTradeTimeframe == PERIOD_M5) ? 15 : (InpTradeTimeframe == PERIOD_H1) ? 100 : 50), // second %.lf ((InpTradeTimeframe == PERIOD_M15) ? 500 : (InpTradeTimeframe == PERIOD_M5) ? 110 : (InpTradeTimeframe == PERIOD_H1) ? 1000 : 500)), // third %.lf
while your code working, I don't know how to make it works for other timeframe. This is the least error that I can do and it still return error.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi, I want to write a set file inside an EA. The condition is like this,
if time frame is PERIOD_M15 using this set value for this variable if it's PERIOD_M5 using this set value, in code would be like this
This part return an error message and I assume because format string can't referring to those value, is there a way to work with ternary operator when the value is more than one?