banner



trend following strategy binary options

Table of Contents

i. 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
iii.2.ii. Include Binary-Options-Strategy-Library
3.two.3. Add together CallStrategy()
3.ii.4. Implement CheckMyRules() and helper-role
iii.2.v. Print out debug values
3.two.half-dozen. Use of external Indicators (ex4 files)
3.3. The complete lawmaking
4. Run a backtest (video)
5. Run a forward exam
6. FAQ
7. Miscellaneous


1. Introduction

This commodity shows how to build a Binary Options strategy and exam it in Strategy-Tester of Metatrader four with Binary-Options-Strategy-Tester utility. By default Strategy-Tester of Metatrader 4 tin can exam Adept Advisors and Indicators against historical data, but information technology cannot handle Binary Options with expire times. As I need a possibility to test Binary Options strategies automatic 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 pace example how to build a Binary Options strategy stored in an Indicator (marked as blood-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 prototype above), to place virtual orders and count their results with backtests and forward tests.

Please go on in mind: Backtesting with historical data will never correspond 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!


ii. Installation

Download and buy 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 lot take to purchase the production to examination Binary Options strategies or this example.

Download free BinaryOptionsStrategyLibrary.mqh and identify it in into folder \Include ([path to your MetaTrader 4]\MQL4\Include):
The costless library volition 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 bear witness the access of external indicators and at that place ex4 files in department "three.2.six Employ 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 "three. 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 lawmaking 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 simply restart your MetaTrader four later on these files are stored in the described folders and MetaTrader 4 volition practice this automatically for you.


3. Binary Options strategy example

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 it 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 course y'all accept to build a profitable strategy by yourself. But as you will see, this utility will help you to test and ameliorate your Binary Options strategy.


three.1 Ascertain 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.

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

Allow us further say, we similar to choose values for "MA averaging menses" (fast and tiresome) and for "applied price" equally well as for the "averaging method". Other values (like symbol, timeframe and shift) depends on the testcase (e.one thousand. the symbol the tester runs on) and should be prepare 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 cheque their crosses, we need the following input parameters for the strategy instance with some default values:

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


3.ii 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 Language Editor (in MetaTrader iv 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 "Side by side":

Language Editor Custom Indicator

Enter the name, copyright and link of the strategy besides equally the input parameters with their types and default values (initial values) by clicking "Add together"-Push button and press "Next":

Language Editor Custom Indicator Values

On tab consequence handlers select checkbox "OnCalculate" as nosotros demand this effect to check for our strategy on every tick. Press "Adjacent":

Language Editor Custom Indicator Events

On tab drawing properties select checkbox "Indicator in seperate window" every bit we need a seperate window to impress out the debug values. Press "End":

Language Editor Custom Indicator Drawing Properties

The initial code of your indicator will announced:






#holding copyright "Copyright 2016, __martin__"
#belongings link"https://www.mql5.com/en/users/__martin__"
#property version "i.00"
#property strict
#belongings indicator_separate_window

input int      period_fast=5;
input int      period_slow=x;
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[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &book[],
const int &spread[])
{


return(rates_total);
}


3.2.1 Input parameters

The initial input parameters are created with the MQL Magician (encounter iii.2 Create Binary Options strategy) and nosotros will enhance them with the post-obit steps.

To avert to have to enter int-values for applied cost and averaging method of the Moving Averages for input parameters, the blazon 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 as labels instead of variable names:

...


inputint                period_fast        =five;
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 besides as "labels" for the input parameters:

Binary Options Strategy Input Paremeters


iii.ii.2 Include Binary-Options-Strategy-Library

If you take 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:






#property copyright "Copyright 2016, __martin__"
#property link"https://www.mql5.com/en/users/__martin__"
#property version "ane.00"
#property strict
#property indicator_separate_window

#include <BinaryOptionsStrategyLibrary.mqh>

...

The library volition just exist available similar described in the example above if you placed it in \Include folder of your MetaTrader four.
Irresolute the content of the library is not needed!

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

  • Place just i SELL or i Buy trade per candle
  • Check simply at the first of a new candle for the strategy

Binary Options Strategy Input Parameters Enhanced


3.ii.iii Add CallStrategy()

Add together 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:

...




intOnCalculate(constint rates_total,
constint prev_calculated,
constdatetime &time[],
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 volition call a function named CheckMyRules() in your indicator where you can place your atmospheric condition for your Binary Options strategy.
Therefore y'all accept to implement the part CheckMyRules() in your Binary Options strategy indicator.


3.2.iv Implement CheckMyRules() and helper-role

In CheckMyRules()-office, which is called through the Binary-Options-Strategy-Library, the conditions 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        =ten;
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, one);
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)
{
return 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 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 every bit labels:

...


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

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.6 Employ 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 the states say we like to include the indicate 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 identify 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 iv will do this automatically for you.

First we have to identify the relevant buffers which stores the relevant values to access. Therefore nosotros press the push button "Data Window" in MetaTrader four to evidence 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 cross) the buffer values of the indicator of the hovered timeperiod will exist shown in data window:

