OsMA (Oscillator of a Moving Average) for ThinkorSwim

BenTen

Administrative
Staff
VIP
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:

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;
OSMA.AssignValueColor(Color.WHITE);
AssignBackgroundColor(if OSMA >= 50 then Color.UPTICK else Color.DOWNTICK);

Not sure if that's what you're looking for.
 

alexR

Member
VIP
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?

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

This is another code for MT4:

Code:
#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);
}
//+------------------------------------------------------------------+

here is the chart:

 
Last edited by a moderator:

Similar threads

Top