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.
Gỡ bỏ khỏi Script Ưa thích Thêm vào Script Ưa thích
//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)
Thanks for making this.
+1 Phản hồi
This is not an OsMA indicator. This is a colored MACD indicator. OsMA has a different code.
+1 Phản hồi
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.
Phản hồi
TraderDJ anexas
@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);
}
//+------------------------------------------------------------------+
Phản hồi
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.
Phản hồi
TraderDJ anexas
@anexas, See the attached picture. Your code is identical to MACD.

Phản hồi
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.
Phản hồi
TraderDJ anexas
@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
Phản hồi
TraderDJ anexas
@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!!
Phản hồi
Trang chủ Bộ lọc cổ phiếu Bộ lọc Forex Bộ lọc Tiền điện tử Lịch kinh tế Shows Cách thức hoạt động Tính năng Biểu đồ Giá Quy tắc Áp dụng Người điều hành Giải pháp cho Website & Nhà môi giới Widget Giải pháp biểu đồ Thư viện Biểu đồ Lightweight Trung tâm Trợ giúp Giới thiệu bạn Yêu cầu tính năng Blog & Tin tức Câu hỏi thường gặp Wiki Twitter
Hồ sơ Tùy chỉnh Hồ sơ Tài khoản và Thanh toán Giới thiệu bạn Ticket Hỗ trợ của tôi Trung tâm Trợ giúp Các ý tưởng đã đăng Người theo dõi Đang theo dõi Tin nhắn riêng Trò chuyện Đăng xuất