Forex EA Example: Huck’s HLHB Trend-Catcher System

Greetings, earthlings! Now that you’ve finished my tutorial series on forex expert advisors (at least I hope you have) I think you’re ready to put those skills to the test by coding Huck’s famous HLHB Trend-Catcher System. Not to worry, I’ll guide you through this one!

Let’s follow the simple outline for a basic forex EA I shared with y’all last week:

I. Variables

Huck seems to be a pretty simple gal with a no-frills mechanical system so let’s keep things simple with the usual variables for stops, targets, lot size, and technical indicator settings. Of course these can be made as external inputs that can be customized by the user.

HLHB variables forex ea

II. Indicators

The HLHB Trend-Catcher System has the following indicators: 5 EMA, 10 EMA, and RSI (10). Checking the extensive database of MQL4 technical indicator functions shows that we’ll have to use the iMA() and iRSI () functions with the following syntax:

iMA (string symbol, int time frame, int ma_period, int ma_shift, int ma_method, int applied_price, int shift);

iRSI (string symbol, int time frame, int period, int applied_price, int shift);

And since these functions return values, we have to declare variables to store those. Note also that the mechanical system looks at upward or downward EMA crossovers to check for buy or sell signals so we’ll have to make variables for the previous and current EMA values.

Similarly, since we’re looking at an RSI cross from above or below the 50.0 mark, we’ll also have to create variables for the previous and current RSI value.

HLHB indicators forex ea

As I’ve discussed in an older entry on the basic structure of forex expert advisors, there’s usually no need to put anything in the initialization and deinitialization sections so you can skip right ahead to the main start() or OnTick() component.

III. Entry Conditions

In plain English, Huck’s entry rules state:

Buy when the 5 EMA crosses above the 10 EMA from underneath and the RSI crosses above the 50.0 mark from the bottom.

Sell when the 5 EMA crosses below the 10 EMA from the top and the RSI crosses below the 50.0 mark from the top.

Translating this to robot language requires the use of compound operators and conditional statements. Remember those?

So the conditions for a buy order are:

PreviousFastMA < PreviousSlowMA && CurrentFastMA > CurrentSlowMA 

PreviousRSI < 50.0 && CurrentRSI > 50.0

And the conditions for a sell order are:

PreviousFastMA > PreviousSlowMA && CurrentFastMA < CurrentSlowMA

PreviousRSI > 50.0 && CurrentRSI < 50.0

Now all we have to do is put these inside if-else statements before creating orders. Since there are two conditions that have to be met before entering a buy or sell order, we’ll need to make use of two nested if-else statements for each position.

IV. Order Details

Almost done! Now we add the specific order details under each condition using the OrderSend() function.

HLHB orders forex eaNow hit “Compile” to check if there are any errors or typos in your code. If there’s none…

CONGRATULATIONS!

You’ve just created a basic forex EA for the HLHB Trend-Catcher System!

As you’ve probably noticed, Huck actually has a trailing stop included in her exit conditions but I’ve just realized that I haven’t covered that topic yet. Better stay tuned for my next entries then!

 

  • Clelio

    Não consegui fazer funcionar apresenta 2 erros nas linhas de condicional para emissão da ordem de compra e venda, não sei como resolver

  • souzacg

    não consegui funcionar o program

  • souzacg

    o program apresentou 2 erros na linha de condicional de compra e outro na linha de venda

  • Clelio

    I would like your help to run the HLHB TREND when compiled presented two errors in conditional lines (if)

    Presented the following message: the expression is not compatible with the scope

    ‘}’ – expressions are not allowed on a global scope HLHB TREND.mq4 58 1

  • Pingback: Forex Mechanical System: SMA Crossover Pullback()

  • souzacg

    emissão de ordens indefinidamente, como posso resolver este problema no HLHB

    • Not really sure if my translation for this is correct but I suggest you make use of a time stop on the OrderSend() function to make sure the orders aren’t open indefinitely.

  • keithhobbs

    this dont work

    • Yikes sorry to hear that. What kind of error did you get?

  • Yeah this system is really basic and doesn’t have any conditions for cancelling orders yet. You can try putting a time stop on the OrderSend() function though.

  • Hmm, have you checked if there are extra { or } that don’t have a matching opening/closing bracket? Also, make sure that the i in ‘if’ is not capitalized.

  • abdul113

    from where exactly should i start to create this hlhb system . imean when i go to NEw > Expert advisor (Generate) > Enter the Name and link Now its asking me to select “signal properties of Expert advisor” what should i select here ..? i am following your blog from the very first now i am stuck here ..

  • abdul113

    In initialization function we have learn that int is just whole number but here you are using int for storing 0.05 for lot size. instead of double. please clear my confusion .

  • abdul113

    one another thing . in iMA it has to be 6 parameters but we are giving 7 parameters here . and compiler giving error of “Wrong Parameter count”