Table of Contents
i. Introduction
2. Installation
three. Binary Options strategy example
three.1. Define Binary Options strategy
3.two. Create Binary Options strategy
3.2.ane. Input parameters
iii.ii.2. Include Binary-Options-Strategy-Library
3.2.3. Add together CallStrategy()
3.2.four. Implement CheckMyRules() and helper-role
3.ii.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 exam
6. FAQ
vii. Miscellaneous
1. Introduction
This article shows how to build a Binary Options strategy and exam it in Strategy-Tester of Metatrader 4 with Binary-Options-Strategy-Tester utility. By default Strategy-Tester of Metatrader 4 can exam Skilful Advisors and Indicators confronting historical data, only 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 equally a utility to fit those needs.
The concept contains the following parts:
This is a footstep past footstep instance how to build a Binary Options strategy stored in an Indicator (marked equally carmine in epitome higher up) to communicate through Binary-Options-Strategy-Library (marked as green in image above) with the Binary-Options-Strategy-Tester (marked as blue in paradigm above), to place virtual orders and count their results with backtests and frontwards tests.
Please go on in mind:
Backtesting with historical data will never represent the real future, but information technology might give you an approximate value to go your strategy more stable.
The quality of your backtest volition depends on your historical data. Therefore information technology is strongly recommended to utilise a set up of hight quality data!
ii. Installation
Download and buy Binary-Options-Strategy-Tester utility from marketplace:
Exam-Framework to exam 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 office 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 take to purchase the production to examination Binary Options strategies or this example.
Download free BinaryOptionsStrategyLibrary.mqh and place it in into folder \Include ([path to your MetaTrader four]\MQL4\Include):
The gratuitous library volition provide several functions to build your Binary Options strategy hands and to communicate with the Binary-Options-Strategy-Tester. Come across Binary-Options-Strategy-Library for more details of the library.
Download complimentary 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 prove the access of external indicators and in that location ex4 files in department “3.ii.6 Use of external Indicators (ex4 files)”. See https://www.mql5.com/en/lawmaking/8677 for more details of the indicator.
Now y’all can go further with section “3. Binary options strategy example” and build the example code past yourself or just download the lawmaking of this case below.
Optional download BinaryOptionsStrategyExample.mq4 and place it (and the compiled BinaryOptionsStrategyExample.ex4 file) into folder \Indicators ([path to your MetaTrader four]\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 up 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 later on these files are stored in the described folders and MetaTrader iv will do this automatically for you.
three. Binary Options strategy example
The following steps volition guide you throgh an instance 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 merely download the code of the BinaryOptionsStrategyExample.mq4.
Please note:This strategy is not a profitable Binary Options strategy! It is only 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. Simply as you volition see, this utility will help you to test and improve your Binary Options strategy.
iii.ane Define Binary Options strategy
First of all we have to ascertain the strategy and the changable values (input parameters). MQL4 documentation shows all technical indicators, which tin can be adressed over the iCustom interface: https://docs.mql4.com/indicators.
Permit usa say we similar to create a simple Moving Average cantankerous strategy with one “fast” and ane “slow” Moving Average to trade on next candle later they take crossed each other. Documentation tells, how we tin can get the value of a unmarried Moving Average: https://docs.mql4.com/indicators/ima.
Allow united states of america further say, we like to choose values for “MA averaging catamenia” (fast and slow) and for “applied price” as well as for the “averaging method”. Other values (similar 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
Equally we need two Moving Averages to cheque their crosses, nosotros need the following input parameters for the strategy case with some default values:
int
period_fast = 5;
int
period_slow = 10;
int
method_both =0;
int
applied_price_both =0;
iii.two Create Binary Options strategy
Y’all need to build an indicator which stores your Binary Options strategy to elevate it on the chart where Binary-Options-Strategy-Tester is running on.
Open MetaQuotes Linguistic communication Editor (in MetaTrader 4 click on “Tools” -> “MetaQuotes Language editor” or just press F4) and click on “New”:
The MQL Wizard will announced. Select “Custom Indicator” to create an empty indicator and click on “Next”:
Enter the name, copyright and link of the strategy besides as the input parameters with their types and default values (initial values) by clicking “Add”-Button and press “Adjacent”:
On tab issue handlers select checkbox “OnCalculate” as we need this issue to cheque for our strategy on every tick. Press “Next”:
On tab drawing properties select checkbox “Indicator in seperate window” equally we need a seperate window to print out the debug values. Press “Cease”:
The initial code of your indicator volition appear:
#property
copyright
“Copyright 2016, __martin__”
#property
link“https://www.mql5.com/en/users/__martin__”
#property
version
“ane.00”
#property
strict
#property
indicator_separate_window
input
int period_fast=5;
input
int period_slow=10;
input
int method_both=0;
input
int applied_price_both=0;
int
OnInit()
{
render(INIT_SUCCEEDED);
}
int
OnCalculate(const
int
rates_total,
const
int
prev_calculated,
const
datetime
&time[],
const
double
&open[],
const
double
&loftier[],
const
double
&low[],
const
double
&close[],
const
long
&tick_volume[],
const
long
&book[],
const
int
&spread[])
{
return(rates_total);
}
iii.2.ane Input parameters
The initial input parameters are created with the MQL Magician (come across three.two Create Binary Options strategy) and we volition heighten 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 testify the comments equally labels instead of variable names:
…
inputint period_fast =5;
inputint period_slow =10;
inputENUM_MA_METHOD method_both =
MODE_SMA;
inputENUM_APPLIED_PRICE applied_price_both =
PRICE_CLOSE;
…
With this modifications the input parameters provides a dropdown with the available values to select every bit well as “labels” for the input parameters:
3.ii.2 Include Binary-Options-Strategy-Library
If you lot have downloaded and stored the library (come across 2. Installation) into \Include folder ([path to your MetaTrader four]\MQL4\Include), you are able to include the library like this:
#holding
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>
…
The library volition just exist available like described in the example above if you placed information technology in \Include binder of your MetaTrader 4.
Changing the content of the library is non needed!
Binary-Options-Strategy-Library will enhance the input parameters with two new parameters:
- Identify just ane SELL or one BUY trade per candle
- Cheque only at the beginning of a new candle for the strategy
iii.2.3 Add CallStrategy()
Add a call to CallStrategy()-function in OnCalculate() of your strategy indicator to phone call the strategy on every new tick. CallStrategy() is provided by Binary-Options-Strategy-Library you have inlcuded like discribed above:
…
intOnCalculate(constint rates_total,
constint prev_calculated,
constdatetime &time[],
constdouble &open up[],
constdouble &high[],
constdouble &depression[],
constdouble &close[],
constlong &tick_volume[],
constlong &volume[],
constint &spread[])
{
CallStrategy();
return(rates_total);
}
CallStrategy()-office in Binary-Options-Strategy-Library will call a part named CheckMyRules() in your indicator where you can place your conditions for your Binary Options strategy.
Therefore
you have to implement the office CheckMyRules() in your Binary Options strategy indicator.
iii.two.iv Implement CheckMyRules() and helper-office
In CheckMyRules()-function, which is chosen 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-role GetValuesForMA():
…
inputint period_fast =five;
inputint period_slow =10;
inputENUM_MA_METHOD method_both = MODE_SMA;
inputENUM_APPLIED_PRICE applied_price_both = PRICE_CLOSE;
…
void
CheckMyRules()
{
double
emaSlow_Current = GetValueForMA(period_slow,
0);
double
emaFast_Current = GetValueForMA(period_fast,
0);
double
emaSlow_Past = GetValueForMA(period_slow,
i);
double
emaFast_Past = GetValueForMA(period_fast,
1);
if(emaFast_Past > emaSlow_Past
&& emaFast_Current < emaSlow_Past)
{
PlaceTrade(OP_SELL);
}
if(emaFast_Past < emaSlow_Past
&& emaFast_Current > emaSlow_Past)
{
PlaceTrade(OP_BUY);
}
}
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 impress out debug values while the tester is running. In the case beneath the values of the Moving Averages are printed out with their variable names as labels:
…
inputint period_fast =v;
inputint period_slow =10;
inputENUM_MA_METHOD method_both = MODE_SMA;
inputENUM_APPLIED_PRICE applied_price_both = PRICE_CLOSE;
…
void
CheckMyRules()
{
double
emaSlow_Current = GetValueForMA(period_slow,
0);
double
emaFast_Current = GetValueForMA(period_fast,
0);
double
emaSlow_Past = GetValueForMA(period_slow,
i);
double
emaFast_Past = GetValueForMA(period_fast,
1);
PrintDebugValue(“emaSlow_Current: “,(string)emaSlow_Current,0);
PrintDebugValue(“emaFast_Current: “,(cord)emaFast_Current,1);
PrintDebugValue(“emaSlow_Past: “,(string)emaSlow_Past,2);
PrintDebugValue(“emaFast_Past: “,(string)emaFast_Past,iii);
if(emaFast_Past > emaSlow_Past
&& emaFast_Current < emaSlow_Past)
{
PlaceTrade(OP_SELL);
}
if(emaFast_Past < emaSlow_Past
&& emaFast_Current > emaSlow_Past)
{
PlaceTrade(OP_BUY);
}
}
double
GetValueForMA(int
_period,int
_shift)
{
return
iMA(NULL,0,_period,0,method_both,applied_price_both,_shift);
}
3.ii.6 Use of external Indicators (ex4 files)
In add-on an external indicator which stores its values in buffers can be accessed for the Binary Options strategy, even if simply the compiled ex4-file exists.
Permit us say we like to include the signal line of the KVO indicator https://www.mql5.com/en/lawmaking/8677 to identify trades only if the bespeak 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 Linguistic communication Editor and click on button “Compile” or just restart your MetaTrader 4 later 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 nautical chart. Past hovering the cantankerous over the chart (press mouse-wheel on chart to bring upwardly the cross) the buffer values of the indicator of the hovered timeperiod will be shown in data window:
The data window labels tells us the second buffer value of the indicator stores the point line. If buffers of indicators did non have labels, we can discover the right one past 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 one = buffer 0, buffer value ii = buffer 1 and then on and we have to access buffer 1 to get the indicate value.
Side by side we have to know all input parameters of the external indicator we like to admission. Past draging the indicator on a chart, we run into all input paremeters:
Let the states further say, we like to admission the indicator with (its default) values: 34, 55 and 13. We utilize 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 final 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-status of the strategy:
…
inputint period_fast =5;
inputint period_slow =x;
inputENUM_MA_METHOD method_both = MODE_SMA;
inputENUM_APPLIED_PRICE applied_price_both = PRICE_CLOSE;
…
void
CheckMyRules()
{
double
emaSlow_Current = GetValueForMA(period_slow,
0);
double
emaFast_Current = GetValueForMA(period_fast,
0);
double
emaSlow_Past = GetValueForMA(period_slow,
1);
double
emaFast_Past = GetValueForMA(period_fast,
1);
double kvoSignal = GetValuesFromIndicator__KVO__(1,0);
PrintDebugValue(“emaSlow_Current: “,(cord)emaSlow_Current,0);
PrintDebugValue(“emaFast_Current: “,(string)emaFast_Current,1);
PrintDebugValue(“emaSlow_Past: “,(string)emaSlow_Past,2);
PrintDebugValue(“emaFast_Past: “,(string)emaFast_Past,three);
if(emaFast_Past > emaSlow_Past
&& emaFast_Current < emaSlow_Past
&& kvoSignal <
0)
{
PlaceTrade(OP_SELL);
}
if(emaFast_Past < emaSlow_Past
&& emaFast_Current > emaSlow_Past
&& kvoSignal >
0)
{
PlaceTrade(OP_BUY);
}
}
double
GetValueForMA(int
_period,int
_shift)
{
return
iMA(Zippo,0,_period,0,method_both,applied_price_both,_shift);
}
double GetValuesFromIndicator__KVO__(int
_buffer,
int
_shift=0)
{
render (
iCustom
(
NULL,
0,
“\\Downloads\\KVO.ex4”,
34,
55,
13,
_buffer,
_shift
)
);
}
It is besides possible to enhance the input parameters of our strategy indicator with the values for the used KVO indicator and ready the values in helper function by variables. As this tutorial should be merely an example and “as elementary equally possible”, this variant is not shown.
iii.3 The complete code
Below you will notice the complete lawmaking of the Binary-Options-Strategy-Example from all the steps above, gear up to elevate on the Binary-Options-Strategy-Tester to exam and see the results on chart:
#property
copyright
“Copyright 2016, __martin__”
#belongings
link“https://world wide web.mql5.com/en/users/__martin__”
#belongings
version
“ane.00”
#property
strict
#belongings
indicator_separate_window
#include
<BinaryOptionsStrategyLibrary.mqh>
input
int period_fast =five;
input
int period_slow =
10;
input
ENUM_MA_METHOD method_both =
MODE_SMA;
input
ENUM_APPLIED_PRICE
applied_price_both =
PRICE_CLOSE;
int
OnInit()
{
render(INIT_SUCCEEDED);
}
int
OnCalculate(const
int
rates_total,
const
int
prev_calculated,
const
datetime
&time[],
const
double
&open up[],
const
double
&high[],
const
double
&low[],
const
double
&close[],
const
long
&tick_volume[],
const
long
&book[],
const
int
&spread[])
{
CallStrategy();
return(rates_total);
}
void
CheckMyRules()
{
double
emaSlow_Current = GetValueForMA(period_slow,
0);
double
emaFast_Current = GetValueForMA(period_fast,
0);
double
emaSlow_Past = GetValueForMA(period_slow,
1);
double
emaFast_Past = GetValueForMA(period_fast,
ane);
double
kvoSignal = GetValuesFromIndicator__KVO__(1,0);
PrintDebugValue(“emaSlow_Current: “,(string)emaSlow_Current,0);
PrintDebugValue(“emaFast_Current: “,(cord)emaFast_Current,i);
PrintDebugValue(“emaSlow_Past: “,(string)emaSlow_Past,2);
PrintDebugValue(“emaFast_Past: “,(string)emaFast_Past,3);
if(emaFast_Past > emaSlow_Past
&& emaFast_Current < emaSlow_Past
&& kvoSignal <
0)
{
PlaceTrade(OP_SELL);
}
if(emaFast_Past < emaSlow_Past
&& emaFast_Current > emaSlow_Past
&& kvoSignal >
0)
{
PlaceTrade(OP_BUY);
}
}
double
GetValueForMA(int
_period,int
_shift)
{
return
iMA(NULL,0,_period,0,method_both,applied_price_both,_shift);
}
double
GetValuesFromIndicator__KVO__(int
_buffer,
int
_shift=0)
{
render
(
iCustom
(
Nix,
0,
“\\Downloads\\KVO.ex4”,
34,
55,
13,
_buffer,
_shift
)
);
}
iv. Run a backtest (video)
The following video shows how to run a backtest of your Binary Options strategy in Strategy-Tester of MetaTrader 4:
- Get-go Binary-Options-Strategy-Tester in Strategy-Tester of MetaTrader iv and fix the input parameters
-
Drag your Binary Options strategy indicator on the chart, set the input parameters and
check “Allow external adept 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)
- Salve all settings in a template to run the exam with all settings once again – using the pause push of the Strategy-Tester (optional)
- See the results of your Binary Options strategy on the Strategy-Tester nautical chart
5. Run a frontwards test
To practice a forrad examination but drag the Binary-Options-Strategy-Tester utility and your strategy indicator on your demo or alive nautical chart of your broker instead of using information technology in Strategy-Tester:
- Drag Binary-Options-Strategy-Tester utility on demo or live chart and set the input parameters
-
Elevate your Binary Options strategy indicator on the chart, fix the input parameters and
cheque “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 forrad 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 nautical chart
vi. FAQ
Question: Why exercise you show an example of a non profitable Binary Options strategy?
Answere: This is just an instance how to build a strategy in an Indicator to communicate with the Binary-Options-Strategy-Tester utility in market to exam and improve your strategy.
Question: Binary-Options-Strategy-Tester stops afterward the exact corporeality of losses with error “Assortment out of range”. Why?
Answere: Binary-Options-Strategy-Tester can ascension an error later ten losses to stop Tester and to analyse the situaion on the chart. If you practise not desire to, just switch off the option in settings.
Question: No arrows appear on chart after I draged my indicator with a working strategy on information technology. What happened?
Answere: You lot 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 fault 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 accept to purchase the product.
Question: No arrows appear on chart afterward 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 four. What tin can I do?
Answere: Utilise the latest version (greater v1.00) of BinaryOptionsStrategyLibrary.mqh. Check version tag in code of your BinaryOptionsStrategyLibrary.mqh and run across changelog v1.01 of BinaryOptionsStrategyLibrary.
Question: I see no results on Strategy-Tester tabs “Results”, “Graph”, “Report”. Where I can encounter the results?
Answere: Strategy-Tester of MetaTrader 4 can not handle Binary Options then these tabs con not be used. Therefore this utility calculates all wins and losses and prints the results on the chart.
vii. Miscellaneous
As I need a possibility to exam Binary Options strategies automated in Strategy-Tester of MetaTrader 4 for long time periods in a brusk fourth dimension and to exercise foward tests on the chart of the broker, this utility was build. I take 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 information technology and maybe some improvements will bring it closer to fit the needs of you. So please experience gratuitous to contact me for ideas for improvements!
Source: https://www.mql5.com/en/articles/2820