anexas

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
[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 = 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)
TraderDJ PRO
2 months ago
This is not an OsMA indicator. This is a colored MACD indicator. OsMA has a different code.
+1 Reply
anexas PRO TraderDJ
2 months ago
@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.
Reply
TraderDJ PRO anexas
2 months ago
@anexas,

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.
IndicatorBuffers(3);
//--- drawing settings
SetIndexStyle(0,DRAW_HISTOGRAM);
SetIndexDrawBegin(0,InpSignalSMA);
IndicatorDigits(Digits+2);
//--- 3 indicator buffers mapping
SetIndexBuffer(0,ExtOsmaBuffer);
SetIndexBuffer(1,ExtMacdBuffer);
SetIndexBuffer(2,ExtSignalBuffer);
//--- name for DataWindow and indicator subwindow label
IndicatorShortName("OsMA("+IntegerToString(InpFastEMA)+","+IntegerToString(InpSlowEMA)+","+IntegerToString(InpSignalSMA)+")");
//--- check for input parameters
if(InpFastEMA<=1 || InpSlowEMA<=1 || InpSignalSMA<=1 || InpFastEMA>=InpSlowEMA)
{
Print("Wrong input parameters");
ExtParameters=false;
return(INIT_FAILED);
}
else
ExtParameters=true;
//--- initialization done
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| 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)
return(0);
//--- last counted bar will be recounted
limit=rates_total-prev_calculated;
if(prev_calculated>0)
limit++;
//--- macd counted in the 1-st buffer
for(i=0; i<limit; i++)
ExtMacdBuffer=iMA(NULL,0,InpFastEMA,0,MODE_EMA,PRICE_CLOSE,i)-
iMA(NULL,0,InpSlowEMA,0,MODE_EMA,PRICE_CLOSE,i);
//--- signal line counted in the 2-nd buffer
SimpleMAOnBuffer(rates_total,prev_calculated,0,InpSignalSMA,ExtMacdBuffer,ExtSignalBuffer);
//--- main loop
for(i=0; i<limit; i++)
ExtOsmaBuffer=ExtMacdBuffer-ExtSignalBuffer;
//--- done
return(0);
}
//+------------------------------------------------------------------+
Reply
anexas PRO TraderDJ
2 months ago
@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.
Reply
TraderDJ PRO anexas
2 months ago
@anexas, See the attached picture. Your code is identical to MACD.

snapshot
Reply
anexas PRO TraderDJ
2 months ago
@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.
Reply
TraderDJ PRO anexas
2 months ago
@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.

https://www.mql5.com/en/charts/6256502/cadjpy-h4-forex-capital-markets
Reply
TraderDJ PRO anexas
2 months ago
@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!!
Reply
TraderDJ PRO anexas
2 months ago
@anexas, I use OSMA code I posted above all the time on Meta Trader, and it is different than MACD. Like I said, I don't know coding, but I know that indicator is different than MACD in metatrader. Your current code is identical to MACD. I will attach a picture in the next post to show you.

Here is another OSMA code from MT4. Not sure what the difference is but check this one.

//---- indicator settings
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 Green
#property indicator_color2 Red
#property indicator_color3 Red
#property indicator_color4 Green
#property indicator_width1 3
#property indicator_width2 3
#property indicator_width3 3
#property indicator_width4 3
//#property indicator_level1 0.0003
//#property indicator_level2 -0.0003
#property indicator_levelcolor White

//---- indicator buffers
extern int FastEMA=21;
extern int SlowEMA=55;
extern int SignalSMA=5;

double ind_buffer1, ind_buffer1s;
double ind_buffer2, ind_buffer2s;
double ind_buffer3;

//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- 1 additional buffer used for counting.
IndicatorBuffers(5);
//---- drawing settings
SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID);
SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID);
SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID);
SetIndexStyle(3,DRAW_HISTOGRAM,STYLE_SOLID);
IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+1);
SetIndexDrawBegin(0,34);
SetIndexDrawBegin(1,34);
SetIndexDrawBegin(3,34);
SetIndexDrawBegin(4,34);
//---- 3 indicator buffers mapping
if(!SetIndexBuffer(0,ind_buffer1) &&
!SetIndexBuffer(1,ind_buffer1s) &&
!SetIndexBuffer(2,ind_buffer2) &&
!SetIndexBuffer(3,ind_buffer2s) &&
!SetIndexBuffer(4,ind_buffer3))
Print("cannot set indicator buffers!");
//---- name for DataWindow and indicator subwindow label
IndicatorShortName("OSMA Color(" + FastEMA + "," + SlowEMA + "," + SignalSMA + ")");
return (0);
//---- initialization done
return(0);
}
//+------------------------------------------------------------------+
//| Awesome Oscillator |
//+------------------------------------------------------------------+
int start()
{
int limit;
int counted_bars=IndicatorCounted();
double prev,current;
//---- 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=iOsMA(NULL,0,FastEMA,SlowEMA,SignalSMA,PRICE_WEIGHTED,i);
//---- dispatch values between 2 buffers
bool up=true;
for(i=limit-1; i>=0; i--)
{
current=ind_buffer3;
prev=ind_buffer3;
if (((current<0)&&(prev>0))||(current<0)) up= false;
if (((current>0)&&(prev<0))||(current>0)) up= true;

if(!up)
{
if(current > prev)
{
ind_buffer2s=current;
ind_buffer2=0.0;
ind_buffer1=0.0;
ind_buffer1s=0.0;
}
else
{
ind_buffer2=current;
ind_buffer2s=0.0;
ind_buffer1=0.0;
ind_buffer1s=0.0;
}
}
else
{
if(current < prev)
{
ind_buffer1s=current;
ind_buffer1=0.0;
ind_buffer2=0.0;
ind_buffer2s=0.0;
}
else
{
ind_buffer1=current;
ind_buffer1s=0.0;
ind_buffer2=0.0;
ind_buffer2s=0.0;
}
}
}
//---- done
return(0);
}

Reply
anexas PRO TraderDJ
2 months ago
@TraderDJ, this code uses a different setting for MACD parameters. So it will produce slightly different results.
Reply
Ideas Scripts Chart
United States
United Kingdom
India
España
Italia
Brasil
Россия
Türkiye
日本
한국
Home Stock Screener Economic Calendar How It Works Chart Features House Rules Moderators For the WEB Widgets Stock Charting Library Priority Support Feature Request Blog & News FAQ Help & Wiki Twitter
Private Messages Chat Ideas Published Followers Following Priority Support Public Profile Profile Settings Account and Billing Sign Out