simply_ma_wrong_145

Author: MetaQuotes
simply_ma_wrong_145

This script is a specialized technical tool designed to visualize price trends on a trading chart by calculating a "Simple Moving Average" (SMA). It provides traders with a smoother representation of price action, filtering out minor market fluctuations to highlight the broader direction of the market.

Core Functionality

The primary purpose of this tool is to take a specific number of previous closing prices and calculate their average. By doing this, it creates a continuous line on your chart that represents the "mean" price over a set timeframe. This helps in identifying whether the current market sentiment is bullish (upward), bearish (downward), or sideways.

How the Logic Works

The script follows a straightforward three-step process to generate its visual data:

  1. Data Collection: The tool looks at the "Closing Price" of each candle on your chart. The user can define exactly how many previous candles the tool should look back at (the PeriodMA). For example, if set to 21, it looks at the last 21 candles.
  2. Mathematical Averaging: For every point on the timeline, the script adds up all the closing prices within that specified period and then divides that total by the number of periods. This creates a single average value for that specific moment in time.
  3. Visual Mapping: Once the average is calculated, the script places a dot on the chart at that value. As it moves backward through the history of the market, it connects these dots to form a smooth red line.

Key Features

  • Customizable Sensitivity: Through the input settings, users can adjust the period. A shorter period makes the line react more quickly to sudden price changes, while a longer period creates a smoother line that ignores "noise" and focuses on long-term trends.
  • Real-Time Calculation: The script automatically updates as new candles form, ensuring that the average is always calculated based on the most recent available market data.
  • Historical Analysis: Because the logic processes all available history, the tool allows traders to see how the moving average behaved in the past, helping them recognize patterns before making future decisions.

In summary, this script transforms raw, jagged price movements into a clean, readable trend line by mathematically smoothing out the data based on a user-defined timeframe.

Miscellaneous
Implements a curve of type %1
15 Views
0 Downloads
0 Favorites
simply_ma_wrong_145
//+------------------------------------------------------------------+
//|                                              Simply MA wrong.mq4 |
//|                                                       MetaQuotes |
//|                 http://forum.alpari-idc.ru/viewtopic.php?t=48186 |
//+------------------------------------------------------------------+
#property copyright "MetaQuotes"
#property link      "http://forum.alpari-idc.ru/viewtopic.php?t=48186"
//----
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red
//---- input parameters
extern int       PeriodMA=21;
extern int       ShiftMA=0;
//---- buffers
double ExtMapBuffer1[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtMapBuffer1);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
   int limit;   // îãðàíè÷èòåëü ðàñ÷åòîâ
   double val;  // âðåìåííàÿ ïåðåìåííàÿ, äëÿ âû÷èñëåíèÿ çíà÷åíèé èíäèêàòîðà
//----
   if (counted_bars==0)   limit=Bars-PeriodMA; // ïîñ÷èòàííûõ áàðîâ åùå íåò, áóäåò ñ÷èòàòü ñ áàðà,
   // ïåðåä êîòîðûì åñòü õîòÿ áû PeriodMA-1 áàðîâ. 
   if (counted_bars>0)   limit=Bars-counted_bars-PeriodMA; // âû÷òåì èç ÷èñëà äîñòóïíûõ áàðîâ êîëè÷åñòâî 
   //ïîñ÷èòàííûõ áàðîâ
   Print("Bars=",Bars,"   limit=",limit,"  counted_bars=",counted_bars);
//----
   int i=limit;
   while(i>=0)
     {
      /*
      Íåîáõîäèìûå âû÷èñëåíèÿ val
      */
      val=0; // íå çàáóäåì êàæäûé ðàç îáíóëÿòü ñëóæåáíóþ
      // ïåðåìåííóþ ïåðåä ïðîõîäîì â öèêëå
      for(int k=0;k<PeriodMA;k++)
        {
         val=val+Close[i+k];
        }
      val=val/PeriodMA;       // ñóììà ïîñëå öèêëà åñòü, óñðåäíÿåì åå   
      ExtMapBuffer1[i]=val;  // óñòàíîâèì ýíà÷åíèå 
      //ñðåäíåé íà áàðå ñ èíäåêñîì i
      i--;
     }
//----
   return(0);
  }
//+------------------------------------------------------------------+

Comments