Viewing the resource: Understanding and Implementing the "1-ONE MA SYSTEM"

Understanding and Implementing the "1-ONE MA SYSTEM"

Allan Munene Mutiiria 2025-06-03 21:38:35 137 Views
The "1-ONE MA SYSTEM" is a beginner-friendly, trend-following trading strategy coded in MQL5 for the...

Introduction

Welcome to the "1-ONE MA SYSTEM", a simple yet powerful trading strategy built for MetaTrader 5 using MQL5! If you’re new to trading, this article is for you. We’ll explain everything in detail: what the strategy is, how it works, and how the code brings it to life. The "1-ONE MA SYSTEM" uses a "21-period Simple Moving Average" (SMA) to help you decide when to buy or sell. Think of the "SMA" as a guide that smooths out price wiggles to show you the market’s direction. This is perfect for beginners or intermediate traders who want to start with a clear, trend-based approach.

Understanding the Strategy

For a new trader, understanding a strategy means knowing why and how we make decisions. In the "1-ONE MA SYSTEM", our goal is to follow the trend—when prices are going up, we buy; when they’re going down, we sell. We use the "21-period SMA", which takes the closing prices of the last 21 candles and averages them. If this "SMA" line is sloping up (each new value higher than the last), it suggests an uptrend. If it’s sloping down, it suggests a downtrend. To make sure we’re right, we also check the price: for a buy, the previous candle’s close must be above the "SMA"; for a sell, the current candle’s close must be below it. This double-check helps us avoid false signals in choppy markets. Note that this code doesn’t tell us when to exit trades, so you might later add rules for taking profits or cutting losses. See below.

Implementation Code

Now, let’s dive into the MQL5 code for the "1-ONE MA SYSTEM". MQL5 is the language for MetaTrader 5, and this code is an Expert Advisor (EA), a program that runs automatically. We’ll break it into pieces, explain each part like you’re seeing it for the first time, and flow from one section to the next to build a clear picture.

int totalBars = 0;
//+------------------------------------------------------------------+
//|                                              1-ONE MA SYSTEM.mq5 |
//|                                  Copyright 2023, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

To start, we define a variable called "totalBars" and set it to 0. In trading, a bar is like a candle—a snapshot of price over time (e.g., 1 hour, 1 day). We use "totalBars" to keep track of how many bars we’ve seen, so we only act when a new one forms. Then, we have metadata with "#property" lines. These tell MetaTrader 5 about our script: "copyright" says who made it (MetaQuotes Ltd., 2023), "link" points to their website, and "version" marks this as version 1.00. This is like labeling a book so you know its author and edition.

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  }

Next, we set up two key functions. The "OnInit" function runs once when you load the EA onto a chart in MetaTrader 5. It’s like turning on a machine—getting it ready to work. Here, it’s simple: we just return "INIT_SUCCEEDED", a code that tells MetaTrader 5 everything started okay. Then, the "OnDeinit" function runs when you remove the EA from the chart. It’s like shutting down the machine. The "reason" parameter tells us why it’s stopping (e.g., chart closed), but we don’t need to do anything here, so it’s empty for now.

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   int bars = iBars(_Symbol,PERIOD_CURRENT);
   if (totalBars == bars) return;
   totalBars = bars;
   
   double Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   double Bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);

Now, we move to the "OnTick" function, the heart of our EA. A “tick” is a tiny price update, and this function runs every time the price changes. First, we use the "iBars" function to count the number of bars (candles) on the current chart for the symbol (e.g., EURUSD) and timeframe (e.g., 1-hour). The "_Symbol" is a built-in value for the current pair, and "PERIOD_CURRENT" means the timeframe you’ve chosen. We check if "totalBars" equals "bars"—if they’re the same, no new candle has formed, so we stop with "return". If a new candle exists, we update "totalBars" to the new count. Then, we grab prices: "Ask" is the price to buy, and "Bid" is the price to sell. We get these with "SymbolInfoDouble", using "_Symbol" and constants like "SYMBOL_ASK". The "NormalizeDouble" function adjusts these prices to the right number of decimals (e.g., 5 for EURUSD) using "_Digits", another built-in value.

double maArray[];
   MqlRates priceRates[];
   
   int handleMA = iMA(_Symbol,_Period,21,0,MODE_SMA,PRICE_CLOSE);
   ArraySetAsSeries(maArray,true);
   ArraySetAsSeries(priceRates,true);
   if (!CopyBuffer(handleMA,0,0,3,maArray))return;
   if (!CopyRates(_Symbol,PERIOD_CURRENT,0,3,priceRates))return;

Continuing in "OnTick", we set up our data. We create "maArray", an array (a list) to hold "SMA" values, and "priceRates", an array for price details. Arrays are like baskets to store numbers. We define "handleMA" using the "iMA" function, which creates a handle (a reference) for our "21-period SMA". Its inputs are: "_Symbol" (the pair), "_Period" (the timeframe), 21 (periods), 0 (no shift), "MODE_SMA" (simple moving average type), and "PRICE_CLOSE" (use closing prices). Next, we use "ArraySetAsSeries" to sort both arrays so the newest data is first (index 0 is the latest). Then, we fill these arrays: "CopyBuffer" grabs the last 3 "SMA" values from "handleMA" into "maArray", and "CopyRates" gets the last 3 bars’ data (open, high, low, close, etc.) for "_Symbol" and "PERIOD_CURRENT" into "priceRates". If either fails, we stop with "return" to avoid errors.

if ( (maArray[0] > maArray[1] && maArray[1] > maArray[2]) ){
      if (priceRates[1].close > maArray[1]){
         Comment("BUY");
         Print("buy");
      }
   }

Flowing forward, we check for a buy signal. We look at "maArray" to see if the "SMA" is rising: "maArray[0]" (current "SMA") must be greater than "maArray[1]" (previous "SMA"), and "maArray[1]" must be greater than "maArray[2]" (two bars ago). This confirms an uptrend over three bars. Next, we check "priceRates[1].close", the closing price of the previous candle, to see if it’s above "maArray[1]", the previous "SMA". If both are true, we have a buy signal! We use the "Comment" function to show “BUY” on the chart (top-left corner) so you can see it, and the "Print" function logs “buy” to the terminal’s log, a record you can check later.

if ( (maArray[0] < maArray[1] && maArray[1] < maArray[2]) ){
      if (priceRates[0].close < maArray[1]){
         Comment("SELL");
         Print("sell");
      }
   }
   else {Comment(" ");}
  }
//+------------------------------------------------------------------+

To finish, we check for a sell signal. We examine "maArray" to confirm a downtrend: "maArray[0]" (current "SMA") must be less than "maArray[1]", and "maArray[1]" less than "maArray[2]". This shows the "SMA" is falling over three bars. Then, we test if "priceRates[0].close", the current candle’s closing price, is below "maArray[1]", the previous "SMA". If both conditions hold, we signal a sell! The "Comment" function displays “SELL” on the chart, and "Print" logs “sell” to the terminal. If no buy or sell signal happens, we use "Comment" to show a blank space, clearing any old signal from the chart. The final line closes our code block cleanly.

Conclusion

The "1-ONE MA SYSTEM" is a beginner-friendly way to trade trends on MetaTrader 5. We’ve defined variables like "totalBars", set up functions like "OnInit" and "OnTick", gathered price and "SMA" data, and built buy and sell signals step by step. This MQL5 code is a foundation you can test and grow with.

Disclaimer: This strategy is for educational purposes only. Trading carries risks—prices can move fast and you could lose money. Always test on a demo account first to see how it works before trying it live.

Happy trading!

Disclaimer: The ideas and strategies presented in this resource are solely those of the author and are intended for informational and educational purposes only. They do not constitute financial advice, and past performance is not indicative of future results. All materials, including but not limited to text, images, files, and any downloadable content, are protected by copyright and intellectual property laws and are the exclusive property of Forex Algo-Trader or its licensors. Reproduction, distribution, modification, or commercial use of these materials without prior written consent from Forex Algo-Trader is strictly prohibited and may result in legal action. Users are advised to exercise extreme caution, perform thorough independent research, and consult with qualified financial professionals before implementing any trading strategies or decisions based on this resource, as trading in financial markets involves significant risk of loss.

Recent Comments

Go to discussion to Comment or View other Comments

No comments yet. Be the first to comment!