Hedging EA is a trade manager that will turn your losing trades into winners using an imbalanced hedging strategy. It was designed to manage manual trades, but the EA can also start trades automatically using custom indicators. Let’s see how to set up the EA to trade using Trend Trading.
This guide assumes that you have Hedging EA and Trend Trading Indicator installed in your Metatrader Terminal.
Since this particular indicator has many different trading signals provided via iCustom(), namely trend-changes, pullbacks, breakouts and corrections, we need to code an intermediate custom indicator to act as a transmission chain between the indicator and the EA.
Code a custom indicator
1. As previously explained in this post, open MetaEditor, create a blank new custom indicator and paste the following code. The indicator coded below will act as an intermediary between the many signal types provided by Trend-Trading and the EA.
//--- Indicator properties #property copyright "Arthur Lopez" #property link "http://www.pointzero-trading.com" #property description "Custom indicator to trade Trend-Trading signals with:" #property description " - Hedging EA" #property description " - Averaging EA" #property description " - Stop/Reverse EA" #property version "1.00" #property strict //--- Drawing and buffers #property indicator_chart_window #property indicator_buffers 2 //--- Buffer colors and sizes #property indicator_color1 clrDodgerBlue #property indicator_color2 clrTomato #property indicator_width1 2 #property indicator_width2 2 //--- Constants #define ShortName "Custom Trend Trading" #define OP_BUY_1 4 // Trend Change (Blue 1) #define OP_SELL_1 5 // Trend Change (Red 1) #define OP_BUY_2 6 // Pullback (2) #define OP_SELL_2 7 // Pullback (2) #define OP_BUY_3 8 // Inside Bar Breakout (3) #define OP_SELL_3 9 // Inside Bar Breakout (3) #define OP_BUY_4 10 // Correction (blue dash) #define OP_SELL_4 11 // Correction (red dash) //--- Input parameters extern string IndiName = "PZ_TrendTrading"; // Indicator Name extern int TrendPeriod = 200; // Indicator Period extern bool TChanges = true; // Trade Trend Changes extern bool TPullbacks = true; // Trade Pullbacks extern bool TBreakouts = true; // Trade Breakouts extern bool TCorrections = false; // Trade Corrections input int MaxHistoryBars = 500; // Max History Bars //--- Buffer arrays double SignalBuffer[]; double ExtMapBuffer1[]; double ExtMapBuffer2[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { // Buffers needed IndicatorBuffers(3); // Bullish arrows SetIndexBuffer(0, ExtMapBuffer1); // Assign buffer array SetIndexStyle(0, DRAW_ARROW); // Style to arrow SetIndexArrow(0, 233); // Arrow code // Bearish arrows SetIndexBuffer(1, ExtMapBuffer2); // Assign buffer array SetIndexStyle(1, DRAW_ARROW); // Style to arrow SetIndexArrow(1, 234); // Arrow code // Signal buffer SetIndexBuffer(2, SignalBuffer); // Shortname IndicatorShortName(ShortName); //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[]) { // Start and limit int start = 1; int limit; // Bars counted so far int counted_bars = IndicatorCounted(); // No more bars? if(counted_bars < 0) return(-1); // Do not check repeated bars limit = Bars - 1 - counted_bars; // Iterate bars from past to present for(int i = limit; i >= start; i--) { // Set values to empty SignalBuffer[i] = EMPTY_VALUE; ExtMapBuffer1[i] = EMPTY_VALUE; ExtMapBuffer2[i] = EMPTY_VALUE; // If not enough data... if(i > MathMin(MaxHistoryBars,Bars)-1) continue; // Make call double value = iCustom(Symbol(), Period(), IndiName, "--", TrendPeriod, true, true, true, true, MaxHistoryBars, "--", false, false, 10, i); // If failed if(value == 0) { // Err code int err = GetLastError(); // Print error if(err == ERR_CUSTOM_INDICATOR_ERROR || err == 4072) { Print(ShortName +" [ERROR] Could not find custom indicator "+ IndiName); return(0); } } // Translate signals to local if(value == OP_BUY_1 && TChanges) ExtMapBuffer1[i] = Low[i]; if(value == OP_SELL_1 && TChanges) ExtMapBuffer2[i] = High[i]; if(value == OP_BUY_2 && TPullbacks) ExtMapBuffer1[i] = Low[i]; if(value == OP_SELL_2 && TPullbacks) ExtMapBuffer2[i] = High[i]; if(value == OP_BUY_3 && TBreakouts) ExtMapBuffer1[i] = Low[i]; if(value == OP_SELL_3 && TBreakouts) ExtMapBuffer2[i] = High[i]; if(value == OP_BUY_4 && TCorrections) ExtMapBuffer1[i] = Low[i]; if(value == OP_SELL_4 && TCorrections) ExtMapBuffer2[i] = High[i]; // Store trade in buffer if(ExtMapBuffer1[i] != EMPTY_VALUE && ExtMapBuffer1[i] != 0) SignalBuffer[i] = OP_BUY; if(ExtMapBuffer2[i] != EMPTY_VALUE && ExtMapBuffer2[i] != 0) SignalBuffer[i] = OP_SELL; } // Exit return(rates_total); }
2. Once the code is pasted, make sure to edit the line 33 of the code typing your own trend-trading indicator filename, which should contain your license number. For instance, if your filename is named “PZ_TrendTrading_0123456789”, the code should look as follows.
extern string IndiName = "PZ_TrendTrading_0123456789"; // Indicator Name
3. Save the file as “Custom_TrendTrading.mq4”, compile the source code and make sure no compilation errors arise. Once done, proceed to the tester as explained below.
Configure the EA to trade
- Open the Metatrader Terminal.
- Click View -> Strategy Tester to open the tester.
- Select Hedging EA from the Expert Advisor’s selection.
- Click on [Expert Properties] to open the EA inputs.
- Set the Entry Strategy input to “Custom Indicator”
- Set the Custom Indicator Name to “Custom_TrendTrading”.
- Set the Custom Signal Buffer to 2.
- Click [OK] to close the dialog and [Start] to start the test.
- Click [Open Chart] to make sure trades match the indicator.
All Done! Hedging EA is now starting trades using the custom indicator Trend Trading, using an intermediate custom indicator that transmits the signals from the indicator to the EA.
Additionally, by editing the input parameters of Custom_TrendTrading.mq4, you can change the period of the indicator and enable/disable the different trading signal types.
These same instructions can be applied to Averaging EA and Stop/Reverse EA. Both are trade-recovery EAs and can start trades using custom indicators as well, using the same parameters as described above.
Feel free to post your comments or questions below. Thank you!
Hello Arthur, this is a great contribution, thank you!
Is there a way to make the EA to disable the recovery strategy once the trade is started and the SL is beyond breakeven, and to start a pyramiding strategy accordingly to trend trading indicator’s setups of type 2 and 3?
That would be cherry on the cake!!
Hi Alessandro. Hedging EA, StopReverse EA and Averaging EA don’t implement pyramiding at all. Just one trade or recovery. My apologies.
I know, this is the “problem”: hedging EA is a great idea, but requires to start with a small lot size, so profits comes out if you pyramid your trade once it enters the safe zone; if you do not pyramid you are going almost no where.
Otherwise you have to follow a traditional SL strategy and suffer some losses.
So, maybe this could be an idea for a new EA project. putting alltogether.
What do you think about?
The EA indeed requires you to reserve free margin for the recovery process. You can decrease the amount of recovery trades or make the lot multiplier less aggressive and it’ll allow you to set a bigger lotsize. My take is to take positional trades that have a lot to run on. If your trades are good you don’t need as many recovery trades.
Hi Arthur, can i apply the harmonic indicator to averaging EA?
Yes, read How to trade Harmonacci Patterns signals with Hedging EA.
Thanks Arthur, would this be possible for the divergence indicator with averaging ea? I tried it out but did not make trades however it did implement the indicator into the chart.
Hi Stuart! It is possible, all you need to do is setting Signal Buffer as “4” in inputs and calling your divergence trading indicator name. Thanks.
Yes. Call the indicator name and signal buffer 1.
Hello Arthur,
Is hedging allowed with U.S. based forex brokers? I would like to use this EA.
Nope, USA brokers don’t accept hedging nor closing trades in random orders. Won’t work with US Brokers.
Hello Arthur.
I rent for a month the Hedging EA and the Trend trading Indicator and I have tried to put the EA to work using the indicator signal. It is not working but seems the problem is that the changing in Indicator’s name on program line 33 is not working. As I have rented, seems it do not change it’s name. I have tried keeping the same name and also changing the name to “PZ Trend Trading” and “PZ_Trend_Trading”, but no sucess.
Could you help me out on it?
Please open a support ticket in the help section. Thanks.
I added a signal buffer recently. Call the custom indicator filename as it appears in mql4/indicators in your file explorer, signal buffer is 12.