External Indicator Buffers And Values

The information window labels tells us the second buffer value of the indicator stores the signal line. If buffers of indicators did not take labels, we tin 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, then we have buffer value 1 = buffer 0, buffer value ii = buffer 1 then on and we take to access buffer one to get the signal value.

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

Input Parameters KVO

Permit us further say, we like to admission the indicator with (its default) values: 34, 55 and 13. We apply a helper office (based on iCostum), wich provides us the possibility to get the values of the indicator with parameters for buffer and shift, while shift 0 volition be the value of the current candle, shift ane the value of the last candle, shift two the value of the 2d to last candle and and then on. In addition nosotros temporarilly shop the values of the indicator buffer and heighten the if-condition of the strategy:

...


inputint                period_fast        =five;
inputint                period_slow        =ten;
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__(one,0);

   PrintDebugValue("emaSlow_Current: ",(string)emaSlow_Current,0);
PrintDebugValue("emaFast_Current: ",(string)emaFast_Current,i);
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(NULL,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,
13,

_buffer,
_shift
)
);

}

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


3.3 The complete code

Below yous volition find the complete code of the Binary-Options-Strategy-Instance from all the steps above, ready to drag on the Binary-Options-Strategy-Tester to test and run into the results on chart:






#property copyright "Copyright 2016, __martin__"
#property link"https://www.mql5.com/en/users/__martin__"
#property version "1.00"
#holding strict
#property 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()
  {


return(INIT_SUCCEEDED);
  }




int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &fourth dimension[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
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, 1);

  
double kvoSignal = GetValuesFromIndicator__KVO__(1,0);

      PrintDebugValue("emaSlow_Current: ",(string)emaSlow_Current,0);
   PrintDebugValue("emaFast_Current: ",(string)emaFast_Current,1);
   PrintDebugValue("emaSlow_Past: ",(cord)emaSlow_Past,ii);
   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(NULL,0,_period,0,method_both,applied_price_both,_shift);
  }








double GetValuesFromIndicator__KVO__(int _buffer, int _shift=0)
  {
render (
iCustom (
Naught,
0,

                  
"\\Downloads\\KVO.ex4",

34,
55,
13,

                                         _buffer,
                      _shift
                    )
          );
  }


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:

  • Commencement Binary-Options-Strategy-Tester in Strategy-Tester of MetaTrader iv and set the input parameters
  • Drag your Binary Options strategy indicator on the chart, fix the input parameters and check "Allow external expert imports" on the "common" tab
  • Elevate 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 examination with all settings over again - using the suspension button of the Strategy-Tester (optional)
  • See the results of your Binary Options strategy on the Strategy-Tester chart


5. Run a frontwards exam

To do a frontwards test merely elevate 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 fix 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
  • Elevate your used indicators with their used input parameters on the chart to meet their values while forrad test is running (optional)
  • Save all settings in a template to run the test once more with all settings (optional)
  • See the results of your Binary Options strategy on demo or live chart


half dozen. FAQ

Question: Why do you evidence an example of a non profitable Binary Options strategy?
Answere: This is merely an instance 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 afterward the verbal corporeality of losses with mistake "Array out of range". Why?
Answere: Binary-Options-Strategy-Tester can rise an mistake after 10 losses to stop Tester and to analyse the situaion on the chart. If you do not want to, just switch off the pick 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 "Permit external adept imports" on the "common" tab while you elevate your strategy-indicator on the chart (log message volition show an error in this example).

Question: No arrows announced on chart after I draged my indicator with a working strategy on it with "Let external skilful 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 but works if the production has a working license. Therefore you lot have to buy the product.

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

Question: I run across no results on Strategy-Tester tabs "Results", "Graph", "Written report". Where I can run across the results?
Answere: Strategy-Tester of MetaTrader 4 tin 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 nautical 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 brusque time and to do foward tests on the nautical chart of the banker, this utility was build. I have spent a lot of time for the concept and the implementation of the Binary-Options-Strategy-Tester likewise equally for the documentation. Perhaps there is a ameliorate manner to do information technology and maybe some improvements will bring information technology closer to fit the needs of you. And so please feel gratis to contact me for ideas for improvements!

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

Posted by: cummingstiledgets.blogspot.com

0 Response to "trend following strategy binary options"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel