How To Program Binary Option For Mt4

Tabular array of Contents

1. Introduction
ii. Installation
3. Binary Options strategy example
three.i. Ascertain Binary Options strategy
3.2. Create Binary Options strategy
3.2.1. Input parameters
3.two.ii. Include Binary-Options-Strategy-Library
3.2.3. Add together CallStrategy()
3.2.iv. Implement CheckMyRules() and helper-part
3.2.5. Impress out debug values
3.2.6. Utilise of external Indicators (ex4 files)
3.3. The consummate code
4. Run a backtest (video)
5. Run a frontwards examination
6. FAQ
seven. 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. Past default Strategy-Tester of Metatrader 4 can exam Practiced Advisors and Indicators against historical data, just it cannot handle Binary Options with expire times. Equally I demand a possibility to test Binary Options strategies automatic in Strategy-Tester of MetaTrader iv, 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 stride by step example how to build a Binary Options strategy stored in an Indicator (marked as red in image to a higher place) to communicate through Binary-Options-Strategy-Library (marked as green in image above) with the Binary-Options-Strategy-Tester (marked as blue in paradigm to a higher place), to place virtual orders and count their results with backtests and frontward tests.

Please proceed in listen:
Backtesting with historical data will never represent the real time to come, but information technology 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 fix of hight quality data!


two. Installation

Download and purchase Binary-Options-Strategy-Tester utility from market:
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 yous have to purchase the product to test Binary Options strategies or this example.

Download free BinaryOptionsStrategyLibrary.mqh and place information technology 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 identify information technology (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 admission of external indicators and at that place ex4 files in section “three.2.6 Use of external Indicators (ex4 files)”. Encounter https://www.mql5.com/en/code/8677 for more than details of the indicator.

Now you can get further with section “3. Binary options strategy case” and build the case lawmaking by yourself or simply download the lawmaking of this case beneath.

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 lawmaking of this Binary Options strategy example to permit it run without edifice it past yourself.

To compile the needed .ex4 files open the .mq4 files (KVO.mq4 and BinaryOptionsStrategyExample.mq4 – Non 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 four will practice this automatically for you.


iii. Binary Options strategy case

The following steps will guide you throgh an example how to build an case Binary Options strategy stored in an Indicator to communicate with Binary-Options-Strategy-Tester. You can build information technology past yourself or only 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 grade you accept to build a profitable strategy past yourself. But as y’all will encounter, this utility will help yous to test and improve your Binary Options strategy.


3.i Define Binary Options strategy

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

Allow us say we like to create a simple Moving Average cross strategy with one “fast” and ane “deadening” Moving Average to trade on adjacent candle after they have crossed each other. Documentation tells, how we can become the value of a single Moving Average: https://docs.mql4.com/indicators/ima.

Let the states further say, we like to choose values for “MA averaging period” (fast and tedious) and for “applied price” likewise equally 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 demand the post-obit variables for a Moving Average:

int
ma_period
int
ma_method
int
applied_price

As nosotros demand ii Moving Averages to check their crosses, we demand the post-obit input parameters for the strategy example with some default values:

Baca juga:  Prop Firm Funding: Everything You Need To Know

int
period_fast        =  5;
int
period_slow        = x;
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 elevate 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 Sorcerer 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 too every bit the input parameters with their types and default values (initial values) past clicking “Add together”-Button and press “Adjacent”:

Language Editor Custom Indicator Values

On tab event handlers select checkbox “OnCalculate” every bit we need this result to bank 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 nosotros need a seperate window to impress out the debug values. Press “Finish”:

Language Editor Custom Indicator Drawing Properties

The initial code of your indicator volition appear:

#holding

copyright
“Copyright 2016, __martin__”

#property

link“https://www.mql5.com/en/users/__martin__”

#property

version
“1.00”

#belongings

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()
{

  

return(INIT_SUCCEEDED);
}

int
OnCalculate(const
int
rates_total,
const
int
prev_calculated,
const
datetime
&fourth dimension[],
const
double
&open up[],
const
double
&loftier[],
const
double
&low[],
const
double
&close[],
const
long
&tick_volume[],
const
long
&book[],
const
int
&spread[])
{

return(rates_total);
}


3.ii.1 Input parameters

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

To avert to have to enter int-values for practical toll 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 add-on comments for the input parameters are added to show the comments as labels instead of variable names:

inputint                period_fast        =v;
inputint                period_slow        =x;
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 bachelor values to select also as “labels” for the input parameters:

Binary Options Strategy Input Paremeters


iii.2.2 Include Binary-Options-Strategy-Library

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

#property

copyright
“Copyright 2016, __martin__”

#property

link“https://www.mql5.com/en/users/__martin__”

#property

version
“1.00”

#belongings

strict

#property

indicator_separate_window

#include

<BinaryOptionsStrategyLibrary.mqh>

The library will only exist bachelor like described in the instance above if yous placed information technology in \Include folder of your MetaTrader iv.
Changing the content of the library is not needed!

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

  • Place just one SELL or one Purchase trade per candle
  • Check only at the first of a new candle for the strategy

Binary Options Strategy Input Parameters Enhanced


3.2.three 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 lot have inlcuded similar discribed higher up:

intOnCalculate(constint rates_total,
constint prev_calculated,
constdatetime &fourth dimension[],
constdouble &open[],
constdouble &high[],
constdouble &low[],
constdouble &close[],
constlong &tick_volume[],
constlong &volume[],
constint &spread[])
{

CallStrategy();

return
(rates_total);
}

CallStrategy()-function in Binary-Options-Strategy-Library will call a part named CheckMyRules() in your indicator where you can place your atmospheric condition for your Binary Options strategy.
Therefore
yous accept to implement the role CheckMyRules() in your Binary Options strategy indicator.


3.2.4 Implement CheckMyRules() and helper-role

In CheckMyRules()-office, which is called through the Binary-Options-Strategy-Library, the weather for the strategy are implemented and trades are placed through PlaceTrade()-role 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-part GetValuesForMA():

inputint                period_fast        =5;
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,
1);



double
emaFast_Past = GetValueForMA(period_fast,
ane);


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)


