Libraries: Expert - page 3

 
An example of another application of the library

Forum on trading, automated trading systems and testing trading strategies

ParameterGetRange()

fxsaber, 2018.11.16 09:17

#include <fxsaber\Expert.mqh> // https://www.mql5.com/en/code/19003

#define  TOSTRING(A) (" " + #A + " = " + (string)(A))

// Collects data on the range of input parameters for optimisation
string OptimizationData( void )
{
  string Str = NULL;
  MqlParam Params[];
  string Names[];
  
  if (EXPERT::Parameters(0, Params, Names))
  {
    const int Size = ArraySize(Names);
    bool Enable;
    long Value, Start, Step, Stop;
    long Total = 1;
    
    Str = Params[0].string_value;
    
    for (int i = 0; i < Size; i++)
      if (ParameterGetRange(Names[i], Enable, Value, Start, Step, Stop))
      {
        const long Interval = Stop - Start + 1;
        const long Amount =  Enable ? Interval / Step + ((bool)(Interval % Step) ? 1 : 0) : 1;
        
        Str += "\n" + Names[i] + " = " + (string)Value + (Enable ? TOSTRING(Start) + TOSTRING(Step) + TOSTRING(Stop) + TOSTRING(Amount) : NULL);
        
        Total *= Amount;
      }
      
    Str += "\n" + TOSTRING(Total);
  }
  
  return(Str);
}

input int Range1 = 5;
input int Range2 = 5;
input int Range3 = 5;

void OnTesterInit()
{
  ParameterSetRange("Range1", true, 5, 1, 2, 3);
  
  Print(__FUNCTION__ + "\n" + OptimizationData());
}

void OnTesterDeinit()
{
  Print(__FUNCTION__ + "\n" + OptimizationData());
  
  ChartClose();
}

int OnInit()
{
  return(INIT_FAILED);
}


OnTesterInit
Experts\fxsaber\Test3.ex5
Range1 = 5 Start = 5 Step = 1 Stop = 50 Amount = 46
Range2 = 5 Start = 23 Step = 1 Stop = 78 Amount = 56
Range3 = 5 Start = 26 Step = 5 Stop = 83 Amount = 12
 Total = 30912

OnTesterDeinit
Experts\fxsaber\Test3.ex5
Range1 = 5 Start = 1 Step = 2 Stop = 3 Amount = 2
Range2 = 5 Start = 23 Step = 1 Stop = 78 Amount = 56
Range3 = 5 Start = 26 Step = 5 Stop = 83 Amount = 12
 Total = 1344
 
 
Fixed bug. Last version.
 

How can you run an Expert Advisor in the .ex4 format by connecting it in an Expert Advisor with a different name, and it is necessary that the input parameters were displayed, i.e. it was possible to fully work with it.

We have a compiled Expert Advisor with the name "Trade.ex4".

Create a new Expert Advisor with the name "Hand.ex4".

How to combine them so that when "Hand.ex4" is launched, "Trade.ex4" will be fully functional and "Trade.ex4" will be embedded in "Hand.ex4".

That is, we have only Hand.ex4 file on the machine, but we use Trade.ex4 in work

Thank you!

 
Vitaly Muzichenko:

How can you run an Expert Advisor in .ex4 format...

The library in its current form works only with EX5.

 
Sergey Eremin:

In myself, as part of a certain project, I solved it this way (with the "name" tag just like that):

Thanks, it works like that!

Only I added chartId to the name of the saved template, so that identical EAs on different charts don't fight for the same file.


@fxsaber, there is "#ifndef __MQL5__" in the code, why not take cross-platform to its logical conclusion?

To make Is work, we add to the beginning of the code:

#property strict

#ifdef __MQL4__
        class SubstringParser
        {
           private:
              string m_text;
              string m_subStart;
              string m_subEnd;
           
           public:
              SubstringParser(const string text, const string subStart, const string subEnd)
              {
                 m_text = text;
                 m_subStart = subStart;
                 m_subEnd = subEnd;
              }
              
              string Get()
              {
                 int startPhraseLengt = StringLen(m_subStart);
                 int startPos = StringFind(m_text, m_subStart) + startPhraseLengt;
                 int endPos = StringFind(m_text, m_subEnd, startPos);
                         
                 if(startPos >= startPhraseLengt && endPos > startPos)
                 {
                    return StringSubstr(m_text, startPos, endPos - startPos);      
                 }
                 else
                 {
                    return "";
                 }
              }
        };

   string GetChartEAName(const long chartId)
   {
      if(!SaveTemplate(chartId))
      {
         return "";
      }
      
      string result = "";
      
      int handle = FileOpen("ea_name_checking_" + (string)chartId + ".tpl",FILE_TXT|FILE_READ);
      if(handle == INVALID_HANDLE)
      {
         Print
         (
            "Error in ", __FILE__,", line ", __LINE__,
            ": can't open template file 'ea_name_checking_" + (string)chartId + ".tpl', error code = ", GetLastError()
         );
      }
      else
      {
         string text = "";
         
         while(!FileIsEnding(handle)) 
         { 
            text = text + FileReadString(handle, (uint)FileSize(handle)) +"\r\n";
         }
         
         SubstringParser eaSectionTextParser(text, "<expert>", "</expert>");            
         string eaSectionText = eaSectionTextParser.Get();
         
         if(StringTrimLeft(StringTrimRight(eaSectionText)) != "")
         {
            SubstringParser eaNameParser(eaSectionText, "name=","\r\n");
            string eaName = StringTrimLeft(StringTrimRight(eaNameParser.Get()));
            
            if(eaName != "")               
            {
               result = eaName;
            }
         }            
      }
      
      FileClose(handle);
      FileDelete("ea_name_checking_" + (string)chartId + ".tpl");
      
      return result;
   }
   
   bool SaveTemplate(const long chartId)
   {
      ResetLastError();
      
      if(!ChartSaveTemplate(chartId, "\\Files\\ea_name_checking_" + (string)chartId + ".tpl"))
      {            
         Print
         (
            "Error in ", __FILE__,", line ", __LINE__,
            ": can't save template to the file 'ea_name_checking_" + (string)chartId + ".tpl', error code = ", GetLastError()
         );
         
         return false;
      }
      
      return true;
   }      
 #endif

And correct the function itself:

  static bool Is( const long Chart_ID = 0 )
  {
                #ifdef __MQL4__
                        string chartEAName = GetChartEAName(Chart_ID);
                #endif 
                
                #ifdef __MQL5__     
                        string chartEAName = ChartGetString(Chart_ID, CHART_EXPERT_NAME);
                #endif
                return(chartEAName != NULL);
  }


Any other difficulties?

 
Andrey Khatimlianskii:

Any other difficulties?

A chart with only one indicator running.


ZY This is how it should work

SubstringParser eaSectionTextParser(text, "\r\n\r\n<expert>", "</expert>");
 

All library functionality now works on MT4.

The library has become cross-platform.

 
All library functionality now works on MT4.
The library has become cross-platform.


MT4-example:

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

Библиотеки: Expert

fxsaber, 2019.04.09 13:19

#include <fxsaber\Expert.mqh> // https://www.mql5.com/ru/code/19003

void OnStart()
{
  MqlParam Params[2];

  // Путь к советнику
  Params[0].string_value = "Moving Average";
  
  // Первый входной параметр советника
  Params[1].type = TYPE_DOUBLE;
  Params[1].double_value = 0.5;  

  Print(EXPERT::Run(ChartOpen(_Symbol, _Period), Params));
}
 
Vitaly Muzichenko:

What is the best way to run an EA in .ex4...

#include <fxsaber\Expert.mqh> // https://www.mql5.com/en/code/19003

void OnStart()
{
  MqlParam Params[2];

  // Pathway to Counsellor
  Params[0].string_value = "Moving Average";
  
  // The first input parameter of the Expert Advisor
  Params[1].type = TYPE_DOUBLE;
  Params[1].double_value = 0.5;  

  Print(EXPERT::Run(ChartOpen(_Symbol, _Period), Params));
}