TradingView
anexas
15 Jul 2016 pukul 02.29

Oscillator Moving Average (OsMA) 

Tesla, Inc.NASDAQ

Huraian

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.
Komen
TraderDJ
This is not an OsMA indicator. This is a colored MACD indicator. OsMA has a different code.
anexas
@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.
TraderDJ
@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);
}
//+------------------------------------------------------------------+
anexas
@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.
TraderDJ
@anexas, See the attached picture. Your code is identical to MACD.

anexas
@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.
TraderDJ
@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.

mql5.com/en/charts/6256502/cadjpy-h4-forex-capital-markets
TraderDJ
@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!!
TraderDJ
@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[i+1];
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);
}

Lebih