Discussion of article "Adaptive Trading Systems and Their Use in the MetaTrader 5 Client Terminal" - page 2

 

Dear Forexistence,

You have answered your question :)

You are right, there are two ways if you want to modify trading conditions, from one side we can add new conditions to strategy CStrategyMA (but we will get the new strategy, different from the initial), the other way is to create a new class (for example, CStrategyMA34) and add additional buy/sell conditions there.

But of course, you should include file with your new strategy and add these new strategies to the Expert_OnInit function of CAdaptiveStrategy class:

#include <CStrategyMA3and4.mqh>

int CAdaptiveStrategy::Expert_OnInit()
{
.....
--- adding new strategies
   for(int i=0; i<10; i++)
     {
      CStrategyMA34 *t_StrategyMA34;
      t_StrategyMA34=new CStrategyMA34;
      if(t_StrategyMA34==NULL)
        {
         delete m_all_strategies;
         Print("Error of creation of object of the CStrategy3and4 type");
         return(-1);
        }
      int period=3+i*10;
      t_StrategyMA34.Initialization(period,true);
      t_StrategyMA34.SetStrategyInfo(_Symbol,"[MA34_"+IntegerToString(period)+"]",period,"Moving Averages with new conditions "+IntegerToString(period));
      m_all_strategies.Add(t_StrategyMA34);
     }
.....
}

The second way is better, you can add many strategies and their variations.

It isn't necessary to remove the CStrategyMA class instances (if you have ones), we will not look in their sandboxes in their results will be bad.

The market will help us to determine the best strategy in the list of strategies, included in m_all_strategies.

 
Quantum:

Dear Forexistence,

  >>Dear 

You have answered your question :) 

 >> Yes, maybe partly i've answered my question myself, and just want a confirmation or different opinions.

 

You are right, there are two ways if you want to modify trading conditions, from one side we can add new conditions to strategy CStrategyMA (but we will get the new strategy, different from the initial), the other way is to create a new class (for example, CStrategyMA34) and add additional buy/sell conditions there.

But of course, you should include file with your new strategy and add these new strategies to the Expert_OnInit function of CAdaptiveStrategy class:

The second way is better, you can add many strategies and their variations.

It isn't necessary to remove the CStrategyMA class instances (if you have ones), we will not look in their sandboxes in their results will be bad.

The market will help us to determine the best strategy in the list of strategies, included in m_all_strategies.

 

>> Ok got the point as i imagined.

 

But the question, now, is another.. I'm working to "inputize" the EA for the goal to work with optimizations.

So in example i mean pieces of codes like this:

 

