MACD OBV into one study

dmaffo

Member
VIP
Ok here we go with new one for me. I am running out of real estate on my screen so I put MACD and ON Balance Vol in single cell and works great until I have to draw on it... Once you combine 2 indicators TOS shuts downt he drawing tooks for that cell. So i cant draw my divergences etc on it...
So when I combined both in single script they not working... lol
I figure it has something to do with letting it know I want MACD (VALUE and AVG) and ON BAL (OBVM and Signal) to arrow signal as I select on menu.
Any help be great!
Otherwise I keep squished on the monitor as 2 separate indicators. Thank you!!!

#ON Balance and MACD CROSS Dio

#MACD PART TOS Default
#want to use as single indicator so I can use drawing tools on it.

declare lower;

input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;
input showBreakoutSignals = no;

plot Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
plot Avg = MovingAverage(averageType, Value, MACDLength);

plot Diff = Value - Avg;
plot ZeroLine = 0;

plot UpSignal = if Diff crosses above ZeroLine then ZeroLine else Double.NaN;
plot DownSignal = if Diff crosses below ZeroLine then ZeroLine else Double.NaN;

UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);

Value.SetDefaultColor(GetColor(1));
Avg.SetDefaultColor(GetColor(8));
Diff.SetDefaultColor(GetColor(5));
Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Diff.SetLineWeight(3);
Diff.DefineColor("Positive and Up", Color.GREEN);
Diff.DefineColor("Positive and Down", Color.DARK_GREEN);
Diff.DefineColor("Negative and Down", Color.RED);
Diff.DefineColor("Negative and Up", Color.DARK_RED);
Diff.AssignValueColor(if Diff >= 0 then if Diff > Diff[1] then Diff.Color("Positive and Up") else Diff.Color("Positive and Down") else if Diff < Diff[1] then Diff.Color("Negative and Down") else Diff.Color("Negative and Up"));
ZeroLine.SetDefaultColor(GetColor(0));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

#
# this is ON BALANCE modded already
#try to figure out how to to cross both and select which lines to display between both maybe?


def OBVM1 = MovingAverage(averageType, obv, length);
def Signal1 = MovingAverage(averageType, OBVM, signalLength);
def UpSignal1 = OBVM crosses above Signal;
def DwnSignal = OBVM crosses below Signal;

# Arrows

plot bullish1 = if UpSignal then OBVM else Double.NaN ;
plot bearis1 = if DwnSignal then OBVM else Double.NaN ;

##### End Code #####
 
Last edited by a moderator:
Solution
As per your request here is a screen shot of both combined. The issue is simply that I am not able to using drawing tools on the chart so I cannot draw trendlines, or text on the screen when I combine 2 or more indicators in the studies screen. I would like to be able to draw on the chart, as I use confluence and reversals through divergence to detect but is impossible currently when MACD and OnBalance are sharing same space. YES I could use them separately but I simply prefer both in same space is all. In the picture below you will see macd and OBV and next picture only the MACD.
I just want to be able to use both like people use multiple moving averages in single indicator so they can still draw without the an issue...
Or, can someone share with me how I can draw when 2 indicators occupy single space. My TOS doesnt allow me to draw lines is all. Appreciate any help,
 

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

As per your request here is a screen shot of both combined. The issue is simply that I am not able to using drawing tools on the chart so I cannot draw trendlines, or text on the screen when I combine 2 or more indicators in the studies screen. I would like to be able to draw on the chart, as I use confluence and reversals through divergence to detect but is impossible currently when MACD and OnBalance are sharing same space. YES I could use them separately but I simply prefer both in same space is all. In the picture below you will see macd and OBV and next picture only the MACD.
I just want to be able to use both like people use multiple moving averages in single indicator so they can still draw without the an issue.
lVxJO8L.jpg


kcBRqNk.jpg
 
OBV

declare lower;

input length = 7;
input signalLength = 10;
input averageType = AverageType.EXPONENTIAL;

def obv = reference OnBalanceVolume();

plot OBVM = MovingAverage(averageType, obv, length);
plot Signal = MovingAverage(averageType, OBVM, signalLength);

OBVM.SetDefaultColor(GetColor(8));
Signal.SetDefaultColor(GetColor(2));


def OBVM1 = MovingAverage(averageType, obv, length);
def Signal1 = MovingAverage(averageType, OBVM, signalLength);
def UpSignal = OBVM crosses above SIGNAL;
def DwnSignal = OBVM crosses below SIGNAL;

# Arrows

plot bullish1 = if UpSignal then OBVM else double.Nan ;
plot bearis1 = if DwnSignal then OBVM else double.Nan ;

##### End Code #####

AND

macd IS
#
# TD Ameritrade IP Company, Inc. (c) 2007-2021
#

declare lower;

input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;
input showBreakoutSignals = no;

plot Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
plot Avg = MovingAverage(averageType, Value, MACDLength);

plot Diff = Value - Avg;
plot ZeroLine = 0;

