Overview
This script is designed as a specialized informational dashboard for the MetaTrader trading platform. Rather than drawing complex lines or shapes across your price chart, its primary purpose is to extract specific mathematical data and display it as a clear, organized text overlay on your screen. It provides real-time calculations regarding moving averages and their relationship to current market prices across various timeframes.
Core Logic and Functionality
1. Customizable Configuration The script allows users to define several key parameters that dictate how the data is calculated:
- Moving Average Type: Users can choose between different calculation methods (such as Simple, Exponential, Smooth, Linear Weighted, or LSMA).
- Timeframe Selection: The logic can monitor specific timeframes (e.g., 1-minute, 1-hour, Daily, Weekly) regardless of what chart you currently have open. If a user selects a timeframe smaller than the current chart, the script automatically adjusts to match the chart's period to ensure data integrity.
- Price Source: The calculations can be based on different price points, such as the opening price, high, low, or closing price of a candle.
2. Dynamic Data Processing The script performs two primary types of calculations simultaneously:
- Moving Average Tracking: It identifies the current value of the moving average and compares it to the previous value. This helps determine the immediate direction of the trend.
- Price Deviation Analysis: It calculates the distance between a fast-moving average (the most recent price action) and the user-defined moving average. This helps traders visualize how far the current price has deviated from the established trend.
3. Visual Feedback System To make the data easier to read at a glance, the script uses a color-coding system:
- Trend Direction: The script compares the current moving average value against its previous value. If it is rising, the text appears in Green; if it is falling, it appears in Crimson.
- Price Relationship: It evaluates whether the price deviation is positive or negative relative to the moving average. Depending on this result, the corresponding text will switch between Green and Crimson, providing an immediate visual cue of market strength.
4. Automated User Interface The script automatically manages the "Labels" (the text boxes) on the screen. It handles:
- Automatic Precision: It detects the specific currency pair you are trading (e.g., Gold vs. Euro/Dollar) and adjusts the decimal places of the numbers shown so they are always accurate to that specific market.
- Real-Time Updates: The dashboard refreshes constantly, updating the numerical values for the Moving Average, the Timeframe being monitored, and the Price Deviation automatically as new market data arrives.
Summary for the User
Essentially, this script acts as a "Live Dashboard." It takes complex background calculations—monitoring trends across different timeframes and measuring price deviations—and simplifies them into a clean row of text on your screen. It tells you what the average price is, which way it is moving (via color), and how far the current price has stretched away from that average.
//+------------------------------------------------------------------+
//| #OTCFX_MA_INFO
//| Copyright © 2008, "OTCFX"
//|
//|
//| Credit to ogeima for the Symbol switch and some other ideas.
//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net/"
//---- indicator settings
#property indicator_separate_window
//---- indicator parameters
extern int MAPeriod = 200;
extern int MAType=0;
extern int MAPrice=0;
extern int TimeFrame = 0;
int nDigits; int FastEMA=1;
string strMAType;
int MAMode;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//ObjectCreate("OT1", OBJ_LABEL, WindowFind("OTFX"), 0, 0);
switch (MAType)
{
case 1: strMAType="EMA"; MAMode=MODE_EMA; break;
case 2: strMAType="SMMA"; MAMode=MODE_SMMA; break;
case 3: strMAType="LWMA"; MAMode=MODE_LWMA; break;
case 4: strMAType="LSMA"; break;
default: strMAType="SMA"; MAMode=MODE_SMA; break;
}
// ObjectSet("OT1", OBJPROP_XDISTANCE, 33);
// ObjectSet("OT1", OBJPROP_YDISTANCE, 2);
// ObjectCreate("OT2", OBJ_LABEL, WindowFind("OTFX"), 0, 0);
// ObjectSetText("OT2",StringSubstr("~MA>PRICE CALC~",0),12, "Verdana", CornflowerBlue);
// ObjectSet("OT2", OBJPROP_CORNER, 0);
// ObjectSet("OT2", OBJPROP_XDISTANCE, 70);
// ObjectSet("OT2", OBJPROP_YDISTANCE, 2);
IndicatorShortName("OTFX");
if(Symbol()=="GBPJPY" || Symbol()=="EURJPY" || Symbol()=="USDJPY" || Symbol()=="GOLD" || Symbol()=="USDMXN") nDigits = 2;
if(Symbol()=="GBPUSD" || Symbol()=="EURUSD" || Symbol()=="NZDUSD" || Symbol()=="USDCHF" ||
Symbol()=="USDCAD" || Symbol()=="AUDUSD" || Symbol()=="EURUSD" || Symbol()=="EURCHF" || Symbol()=="EURGBP"
|| Symbol()=="EURCAD" || Symbol()=="EURAUD" || Symbol()=="AUDNZD")nDigits = 4;
switch(TimeFrame)
{
case 1 : string TimeFrameStr="M1" ; break; //"MAMethod: 0=SMA,1=EMA,2=SMMA,3=LWMA";
case 5 : TimeFrameStr= "M5" ; break;
case 15 : TimeFrameStr= "M15"; break; // "MAPrice: 0=C,1=O,2=H,3=L,4=M,5=T,6=W";
case 30 : TimeFrameStr= "M30"; break;
case 60 : TimeFrameStr= "H1" ; break;
case 240 : TimeFrameStr= "H4" ; break;
case 1440 : TimeFrameStr= "D1" ; break;
case 10080 : TimeFrameStr= "W1" ; break;
case 43200 : TimeFrameStr= "MN1"; break;
default : TimeFrameStr= "CurrTF";
}
if (TimeFrame<Period()) TimeFrame=Period();
//---- initialization done
return(0);
}
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence |
//+------------------------------------------------------------------+
int start()
{
int limit;string Text="";
int i=0;
string T= TimeFrame;
string MA= MAPeriod; string MT= strMAType;
double MA1=iMA(Symbol(),TimeFrame,FastEMA,0,MODE_EMA,PRICE_CLOSE,0)-iMA(Symbol(), TimeFrame,MAPeriod,0,MAType,MAPrice,0)-Point ;
double MA3 =iMA(Symbol(), TimeFrame,MAPeriod,0,MAType,MAPrice,0);
double MA31 =iMA(Symbol(), TimeFrame,MAPeriod,0,MAType,MAPrice,1);
color ColorCOV ;
if( MA3 > MA31 ) { ColorCOV = Green ; } else { ColorCOV = Crimson ; }
color ColorCOR ;
if( MA1 >=0) { ColorCOR = Green ; } else { ColorCOR = Crimson ; }
Comment(Text);
string P=Period();
ObjectCreate("OT1", OBJ_LABEL, WindowFind("OTFX"), 0, 0);
ObjectSetText("OT1",StringSubstr((T),0),11, "Verdana", CornflowerBlue);
ObjectSet("OT1", OBJPROP_CORNER, 0);
ObjectSet("OT1", OBJPROP_XDISTANCE, 33);
ObjectSet("OT1", OBJPROP_YDISTANCE, 2);
ObjectCreate("OT2", OBJ_LABEL, WindowFind("OTFX"), 0, 0);
ObjectSetText("OT2",StringSubstr("~MA>PRICE CALC~",0),12, "Verdana", CornflowerBlue);
ObjectSet("OT2", OBJPROP_CORNER, 0);
ObjectSet("OT2", OBJPROP_XDISTANCE, 70);
ObjectSet("OT2", OBJPROP_YDISTANCE, 2);
ObjectCreate("OT3", OBJ_LABEL, WindowFind("OTFX"), 0, 0);
ObjectSetText("OT3",DoubleToStr( MA3,Digits), 12, "Arial Bold",ColorCOV);
ObjectSet("OT3", OBJPROP_CORNER, 0);
ObjectSet("OT3", OBJPROP_XDISTANCE, 250);
ObjectSet("OT3", OBJPROP_YDISTANCE,20 );
ObjectCreate("OT4", OBJ_LABEL, WindowFind("OTFX"), 0, 0);
ObjectSetText("OT4","MA=", 11, "Arial ", LightSteelBlue);
ObjectSet("OT4", OBJPROP_CORNER, 0);
ObjectSet("OT4", OBJPROP_XDISTANCE, 210);
ObjectSet("OT4", OBJPROP_YDISTANCE, 20);
ObjectCreate("OT5", OBJ_LABEL, WindowFind("OTFX"), 0, 0);
ObjectSetText("OT5",DoubleToStr( MA1,Digits), 12, "Arial Bold",ColorCOR);
ObjectSet("OT5", OBJPROP_CORNER, 0);
ObjectSet("OT5", OBJPROP_XDISTANCE, 380);
ObjectSet("OT5", OBJPROP_YDISTANCE,20 );
ObjectCreate("OT6", OBJ_LABEL, WindowFind("OTFX"), 0, 0);
ObjectSetText("OT6","MA>CL+/-", 9, "Arial ", LightSteelBlue);
ObjectSet("OT6", OBJPROP_CORNER, 0);
ObjectSet("OT6", OBJPROP_XDISTANCE, 310);
ObjectSet("OT6", OBJPROP_YDISTANCE, 20);
ObjectCreate("OT7", OBJ_LABEL, WindowFind("OTFX"), 0, 0);
ObjectSetText("OT7",StringSubstr((MA),0),11, "Verdana", SteelBlue);
ObjectSet("OT7", OBJPROP_CORNER, 0);
ObjectSet("OT7", OBJPROP_XDISTANCE, 135);
ObjectSet("OT7", OBJPROP_YDISTANCE, 20);
ObjectCreate("OT8", OBJ_LABEL, WindowFind("OTFX"), 0, 0);
ObjectSetText("OT8",StringSubstr((MT),0),10, "Verdana", SteelBlue);
ObjectSet("OT8", OBJPROP_CORNER, 0);
ObjectSet("OT8", OBJPROP_XDISTANCE, 165);
ObjectSet("OT8", OBJPROP_YDISTANCE, 20);
return(0);
}
Comments