More than one condition for ternary operator inside file write function

 

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

FileWrite(filehandle,
                   ...
                   StringFormat("InpSLCoef=%lf||100||50||700||N\n", InpSLCoef),
                   ...;

// I want to change to something like this
FileWrite(filehandle,
                   ...
                   StringFormat("InpSLCoef=0||%lf||%lf||%lf||Y\n", (InpTradeTimeframe ? PERIOD_M15 : 300,50,500 ? PERIOD_M5 : 50,15,110)),
                   ...;

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?

 

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 &param1, double &param2, double &param3, 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);
  }
 
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.
 
Luandre Ezra:
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:

https://www.mql5.com/ru/docs/common/printformat

 
Luandre Ezra #:
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

 
Vladislav Boyko #:

It looks like %lf is meaningless in MQL. But I'm not sure. See documentation:

https://www.mql5.com/ru/docs/common/printformat

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

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:

Missed this part of the documentation. It can only return "one of the two values".
 
Luandre Ezra #:
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));
  }
 
Vladislav Boyko #:

No variables (but still using switch):

This is a joke code written for fun. Don't take this code seriously

 
Luandre Ezra:

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?


You need to use it for each value. Like this:

StringFormat("InpSLCoef=0||%lf||%lf||%lf||Y\n", (InpTradeTimeframe == PERIOD_M15) ? 300 : 50, (InpTradeTimeframe == PERIOD_M15) ? 50 : 15, (InpTradeTimeframe == PERIOD_M15) ? 500 : 110);



 
Dominik Egert #:

You need to use it for each value. Like this:

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 
 
Luandre Ezra #:

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.

Then stop using the ternary operator and go with a switch statement.

You either are not understanding how the ternary operator works, or you are trying to force something you should not be doing in the first place.


Reason: