Discussion of article "Parallel Particle Swarm Optimization"

 

New article Parallel Particle Swarm Optimization has been published:

The article describes a method of fast optimization using the particle swarm algorithm. It also presents the method implementation in MQL, which is ready for use both in single-threaded mode inside an Expert Advisor and in a parallel multi-threaded mode as an add-on that runs on local tester agents.

From an algorithmic point of view, the PSO method is relatively simple. The main idea is to generate a set of virtual "particles" in the space of the Expert Advisor's input parameters. The particles then move and change their speed depending on the EA's trading metrics at the corresponding points in space. The process is repeated many times until the performance stops improving. The pseudo-code of the algorithm is shown below:

Particle Swarm Optimization Pseudo-Code

Particle Swarm Optimization Pseudo-Code

According to this code, each particle has a current position, speed and memory of its "best" point in the past. Here, the "best" point means the point (a set of EA input parameters), where the highest value of the objective function for this particle was achieved. Let us describe this in a class.

  class Particle
  {
    public:
      double position[];    // current point
      double best[];        // best point known to the particle
      double velocity[];    // current speed
      
      double positionValue; // EA performance in current point
      double bestValue;     // EA performance in the best point
      int    group;
      
      Particle(const int params)
      {
        ArrayResize(position, params);
        ArrayResize(best, params);
        ArrayResize(velocity, params);
        bestValue = -DBL_MAX;
        group = -1;
      }
  };

The size of all arrays is equal to the dimension of the optimization space, so it is equal to the number of the Expert Advisor parameters being optimized (passed to the constructor). By default, the larger the objective function value, the better the optimization. Therefore, initialize the bestValue field with the minimum possible -DBL_MAX number. One of the trading metrics is usually used as a criterion for evaluating an EA, such as profit, profitability, Sharpe ratio, etc. If optimization is performed by the parameter whose lower values are considered better, such as for example drawdown, appropriate transformations can be made to maximize opposite values.

Arrays and variables are made public to simplify access and their recalculation code. Strict adherence to the OOP principles would require hiding them using the 'private' modifier and describing read and modify methods.

Author: Stanislav Korotky

 

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

Обсуждение статьи "Параллельная оптимизация методом роя частиц (Particle Swarm Optimization)"

Stanislav Korotky, 2020.08.24 10:45

I am attaching a slightly modified header file and an example of a test EA. The Settings class, helper functors, and templated event handlers have been added to the ParticleSwarmEmbed.mqh file. It is assumed that the user is satisfied with the default implementation. Then the code of the Expert Advisor ExprBotPSOEmbed.mq5 is greatly simplified. It is enough to describe your trading calculation and to forward the handlers to the PPSO_EventHandlers class. The input variables also contain the PSO dynamics adjustment factors (inertia, etc.), as well as the disabling of index files.
Файлы:

 

Hey, great topic, unfortunately when trying to connect all the pieces, compiler still throws errors:

It looks as if due to some update it is not possible now? If you could look into it, that would be nice.

 
Tobias Johannes Zimmer #:

Hey, great topic, unfortunately when trying to connect all the pieces, compiler still throws errors:

The vector and the position compilation error belong together since position is an array of the dimensions (degrees of freedom/inputs) I guess which could be inserted into the calculate method, but due to some update it is not possible now? And then the positions[] array can't be casted into a positionValue... if you could look into it, that would be nice.

Then there are things about worker functor where I have no clue what seems to be the problem.

I hope there is an easy way to fix this. If there isn't I would totally understand if you would prefer not to.

Just make a context substitution of "vector" to "_vector" in ParticleSwarmParallel.mqh or/and ParticleSwarmEmbed.mqh.

MQL5 has introduced vector type since the publication, this broke many sources codes where the identifier vector has been used already.

 
Stanislav Korotky #:

Just make a context substitution of "vector" to "_vector" in ParticleSwarmParallel.mqh or/and ParticleSwarmEmbed.mqh.

MQL5 has introduced vector type since the publication, this broke many sources codes where the identifier vector has been used already.

Oh of course vector shouldn't be a type... thank you, that works.
 
cannot convert  to enum
 
dustovshio #:
cannot convert  to enum

MT4Orders and Virtual are 3-rd party libraries. You should check for latest versions on their pages in the codebase.

 
Stanislav Korotky #:

MT4Orders and Virtual are 3-rd party libraries. You should check for latest versions on their pages in the codebase.

I downloaded the lasted MT4Orders and it compiled.  However now it gives me this error after I load the PSO set files. There is no error for standard optimize mode.  Is it possible to change the dates to optimize to the last bar instead of the last day? 



 
dustovshio #:

I downloaded the lasted MT4Orders and it compiled.  However now it gives me this error after I load the PSO set files. There is no error for standard optimize mode.  Is it possible to change the dates to optimize to the last bar instead of the last day? 

If you're asking about "incorrect input parameters" error, you can double click optimization table on the pass which gives the error, and get the single pass log for detailed description what goes wrong. Also you did not provide info about which EA and with what settings you've tried to run.

I have already told you that the last-day-out limitation is imposed by the tester itself.