Download MetaTrader 5

How to build and test a Binary Options strategy with the MetaTrader 4 Strategy Tester

22 December 2016, 14:39
Martin Amiri
10
16 220

Table of Contents

1. Introduction
2. Installation
3. Binary Options strategy example
3.1. Define Binary Options strategy
3.2. Create Binary Options strategy
3.2.1. Input parameters
3.2.2. Include Binary-Options-Strategy-Library
3.2.3. Add CallStrategy()
3.2.4. Implement CheckMyRules() and helper-function
3.2.5. Print out debug values
3.2.6. Use of external Indicators (ex4 files)
3.3. The complete code
4. Run a backtest (video)
5. Run a forward test
6. FAQ
7. Miscellaneous


1. Introduction

This article shows how to build a Binary Options strategy and test it in Strategy-Tester of Metatrader 4 with Binary-Options-Strategy-Tester utility. By default Strategy-Tester of Metatrader 4 can test Expert Advisors and Indicators against historical data, but it cannot handle Binary Options with expire times. As I need a possibility to test Binary Options strategies automated in Strategy-Tester of MetaTrader 4, the Binary-Options-Strategy-Tester was build as a utility to fit those needs.

The concept contains the following parts:

Binary Options Strategy Tester Concept

This is a step by step example how to build a Binary Options strategy stored in an Indicator (marked as red in image above) to communicate through Binary-Options-Strategy-Library (marked as green in image above) with the Binary-Options-Strategy-Tester (marked as blue in image above), to place virtual orders and count their results with backtests and forward tests.

Please keep in mind: Backtesting with historical data will never represent the real future, but it might give you an approximate value to get your strategy more stable.
The quality of your backtest will depends on your historical data. Therefore it is strongly recommended to use a set of hight quality data!


2. Installation

Download and purchase Binary-Options-Strategy-Tester utility from marketplace:
Test-Framework to test Binary Options strategies in Strategy-Tester of MetaTrader 4.

Why a purchased version of Binary-Options-Strategy-Tester utility is needed?
A Binary-Options strategy has to call a function of the Binary-Options-Strategy-Tester (via Binary-Options-Strategy-Library) to place the virtual trades. Related to the license concept of MQL4 this only works if the product has a working license. Therefore you have to purchase the product to test Binary Options strategies or this example.

Download free BinaryOptionsStrategyLibrary.mqh and place it in into folder \Include ([path to your MetaTrader 4]\MQL4\Include):
The free library will provide several functions to build your Binary Options strategy easily and to communicate with the Binary-Options-Strategy-Tester. See Binary-Options-Strategy-Library for more details of the library.

Download free KVO.mq4 indicator and place it (and the compiled KVO.ex4 file) into folder \Indicators\Downloads ([path to your MetaTrader 4]\MQL4\Indicators\Downloads):
The KVO indicator is used as an example to show the access of external indicators and there ex4 files in section "3.2.6 Use of external Indicators (ex4 files)". See https://www.mql5.com/en/code/8677 for more details of the indicator.

Now you can go further with section "3. Binary options strategy example" and build the example code by yourself or just download the code of this example below.

Optional download BinaryOptionsStrategyExample.mq4 and place it (and the compiled BinaryOptionsStrategyExample.ex4 file) into folder \Indicators ([path to your MetaTrader 4]\MQL4\Indicators):
Download the code of this Binary Options strategy example to let it run without building it by yourself.

To compile the needed .ex4 files open the .mq4 files (KVO.mq4 and BinaryOptionsStrategyExample.mq4 - NOT Binary-Options-Strategy-Library.mqh) in MetaQuotes Language Editor and click on button "Compile" or just restart your MetaTrader 4 after these files are stored in the described folders and MetaTrader 4 will do this automatically for you.


3. Binary Options strategy example

The following steps will guide you throgh an example how to build an example Binary Options strategy stored in an Indicator to communicate with Binary-Options-Strategy-Tester. You can build it by yourself or just download the code of the BinaryOptionsStrategyExample.mq4.

Please note: This strategy is not a profitable Binary Options strategy! It is just an example how to build a strategy in an indicator to communicate with the Binary-Options-Strategy-Tester utility. Of course you have to build a profitable strategy by yourself. But as you will see, this utility will help you to test and improve your Binary Options strategy.


3.1 Define Binary Options strategy

First of all we have to define the strategy and the changable values (input parameters). MQL4 documentation shows all technical indicators, which can be adressed over the iCustom interface: https://docs.mql4.com/indicators.

Let us say we like to create a simple Moving Average cross strategy with one "fast" and one "slow" Moving Average to trade on next candle after they have crossed each other. Documentation tells, how we can get the value of a single Moving Average: https://docs.mql4.com/indicators/ima.

Let us further say, we like to choose values for "MA averaging period" (fast and slow) and for "applied price" as well as for the "averaging method". Other values (like symbol, timeframe and shift) depends on the testcase (e.g. the symbol the tester runs on) and should be set automatically. Therefore we basically need the following variables for a Moving Average:

int ma_period
int ma_method
int applied_price

