• Get $40 off VIP by signing up for a free account! Sign Up

OsMA (Oscillator of a Moving Average) for ThinkorSwim

Request =
After using your code with my watch list all day I realized that I needed the following:
I think the OSMA that I want to use is based on MACD. This is the OSMA code that I am using (below):
The OSMA's zero line in fact is at 50 and not 0.
-> I would like to have the signals (in my custom column) based on the OSMA crossing the 50 level line with the following nuances:
a- Going Down: a red cell when OSMA crosses above 50 (<50), dark red when it is still below 50 but going up
b- Going Up: a green cell when OSMA crosses below 50 (>50), dark green when it is above 50 but going down.

EMA 13 Crosses EMA 21 By R. Houser
HighestAll and LowestAll are not valid functions for a custom column so I changed them to Highest(OSMAc,52) and Lowest(OSMAc,52), 52 being twice the slow period, and I simplified the script a bit.

This was shared in the thinkScript lounge. Here's the code:

declare lower;
def MACD_Calc = expAverage(close, 12)-expAverage(close, 26);
def SIGNAL = average(MACD_Calc, 9);
def OSMAc = MACD_Calc - SIGNAL;
def OSMAh = Highest(OSMAc,52);
def OSMAl = Lowest(OSMAc,52);
plot OSMA = (OSMAc-OSMAl)/(OSMAh-OSMAl)*100;
AssignBackgroundColor(if OSMA >= 50 then Color.UPTICK else Color.DOWNTICK);

Not sure if that's what you're looking for.
Thank you ben, but I don't think this is the right code. I will post the MT4 code and maybe you be able to converted?

//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)

This is another code for MT4:

#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

here is the chart:

Last edited by a moderator:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
674 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.