The Basic of Coding A Hedge Expert Advisor MQL4 Articles
Post on: 11 Май, 2015 No Comment
Introduction
I’m going to give an idea of a simple hedge expert advisor. The Big Note Of Hedging EA Basic:
Hedge (finance) (From Wikipedia, the free encyclopedia)
(Redirected from Hedging )
In finance. a hedge is an investment that is taken out specifically to reduce or cancel out the risk in another investment. Hedging is a strategy designed to minimize exposure to an unwanted business risk, while still allowing the business to profit from an investment activity. Typically, a hedger might invest in a security that he believes is under-priced relative to its fair value (for example a mortgage loan that he is then making), and combine this with a short sale of a related security or securities. Thus the hedger doesn’t care whether the market as a whole goes up or down in value, only whether the under-priced security appreciates relative to the hedge. Holbrook Working. a pioneer in hedging theory, called this strategy speculation in the basis, [1] where the basis is the difference between the hedge’s theoretical value and its actual value (or between spot and futures prices in Working’s time).
Some form of risk taking is inherent to any business activity. Some risks are considered to be natural to specific businesses, such as the risk of oil prices increasing or decreasing is natural to oil drilling and refining firms. Other forms of risk are not wanted, but cannot be avoided without hedging. Someone who has a shop, for example, can take care of natural risks such as the risk of competition, of poor or unpopular products, and so on. The risk of the shopkeeper’s inventory being destroyed by fire is unwanted, however, and can be hedged via a fire insurance contract. Not all hedges are financial instruments: a producer that exports to another country, for example, may hedge its currency risk when selling by linking its expenses to the desired currency.
All the things we need from the server must be called by the MarketInfo(string symbol, int type) function. This function allow us to call any data beyond the data that appearing on the active chart window. This also allow us to send any kind of order of any symbol beyond the symbol that actively shown in the active chart window. And this can let us hedge the 2 symbol easily. Thanks God and MT4 team members, it helps so much.
One thing necessary in hedging is The Correlation between the 2 monitoring symbols which can find out with some small functions that will be shown below.
Correlation. in the financial world, is the statistical measure of the relationship between two securities. The correlation coefficient ranges between -1 and +1. A correlation of +1 implies that the two currency pairs will move in the same direction 100% of the time. A correlation of -1 implies the two currency pairs will move in the opposite direction 100% of the time. A correlation of zero implies that the relationship between the currency pairs is completely random.read more here
All above are the simple things the Forex hedgers using MT4 Expert Advisor need to know. Now we can start making a hedge EA.
Step by Step to Code the Hedge EA
Step 1. The Input Parameters
First before start writing the Hedge EA, we need to choose 2 correlate symbols, i.e.
- GBPUSD & EURUSD that always move the same way;
- EURUSD & USDCHF that always move the opposite way;
- * etc.
In this article, I will choose my very own favorite hedge pair that is EURJPY & GBPJPY. It always moves the same way, easier to set the hedging order type. Now let’s get started. To start making a Hedge EA, let’s get known the input variables below.
Step 2. The Variable Declaration
Following is the variables used in this EA and I will only explain the variable necessary to understand how the EA works.
Step 3. Get All Necessary Static Parameters
Step 4. The Useful Functions
Before we get into the funniest part we are waiting for, the start() function, let us begin with the functions used in this EA. But, please note that all functions will stay outside the start() function.
1. The Correlation Function
First we need to start with the correlation calculation functions. The functions below are applied from a man giving away the free correlation indicator (igorad2004@list.ru) and I modified them to easier use in this EA, this we will not need to call the correlation value from the outside indicator any more. Good idea?
The CorPeriod variable will be extern as an input variable to allow us to adjust it. When you want to calculate the correlation between 2 symbols, just call the Cor(string base,string hedge) function like this Cor(EURJPY,GBPJPY). It’s easy, isn’t it?
2. The Send Hedge Function
I think it’s easier to manage how we can send the hedge orders by creating a SendH function below.
This ErrorDescription(GetLastError()) function above lets our EA tell us what error occurred when the trading function was working for us. To use it, we need to include the stdlib.mqh file by placing the code like this:
And to use it, just call the ErrorDescription() function like shown above.
3. The Close Hedge Function
Beyond sending the orders, we also need a function to close all hedging orders when they reach our expected profit. And here you go:
This function will only close the orders with the same magic number, meaning that it will not interfere the hedge orders with other magic numbers. Not really a thing to worry about. Before using that close function, we need to define how much we’ve got now by using the function below.
4. The Finding The Total Profit Function
Like with the close function, to know the hedge profit we need to monitor only the orders with the same magic number, to close them correctly. To use them, just code them like this:
All profit values are calculated in USD. From the line above, when overall profit of orders with magic number 318 is greater than $100 they will be closed. That’s it. To open the hedge orders, we need to know that there’s no any order with same symbol and magic number floating at the moment we need to send the hedge. This can be defined by this function.
5. Get The Amount Of Existing Positions
It can be used like this:
This function will return us how many opening orders of GBPJPY with magic number of 318 are floating at the moment. One more function to define the type of floating order.