As we need two Moving Averages to check their crosses, we need the following input parameters for the strategy example with some default values:

int period_fast        =  5;
int period_slow        = 10;
int method_both        =  0; 
int applied_price_both =  0; 


3.2 Create Binary Options strategy

You need to build an indicator which stores your Binary Options strategy to drag it on the chart where Binary-Options-Strategy-Tester is running on.

Open MetaQuotes Language Editor (in MetaTrader 4 click on "Tools" -> "MetaQuotes Language editor" or just press F4) and click on "New":

Language Editor New

The MQL Wizard will appear. Select "Custom Indicator" to create an empty indicator and click on "Next":

Language Editor Custom Indicator

Enter the name, copyright and link of the strategy as well as the input parameters with their types and default values (initial values) by clicking "Add"-Button and press "Next":

Language Editor Custom Indicator Values

On tab event handlers select checkbox "OnCalculate" as we need this event to check for our strategy on every tick. Press "Next":

Language Editor Custom Indicator Events

On tab drawing properties select checkbox "Indicator in seperate window" as we need a seperate window to print out the debug values. Press "Finish":

Language Editor Custom Indicator Drawing Properties

The initial code of your indicator will appear:

//+------------------------------------------------------------------+
//|                                 BinaryOptionsStrategyExample.mq4 |
//|                                       Copyright 2016, __martin__ |
//|                         https://www.mql5.com/en/users/__martin__ |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, __martin__"
#property link      "https://www.mql5.com/en/users/__martin__"
#property version   "1.00"
#property strict
#property indicator_separate_window
//--- input parameters
input int      period_fast=5;
input int      period_slow=10;
input int      method_both=0;
input int      applied_price_both=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
  
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
  
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+


3.2.1 Input parameters

The initial input parameters are created with the MQL Wizard (see 3.2 Create Binary Options strategy) and we will enhance them with the following steps.

To avoid to have to enter int-values for applied price and averaging method of the Moving Averages for input parameters, the type for method_both and applied_price_both is changed from int to type of enumeration with a default value.

ENUM_MA_METHOD: https://docs.mql4.com/constants/indicatorconstants/enum_ma_method
ENUM_APPLIED_PRICE: https://docs.mql4.com/constants/indicatorconstants/prices#enum_applied_price_enum

In addition comments for the input parameters are added to show the comments as labels instead of variable names:

...

//--- input parameters
input int                period_fast        =  5;           //Fast MA value
input int                period_slow        = 10;           //Slow MA value
input ENUM_MA_METHOD     method_both        = MODE_SMA;     //MA method
input ENUM_APPLIED_PRICE applied_price_both = PRICE_CLOSE;  //MA applied price

...

With this modifications the input parameters provides a dropdown with the available values to select as well as "labels" for the input parameters:

Binary Options Strategy Input Paremeters


3.2.2 Include Binary-Options-Strategy-Library

If you have downloaded and stored the library (see 2. Installation) into \Include folder ([path to your MetaTrader 4]\MQL4\Include), you are able to include the library like this:

//+------------------------------------------------------------------+
//|                                 BinaryOptionsStrategyExample.mq4 |
//|                                       Copyright 2016, __martin__ |
//|                         https://www.mql5.com/en/users/__martin__ |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, __martin__"
#property link      "https://www.mql5.com/en/users/__martin__"
#property version   "1.00"
#property strict
#property indicator_separate_window

#include <BinaryOptionsStrategyLibrary.mqh>

//--- input parameters

...
The library will only be available like described in the example above if you placed it in \Include folder of your MetaTrader 4.
Changing the content of the library is not needed!

Binary-Options-Strategy-Library will enhance the input parameters with two new parameters:

  • Place only one SELL or one BUY trade per candle
  • Check only at the beginning of a new candle for the strategy

Binary Options Strategy Input Parameters Enhanced


3.2.3 Add CallStrategy()

Add a call to CallStrategy()-function in OnCalculate() of your strategy indicator to call the strategy on every new tick. CallStrategy() is provided by Binary-Options-Strategy-Library you have inlcuded like discribed above:

...

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---

   CallStrategy(); //Call the strategy, function is locatet in BinaryOptionsStrategyLibrary.mqh (included above)

//--- return value of prev_calculated for next call
 
   return
(rates_total);
  }
//+------------------------------------------------------------------+

CallStrategy()-function in Binary-Options-Strategy-Library will call a function named CheckMyRules() in your indicator where you can place your conditions for your Binary Options strategy.
Therefore you have to implement the function CheckMyRules() in your Binary Options strategy indicator.


3.2.4 Implement CheckMyRules() and helper-function

In CheckMyRules()-function, which is called through the Binary-Options-Strategy-Library, the conditions for the strategy are implemented and trades are placed through PlaceTrade()-function of the library. Values of both Moving Averages are temporarilly stored in variables to compare them in if-conditions while the values of the Moving Averages are taken from the helper-function GetValuesForMA():


...

//--- input parameters
input int                period_fast        =  5;           //Fast MA value
input int                period_slow        = 10;           //Slow MA value
input ENUM_MA_METHOD     method_both        = MODE_SMA;     //MA method
input ENUM_APPLIED_PRICE applied_price_both = PRICE_CLOSE;  //MA applied price

...

//+------------------------------------------------------------------+
//| Place your Trading-Rules here - see example below.               |
//| StrategyTester will call this function to place trades.          |
//| DO NOT:                                                          |
//| - Rename function                                                |
//| - Add function paramaters, e.g. CheckMyRules(int a)              |
//| - Change function return type, e.g. int CheckMyRules()           |
//+------------------------------------------------------------------+
void CheckMyRules()
  {
  
   //Store MA values with shift=0 (current candle) -> current candle,
   //call helper function GetValueForMA() to get the value - see helper-functions below
   double emaSlow_Current = GetValueForMA(period_slow, 0);
   double emaFast_Current = GetValueForMA(period_fast, 0);

   //Store MA values with shift=1 (past candle) -> last candle,
   //call helper function GetValueForMA() to get the value - see helper-functions below
   double emaSlow_Past = GetValueForMA(period_slow, 1);
   double emaFast_Past = GetValueForMA(period_fast, 1);

   if(emaFast_Past > emaSlow_Past
   && emaFast_Current < emaSlow_Past) //Check if slow MA and fast MA crosses
     {
      PlaceTrade(OP_SELL); //Place SELL-Trade for Strategy-Tester, function is located in BinaryOptionsStrategyFunctions.mqh
     }
  
   if(emaFast_Past < emaSlow_Past
   && emaFast_Current > emaSlow_Past) //Check if slow MA and fast MA crosses
     {
      PlaceTrade(OP_BUY); //Place BUY-Trade for Strategy-Tester, function is located in BinaryOptionsStrategyFunctions.mqh
     }
  
  }

//+------------------------------------------------------------------+
//| Place your Helper-Functions here, see example below              |
//+------------------------------------------------------------------+
//----
//+------------------------------------------------------------------+
//| Get MA values for period, method, applied price and shift.       |
//| For details of iMA() see https://docs.mql4.com/indicators/ima    |
//+------------------------------------------------------------------+
double GetValueForMA(int _period,int _shift)
  {
   return iMA(NULL,0,_period,0,method_both,applied_price_both,_shift);
  }
 


3.2.5 Print out debug values

The function PrintDebugValue() privides a possibility to print out debug values while the tester is running. In the example below the values of the Moving Averages are printed out with their variable names as labels:


...

//--- input parameters
input int                period_fast        =  5;           //Fast MA value
input int                period_slow        = 10;           //Slow MA value
input ENUM_MA_METHOD     method_both        = MODE_SMA;     //MA method
input ENUM_APPLIED_PRICE applied_price_both = PRICE_CLOSE;  //MA applied price

...

//+------------------------------------------------------------------+
//| Place your Trading-Rules here - see example below.               |
//| StrategyTester will call this function to place trades.          |
//| DO NOT:                                                          |
//| - Rename function                                                |
//| - Add function paramaters, e.g. CheckMyRules(int a)              |
//| - Change function return type, e.g. int CheckMyRules()           |
//+------------------------------------------------------------------+
void CheckMyRules()
  {
  
   //Store MA values with shift=0 (current candle) -> current candle,
   //call helper function GetValueForMA() to get the value - see helper-functions below
   double emaSlow_Current = GetValueForMA(period_slow, 0);
   double emaFast_Current = GetValueForMA(period_fast, 0);

   //Store MA values with shift=1 (past candle) -> last candle,
   //call helper function GetValueForMA() to get the value - see helper-functions below
   double emaSlow_Past = GetValueForMA(period_slow, 1);
   double emaFast_Past = GetValueForMA(period_fast, 1);

   PrintDebugValue("emaSlow_Current: ",(string)emaSlow_Current,0); //Label and value in line 0
   PrintDebugValue("emaFast_Current: ",(string)emaFast_Current,1); //Label and value in line 1
   PrintDebugValue("emaSlow_Past: ",(string)emaSlow_Past,2);       //Label and value in line 2
   PrintDebugValue("emaFast_Past: ",(string)emaFast_Past,3);       //Label and value in line 3

   if(emaFast_Past > emaSlow_Past
   && emaFast_Current < emaSlow_Past) //Check if slow MA and fast MA crosses
     {
      PlaceTrade(OP_SELL); //Place SELL-Trade for Strategy-Tester, function is located in BinaryOptionsStrategyFunctions.mqh
     }
  
   if(emaFast_Past < emaSlow_Past
   && emaFast_Current > emaSlow_Past) //Check if slow MA and fast MA crosses
     {
      PlaceTrade(OP_BUY); //Place BUY-Trade for Strategy-Tester, function is located in BinaryOptionsStrategyFunctions.mqh
     }
  
  }

//+------------------------------------------------------------------+
//| Place your Helper-Functions here, see example below              |
//+------------------------------------------------------------------+
//----

