How do code the BWMFI with MQL5

video
play-sharp-fill

VIDEO DESCRIPTION

1. The video demonstrates how to create an Expert Advisor using MQL5 in just 3 minutes.
2. To begin, open MetaEditor by clicking its icon or pressing F4.
3. Include the trade library using #include<Trade\Trade.mqh> for accessing trading functions.
4. Create an instance of the CTrade class and name it ‘trade’.
5. Calculate Ask and Bid prices using SymbolInfoDouble(_Symbol,SYMBOL_ASK) and normalize them using NormalizeDouble.
6. Declare a string variable called ‘signal’ to hold trading signals (‘buy’ or ‘sell’).
7. Define an array BWMFIArray for holding Bill Williams Market Facilitation Index (BWMFI) values.
8. Initialize the BWMFI using iBWMFI(_Symbol,_Period,VOLUME_TICK) and store its definition in an integer variable BWMFIDefinition.
9. Sort the array from the current candle downwards using ArraySetAsSeries(BWMFIArray,true).
10. Populate the array with BWMFI values using CopyBuffer, copying 3 values starting from the current candle.
11. Calculate and normalize the current and last candle’s BWMFI values using NormalizeDouble.
12. Compare CurrentBWMFIValue and LastBWMFIValue to determine the signal (‘buy’ or ‘sell’).
13. Execute trades based on the signal: buy 0.10 microlots for a ‘buy’ signal, sell 0.10 microlots for a ‘sell’ signal using trade.Buy() and trade.Sell().
14. Press F7 to compile the code after coding is complete.
15. Test the Expert Advisor in MetaTrader by going back to MetaTrader, pressing Control and R, and starting the strategy tester.
16. The video concludes with a summary of creating an Expert Advisor using MQL5, coding it yourself, and testing it in MetaTrader.

 

TASKS

First, open MetaEditor by clicking its icon or pressing F4. Next, include the trade library using #include<Trade\Trade.mqh>. This allows us to use trading functions in our code.

Create an instance of the CTrade class and name it ‘trade’. Calculate the Ask and Bid prices using SymbolInfoDouble(_Symbol,SYMBOL_ASK) for the Ask price and normalize it using NormalizeDouble. Do the same for the Bid price. Store these values in double variables called ‘Ask’ and ‘Bid’.

Declare a string variable named ‘signal’ to hold our trading signal, either ‘buy’ or ‘sell’. Define an array ‘BWMFIArray’ to hold the Bill Williams Market Facilitation Index (BWMFI) values. Initialize BWMFI using iBWMFI(_Symbol,_Period,VOLUME_TICK) and store its definition in an integer variable called ‘BWMFIDefinition’.

Sort the array from the current candle downwards using ArraySetAsSeries(BWMFIArray,true). Populate the array with BWMFI values using CopyBuffer, copying 3 values starting from the current candle. Store these values in BWMFIArray.

Calculate the current BWMFI value and normalize it to 5 decimal places using NormalizeDouble(BWMFIArray[0],5), storing this value in ‘CurrentBWMFIValue’. Calculate the last candle’s BWMFI value, normalizing it to 5 decimal places and store it in ‘LastBWMFIValue’. Compare these values, setting the signal to ‘buy’ if CurrentBWMFIValue is greater or ‘sell’ if it’s smaller.

Execute trades based on the signal: for a ‘buy’ signal, use trade.Buy() with 0.10 microlots; for a ‘sell’ signal, use trade.Sell() with 0.10 microlots. After coding, press F7 to compile. Test your Expert Advisor in MetaTrader by going back to the platform, pressing Control and R, and starting the strategy tester.

In summary, you have learned how to create an Expert Advisor using MQL5, coded it yourself, and tested it in MetaTrader. If you are a Premium course member with an idea for a video, send us an email.

QUESTIONS

1. What is the purpose of the #include<Trade\Trade.mqh> line in the code?
The purpose of the #include<Trade\Trade.mqh> line in the code is to include the trade library, which allows us to use trading functions in our code.

2. What is the CTrade class used for?
The CTrade class is used to create an instance that will be utilized for trading purposes within the Expert Advisor.

3. How are Ask and Bid prices calculated in the code?
Ask and Bid prices are calculated by using SymbolInfoDouble(_Symbol,SYMBOL_ASK) to get the Ask price and normalizing it using NormalizeDouble. The same process is applied for the Bid price.

4. What is the purpose of the signal variable?
The signal variable holds our trading signal, either ‘buy’ or ‘sell’, based on the comparison of current and last BWMFI values.

5. How are Bill Williams Market Facilitation Index (BWMFI) values obtained and stored in the code?
BWMFI values are initialized using iBWMFI(_Symbol,_Period,VOLUME_TICK) and stored in an integer variable called BWMFIDefinition. The array BWMFIArray is used to hold these values, which are populated by CopyBuffer with 3 values starting from the current candle.

6. What is the role of NormalizeDouble function in the code?
The NormalizeDouble function is used to normalize the Ask and Bid prices as well as the calculated BWMFI values to 5 decimal places.

7. How are trades executed based on the signal variable?
Trades are executed by calling trade.Buy() for a ‘buy’ signal, which buys 0.10 microlots, and trade.Sell() for a ‘sell’ signal, which sells 0.10 microlots.

8. What is the role of F7 in the code?
Pressing F7 compiles the coded Expert Advisor.

9. How can the Expert Advisor be tested in MetaTrader?
To test the Expert Advisor, go back to MetaTrader, press Control and R, and start the strategy tester. The Expert Advisor should appear on the chart.