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