//+------------------------------------------------------------------+
//| Get MA values for period, method, applied price and shift.       |
//| For details of iMA() see https://docs.mql4.com/indicators/ima    |
//+------------------------------------------------------------------+
double GetValueForMA(int _period,int _shift)
  {
   return iMA(NULL,0,_period,0,method_both,applied_price_both,_shift);
  }
 


3.2.6 Use of external Indicators (ex4 files)

In addition an external indicator which stores its values in buffers can be accessed for the Binary Options strategy, even if only the compiled ex4-file exists.

Let us say we like to include the signal line of the KVO indicator https://www.mql5.com/en/code/8677 to place trades only if the signal line is over 0 for BUY trades and under 0 for SELL trades. Download the KVO.mq4 indicator and place the compiled (ex4 file) into folder \Indicators\Downloads ([path to your MetaTrader 4]\MQL4\Indicators\Downloads).

To compile the needed .ex4 file open KVO.mq4 in MetaQuotes Language Editor and click on button "Compile" or just restart your MetaTrader 4 after the file is stored in the described folder and MetaTrader 4 will do this automatically for you.

First we have to identify the relevant buffers which stores the relevant values to access. Therefore we press the button "Data Window" in MetaTrader 4 to show all available buffers of the used indicators and drag the KVO indicator on a chart. By hovering the cross over the chart (press mouse-wheel on chart to bring up the cross) the buffer values of the indicator of the hovered timeperiod will be shown in data window:

External Indicator Buffers And Values

The data window labels tells us the second buffer value of the indicator stores the signal line. If buffers of indicators did not have labels, we can find the right one by comparing the buffer values with the displayed value under the cross in the chart and indicator. Buffers of an indicator starts with 0, so we have buffer value 1 = buffer 0, buffer value 2 = buffer 1 and so on and we have to access buffer 1 to get the signal value.

Next we have to know all input parameters of the external indicator we like to access. By draging the indicator on a chart, we see all input paremeters:

Input Parameters KVO

Let us further say, we like to access the indicator with (its default) values: 34, 55 and 13. We use a helper function (based on iCostum), wich provides us the possibility to get the values of the indicator with parameters for buffer and shift, while shift 0 will be the value of the current candle, shift 1 the value of the last candle, shift 2 the value of the second to last candle and so on. In addition we temporarilly store the values of the indicator buffer and enhance the if-condition of the strategy:


...

//--- input parameters
input int                period_fast        =  5;           //Fast MA value
input int                period_slow        = 10;           //Slow MA value
input ENUM_MA_METHOD     method_both        = MODE_SMA;     //MA method
input ENUM_APPLIED_PRICE applied_price_both = PRICE_CLOSE;  //MA applied price

...

//+------------------------------------------------------------------+
//| Place your Trading-Rules here - see example below.               |
//| StrategyTester will call this function to place trades.          |
//| DO NOT:                                                          |
//| - Rename function                                                |
//| - Add function paramaters, e.g. CheckMyRules(int a)              |
//| - Change function return type, e.g. int CheckMyRules()           |
//+------------------------------------------------------------------+
void CheckMyRules()
  {
  
   //Store MA values with shift=0 (current candle) -> current candle,
   //call helper function GetValueForMA() to get the value - see helper-functions below
   double emaSlow_Current = GetValueForMA(period_slow, 0);
   double emaFast_Current = GetValueForMA(period_fast, 0);

   //Store MA values with shift=1 (past candle) -> last candle,
   //call helper function GetValueForMA() to get the value - see helper-functions below
   double emaSlow_Past = GetValueForMA(period_slow, 1);
   double emaFast_Past = GetValueForMA(period_fast, 1);

   //Store signal value (buffer 1) of KVO indicator from current candle (shift 0)
   double kvoSignal = GetValuesFromIndicator__KVO__(1,0);

   PrintDebugValue("emaSlow_Current: ",(string)emaSlow_Current,0); //Label and value in line 0
   PrintDebugValue("emaFast_Current: ",(string)emaFast_Current,1); //Label and value in line 1
   PrintDebugValue("emaSlow_Past: ",(string)emaSlow_Past,2);       //Label and value in line 2
   PrintDebugValue("emaFast_Past: ",(string)emaFast_Past,3);       //Label and value in line 3

   if(emaFast_Past > emaSlow_Past
   && emaFast_Current < emaSlow_Past //Check if slow MA and fast MA crosses
   && kvoSignal < 0) //Check if signal value of KVO is under 0
     {
      PlaceTrade(OP_SELL); //Place SELL-Trade for Strategy-Tester, function is located in BinaryOptionsStrategyFunctions.mqh
     }
  
   if(emaFast_Past < emaSlow_Past
   && emaFast_Current > emaSlow_Past //Check if slow MA and fast MA crosses
   && kvoSignal > 0) //Check if signal value of KVO is over 0
     {
      PlaceTrade(OP_BUY); //Place BUY-Trade for Strategy-Tester, function is located in BinaryOptionsStrategyFunctions.mqh
     }
  
  }

//+------------------------------------------------------------------+
//| Place your Helper-Functions here, see example below              |
//+------------------------------------------------------------------+
//----

