Author: Copyright � 2009, MetaQuotes Software Corp.
LineMA

This tool is designed to provide a visual representation of momentum and trend shifts on a trading chart by identifying specific interactions between two moving averages. Instead of just showing smooth lines that follow price, this script focuses on highlighting the precise moments when those lines cross each other.

Core Logic and Functionality

Moving Average Monitoring The script continuously tracks two separate Simple Moving Averages (SMA). By using different time periods for each average, the tool can distinguish between short-term price fluctuations and long-term trend directions. It looks at the current values, the previous values, and the values from two steps back to understand the "velocity" of the price movement.

Crossover Detection The primary logic is built to detect a "cross." The script identifies a specific sequence of events:

  1. It looks for a moment where the moving averages switch positions relative to one another.
  2. It verifies that this change is consistent, ensuring that a simple overlap isn't mistaken for a significant trend shift.
  3. When a definitive crossover occurs, the script triggers an action to draw a visual marker on your chart.

Visual Trend Mapping Once a crossover is detected, the script automatically draws a trend line connecting the two points of the cross. These lines serve as visual anchors:

  • Directional Coloring: The tool automatically assigns colors based on the direction of the move (typically using Red for downward momentum and Lime for upward momentum). This allows traders to see the "flow" of the market at a glance.
  • Dynamic Updates: As new price data comes in, the script is capable of extending these lines to keep them current, ensuring that the visual representation stays synced with the live market movement.
  • Clean Visualization: The lines are configured to appear in the background with a specific thickness, making them easy to see without cluttering the primary price action on your screen.

Summary for the User

In simple terms, this script acts as an automated "trend highlighter." It watches the interaction between two different moving averages and draws a colored line every time they cross paths. This helps you quickly identify where a trend might be changing direction without having to manually monitor the overlapping lines yourself.

Indicators Used
Moving average indicator
5 Views
0 Downloads
0 Favorites
LineMA
//+------------------------------------------------------------------+
//|                                                       LineMA.mq4 |
//|                      Copyright © 2009, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_chart_window

extern string t1="Ïåðèîä ïåðâîé ÌÀ:";
extern int PerMA1=13;
extern string t2="Ïåðèîä âòîðîé ÌÀ:";
extern int PerMA2=26;
extern string t3="Òîëùèíà ëèíèé:";
extern int tol=20;
double MA1;
double MA2;
double MA1old;
double MA2old;
double MA1oo;
double MA2oo;
double li;
string no;
int nomer;
bool line=false;
color cls=Red;
color clb=Lime;


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
 while(nomer>0)
 {
 ObjectDelete(PerMA1+PerMA2+"lineMA"+nomer);
 nomer--;
 }  
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int i, 
       Counted_bars=IndicatorCounted();
   i=Bars-Counted_bars-1;
//----
      while(i>=0)                      // Öèêë ïî íåïîñ÷èòàííûì áàðàì     
      {
       MA1=iMA(Symbol(),0,PerMA1,0,MODE_SMA,PRICE_CLOSE,i);
       MA2=iMA(Symbol(),0,PerMA2,0,MODE_SMA,PRICE_CLOSE,i);  
       MA1old=iMA(Symbol(),0,PerMA1,0,MODE_SMA,PRICE_CLOSE,i+1);
       MA2old=iMA(Symbol(),0,PerMA2,0,MODE_SMA,PRICE_CLOSE,i+1);  
       MA1oo=iMA(Symbol(),0,PerMA1,0,MODE_SMA,PRICE_CLOSE,i+2);
       MA2oo=iMA(Symbol(),0,PerMA2,0,MODE_SMA,PRICE_CLOSE,i+2);  
       
       //--------------1--------------
       if (MA1oo>MA2oo && MA2old>MA1old && MA2>MA1) {line=true;CreateOb(Time[i+1],((MA1old+MA2old)/2),Time[i],((MA1+MA2)/2));}
       if (MA1oo<MA2oo && MA2old<MA1old && MA2<MA1) {line=true;CreateOb(Time[i+1],((MA1old+MA2old)/2),Time[i],((MA1+MA2)/2));}
       
       //--------------2--------------
       if (MA1old>MA2old && MA2>MA1) {line=false;}
       if (MA1old<MA2old && MA2<MA1) {line=false;}
       
       //--------------3--------------
       if (line==true) {SetOb(li,Time[i],((MA1+MA2)/2));}
       
       i--;                          // Ðàñ÷¸ò èíäåêñà ñëåäóþùåãî áàðà     
      }
//----
   return(0);
  }
//+------------------------------------------------------------------+

void CreateOb(datetime t1,double k1,datetime t2, double k2)
   {
   nomer=nomer+1;
   no=PerMA1+PerMA2+"lineMA"+nomer;
   ObjectCreate(no, OBJ_TREND, 0, t1, k1, t2, k2);
   ObjectSet(no, OBJPROP_STYLE, STYLE_SOLID);
   li=k1;
   if (k1>k2) {ObjectSet(no, OBJPROP_COLOR, cls);}
   if (k1<k2) {ObjectSet(no, OBJPROP_COLOR, clb);}
   ObjectSet(no, OBJPROP_BACK, True);
   ObjectSet(no, OBJPROP_RAY,false);
   ObjectSet(no, OBJPROP_WIDTH,tol);
   }

void SetOb(double k1, datetime t2,double k2)
   {
   ObjectSet(no,OBJPROP_TIME2,t2);
   ObjectSet(no,OBJPROP_PRICE2,k2);
   if (k1>k2) {ObjectSet(no, OBJPROP_COLOR, cls);}
   if (k1<k2) {ObjectSet(no, OBJPROP_COLOR, clb);}
   }

Comments