Trend Trading Indicator

How to trade Trend Trading signals with Hedging EA

4.8
(6)

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.

PZ Trend Trading Indicator

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

  1. Open the Metatrader Terminal.
  2. Click View -> Strategy Tester to open the tester.
  3. Select Hedging EA from the Expert Advisor’s selection.
    Hedging EA in the tester
  4. Click on [Expert Properties]  to open the EA inputs.
  5. Set the Entry Strategy input to “Custom Indicator”
  6. Set the Custom Indicator Name to “Custom_TrendTrading”.
  7. Set the Custom Signal Buffer to 2.
    Custom Trend Trading and Hedging EA
  8. Click [OK] to close the dialog and [Start] to start the test.
    Input parameters for the test
  9. Click [Open Chart] to make sure trades match the indicator. Trend-Trading and Hedging EA

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!

635 total views, 8 views today

How useful was this post?

Average rating 4.8 / 5. Vote count: 6

Thanks for rating!

Would you like to share this post?

We are sorry that this post was not useful for you!

Let us improve this post!

Published by

Arthur Lopez

Private investor and speculator, software engineer and founder of PZ Trading Solutions.

6 thoughts on “How to trade Trend Trading signals with Hedging EA”

  1. 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!!

      1. 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?

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

Leave a Reply

Your email address will not be published. Required fields are marked *