plot UpSignal = if Diff crosses above ZeroLine then ZeroLine else Double.NaN;
plot DownSignal = if Diff crosses below ZeroLine then ZeroLine else Double.NaN;

UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);

Value.SetDefaultColor(GetColor(1));
Avg.SetDefaultColor(GetColor(8));
Diff.SetDefaultColor(GetColor(5));
Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Diff.SetLineWeight(3);
Diff.DefineColor("Positive and Up", Color.GREEN);
Diff.DefineColor("Positive and Down", Color.DARK_GREEN);
Diff.DefineColor("Negative and Down", Color.RED);
Diff.DefineColor("Negative and Up", Color.DARK_RED);
Diff.AssignValueColor(if Diff >= 0 then if Diff > Diff[1] then Diff.color("Positive and Up") else Diff.color("Positive and Down") else if Diff < Diff[1] then Diff.color("Negative and Down") else Diff.color("Negative and Up"));
ZeroLine.SetDefaultColor(GetColor(0));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
 
As per your request here is a screen shot of both combined. The issue is simply that I am not able to using drawing tools on the chart so I cannot draw trendlines, or text on the screen when I combine 2 or more indicators in the studies screen. I would like to be able to draw on the chart, as I use confluence and reversals through divergence to detect but is impossible currently when MACD and OnBalance are sharing same space. YES I could use them separately but I simply prefer both in same space is all. In the picture below you will see macd and OBV and next picture only the MACD.
I just want to be able to use both like people use multiple moving averages in single indicator so they can still draw without the an issue.
lVxJO8L.jpg


kcBRqNk.jpg

This is an attempt to normalize the OBVM to the MACD. I drew a random trendline on the code below to show that you can see the ability to do so.

In the picture below, there are the individual indicators and the combined/normalized indicator with the red trendline manually drawn.

Capture.jpg
Ruby:
declare lower;

#MACD----------------------------------------------------------
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;
input showBreakoutSignals = no;

plot Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
plot Avg   = MovingAverage(averageType, Value, MACDLength);

plot Diff     = Value - Avg;
plot ZeroLine = 0;

plot UpSignal   = if Diff crosses above ZeroLine then ZeroLine else Double.NaN;
plot DownSignal = if Diff crosses below ZeroLine then ZeroLine else Double.NaN;

UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);

Value.SetDefaultColor(GetColor(1));
Avg.SetDefaultColor(GetColor(8));
Diff.SetDefaultColor(GetColor(5));
Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Diff.SetLineWeight(3);
Diff.DefineColor("Positive and Up", Color.GREEN);
Diff.DefineColor("Positive and Down", Color.DARK_GREEN);
Diff.DefineColor("Negative and Down", Color.RED);
Diff.DefineColor("Negative and Up", Color.DARK_RED);
Diff.AssignValueColor(if Diff >= 0 then if Diff > Diff[1] then Diff.Color("Positive and Up") else Diff.Color("Positive and Down") else if Diff < Diff[1] then Diff.Color("Negative and Down") else Diff.Color("Negative and Up"));
ZeroLine.SetDefaultColor(GetColor(0));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
#OBV Normalized------------------------------------------------
script normalizePlot {
    input data      = hl2;
    input newRngMin = 0;
    input newRngMax = 100;
    def hhData = HighestAll( data );
    def llData = LowestAll( data );
    plot nr    = ((( newRngMax - newRngMin ) * ( data - llData )) /
                    ( hhData - llData )) + newRngMin;
}

def h = HighestAll(Diff);
def l = LowestAll(Diff);

input length       = 7;
input signalLength = 10;
input averageType_obv = AverageType.EXPONENTIAL;

def obv = reference OnBalanceVolume();

def OBVM_   = MovingAverage(averageType_obv, obv, length);
def Signal_ = MovingAverage(averageType_obv, OBVM_, signalLength);

plot OBVM   = normalizePlot(OBVM_, l, h);
OBVM.SetDefaultColor(GetColor(8));
OBVM.SetPaintingStrategy(PaintingStrategy.LINE);

plot Signal = normalizePlot(Signal_, l, h);
Signal.SetDefaultColor(Color.WHITE);
Signal.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
 
Solution
Wanted to start by saying thank you. I looked at code and I am ever so impressed always by what you guys do here. That said the OBV seems to be off a bit. I cant figure out why as the agg is set to day, and the 7 10 exponential is same on both, so not sure why they are off? The drawing tools work amazing now so that part you solved perfectly, the MACD is on point. This OBV is the culprit at the moment. lol Here is a picture of what I mean by it being a bit off.

DejZSGI.jpg
 
Last edited by a moderator:
Here is the code I have for the on balance volume.

#ON BALANCE VOLUME ONLY

declare lower;

input length = 7;
input signalLength = 10;
input averageType = AverageType.EXPONENTIAL;

def obv = reference OnBalanceVolume();

plot OBVM = MovingAverage(averageType, obv, length);
plot Signal = MovingAverage(averageType, OBVM, signalLength);

OBVM.SetDefaultColor(GetColor(8));
Signal.SetDefaultColor(GetColor(2));


def OBVM1 = MovingAverage(averageType, obv, length);
def Signal1 = MovingAverage(averageType, OBVM, signalLength);
def UpSignal = OBVM crosses above SIGNAL;
def DwnSignal = OBVM crosses below SIGNAL;

# Arrows

plot bullish1 = if UpSignal then OBVM else double.Nan ;
plot bearis1 = if DwnSignal then OBVM else double.Nan ;

##### End Code #####
 
Here I got it to cross where it needs to unfortunately doesnt paint correctly and the MACD doesnt plot now . I copied my OBV code and inserted and at the bottom the SIGNAL terms I started adding L and 1's to things but never got it to work right. Spent all night messing around. Hate buggin as I try to figure things out on my own but now its 7am, and I am roasted. Cant figure it out. NEed help...
THanks
DrhME6s.jpg

declare lower;

#MACD----------------------------------------------------------
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;
input showBreakoutSignals = no;

plot Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
plot Avg = MovingAverage(averageType, Value, MACDLength);

plot Diff = Value - Avg;
plot ZeroLine = 0;

plot UpSignal = if Diff crosses above ZeroLine then ZeroLine else Double.NaN;
plot DownSignal = if Diff crosses below ZeroLine then ZeroLine else Double.NaN;

UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);

Value.SetDefaultColor(GetColor(1));
Avg.SetDefaultColor(GetColor(8));
Diff.SetDefaultColor(GetColor(5));
Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Diff.SetLineWeight(3);
Diff.DefineColor("Positive and Up", Color.GREEN);
Diff.DefineColor("Positive and Down", Color.DARK_GREEN);
Diff.DefineColor("Negative and Down", Color.RED);
Diff.DefineColor("Negative and Up", Color.DARK_RED);
Diff.AssignValueColor(if Diff >= 0 then if Diff > Diff[1] then Diff.Color("Positive and Up") else Diff.Color("Positive and Down") else if Diff < Diff[1] then Diff.Color("Negative and Down") else Diff.Color("Negative and Up"));
ZeroLine.SetDefaultColor(GetColor(0));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
#OBV Normalized------------------------------------------------
script normalizePlot {
input data = hl2;
input newRngMin = 0;
input newRngMax = 100;
def hhData = HighestAll( data );
def llData = LowestAll( data );
plot nr = ((( newRngMax - newRngMin ) * ( data - llData )) /
( hhData - llData )) + newRngMin;
}

def h = HighestAll(Diff);
def l = LowestAll(Diff);


input length = 7;
input signalLength = 10;
input averageType1 = AverageType.EXPONENTIAL;

def obv = reference OnBalanceVolume();

plot OBVM = MovingAverage(averageType, obv, length);
plot Signal = MovingAverage(averageType, OBVM, signalLength);

OBVM.SetDefaultColor(GetColor(8));
Signal.SetDefaultColor(GetColor(2));


def OBVM1 = MovingAverage(averageType, obv, length);
def Signal1 = MovingAverage(averageType, OBVM, signalLength);
def UpSignal1 = OBVM crosses above SIGNAL;
def DwnSignal = OBVM crosses below SIGNAL;


# Arrows

plot bullish1 = if UpSignal then OBVM else double.Nan ;
plot bearis1 = if DwnSignal then OBVM else double.Nan ;

#plot Signal1 = normalizePlot(Signal_, l, h);
Signal.SetDefaultColor(Color.WHITE);
Signal.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
 
Last edited by a moderator:
Here is the code I have for the on balance volume.

#ON BALANCE VOLUME ONLY

declare lower;

input length = 7;
input signalLength = 10;
input averageType = AverageType.EXPONENTIAL;

def obv = reference OnBalanceVolume();

plot OBVM = MovingAverage(averageType, obv, length);
plot Signal = MovingAverage(averageType, OBVM, signalLength);

OBVM.SetDefaultColor(GetColor(8));
Signal.SetDefaultColor(GetColor(2));


def OBVM1 = MovingAverage(averageType, obv, length);
def Signal1 = MovingAverage(averageType, OBVM, signalLength);
def UpSignal = OBVM crosses above SIGNAL;
def DwnSignal = OBVM crosses below SIGNAL;

# Arrows

plot bullish1 = if UpSignal then OBVM else double.Nan ;
plot bearis1 = if DwnSignal then OBVM else double.Nan ;

##### End Code #####
That is the only normalization process that I have seen in TOS. Regrettably it is not perfect, it is just a proportional representation similar to the built-in TOS normalization, but you can draw on it. The MACD worked because there were no changes to that code. The OBV was the indicator normalized to it. If it is done the opposite way, the OBV will work, but the MACD will not be an exact match.
 
Rats...lol... Again this is just a convenience thing that I really wanted to work as combining them both in single space allows for more on my screen. I do appreciate the help.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
407 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.
Back
Top