Idea by: Vladimir Pastushak.
MQL5 Code by: Vladimir Karputov.
The VR-Moving Expert Advisor (EA) leverages the Moving Average indicator, with all the necessary trading functions neatly organized in the CMoving trading class housed in a dedicated file called Moving.mqh.
Input Parameters
- Magic Number - unique identifier for the EA;
- Lots - volume of the position (specify a volume over zero or risk size over zero);
- Risk - risk percentage of free margin per trade (again, position volume over zero or risk size over zero can be specified);
- MA: Period - Moving Average indicator timeframe;
- MA: Averaging Period - the length for the Moving Average calculation;
- MA: Horizontal Shift - horizontal displacement of the Moving Average;
- MA: Smoothing Type - method for averaging;
- MA: Type of Price - price type used for the indicator calculation;
- Take Profit - target profit level for open positions;
- Lot Multiplier for Series of Positions - increasing volume for a series of trades;
- Distance from Moving Average - set distance from the Moving Average indicator;
- Additive in Presence of General Profit - additional space if there’s overall profit.
The EA operates on a swing trading principle, offering the flexibility to increase lot sizes. Here’s the strategy breakdown:
- If the price is above the Moving Average at a specified Distance from Moving Average, we open a BUY position;
- If the price dips below the Moving Average at the same Distance from Moving Average, we open a SELL position with matching lot size;
- If the price rises from the highest BUY position beyond the Distance from Moving Average, we initiate another BUY;
- If the price drops from the lowest BUY position past the Distance from Moving Average, we initiate another SELL.
Class CMoving Overview
| Setting Parameters | |
Initialization |
Initial setup includes:
|
GetFlag |
This checks the initialization flag status, a helpful function for the EA’s internal m_init variable in class CMoving. |
LotsOrRisk |
Determining how position lots are calculated: either manually (if lots is above zero) or as a percentage of free margin per trade (if risk is above zero). |
CreateMA |
Establishes the Moving Average indicator. |
TakeProfit |
Sets the Take Profit parameters. |
Multiplier |
Defines the parameter for increasing lot size. |
DistanceMA |
Sets the distance from the Moving Average indicator. |
ProfitPlus |
Defines the additional margin for overall profit. |
| Main Function | |
Processing |
The main function where all calculations take place. |
| Class Functions (Protected Access) | |
RefreshRates |
Updates quotes for the symbol. |
CheckVolumeValue |
Validates the entered position volume (used during manual entry). |
IsFillingTypeAllowed |
Checks the order filling mode. |
iMAGet |
Fetches Moving Average values for a bar with the specified index. |
CalculateAllPositions |
Retrieves information on positions:
|
Prices |
Fetches reference prices for BUY and SELL positions, considering the set Distance from Moving Average. |
LotCheck |
Validates and normalizes position volume (applies during manual entry). |
OpenBuy |
Initiates a BUY position. |
OpenSell |
Initiates a SELL position. |
PrintResult |
Displays results of the trading operation after opening a position. |
SetTakeProfit |
Establishes the Take Profit level for positions or removes all Take Profit and Stop Loss levels (if delete_take_profit is set to "true"). |
CompareDoubles |
Compares two real numbers to check for equality. |
AveragedTakeProfit |
Calculates and sets a unified Take Profit level for a specific type of position pos_type. |
Example: Trading GBPUSD on the H1 timeframe, "Every tick based on real ticks."

Comments 0