OsMA THV
Download scriptThis technical indicator is designed to help traders visualize and identify momentum shifts in the financial markets by analyzing the relationship between different moving averages. It combines three distinct mathematical layers to provide a comprehensive view of price trends.
Core Logical Components
1. The Foundation (MACD Calculation) The script begins by calculating the Moving Average Convergence Divergence (MACD). This is achieved by measuring the difference between two different types of moving averages:
- A "Fast" average (which reacts quickly to recent price changes).
- A "Slow" average (which tracks the broader trend over a longer period). By subtracting the slow average from the fast one, the tool identifies whether the current price momentum is accelerating or decelerating.
2. The Signal Layer To smooth out the data and provide more reliable directions, the script then takes that MACD value and calculates a Signal Line. This acts as an "average of an average," helping to filter out minor market noise and highlighting significant trend shifts.
3. The Final Oscillator (OsMA) The final result—the Oscillator of Moving Averages (OsMA)—is created by subtracting the Signal Line from the MACD. This produces a value that oscillates around a "Zero Line."
- When the value is positive, it suggests upward momentum.
- When the value is negative, it suggests downward momentum.
Visual Presentation and Alerts
The script translates these complex calculations into two primary visual elements on your chart:
- Dynamic Histogram: The indicator renders a series of vertical bars. These bars change color based on whether the current momentum is higher or lower than the previous period. This allows for a quick "at-a-glance" assessment of whether the trend is gaining strength or beginning to weaken.
- Real-Time Notifications: The logic includes an automated monitoring system that watches for two specific types of events:
- Histogram Crosses: It detects when the histogram changes color, signifying a change in momentum direction.
- Zero-Line Crosses: It detects when the OsMA crosses from negative to positive (or vice versa), signaling a potential major trend shift.
Depending on your preferences, the script can be configured to notify you of these specific events immediately via audible alerts, visual pop-ups on the screen, or automated emails sent directly to your inbox. This ensures you are alerted to significant market movements without having to watch the charts constantly.
//+------------------------------------------------------------------+
//| OsMA.mq4 |
//| Copyright © 2004, MetaQuotes Software Corp. |
//| Modified by Cobraforex for THV System, www.cobraforex.com |
//| Copyright © 2006, Robert Hill |
//| Copyright © 2008, Linuxser |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Robert Hill"
#property copyright "Copyright © 2008, Linuxser and Forex-TSD"
#property link "http://www.metaquotes.net/"
//---- indicator settings
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 LimeGreen
#property indicator_color2 FireBrick
//---- indicator parameters
extern bool SoundON=false;
extern bool EmailON=false;
extern bool HistogramAlarm=false;
extern bool ZeroLineAlarm=false;
extern int FastEMA=12;
extern int SlowEMA=26;
extern int SignalSMA=7;
//---- indicator buffers
double OsmaBuffer[];
double MacdBuffer[];
double SignalBuffer[];
double HistogramBufferUp[];
double HistogramBufferDown[];
bool HistAboveZero = false;
bool HistBelowZero = false;
bool MACDAboveZero = false;
bool MACDBelowZero = false;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- 2 additional buffers are used for counting.
IndicatorBuffers(5);
//---- drawing settings
SetIndexDrawBegin(0,SignalSMA);
SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID);
SetIndexBuffer(0,HistogramBufferUp);
SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID);
SetIndexBuffer(1,HistogramBufferDown);
IndicatorDigits(Digits+2);
//---- 3 indicator buffers mapping
SetIndexBuffer(2,OsmaBuffer);
SetIndexBuffer(3,MacdBuffer);
SetIndexBuffer(4,SignalBuffer);
SetIndexBuffer(0,HistogramBufferUp);
SetIndexBuffer(1,HistogramBufferDown);
//---- name for DataWindow and indicator subwindow label
IndicatorShortName("OsMA("+FastEMA+","+SlowEMA+","+SignalSMA+")");
//---- initialization done
return(0);
}
//+------------------------------------------------------------------+
//| Moving Average of Oscillator |
//+------------------------------------------------------------------+
int start()
{
double current, prev;
int limit;
int counted_bars=IndicatorCounted();
//---- 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++)
MacdBuffer[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++)
SignalBuffer[i]=iMAOnArray(MacdBuffer,Bars,SignalSMA,0,MODE_SMA,i);
//---- main loop
for(i=0; i<limit; i++)
OsmaBuffer[i]=MacdBuffer[i]-SignalBuffer[i];
for(i=0; i<limit; i++)
{
HistogramBufferUp[i] = 0;
HistogramBufferDown[i] = 0;
current = MacdBuffer[i] - SignalBuffer[i];
prev = MacdBuffer[i+1] - SignalBuffer[i+1];
if (current > prev)
{
HistogramBufferUp[i] = current;
HistogramBufferDown[i] = 0.0;
}
else
{
HistogramBufferDown[i] = current;
HistogramBufferUp[i] = 0.0;
}
if (MACDAboveZero) Comment ("\nThe trend has changed to UP");
if (MACDBelowZero) Comment ("\nThe trend has changed to DOWN");
if (i == 1)
{
// Check for Histogram Change Color
if (HistogramAlarm==true)
{
if (HistogramBufferUp[i] > HistogramBufferDown[i + 1])
{
// Cross up
if (HistAboveZero == false)
{
HistAboveZero=true;
HistBelowZero=false;
if (SoundON) Alert("OSMA is Positive","\n Time=",TimeToStr(CurTime(),TIME_DATE)," ",TimeHour(CurTime()),":",TimeMinute(CurTime()),"\n Symbol=",Symbol()," Period=",Period());
if (EmailON) SendMail("OSMA is Positive", "MACD Crossed up, Date="+TimeToStr(CurTime(),TIME_DATE)+" "+TimeHour(CurTime())+":"+TimeMinute(CurTime())+" Symbol="+Symbol()+" Period="+Period());
}
}
else if (HistogramBufferDown[i] < HistogramBufferUp[i + 1] > 0)
{
// Cross down
if (HistBelowZero == false)
{
HistBelowZero=true;
HistAboveZero=false;
if (SoundON) Alert("OSMA is Negative","\n Date=",TimeToStr(CurTime(),TIME_DATE)," ",TimeHour(CurTime()),":",TimeMinute(CurTime()),"\n Symbol=",Symbol()," Period=",Period());
if (EmailON) SendMail("OSMA is Negative","MACD Crossed Down, Date="+TimeToStr(CurTime(),TIME_DATE)+" "+TimeHour(CurTime())+":"+TimeMinute(CurTime())+" Symbol="+Symbol()+" Period="+Period());
}
}
}
// Check for MACD Signal line crossing 0 line
if (ZeroLineAlarm==true)
{
if (OsmaBuffer[i] > 0 && OsmaBuffer[i + 1] < 0)
{
// Cross up
if (MACDAboveZero == false)
{
MACDAboveZero=true;
MACDBelowZero=false;
if (SoundON) Alert("Histogram is Above Zero Line","\n Time=",TimeToStr(CurTime(),TIME_DATE)," ",TimeHour(CurTime()),":",TimeMinute(CurTime()),"\n Symbol=",Symbol()," Period=",Period());
if (EmailON) SendMail("Histogram is Above Zero Line", "OSMA Crossed up, Date="+TimeToStr(CurTime(),TIME_DATE)+" "+TimeHour(CurTime())+":"+TimeMinute(CurTime())+" Symbol="+Symbol()+" Period="+Period());
}
}
else if (OsmaBuffer[i] < 0 && OsmaBuffer[i + 1] > 0)
{
// Cross down
if (MACDBelowZero == false)
{
MACDBelowZero=true;
MACDAboveZero=false;
if (SoundON) Alert("Histogram is Below Zero Line","\n Date=",TimeToStr(CurTime(),TIME_DATE)," ",TimeHour(CurTime()),":",TimeMinute(CurTime()),"\n Symbol=",Symbol()," Period=",Period());
if (EmailON) SendMail("Histogram is Below Zero Line","OSMA Crossed Down, Date="+TimeToStr(CurTime(),TIME_DATE)+" "+TimeHour(CurTime())+":"+TimeMinute(CurTime())+" Symbol="+Symbol()+" Period="+Period());
}
}
}
}
}
//---- done
return(0);
}
Comments