Switch operator to change variable according to timeframe

 

Hi everyone, I want to create a strategy that can be used in any timeframe. In the strategy there's a dynamic variable and there's a static variable (hardcoded). For it to works in any timeframe I need to change the static variable into a semi-static variable (changing the value according to timeframe). To achieve this I have two guesses,

1. Creating an if statement for every static variable. This way is kinda long but I know it will works.

2. Using a switch operator. Honestly I never use switch operator before.

the scheme if I want to go along using a switch is like:

Check timeframevar = Periode(); //return of timeframe is int

switch (timeframevar)
        {
           //using the timeframevar return as case constant
   
           case (timeframevar) :   //timeframe M5 
                staticvariable1 = 
                staticvariable2 =
                break;
           case (timeframevar) :   //timeframe M15
                staticvariable1 =
                staticvariable2 =
                break;
        }

Since I'm new to switch, my question is:

1. Can I use the timeframevar return as the case constant?

2. Does the case constant working like an if statement? e.g. if the return of timeframevar is "a" then do the following?

 
Luandre Ezra:

1. Can I use the timeframevar return as the case constant?

2. Does the case constant working like an if statement? e.g. if the return of timeframevar is "a" then do the following?

  • you need to match datatype pass to switch operator, with case constant.
  • in your example, you pass an int type (timeframe), case should be int type too.. likewise if pass a string type..
  • code below as reference

switch(timeframevar) {
      case 5:                //timeframe M5 
         staticvariable1 = 5;
         staticvariable2 = 5;
         break;
      case 15:               //timeframe M15
         staticvariable1 = 15;
         staticvariable2 = 15;
         break;
      default: 
         staticvariable1 = 0;
         staticvariable2 = 0;;
}
 
Mohamad Zulhairi Baba:

  • you need to match datatype pass to switch operator, with case constant.
  • in your example, you pass an int type (timeframe), case should be int type too.. likewise if pass a string type..
  • code below as reference

Thank you for your answer and adding the default setting that I forgot.
Reason: