[request]simple hedging EA

 

hi, i have a strategy that i posted at https://www.mql5.com/en/forum/173595

i'll apreciate if somebody make an EA for this strategy & help me to test that strategy

welcome to any improvement...

 

hedging

Hi,

Here is help from CodersGuru:

HEDGING:

http://www.metatrader.info/node/113

Today we are going to talk about a very important trading idea and take further step by creating a simple Expert Advisor for this idea.

We are going to study the "Hedging"

What's the meaning of Hedging?

Hedging is a method the Forex trader take to reduce the risk involved in holding an investment. You can think in it as the insurance!

When you open an EURUSD position, there are only two future possibilities, the price moves in your direction or it moves against you. Hedging this position is the method you'll take to reduce the risk of the open EURUSD position by opening an opposite position (Buy when you've already sold and sell when you've already bought).

Hedging methods in Forex:

Opening an opposite position as mentioned above is is not the only method of hedging positions in Forex. And a lot of brokers do not allow their client to open two opposite positions of the same currency at the same time!

There are a lot of hedging methods but we are going to study one of them that works and in the same time no brokers will prevent you from using this method!

Our method is hedging the position by opening the same position (buy/sell) for another currency pairs that has negative correlation with the first currency we trade.

Negative correlation?

The correlation is the relation between the currency pairs. When two pairs have a positive correlation that means they are going the same direction. i.e. The EURUSD has a positive correlation with GBPUSD. (figure 1).

When two pairs have a negative correlation that means they are going the opposite direction. i.e. The EURUSD has a negative correlation with USDCHF. (figure 2).

Note: More details about correlation will be discuss in a separated article!

Our Expert advisor:

We are going to implement the idea of hedging by using the negative correlation between two pairs to write a simple Expert advisor.

Our expert advisor will open two positions (buy) of EURUSD and USDCHF (no much no less). Just notice the sum of the two trades and you will see clearly how the two positions have been hedged.

Note: You can take this Expert further more if you want to double the lot size of one of the opened traders when it make profit. or you can close the two opened positions when the total profit is a specified value (ex: 100 Pips).

Note: This kind of Expert Advisors (which trade more than one currency pair) couldn't be tested with MetaTrader Strategy Tester due the limitation detailed here:

"Trading is permitted for the symbol under test only, no portfolio testing

Attempts to trade using another symbol will return error"

http://www.metaquotes.net/experts/articles/tester_limits

The code:

//+------------------------------------------------------------------+

//| Hedging.mq4 |

//| Coders Guru |

//| https://www.mql5.com/en/forum |

//+------------------------------------------------------------------+

#property copyright "Coders Guru"

#property link "https://www.forex-tsd.com"

extern string Sym_1 = "EURUSD";

extern string Sym_2 = "USDCHF";

extern double Lots = 1;

extern int Slippage = 5;

bool Sell = true;

//+------------------------------------------------------------------+

int start()

{

int cnt,total;

if(Bars<100) {Print("bars less than 100"); return(0);}

total = OrdersTotal();

if(total < 1)

{

if(Sell==0)

{

RefreshRates();

OrderSend(Sym_1,OP_BUY,Lots,MarketInfo(Sym_1,MODE_ASK),Slippage,0,MarketInfo(Sym_1,MODE_ASK)+1000*Point,"Hedging",1234,0,Green);

RefreshRates();

OrderSend(Sym_2,OP_BUY,Lots,MarketInfo(Sym_2,MODE_ASK),Slippage,0,MarketInfo(Sym_2,MODE_ASK)+1000*Point,"Hedging",1234,0,Green);

}

else

{

RefreshRates();

OrderSend(Sym_1,OP_SELL,Lots,MarketInfo(Sym_1,MODE_BID),Slippage,0,MarketInfo(Sym_1,MODE_BID)-1000*Point,"Hedging",1234,0,Red);

RefreshRates();

OrderSend(Sym_2,OP_SELL,Lots,MarketInfo(Sym_2,MODE_BID),Slippage,0,MarketInfo(Sym_2,MODE_BID)-1000*Point,"Hedging",1234,0,Red);

}

return(0);

}

return(0);

}

About the code:

As you see in the code above we open two similar trades for EURUSD and USDCHF which have a negative correlation.

We used the MarketInfo() function to get the bid/ask price for each pairs. This is the most important thing in this code because MarketInfo() function is the only way to get the bid/ask prices for another pairs of the currently symbol of chart! You can't use here the functions Bid or Ask.

Before using the MarketInfo() we have used the function RefreshRates() to be sure that we getting the up-to-date market data.

Hope you find the code and the article helpful and hope to drop me a comment!