ParameterGetRange()

 

How does one use this function?

The below code is not working.

 

//+------------------------------------------------------------------+
//|                                                         test.mq5 |
//|                                                   Copyright 2017 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017"
#property version   "1.00"

input int                                                     _r_0 = 0;
input int                                                    _r_1 = 0;
int                                                          _r[2];
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create timer
                EventSetTimer(PeriodSeconds(Period()));
      
                  for(int i=0;i<2;i++)
            {
                        string _name = "_r_"+IntegerToString(i);
                        bool _enabled = true;
                        long _value;
                        long _start,_step,_stop;
                        ParameterGetRange(_name,_enabled,_value,_start,_step,_stop);
                        _r[i] = (int)_value;
            }
            
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
                        printf(__FUNCSIG__+" r 0:"+IntegerToString(_r[0])+" r 1:"+IntegerToString(_r[1]));
                        printf(__FUNCSIG__+" actual... r 0:"+IntegerToString(_r_0)+" r 1:"+IntegerToString(_r_1));
                        
  }
//+------------------------------------------------------------------+
//| TesterInit function                                              |
//+------------------------------------------------------------------+
void OnTesterInit()
  {
//---
                for(int i=0;i<2;i++)
                  {
                                        ParameterSetRange("_r_"+IntegerToString(i),true,1.0,0,1,511);
                  }
                                                  
                  for(int i=0;i<2;i++)
            {
                        string _name = "_r_"+IntegerToString(i);
                        bool _enabled = true;
                        long _value;
                        long _start,_step,_stop;
                        ParameterGetRange(_name,_enabled,_value,_start,_step,_stop);
                        _r[i] = (int)_value;
            }
  }
//+------------------------------------------------------------------+
//| Tester function                                                  |
//+------------------------------------------------------------------+
double OnTester()
  {
//---
   double ret=0.0;
//---

                        printf(__FUNCSIG__+" loaded?... r 0:"+IntegerToString(_r[0])+" r 1:"+IntegerToString(_r[1]));
                        printf(__FUNCSIG__+" actual... r 0:"+IntegerToString(_r_0)+" r 1:"+IntegerToString(_r_1));

//---
   return(ret);
  }
//+------------------------------------------------------------------+
//| TesterPass function                                              |
//+------------------------------------------------------------------+
void OnTesterPass()
  {
//---
  
  }
//+------------------------------------------------------------------+
//| TesterDeinit function                                            |
//+------------------------------------------------------------------+
void OnTesterDeinit()
  {
//---
  
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();
      
  }
  
//+------------------------------------------------------------------+
 

Did you bother to read the documentation before posting ?

The function can be called only from OnTesterInit(), OnTesterPass() and OnTesterDeinit() handlers.

 
Alain Verleyen:

Did you bother to read the documentation before posting ?

I posted the entire code here. You should have made such a comment only after running the code. 

And to my point ParameterGetRange() is not working in TesterInit()
 
strategy_tester:
I posted the entire code here. You should have made such a comment only after running the code. 

And to my point ParameterGetRange() is not working in TesterInit()

  • You are using it in OnInit() which is not allowed. If you had checked the returned value you would have seen it.
  • "Not working" is meaning less.
  • There is no problem with ParameterGetRange(), you have a problem to use it. 
  • My previous comment is perfectly valid and I suggest you kindly to get off your big horses.
  • You don't know if I ran your code or not, stop guessing. Read the documentation and understand it, stop guessing how it should work.

//+------------------------------------------------------------------+
//|                                                         test.mq5 |
//|                                                   Copyright 2017 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017"
#property version   "1.00"

input int                                                     _r_0 = 0;
input int                                                    _r_1 = 0;
int                                                          _r[2];
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create timer
                EventSetTimer(PeriodSeconds(Period()));
      // Can't use ParameterGetRange() here. Check the return value, read the documentation !
                  for(int i=0;i<2;i++)
            {
                        string _name = "_r_"+IntegerToString(i);
                        bool _enabled = true;
                        long _value;
                        long _start,_step,_stop;
                        ParameterGetRange(_name,_enabled,_value,_start,_step,_stop);
                        _r[i] = (int)_value;
            }

            
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---

// r[] array is never initialized correctly.


                        printf(__FUNCSIG__+" r 0:"+IntegerToString(_r[0])+" r 1:"+IntegerToString(_r[1]));
                        printf(__FUNCSIG__+" actual... r 0:"+IntegerToString(_r_0)+" r 1:"+IntegerToString(_r_1));
                        
  }
//+------------------------------------------------------------------+
//| TesterInit function                                              |
//+------------------------------------------------------------------+
void OnTesterInit()
  {
//---
                for(int i=0;i<2;i++)
                  {
                                        ParameterSetRange("_r_"+IntegerToString(i),true,1.0,0,1,511);
                  }
                                                  
                  for(int i=0;i<2;i++)
            {
                        string _name = "_r_"+IntegerToString(i);
                        bool _enabled = true;
                        long _value;
                        long _start,_step,_stop;
                        ParameterGetRange(_name,_enabled,_value,_start,_step,_stop);
                        _r[i] = (int)_value;
            }
  }
//+------------------------------------------------------------------+
//| Tester function                                                  |
//+------------------------------------------------------------------+
double OnTester()
  {
//---
   double ret=0.0;
//---
// r[] array is never initialized correctly.
                        printf(__FUNCSIG__+" loaded?... r 0:"+IntegerToString(_r[0])+" r 1:"+IntegerToString(_r[1]));
                        printf(__FUNCSIG__+" actual... r 0:"+IntegerToString(_r_0)+" r 1:"+IntegerToString(_r_1));

//---
   return(ret);
  }
//+------------------------------------------------------------------+
//| TesterPass function                                              |
//+------------------------------------------------------------------+
void OnTesterPass()
  {
//---
  
  }
//+------------------------------------------------------------------+
//| TesterDeinit function                                            |
//+------------------------------------------------------------------+
void OnTesterDeinit()
  {
//---
  
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();
      
  }
  
//+------------------------------------------------------------------+

 
It is still not working. (Even with initializing the array)
 
strategy_tester:
It is still not working. (Even with initializing the array)
Ok. What are you trying to do ?
 
strategy_tester: It is still not working. (Even with initializing the array)
  1. "Doesn't work" is meaningless - just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires - meaningless. There are no mind readers here.
  2. No one can even try to answer you. We can't see your new code. There are no mind readers here.
 

I'm adding a few comments on using ParameterGetRange() and ParameterSetRange() for posterity.


(1) The Print() command does not work in the optimizer. Per the documentation:

Print() function does not work during optimization in the Strategy Tester.

Well, that sums it up pretty well. What threw me was the example on the ParameterGetRange(), which has Print() statements. These do not actually print anything anywhere. Print() inside OnInit() still prints, but Print() within OnTesterInit(), OnTesterDeinit(), and OnTesterPass() does not.

I get the sense that Print() used to work but was pulled for some reason. Perhaps because log files get too large.

To that end, I create my own log file to dump values to see what's happening.

(2) The ParameterGetRange() function sort of works. By "sort of works" I mean:

(2.a) The function will return valid enable,value,start,step,stop values iff the Input parameter is initially checked. If the Input parameter is not checked, only the enable and value values are correct, i.e. start,step,stop are always 0.

(2.b) If I change parameters using ParameterSetRange(), then re-poll these values using ParameterGetRange(), I do not get the changed values. However, do note, the values were actually changed internally, which is evidenced by the testing results.

Here's some code. Think of DEBUG() as Print().

void OnTesterInit()
{
.
.
.

        // Original values are: checked, 0., 0., 5., 10.
        parameterName = "InpLimitOrderPips";
        if ( ! ParameterGetRange(parameterName,enable,dblValue, dblStart, dblStep, dblStop))
        {
                DEBUG(StringFormat("%s ParameterGetRange failed!", parameterName));
        }
        else
        {
                DEBUG(StringFormat("%s enable [%d] value [%.1f] start [%.1f] step [%.1f] stop [%.1f]",parameterName,enable,dblValue, dblStart, dblStep, dblStop));      
        }

        // Now set new values. Leave enable,dblValue to original values.
        if ( ! ParameterSetRange(parameterName,enable,dblValue, 3., 3., 9.) )
        {
                DEBUG(StringFormat("%s ParameterSetRange failed!", parameterName));
        }

        // Read values again (to dump to log)
        if ( ! ParameterGetRange(parameterName,enable,dblValue, dblStart, dblStep, dblStop))
        {
                DEBUG(StringFormat("%s ParameterGetRange failed!", parameterName));
        }
        else
        {
                DEBUG(StringFormat("%s enable [%d] value [%.1f] start [%.1f] step [%.1f] stop [%.1f]",parameterName,enable,dblValue, dblStart, dblStep, dblStop));
        }
}

Here's the result in the log file:

2017.11.15 17:33:09 InpLimitOrderPips enable [1] value [0.0] start [0.0] step [5.0] stop [10.0] - Mr-Pulse.mq5 (1827)
2017.11.15 17:33:09 InpLimitOrderPips enable [1] value [0.0] start [0.0] step [5.0] stop [10.0] - Mr-Pulse.mq5 (1839)

And here's what the Optimization Results tab shows:

Optimization Results

You can see that the start, step, stop values were indeed switched to 3,3,9.

 
Anthony Garot:

I'm adding a few comments on using ParameterGetRange() and ParameterSetRange() for posterity.

Форум по трейдингу, автоматическим торговым системам и тестированию торговых стратегий

Ошибки, баги, вопросы

fxsaber, 2017.08.15 17:49

ParameterGetRange does not work in OnTesterInit.
sinput int Range = 5;

#define PRINT(A) Print(#A + " = " + (string)(A));

void PrintParameter( const string Name, const string From )
{
  PRINT(From)
  
  bool Enable;
  long Value, Start, Step, Stop;
  
  if (ParameterGetRange(Name, Enable, Value, Start, Step, Stop))
  {
    PRINT(Start)
    PRINT(Step)
    PRINT(Stop)
    PRINT(Value)
    PRINT(Enable)
  }   
}

void OnTesterInit()
{
  ParameterSetRange("Range", true, 5, 1, 2, 3); // All nonzero values were set
  
  PrintParameter("Range", __FUNCTION__); // Returns one zeros
}

void OnTesterDeinit()
{
  PrintParameter("Range", __FUNCTION__); // Works fine
  
  ChartClose();
}

void OnTesterPass()
{
  PrintParameter("Range", __FUNCTION__); // Works fine
}

int OnInit()
{
  uchar Data[];
  
  FrameAdd(NULL, 0, 0, Data);
  
  return(INIT_FAILED);
}
 
  • ParametersGetRange() works perfectly, it returns the values as set in the Strategy Tester GUI. There is no point to use it to see the values you just set with ParametersSetRange(). What to do anyway ? You just set them why would you want to read them. If you want to check something, test the return value of the function and GetLastError() eventually.
  • If a parameter is NOT checked in the GUI it's perfectly normal the start/step/stop are 0 when using ParametersGetRange. Ok the values from the GUI could be available...but what to do ? they are not used.
  • When you use ParametersSetRange(), it's logged in the tester Journal, exemple :

2017.12.16 18:17:07.301 Tester input parameter 'TrailingStop' set to: enable=true, value=0, start=10, step=1, stop=20

but I agree it should be possible to write in main tester log from OnTesterXXX() functions.
  • When you are using OnTesterXXX functions() a chart is open, you can use "Comment" on this chart in place of Print statement.
 
For up to date information (build 1940), Print() statement are now working within OnTesterInit(), OnTesterPass() and OnTesterDeinit(). Not sure from which build.
Reason: