Oscillator Moving Average (OsMA)

This code for Oscillator of Moving Averages (OsMA) is based on MACD 4C indicator code published by vkno422. Many thanks to vkno422. I have borrowed the concept of 4 colours which I find very useful.

For those who are not familiar with OsMA, it is histogram of difference between MACD (oscillator) and its MA (signal line). The zero line cross over of this indicator is used in many strategies.

This version includes MACD & its signal line together with OsMA histogram. I have programmed flexibility for switching OFF/ON individual indicator components as well as changing the periods for various moving averages.

I am dedicating this indicator to the TV trading community hoping that people will find it useful.
Remove from Favorite Scripts Add to Favorite Scripts
//Indicator - Oscillator of Moving Averages.
//Histogram of difference between MACD (oscillator) and its MA (signal line)
//Version 2.0 Created on 14 July 2016 by - Shailesh Saxena
//code based on MACD 4C indicator code published by vkno422
study(shorttitle = "OsMA", title = "Oscillator Moving Average")

//User Inputs
fastMA = input(title="Fast MA Period", type = integer, defval = 12, minval = 3)
slowMA = input(title="Slow MA Period", type = integer, defval = 26, minval = 3)
smoothing = input(title="MACD MA Period", type = integer, defval = 9, minval = 3)
sLine = input(title="Show Signal Line", type=bool, defval=true)
MACD_visible = input(title="Show MACD", type = bool, defval = true)
OsMA_histogram = input(title="Show OsMA Histogram", type = bool, defval = true)

[MACD,signalLine,_] = macd(close[0], fastMA, slowMA, smoothing)
show_MACD = MACD_visible ? MACD : na

MACDColor = MACD > 0 
    ? MACD > MACD[1] ? lime : green 
    : MACD < MACD[1] ? red : orange
plot(show_MACD, style = line, color = MACDColor, linewidth = 1)
plot(0, title = "Zero line", linewidth = 1, color = gray)

sl = sLine ? signalLine : na
plot(sl, color = yellow, linewidth = 1)

OsMA = OsMA_histogram ? MACD - signalLine : na

OsMAColor = OsMA > 0 
    ? OsMA > OsMA[1] ? aqua : teal 
    : OsMA < OsMA[1] ? fuchsia : purple
plot(OsMA, style = histogram, color = OsMAColor, linewidth = 2)
plot(0, title = "Zero line", linewidth = 1, color = gray)
Thanks for making this.
This is not an OsMA indicator. This is a colored MACD indicator. OsMA has a different code.
+1 Reply
anexas TraderDJ
@TraderDJ, Please share the OsMA code that you mention above and I will learn from it as well. My code above is based on what I have seen on the trading platforms that I have.
Thank you for your input.

Here is the code from Metatrader 4. I don't know how to change code to work with trading view.
#property description "Moving Averages of Oscillator"
#property strict

#include <MovingAverages.mqh>

//--- indicator settings
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Silver
#property indicator_width1 2
//--- indicator parameters
input int InpFastEMA=12; // Fast EMA Period
input int InpSlowEMA=26; // Slow EMA Period
input int InpSignalSMA=9; // Signal SMA Period
//--- indicator buffers
double ExtOsmaBuffer;
double ExtMacdBuffer;
double ExtSignalBuffer;
//--- right input parameters flag
bool ExtParameters=false;
//| Custom indicator initialization function |
int OnInit(void)
//--- 2 additional buffers are used for counting.
//--- drawing settings
//--- 3 indicator buffers mapping
//--- name for DataWindow and indicator subwindow label
//--- check for input parameters
if(InpFastEMA<=1 || InpSlowEMA<=1 || InpSignalSMA<=1 || InpFastEMA>=InpSlowEMA)
Print("Wrong input parameters");
//--- initialization done
//| Moving Average of Oscillator |
int OnCalculate (const int rates_total,
const int prev_calculated,
const datetime& time,
const double& open,
const double& high,
const double& low,
const double& close,
const long& tick_volume,
const long& volume,
const int& spread)
int i,limit;
if(rates_total<=InpSignalSMA || !ExtParameters)
//--- last counted bar will be recounted
//--- macd counted in the 1-st buffer
for(i=0; i<limit; i++)
//--- signal line counted in the 2-nd buffer
//--- main loop
for(i=0; i<limit; i++)
//--- done
anexas TraderDJ
@TraderDJ, thanks for sharing. Your code is the same as mine. See the main loop in your code (at the very bottom). It is same as line 29 of my code above. Hope this clarifies.
@anexas, See the attached picture. Your code is identical to MACD.

anexas TraderDJ
@TraderDJ, The picture above explains it all. Both indicators shown in your snapshot are the same. They both contain MACD as well as OsMA. The lines are MACD and the histogram is the OsMA. In my code, I have only added colours and the flexibility to turn off parts of the indicator if you need to do so. So if you dont want to see MACD then use the format option to turn it off. Then you will see only OsMA.
@anexas, Attached is a picture of an OsMA indicator and along side the MACD histogram (with the lines removed). Both are set at 12-26-9, and you can see they are clearly different.

I am looking for an OsMA indicator to use in trading view charts, as it is one of my favorite indicators. It acts completely differently than the MACD histogram does (as shown in the chart below). A colored MACD histogram is not the "Oscillator of Moving Average" indicator.
@anexas, My apologies....I have mis-understood what you wrote. You have the OsMA and MACD wrapped into one indicator. What usually shows as a MACD histogram is shown as a line, and the OsMA histogram is shown instead.

Thank you for your time and patience with me. Like I said, I don't understand code and I was unaware that both were in the same indicator.

And I like the color coding you did on it...very nice work!!
EN English
EN English (UK)
EN English (IN)
DE Deutsch
FR Français
ES Español
IT Italiano
PL Polski
SV Svenska
TR Türkçe
RU Русский
PT Português
ID Bahasa Indonesia
MS Bahasa Melayu
TH ภาษาไทย
VI Tiếng Việt
JA 日本語
KO 한국어
ZH 简体中文
ZH 繁體中文
AR العربية
Home Stock Screener Forex Signal Finder Cryptocurrency Signal Finder Economic Calendar How It Works Chart Features House Rules Moderators Website & Broker Solutions Widgets Stock Charting Library Feature Request Blog & News FAQ Help & Wiki Twitter
Profile Profile Settings Account and Billing My Support Tickets Contact Support Ideas Published Followers Following Private Messages Chat Sign Out