OsMA_In_Color
Download scriptThis specialized trading tool is designed to provide a visual representation of the OsMA (Moving Average of Oscillator), which is also commonly recognized in the trading world as the MACD Histogram. The primary goal of this script is to transform complex mathematical calculations into an easy-to-read visual format that helps traders identify momentum and trend shifts at a glance.
How the Logic Works
The tool operates by processing price data through three distinct layers of calculation:
-
The Core Oscillator (MACD): First, the script calculates two different types of "Moving Averages" based on the closing prices of the market. It subtracts a slower-moving average from a faster-moving average. The result tells us how much the price is deviating from its recent trend.
-
The Signal Line: The script then takes that "Oscillator" value and performs an additional calculation—a Simple Moving Average of the oscillator itself. This creates a "Signal Line," which acts as a smoothed reference point to help filter out minor market noise.
-
The Visual Difference (The Histogram): Finally, the tool looks at the difference between the Core Oscillator and the Signal Line. Instead of just showing two lines on your screen, this script converts that difference into a colored histogram:
- Green Bars: These appear when the oscillator is above the signal line, generally suggesting bullish momentum.
- Red Bars: These appear when the oscillator falls below the signal line, generally suggesting bearish momentum.
Key Benefits for the Trader
By using this script, you are able to see the "health" of a trend more clearly than with standard lines alone. Because the bars change color based on whether the momentum is moving upward or downward relative to the signal, it allows for a quicker visual assessment of market sentiment. The tool handles all the heavy lifting—the subtraction of averages and the smoothing of data—leaving you with a clean, color-coded visual of market dynamics in a separate window on your trading platform.
/*-----------------------------+
| |
| Shared by www.Aptrafx.com |
| |
+------------------------------*/
//+------------------------------------------------------------------+
//| OsMA_In_Color.mq4 |
//| Copyright © 2006, Robert Hill. |
//| |
//| Draws histogram in color for OsMA aka MACD Histogram |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Robert Hill"
//---- indicator settings
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Green
#property indicator_color2 Red
#property indicator_width1 3
#property indicator_width2 3
//---- indicator parameters
extern int FastEMA=12;
extern int SlowEMA=26;
extern int SignalSMA=9;
//---- indicator buffers
double ind_buffer1[];
double ind_buffer2[];
double ind_buffer3[];
double ind_buffer4[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- 2 additional buffers are used for counting.
IndicatorBuffers(4);
//---- drawing settings
SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID);
SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID);
SetIndexDrawBegin(0,SignalSMA);
IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+2);
//---- 3 indicator buffers mapping
if(!SetIndexBuffer(0,ind_buffer1) &&
!SetIndexBuffer(1,ind_buffer2) &&
!SetIndexBuffer(2,ind_buffer3) &&
!SetIndexBuffer(3,ind_buffer4))
Print("cannot set indicator buffers!");
//---- name for DataWindow and indicator subwindow label
IndicatorShortName("OsMA("+FastEMA+","+SlowEMA+","+SignalSMA+")");
//---- initialization done
return(0);
}
//+------------------------------------------------------------------+
//| Moving Average of Oscillator |
//+------------------------------------------------------------------+
int start()
{
int limit;
double temp;
int counted_bars=IndicatorCounted();
//---- check for possible errors
if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
//---- macd counted in the 1-st additional buffer
for(int i=0; i<limit; i++)
ind_buffer3[i]=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);
//---- signal line counted in the 2-nd additional buffer
for(i=0; i<limit; i++)
ind_buffer4[i]=iMAOnArray(ind_buffer3,Bars,SignalSMA,0,MODE_SMA,i);
//---- main loop
for(i=0; i<limit; i++)
{
ind_buffer1[i]=0;
ind_buffer2[i] = 0;
temp=ind_buffer3[i]-ind_buffer4[i];
if (temp >=0)
ind_buffer1[i] = temp;
else
ind_buffer2[i] = temp;
}
//---- done
return(0);
}
//+------------------------------------------------------------------+
Comments