{


render
iMA(NULL,0,_period,0,method_both,applied_price_both,_shift);


}


3.ii.5 Print out debug values

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

inputint                period_fast        =5;
inputint                period_slow        =x;
inputENUM_MA_METHOD     method_both        = MODE_SMA;
inputENUM_APPLIED_PRICE applied_price_both = PRICE_CLOSE;

Baca juga:  What Is Pocket Option?

void
CheckMyRules()
{

double
emaSlow_Current = GetValueForMA(period_slow,
0);
double
emaFast_Current = GetValueForMA(period_fast,
0);

double
emaSlow_Past = GetValueForMA(period_slow,
ane);
double
emaFast_Past = GetValueForMA(period_fast,
1);

PrintDebugValue(“emaSlow_Current: “,(string)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)

{
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);
}


three.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, fifty-fifty if simply the compiled ex4-file exists.

Let us say we like to include the point line of the KVO indicator https://www.mql5.com/en/lawmaking/8677 to place trades simply if the bespeak line is over 0 for Purchase 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 iv]\MQL4\Indicators\Downloads).

To compile the needed .ex4 file open up KVO.mq4 in MetaQuotes Language Editor and click on button “Compile” or just restart your MetaTrader 4 afterward the file is stored in the described binder and MetaTrader 4 will exercise this automatically for you.

Showtime we have to identify the relevant buffers which stores the relevant values to admission. Therefore we press the push “Data Window” in MetaTrader 4 to testify all available buffers of the used indicators and drag the KVO indicator on a chart. By hovering the cross over the chart (press mouse-cycle on chart to bring up the cantankerous) the buffer values of the indicator of the hovered timeperiod volition be shown in data window:

External Indicator Buffers And Values

The information window labels tells usa the 2d buffer value of the indicator stores the signal line. If buffers of indicators did non take labels, we can find the right 1 by comparing the buffer values with the displayed value under the cantankerous in the chart and indicator. Buffers of an indicator starts with 0, so we have buffer value i = buffer 0, buffer value ii = buffer ane and so on and nosotros accept to admission buffer ane to get the signal value.

Next nosotros take to know all input parameters of the external indicator nosotros similar to admission. By draging the indicator on a chart, we see all input paremeters:

Input Parameters KVO

Let united states farther say, we similar to access the indicator with (its default) values: 34, 55 and xiii. We utilise a helper part (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 electric current candle, shift one the value of the last candle, shift ii the value of the 2nd to concluding candle and so on. In addition we temporarilly store the values of the indicator buffer and heighten the if-status of the strategy:

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,
1);
double
emaFast_Past = GetValueForMA(period_fast,
1);


double kvoSignal = GetValuesFromIndicator__KVO__(1,0);

   PrintDebugValue(“emaSlow_Current: “,(string)emaSlow_Current,0);

PrintDebugValue(“emaFast_Current: “,(cord)emaFast_Current,1);

PrintDebugValue(“emaSlow_Past: “,(cord)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)
{
render
iMA(Naught,0,_period,0,method_both,applied_price_both,_shift);
}


double GetValuesFromIndicator__KVO__(int
_buffer,
int
_shift=0)

{


return (



iCustom
(



NULL,


0,


“\\Downloads\\KVO.ex4”,


34,



55,



thirteen,

_buffer,
_shift
)

);

}

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


3.iii The consummate code

Below yous will notice 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:

#property

copyright
“Copyright 2016, __martin__”

#property

link“https://www.mql5.com/en/users/__martin__”

#holding

version
“1.00”

#property

strict

#property

indicator_separate_window

#include
<BinaryOptionsStrategyLibrary.mqh>

input
int                period_fast        =5;

input
int                period_slow        =
10;

input
ENUM_MA_METHOD     method_both        =
MODE_SMA;

input
ENUM_APPLIED_PRICE
applied_price_both =
PRICE_CLOSE;

int
OnInit()
  {


return(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
&volume[],
const
int
&spread[])
  {

Baca juga:  Nadex 5 Minute Binary Options Signals

   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,
i);

  

double
kvoSignal = GetValuesFromIndicator__KVO__(ane,0);

      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
   && 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(Aught,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,
xiii,

                                         _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:

  • Start Binary-Options-Strategy-Tester in Strategy-Tester of MetaTrader 4 and set up the input parameters
  • Drag your Binary Options strategy indicator on the chart, set the input parameters and
    check “Permit external expert imports” on the “common” tab
  • Drag your used indicators with their used input parameters on the nautical chart to see their values while tester is running (optional)
  • Salvage all settings in a template to run the exam with all settings once again – using the intermission 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 practise a frontward 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:

  • Elevate Binary-Options-Strategy-Tester utility on demo or live chart and gear up 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 examination 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 merely an case 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 later on the verbal corporeality of losses with error “Assortment 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 non want to, but 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: Y’all have to enable “Allow external good imports” on the “common” tab while you drag your strategy-indicator on the chart (log message volition testify an mistake in this example).

Question: No arrows announced on chart after I draged my indicator with a working strategy on it with “Let external expert imports” enabled. Why?
Answere: A strategy has to telephone call a part of Binary-Options-Strategy-Tester to place virtual trades. Related to the MQL4 license concept this only works if the production has a working license. Therefore y’all accept to buy the production.

Question: No arrows announced on chart subsequently I dragged my indicator with a working strategy on information technology and I got errors like “Cannot telephone call ..” or “Cannot load ..” in the log of MetaTrader 4. What can I exercise?
Answere: Utilize the latest version (greater v1.00) of BinaryOptionsStrategyLibrary.mqh. Bank check version tag in code of your BinaryOptionsStrategyLibrary.mqh and run into changelog v1.01 of BinaryOptionsStrategyLibrary.

Question: I run into no results on Strategy-Tester tabs “Results”, “Graph”, “Written report”. Where I can see the results?
Answere: Strategy-Tester of MetaTrader four tin not handle Binary Options and then these tabs con not be used. Therefore this utility calculates all wins and losses and prints the results on the chart.


7. Miscellaneous

Equally I need a possibility to test Binary Options strategies automatic in Strategy-Tester of MetaTrader iv for long time periods in a brusque fourth dimension 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 equally well equally for the documentation. Maybe there is a better manner to practice it and peradventure some improvements will bring it closer to fit the needs of you. So please experience free to contact me for ideas for improvements!

Source: https://www.mql5.com/en/articles/2820

You May Also Like