//+------------------------------------------------------------------+
//| Get MA values for period, method, applied price and shift.       |
//| For details of iMA() see https://docs.mql4.com/indicators/ima    |
//+------------------------------------------------------------------+
double GetValueForMA(int _period,int _shift)
  {
   return iMA(NULL,0,_period,0,method_both,applied_price_both,_shift);
  }

//+------------------------------------------------------------------+
//| Example how to get values from external indicators               |
//| see https://docs.mql4.com/indicators/icustom                     |
//| Parameters:                                                      |
//| int _buffer - indicator-buffer (starts with 0)                   |
//| int _shift - value to shift; 0 = current candle, 1 = prev candle |
//+------------------------------------------------------------------+
double GetValuesFromIndicator__KVO__(int _buffer, int _shift=0) //Change "__KVO__" to indicator name
  {

   return (
            iCustom (
                      NULL,                      //NULL for current timeframe selected in tester - NO CHANGES NEEDED
                      0,                         //0 for current symbol selected in tester - NO CHANGES NEEDED

                      //BEGIN EDIT
                      "\\Downloads\\KVO.ex4",    //Filepath and filename of the indicator (*.ex4 file)
                      //BEGIN INDICATORS INPUTS
                      34,
                      55,
                      13,
                      //END FOR INPUTS
                      //END EDIT

                      _buffer,                   //Buffer index (begins with 0), _buffer is adressed over function parameter - NO CHANGES NEEDED
                      _shift                     //Shift (0 for current candle), _shift is adressed over function parameter - NO CHANGES NEEDED
                    )
          );

  }
 

It is also possible to enhance the input parameters of our strategy indicator with the values for the used KVO indicator and set the values in helper function by variables. As this tutorial should be just an example and "as simple as possible", this variant is not shown.


3.3 The complete code

Below you will find the complete code of the Binary-Options-Strategy-Example from all the steps above, ready to drag on the Binary-Options-Strategy-Tester to test and see the results on chart:

//+------------------------------------------------------------------+
//|                                 BinaryOptionsStrategyExample.mq4 |
//|                                       Copyright 2016, __martin__ |
//|                         https://www.mql5.com/en/users/__martin__ |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, __martin__"
#property link      "https://www.mql5.com/en/users/__martin__"
#property version   "1.00"
#property strict
#property indicator_separate_window

#include <BinaryOptionsStrategyLibrary.mqh>

//+------------------------------------------------------------------+
//| Place your input parameters here - see example below             |
//+------------------------------------------------------------------+
//--- input parameters
input int                period_fast        =  5;           //Fast MA value
input int                period_slow        = 10;           //Slow MA value
input ENUM_MA_METHOD     method_both        = MODE_SMA;     //MA method
input ENUM_APPLIED_PRICE applied_price_both = PRICE_CLOSE;  //MA applied price

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping

//---
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---

   CallStrategy(); //Call the strategy, function is locatet in BinaryOptionsStrategyLibrary.mqh (included above)

//--- return value of prev_calculated for next call
   return(rates_total);
  }

//+------------------------------------------------------------------+
//| Place your Trading-Rules here - see example below.               |
//| StrategyTester will call this function to place trades.          |
//| DO NOT:                                                          |
//| - Rename function                                                |
//| - Add function paramaters, e.g. CheckMyRules(int a)              |
//| - Change function return type, e.g. int CheckMyRules()           |
//+------------------------------------------------------------------+
void CheckMyRules()
  {
  
   //Store MA values with shift=0 (current candle) -> current candle,
   //call helper function GetValueForMA() to get the value - see helper-functions below
   double emaSlow_Current = GetValueForMA(period_slow, 0);
   double emaFast_Current = GetValueForMA(period_fast, 0);

   //Store MA values with shift=1 (past candle) -> last candle,
   //call helper function GetValueForMA() to get the value - see helper-functions below
   double emaSlow_Past = GetValueForMA(period_slow, 1);
   double emaFast_Past = GetValueForMA(period_fast, 1);
  
   //Store signal value (buffer 1) of KVO indicator from current candle (shift 0)
   double kvoSignal = GetValuesFromIndicator__KVO__(1,0);
  
   PrintDebugValue("emaSlow_Current: ",(string)emaSlow_Current,0); //Label and value in line 0
   PrintDebugValue("emaFast_Current: ",(string)emaFast_Current,1); //Label and value in line 1
   PrintDebugValue("emaSlow_Past: ",(string)emaSlow_Past,2);       //Label and value in line 2
   PrintDebugValue("emaFast_Past: ",(string)emaFast_Past,3);       //Label and value in line 3

   if(emaFast_Past > emaSlow_Past
   && emaFast_Current < emaSlow_Past    //Check if slow MA and fast MA crosses
   && kvoSignal < 0)                    //Check if signal value of KVO is under 0  
     {
      PlaceTrade(OP_SELL);              //Place SELL-Trade for Strategy-Tester, function is located in BinaryOptionsStrategyLibrary.mqh
     }
  
   if(emaFast_Past < emaSlow_Past
   && emaFast_Current > emaSlow_Past    //Check if slow MA and fast MA crosses
   && kvoSignal > 0)                    //Check if signal value of KVO is over 0
     {
      PlaceTrade(OP_BUY);               //Place BUY-Trade for Strategy-Tester, function is located in BinaryOptionsStrategyLibrary.mqh
     }
  
  }

//+------------------------------------------------------------------+
//| Place your Helper-Functions here, see example below              |
//+------------------------------------------------------------------+
//----

//+------------------------------------------------------------------+
//| Get MA values for period, method, applied price and shift.       |
//| For details of iMA() see https://docs.mql4.com/indicators/ima    |
//+------------------------------------------------------------------+
double GetValueForMA(int _period,int _shift)
  {
   return iMA(NULL,0,_period,0,method_both,applied_price_both,_shift);
  }

//+------------------------------------------------------------------+
//| Example how to get values from external indicators,              |
//| see https://docs.mql4.com/indicators/icustom                     |
//| Parameters:                                                      |
//| int _buffer - indicator-buffer (starts with 0)                   |
//| int _shift - value to shift; 0 = current candle, 1 = prev candle |
//+------------------------------------------------------------------+
double GetValuesFromIndicator__KVO__(int _buffer, int _shift=0) //Change "__KVO__" to indicator name
  {
   return (
            iCustom (
                      NULL,                      //NULL for current timeframe selected in tester - NO CHANGES NEEDED
                      0,                         //0 for current symbol selected in tester - NO CHANGES NEEDED
                  
                      //BEGIN EDIT
                      "\\Downloads\\KVO.ex4",    //Filepath and filename of the indicator (*.ex4 file)                  
                      //BEGIN INDCATORS INPUTS
                      34,
                      55,
                      13,
                      //END FOR INPUTS
                      //END EDIT
                  
                      _buffer,                   //Buffer index (begins with 0), _buffer is adressed over function parameter - NO CHANGES NEEDED
                      _shift                     //Shift (0 for current candle), _shift is adressed over function parameter - NO CHANGES NEEDED
                    )
          );
  }
//+-----------------------------------------------------------------+


4. Run a backtest (video)

The following video shows how to run a backtest of your Binary Options strategy in Strategy-Tester of MetaTrader 4:

  • Start Binary-Options-Strategy-Tester in Strategy-Tester of MetaTrader 4 and set the input parameters
  • Drag your Binary Options strategy indicator on the chart, set the input parameters and check "Allow external expert imports" on the "common" tab
  • Drag your used indicators with their used input parameters on the chart to see their values while tester is running (optional)
  • Save all settings in a template to run the test with all settings again - using the pause button of the Strategy-Tester (optional)
  • See the results of your Binary Options strategy on the Strategy-Tester chart 


5. Run a forward test

To do a forward test simply drag the Binary-Options-Strategy-Tester utility and your strategy indicator on your demo or live chart of your broker instead of using it in Strategy-Tester:

  • Drag Binary-Options-Strategy-Tester utility on demo or live chart and set the input parameters
  • Drag your Binary Options strategy indicator on the chart, set the input parameters and check "Allow external expert imports" on the "common" tab
  • Drag your used indicators with their used input parameters on the chart to see their values while forward test is running (optional)
  • Save all settings in a template to run the test again with all settings (optional)
  • See the results of your Binary Options strategy on demo or live chart


6. FAQ

Question: Why do you show an example of a non profitable Binary Options strategy?
Answere: This is just an example how to build a strategy in an Indicator to communicate with the Binary-Options-Strategy-Tester utility in marketplace to test and improve your strategy.

Question: Binary-Options-Strategy-Tester stops after the exact amount of losses with error "Array out of range". Why?
Answere: Binary-Options-Strategy-Tester can rise an error after x losses to stop Tester and to analyse the situaion on the chart. If you do not want to, just switch off the option in settings.

Question: No arrows appear on chart after I draged my indicator with a working strategy on it. What happened?
Answere: You have to enable "Allow external expert imports" on the "common" tab while you drag your strategy-indicator on the chart (log message will show an error in this case).

Question: No arrows appear on chart after I draged my indicator with a working strategy on it with "Allow external expert imports" enabled. Why?
Answere: A strategy has to call a function of Binary-Options-Strategy-Tester to place virtual trades. Related to the MQL4 license concept this only works if the product has a working license. Therefore you have to purchase the product.

Question: No arrows appear on chart after I dragged my indicator with a working strategy on it and I got errors like "Cannot call .." or "Cannot load .." in the log of MetaTrader 4. What can I do?
Answere: Use the latest version (greater v1.00) of BinaryOptionsStrategyLibrary.mqh. Check version tag in code of your BinaryOptionsStrategyLibrary.mqh and see changelog v1.01 of BinaryOptionsStrategyLibrary.

Question: I see no results on Strategy-Tester tabs "Results", "Graph", "Report". Where I can see the results?
Answere: Strategy-Tester of MetaTrader 4 can not handle Binary Options so these tabs con not be used. Therefore this utility calculates all wins and losses and prints the results on the chart.


7. Miscellaneous

As I need a possibility to test Binary Options strategies automated in Strategy-Tester of MetaTrader 4 for long time periods in a short time and to do foward tests on the chart of the broker, this utility was build. I have spent a lot of time for the concept and the implementation of the Binary-Options-Strategy-Tester as well as for the documentation. Maybe there is a better way to do it and maybe some improvements will bring it closer to fit the needs of you. So please feel free to contact me for ideas for improvements!
 

Attached files |
Last comments | Go to discussion (10)
Martin Amiri
Martin Amiri | 4 Feb 2017 at 12:49
juwohall1:

Hi Martin,

1- The fuller form of the error code: " can't open 'C' \ Terminal\............\MQL4\include\BinaryOptionStrategyLibrary.mqh" include forlder.

2- The error code shows up as soon as I hit 'compile' in order to compile the BinaryOptionStrategyExample.mq4. Then 8 other error codes follow: printdebug value, callStrategy, placetrade all- funtion not defined.

3- The BinaryOptionStrategyLibrary is v1.01 while the BinaryOptionStrategyExample is v 1.0.

4- I have not got any yet but will go for the purchased version.

By multiple strategy i actually meant ' a strategy built on candle or indicator data for more than one timeframe, say 15M and 30M time frame for one strategy for example.

Regards,

Johnson. 

Hi Johnson,

Thank you for specifying the situation!

Please take a look at the unanswered question "- At which folder your BinaryOptionsStrategyLibrary is locatet ([Path to your MT4]/MQL4/Include)?" and check where you have placed the BinaryOptionsStrategyLibrary.mqh. 

To make a build without errors please make shure the BinaryOptionsStrategyLibrary.mqh (v1.01) is placed into folder [path to your MT4]\MQL4\Include e.g. by clicking on "Include" folder in navigator of Metaquotes Language Editor (see picture below). If the library is stored into folder [path to your MT4]\MQL4\Include, BinaryOptionsStrategyExample.mq4 should compile without any errors (see picture below).

 


A little more dedailed: 

The only way to repruduce the error is to not place the BinaryOptionsStrategyLibrary.mqh file into include folder of the MT4 so the BinaryOptionsStrategyExample can not open it like described in the error message:

"can´t open 'C:\....\MQL4\Include\BinaryOptionsStrategyLibrary.mqh include folder.". 

 

If the BinaryOptionsStrategyLibrary.mqh could not be found in folder "Include" of your MT4, the BinaryOptionsStrategyExample can not call further needed methods of the library which will result in the next errors (see picture above).

As you can see in picture below BinaryOptionsStrategyLibrary.mqh is included with "#include" in line 12 in BinaryOptionsStrategyExample.mq4. Therefore the library has to be placed into "Include" folder to be available for the "#import". If you click on "Include" in navigator of Metaquotes Language Editor you can see if BinaryOptionsStrategyLibrary is locatet in folder "Include" of your MT4. If BinaryOptionsLibrary is not shown (and therefore not stored into "Include" folder) the build will result in the described error (see picture below).

 

 

The solution should be to place the BinaryOptionsStrategyLibrary.mqh (v1.01) into folder [Path to your MT4]\MQL4\Include like described in the article in section "2. Installation" or in section "Installation" of BinaryOptionsStrategyLibrary.

 

Extract section 2. Installation of the article:


...

2. Installation
... 

Download free BinaryOptionsStrategyLibrary.mqh and place it in into folder \Include ([path to your MetaTrader 4]\MQL4\Include):

...


 

Extract section Installation of BinaryOptionsStrategyLibrary:


... 

Installation:

Download this library (BinaryOptionsStrategyLibrary.mqh) and place it into\Include folder ([path to your MetaTrader 4]\MQL4\Include). After that you are able to use the functions by including the file (see Usage).
...



I hope the explanation will help to build the BinaryOptionsStrategyExample and BinaryOptionsStrategyTester will help you to test and improve your Binary Options strategies!

Yes, in forward tests it is possible to build a strategy based on mulitple timeframes of a currency.  

If you have any questions please feel free to contact me or to place any questions here!

I would be happy to hear from you if the build could be done!

Regards,

Martin 

juwohall1
juwohall1 | 4 Feb 2017 at 20:46
Martin Amiri:

Hi Johnson,

Thank you for specifying the situation!

Please take a look at the unanswered question "- At which folder your BinaryOptionsStrategyLibrary is locatet ([Path to your MT4]/MQL4/Include)?" and check where you have placed the BinaryOptionsStrategyLibrary.mqh. 

To make a build without errors please make shure the BinaryOptionsStrategyLibrary.mqh (v1.01) is placed into folder [path to your MT4]\MQL4\Include e.g. by clicking on "Include" folder in navigator of Metaquotes Language Editor (see picture below). If the library is stored into folder [path to your MT4]\MQL4\Include, BinaryOptionsStrategyExample.mq4 should compile without any errors (see picture below).

 


A little more dedailed: 

The only way to repruduce the error is to not place the BinaryOptionsStrategyLibrary.mqh file into include folder of the MT4 so the BinaryOptionsStrategyExample can not open it like described in the error message:

"can´t open 'C:\....\MQL4\Include\BinaryOptionsStrategyLibrary.mqh include folder.". 

 

If the BinaryOptionsStrategyLibrary.mqh could not be found in folder "Include" of your MT4, the BinaryOptionsStrategyExample can not call further needed methods of the library which will result in the next errors (see picture above).

As you can see in picture below BinaryOptionsStrategyLibrary.mqh is included with "#include" in line 12 in BinaryOptionsStrategyExample.mq4. Therefore the library has to be placed into "Include" folder to be available for the "#import". If you click on "Include" in navigator of Metaquotes Language Editor you can see if BinaryOptionsStrategyLibrary is locatet in folder "Include" of your MT4. If BinaryOptionsLibrary is not shown (and therefore not stored into "Include" folder) the build will result in the described error (see picture below).

 

 

The solution should be to place the BinaryOptionsStrategyLibrary.mqh (v1.01) into folder [Path to your MT4]\MQL4\Include like described in the article in section "2. Installation" or in section "Installation" of BinaryOptionsStrategyLibrary.

 

Extract section 2. Installation of the article:


...

2. Installation
... 

Download free BinaryOptionsStrategyLibrary.mqh and place it in into folder \Include ([path to your MetaTrader 4]\MQL4\Include):

...


 

Extract section Installation of BinaryOptionsStrategyLibrary:


... 

Installation:

Download this library (BinaryOptionsStrategyLibrary.mqh) and place it into\Include folder ([path to your MetaTrader 4]\MQL4\Include). After that you are able to use the functions by including the file (see Usage).
...



I hope the explanation will help to build the BinaryOptionsStrategyExample and BinaryOptionsStrategyTester will help you to test and improve your Binary Options strategies!

Yes, in forward tests it is possible to build a strategy based on mulitple timeframes of a currency.  

If you have any questions please feel free to contact me or to place any questions here!

I would be happy to hear from you if the build could be done!

Regards,

Martin 

Hi Martin,

That was a grand explanation! All in one!

I am sure I have more question and will contact you. Thanks and Regards,

Johnson. 

Martin Amiri
Martin Amiri | 4 Feb 2017 at 21:38
juwohall1:

Hi Martin,

That was a grand explanation! All in one!

I am sure I have more question and will contact you. Thanks and Regards,

Johnson. 

Hi Johnson,

Thank you, I am glad to hear that!

Regards,

Martin 

Florian Grünwald
Florian Grünwald | 23 Feb 2017 at 13:19

Hi Martin,

is it possible to let the MT4 optimizer optimize my strategy? 

 

Regards,

flo 

Martin Amiri
Martin Amiri | 23 Feb 2017 at 21:13
Florian Grünwald:

Hi Martin,

is it possible to let the MT4 optimizer optimize my strategy? 

 

Regards,

flo 

Hi Flo,

Unfortunately it is not possible to let the optimizer optimize your strategy..

Why:

As the strategy tester of MT4 can not handle Binary Option trades with expire times by himself, Binary Options Strategy Tester runs in strategy tester of MT4 and places virtual orders to count/display their results:

 

Unfortunately it is not possible to pass the virtual results to the MT4 strategy tester. Therefore the virtual orders are not known by the strategy tester of MT4 himself and can not be calculated or optimized automatically.

But you can define values of your strategy in your strategy indicator file as external inputs/variables (https://www.mql5.com/en/articles/2820#p321) to change parameters easily and to run your strategy several times with different settings to compare the results.

I will enhance the Binary Options Strategy Tester in the next version to print out a textfile with the current settings and the results to make it easier to compare them.

I hope Binary Options Strategy Tester will still help you to check your strategies for long time periods in short times automatically and to improve your strategies.

If you have any further questions don´t hasitate to contact me!

Regards,

Martin 

Working with currency baskets in the Forex market Working with currency baskets in the Forex market

The article describes how currency pairs can be divided into groups (baskets), as well as how to obtain data about their status (for example, overbought and oversold) using certain indicators and how to apply this data in trading.

Portfolio trading in MetaTrader 4 Portfolio trading in MetaTrader 4

The article reveals the portfolio trading principles and their application to Forex market. A few simple mathematical portfolio arrangement models are considered. The article contains examples of practical implementation of the portfolio trading in MetaTrader 4: portfolio indicator and Expert Advisor for semi-automated trading. The elements of trading strategies, as well as their advantages and pitfalls are described.

ZUP - universal ZigZag with Pesavento patterns. Graphical interface ZUP - universal ZigZag with Pesavento patterns. Graphical interface

Over the ten years since the release of the first version of the ZUP platform, it has undergone through multiple changes and improvements. As a result, now we have a unique graphical add-on for MetaTrader 4 allowing you to quickly and conveniently analyze market data. The article describes how to work with the graphical interface of the ZUP indicator platform.

Features of Experts Advisors Features of Experts Advisors

Creation of expert advisors in the MetaTrader trading system has a number of features.