for(int i=0; i<RSIcicle; i++)
     {
      CStrategyRSI *t_StrategyRSI;
      t_StrategyRSI=new CStrategyRSI;
      if(t_StrategyRSI==NULL)
        {
         delete m_all_strategies;
         Print("Error of creation of object of the CStrategyRSI type");
         return(-1);
        }

      

       ////////////////////////// PLEASE LOOK AT THIS FOLLOWING PIECE OF CODE ///////////////////////
   //set period for each strategy
    int period=Trsi+Drsi*i;
    
    ENUM_TIMEFRAMES RSItempoframe=RSItempoframe_in; // SET TIMEFRAME (enum), INPUT, FOR STRATEGY 
    ENUM_APPLIED_PRICE RSIappprice=RSIappprice_in;  // SET APPPLIED PRICE (enum), INPUT, FOR STRATEGY
    
    int RSIvarA_root=RSIvarA_in;
    int RSIvarB_root=RSIvarB_in;
    
      // initialization of strategy
      t_StrategyRSI.Initialization(period,true,RSItempoframe,RSIappprice,RSIvarA_root,RSIvarB_root);
      /////////////////////////////////////////////////////////////////////////////////////////////


      // set details of the strategy
      t_StrategyRSI.SetStrategyInfo(_Symbol,"[RSI_"+IntegerToString(period)+"]",period,"Relative Strenght Index "+IntegerToString(period));
      
      if(t_StrategyRSISet_Stops_ ==true) {
      t_StrategyRSI.Set_Stops(tSMSSone,tSMSStwo); //3500,1000);
      }

where (declarations) : 

//+------------------------------------------------------------------+
//|                                            CAdaptiveStrategy.mqh |
//|                        Copyright 2010, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2010, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"

#include <Arrays\ArrayObj.mqh>
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
#include <CStrategyMA.mqh>
#include <CStrategyMAinv.mqh>
#include <CStrategyStoch.mqh>

#include <CStrategyRSI.mqh>   // ADDED STRATEGY

/*
......
*/

input int Trsi=0, Drsi=10, RSIcicle=10;

input ENUM_TIMEFRAMES RSItempoframe_in= PERIOD_CURRENT;
input ENUM_APPLIED_PRICE RSIappprice_in= PRICE_CLOSE;

input int RSIvarA_in=60, RSIvarB_in=40;




input int tSMSSone=3500, tSMSStwo=1000;


input bool t_StrategyRSISet_Stops_=false;

/*
...
*/

//+------------------------------------------------------------------+
//| Class CAdaptiveStrategy                                          |

/*
...
*/

 

 and where variables:

 

RSItempoframe, RSIappprice, RSIvarA_root, RSIvarB_root 

work in collaboration with

//+------------------------------------------------------------------+
//|                                                  CStrategyMA.mqh |
//|                        Copyright 2010, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2010, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#include <CSampleStrategy.mqh>

input int mval=0, mval1=1, mval2=2, mval3=3, mval4=4, mvalVar=0, mvalFix=0,

          mvalVar2=0, mvalFix2=0;
          

//+------------------------------------------------------------------+
//| Class CStrategyMA for implementation of virtual trading          |
//| by the moving average strategy                                   |
//+------------------------------------------------------------------+
class CStrategyRSI : public CSampleStrategy
  {
protected:
   int               m_handle;     // handle of the Moving Average (iMA) indicator
   int               m_period;     // period of the Moving Average indicator
   double            m_values[];   // array for storing the value of indicator





   int               RSIvarA, RSIvarB; 
   
   ENUM_TIMEFRAMES   m_timeframe;
   ENUM_APPLIED_PRICE m_appprice;



   
public:
   // initialization of strategy // Notice added inputs


   int               Initialization(int period,bool virtual_trade_flag,ENUM_TIMEFRAMES RSItempoframe,
                                    ENUM_APPLIED_PRICE RSIappprice,int RSIvarA_root, int RSIvarB_root);


   // deinitialization of the strategy
   int               Deinitialization();
   // check of the trade conditions and opening of virtual positions
   bool              CheckTradeConditions();
  };
//+------------------------------------------------------------------+
//| Method of initialization of the strategy                         |
//+------------------------------------------------------------------+



int CStrategyRSI::Initialization(int period,bool virtual_trade_flag,ENUM_TIMEFRAMES RSItempoframe,
                                 ENUM_APPLIED_PRICE RSIappprice,int RSIvarA_root, int RSIvarB_root)


  {
   //set period for the RSI     (was //set period for the moving average)
   m_period=period;
      
   m_timeframe=RSItempoframe;
   m_appprice=RSIappprice;
   
   RSIvarA=RSIvarA_root;
   RSIvarB=RSIvarB_root;

/*
...
*/

//create handle of the indicator 
   
   m_handle=iRSI(_Symbol,m_timeframe,m_period,m_appprice);

/*
...
*/

//+------------------------------------------------------------------+
//| checking the trade conditions and opening the virtual positions  |
//+------------------------------------------------------------------+
bool CStrategyRSI::CheckTradeConditions()
  {
   RecalcPositionProperties(m_position);
  // double p_close;

/*
...
*/
   
     if(CopyRates(_Symbol,m_timeframe,0,3,m_rates)<0)

{
      Alert("Error of copying history data - error:",GetLastError(),"!!");
      return(false);
     }
// Copy the current close price of the previous bar (this is bar 1)
  // p_close=m_rates[1].close;  // close price of the previous bar          

   if(CopyBuffer(m_handle,0,0,3,m_values)<0)
     {
      Alert("Error of copying buffers of the Moving Average indicator - error number:",GetLastError());
      return(false);
     }

/*
...
*/

// Buy condition 1: RSI rises
   bool buy_condition_1=(m_values[mval]>m_values[mval1]) && (m_values[mval1]>m_values[mval2]);  // NOTICE integers variable in the [] of the array

// Buy condition 2: previous close price is higher than MA
 //  bool buy_condition_2=(p_close>m_values[1]);


     bool buy_condition_2=(m_values[mval+mvalVar+mvalFix]>RSIvarA);

/*
... etc..
*/


   

My question is:

It's opportune to modify the code, in order to have input variable AT strategy classfile (include file i.e CStrategyRSI.mqh) level , or with the proper hiearachy/connections/trees AT the other class/includes level (CAdaptiveStrategy.mqh) ?

In other words, for affecting the core system of the EA idea, with the adding of some input parameters for optimizations testing,

i should do something like this :  (input variables in CAdaptiveStrategy.mqh)

/*
...
*/

class CStrategyRSI : public CSampleStrategy
  {
protected:
   int               m_handle;     // handle of the Moving Average (iMA) indicator
   int               m_period;     // period of the Moving Average indicator
   double            m_values[];   // array for storing the value of indicator
   int               RSIvarA, RSIvarB;
   
   ENUM_TIMEFRAMES   m_timeframe;
   ENUM_APPLIED_PRICE m_appprice;
   
public:
   // initialization of strategy
   int               Initialization(int period,bool virtual_trade_flag,ENUM_TIMEFRAMES RSItempoframe,
                                    ENUM_APPLIED_PRICE RSIappprice,int RSIvarA_root, int RSIvarB_root);

/*
...
*/ 

//set period for the RSI     (was //set period for the moving average)
   m_period=period;
      
   m_timeframe=RSItempoframe;
   m_appprice=RSIappprice;
   
   RSIvarA=RSIvarA_root;
   RSIvarB=RSIvarB_root;

/*
...
*/

//create handle of the indicator 
   
   m_handle=iRSI(_Symbol,m_timeframe,m_period,m_appprice);

/*
...
*/
 

or some other like this: (input variables in CSampleRSI.mqh)

/*
...
*/

input   int               RSIvarA_root, RSIvarB_root;
   
input  ENUM_TIMEFRAMES  RSItempoframe=PERIOD_M5;
input   ENUM_APPLIED_PRICE RSIappprice=PRICE_CLOSE;
   

class CStrategyRSI : public CSampleStrategy
  {
protected:
   int               m_handle;     // handle of the Moving Average (iMA) indicator
   int               m_period;     // period of the Moving Average indicator
   double            m_values[];   // array for storing the value of indicator

  ENUM_TIMEFRAMES   m_timeframe;
  ENUM_APPLIED_PRICE m_appprice;

int               RSIvarA, RSIvarB;

public:
   // initialization of strategy
   int               Initialization(int period,bool virtual_trade_flag);  // NOTICE THE ORIGINAL CODE

/*
...
*/ 

//set period for the RSI     (was //set period for the moving average)
   m_period=period;
      
   m_timeframe=RSItempoframe;
   m_appprice=RSIappprice;
   
   RSIvarA=RSIvarA_root;
   RSIvarB=RSIvarB_root;

/*
...
*/

//create handle of the indicator 
   
   m_handle=iRSI(_Symbol,m_timeframe,m_period,m_appprice);

/*
...
*/

 In other more words, input variables should be used in the strategy include file, before class declaration (example2, just this above here) or input variables should

derive from the "for" cicle in the CAdaptive strategy, and should take consideration of all classes initializazion and input parameters? (i mean for this first example(just above this code above) :)

public:
// initialization of strategy
int Initialization(int period,bool virtual_trade_flag,ENUM_TIMEFRAMES RSItempoframe,
ENUM_APPLIED_PRICE RSIappprice,int RSIvarA_root, int RSIvarB_root);

 

How differently will affect the core of the system, if using input variables in the CStrategyRSI.mqh file or in the CAdaptiveStrategy.mqh? 

 

I suppose, if just a matter of testing, i can use input variables in the  CStrategyRSI.mqh, if instead i want both testing and also

affecting the core system of the EA idea (the adaptive system and trade simulator, the virtual trader include) i should declare

input variables not in the strategy include, but in the Adaptive include file, with all proper connections and calling

of input/output of the inizialization and declaration of class/functions?

Right?

 

I hope you got my point.

 Tnkx 

 

 

 

 

 


 
forexistence:

....But the question, now, is another.. I'm working to "inputize" the EA for the goal to work with optimizations....

If you want to find best parameters of the strategy using optimization option in the Strategy Tester, it's better to rewrite the EA with some particular strategy and play with it.

The concept of the adaptive EA and its structure doesn't allow to optimize its parameters in Strategy Tester directly, because there isn't any particular strategy and parameters to optimize.

When we use the adaptive EA in trading or testing it in Strategy Tester we have a trading system as a set of signals from the different strategies. In other words, the Tester cannot access to the virtual sandboxes we created and it doesn't know anything about them.

If you want to use the input parameters to configure the parameters of trading strategies to use, it's better to use the files, it will allow you to configure the parameters and list of trading strategies.

 
Quantum:

If you want to find best parameters of the strategy using optimization option in the Strategy Tester, it's better to rewrite the EA with some particular strategy and play with it.

>> Ok, i got the point. It will be not an easy task..

 

The concept of the adaptive EA and its structure doesn't allow to optimize its parameters in Strategy Tester directly, because there isn't any particular strategy and parameters to optimize.

>> Yes. The EA is idealized to work with still-optimized parameters for the strategies. What i've understood is that it utilizes different strategies in basis how/which the market respond (or vice versa on another point of view..) 

 

When we use the adaptive EA in trading or testing it in Strategy Tester we have a trading system as a set of signals from the different strategies. In other words, the Tester cannot access to the virtual sandboxes we created and it doesn't know anything about them.

>> Understood this concept, but not completely.

I understand that have no sense to interact the tester with virtual sandboxes, no trading logical sense.

My only goal were to have the EA modified, in order to use it with an addon: the objective of using this EA also for optimizing parameters, without rewrite another EA, or rewrite all the strategies, or testing each strategies individually. My idea were only for "comfort" to the possibility to optimize the parametes, within the same EA, but this optimization is not intended to work with the virtual trade / adaptive system. It's just a matter of have the possibility to optimize the parameters of the strategies, using the same EA input window, and not to write another each EA for each strategy, get the optimize parameters, and put them as fixed values into the Adaptive Strategy System. I hope you got my point. 

If you want to use the input parameters to configure the parameters of trading strategies to use, it's better to use the files, it will allow you to configure the parameters and list of trading strategies.

>> Which files you are talking about?  ( If you want to use the input parameters to configure the parameters of trading strategies to use, it's better to use the files )

Are you talking about to use CStrategyXXX.mqh or the fact to write another individual EA for each strategy, optimize it, and copy the parameters as fixed values in CStrategyXXX.mqh?

 

Are you talking about to use CStrategyXXX.mqh or the fact to write another individual EA for each strategy, optimize it, and copy the parameters as fixed values in CStrategyXXX.mqh?

I mean that strategy container can be filled according some settings of the adaptive EA.

For example, in the Adaptive_Expert.Expert_OnInit() it may load a file with adaptive EA settings:

MA;3
MA;15
MA;25
Stoch;3;12;20
Stoch;3;15;30

Strategy1;Param1;Param2;
Strategy2;Param1;Param2;

By parsing each line it recognizes the strategy needed to include and add it with corresponding parameters specified. It's one of the ways to configure the adaptive EA without compilations. For the simplicity, I haven't considered it in the article.

I understand that have no sense to interact the tester with virtual sandboxes, no trading logical sense.

My only goal were to have the EA modified, in order to use it with an addon: the objective of using this EA also for optimizing parameters, without rewrite another EA, or rewrite all the strategies, or testing each strategies individually. My idea were only for "comfort" to the possibility to optimize the parametes, within the same EA, but this optimization is not intended to work with the virtual trade / adaptive system. It's just a matter of have the possibility to optimize the parameters of the strategies, using the same EA input window, and not to write another each EA for each strategy, get the optimize parameters, and put them as fixed values into the Adaptive Strategy System. I hope you got my point.

There is a way to look at the sandboxes. It can be done indirectly using the Strategy Tester.

Suppose that we want to reproduce the results, presented at fig. 2 of the article.

We need the debug version of the adaptive EA adaptive-systems-mql5-sources-debug-en.zip (it report the sandboxes) The next, compile and open Adaptive_Expert_Sample in Strategy tester, select EURUSD H1 01.01.2010 to 20.08.2010 and start testing. The sandboxes and the adaptive strategy results will be saved in tester\files\equity_res.txt  Using this way it's very easy to reproduce all the figures.

The analysis of the virtual trading equities/balances will allow you to simplify the optimization.

 

Hello everyone,

Thanks for this great article, I am now only working on an adaptive EA.

However, I am not sure where to include a trailing stop function in this kind of EA.

I think such a function could be added in the CAdaptiveStrategy.mqh part.

Could any of you provide me with some help? Maybe you guys already have developed such a function?

Thank you very much in advance!

Best regards,

Patrick

 
Does anyone have a version of Adaptive Trading that will work with the experts based off of the OO Expert Class?
 
Thanks for the great article, I have been considering how to implement an adaptive system, this was one approach I devised but haven't had the time to work out yet. Have you considered using any of the built in backtesting features in conjunction with the live data?
 

 I can't reproduce the results in the article. 

Downloaded the source files, compiled, and ran over the same timeframe (EURUSD H1, 04/01/2010 - 20/08/2010) and I get a different result.

I used the debug files and checked the virtual trading ouputs . . . the virtual equity graphs are identical, however, the actual trades do not match.

Without a log file of the actual trades it's hard to find why my trades don't match the article's.

Any idea why I don' match? 

 
Very nice article, The distraction come from people trying to reproduce the results instead of taking away from and integrate the functional possibilities proposed. Now the probability of creating a fully functional and profitable automated and semi automated trading EA is realized. Testing of Indicators on a broad scale, compiling the results and extracting statistical data of indicators on top of conformational statistics of the said data and allowing the the EA to choose the best trading strategy to implement. Never has Fundamental and Technical analysis been combined in an EA until now with the Auxiliary of MQL5 Language. Looking into the future I see the realization EA's  in all Markets coming together to predict market fluctuations.
Reason: