Combine Studies (Indicators) In ThinkOrSwim

MerryDay

Administrative
Staff member
Staff
VIP
Lifetime
How To Combine Studies (Indicators) For ThinkOrSwim
Members combine studies to create comprehensive strategies to be able to test for multiple conditions.
If the combined study is of ToS built-in indicators and it is only needed for upper chart calculations.
It is as simple as referencing the studies:
Ruby:
def RSI = reference RSI() ;
def Vroc= reference VolumeRateOfChange();

When combining lower chart studies with the purpose of creating a combined plot:
Combining and plotting lower chart studies can be problematic. While they may look good when you overlay them in your chart. Lower chart oscillators cannot be plotted together in the same script unless they show the same SCALE on the vertical axis. While it is possible to "normalize" indicators to the same scale, this has shown to sometimes have the habit of skewing the data.

Below are the simple steps to combine as many studies as you want for the purpose of manipulating the data (not attempting to plot the data on a lower chart).
It should be noted that studies should still be used separately in the Scan Hacker, where possible. The scanner is temperamental about studies that get too complex.

Bad News: It is an excruciatingly boring detailed process so unless the studies being combined are small and simple or another member is interested enough in the results to do it for you, the requests to combine indicators tend to not be fulfilled on this forum.
Good News: Combining Indicators is a relatively easy procedure. So feel free to do this yourself! (y)
- - - - - - - - - - - - - - - - - - - - - -
Before Combining The Studies:
Review the code in each study.
Every statement in each of the studies that you want to smoosh together must have a unique identifying field names.

For example: if
StudyA has
input length =13 ;
plot ema21 = ExpAverage(close, length);
and

StudyB has
input length= 30;
def VolDayAvg = (fold index = 1 to length+ 1 with Avg = 0 do (Avg + volume(period = "DAY")[index])) / length;

The Combined Study will vomit an error when you paste them both together. Because they are both using the same named field: length .
Duplicate names are not allowed.

- - - - - - - - - - - - - - - - - - - - - -
  1. THEREFORE, EVERY NAME TO THE LEFT OF AN EQUAL SIGN MUST BE UNIQUE.
  2. Once you create a unique name THAT NEW NAME must be substituted throughout the code.
StudyA becomes:
input lengthA =13 ;
plot ema21 = ExpAverage(close, lengthA );

StudyB has
input lengthB= 30;
def VolDayAvg = (fold index = 1 to lengthB+ 1 with Avg = 0 do (Avg + volume(period = "DAY")[index])) / lengthB;

That is all there is too it. Rinse and repeat for every duplicate name.
Then copy and paste your indicators into a new combined study.

Here are the additional common errors that will need to be cleaned up:
Error: Can only have one declare statement. Fix: delete any extra declare statements​
Most Common Error: Identifier Already Used or Already assigned This means there STILL duplicate names. The error message will tell you what the duplicate name is. Go back to square one and rename the duplicates.
 
Last edited:

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

MerryDay

Administrative
Staff member
Staff
VIP
Lifetime
@MerryDay Do you know weather or not a combined study can be red by a scanner or is it better separated in that case ?
Members generally combine studies to create strategies to backtest or to get that elusive "buy me now" arrow on a chart.

However, it is generally accepted practice to scan studies separately, where possible. The scanner is temperamental about studies that get too complex.

Great question, BTW. I will add this information to the original post.
 

AnimalMother

New member
Hello traders, I have a request for anyone who would like to help me combine the OBV RSI with the Slow RSI MA. The final code would be a RSI calculated using the OBV, the slow RSI smoothing and MAs. These indicators work really well using tick charts.
@diazlaz @BenTen @MerryDay @barbaros @horserider @FateOwnzYou @wtf_dude @tomsk @Pensar and any other dude who wants to contribute. It would be much appreciated. Thank you.

Disclaimer: I do not claim ownership of any of these codes and these codes can be found on this website. All rights and praise go to their respective developers.

https://ibb.co/PgB3Z3G

Here are the codes.

Code:
#
# 2019.11.24 1.0 - Original Port
#

declare lower;

input length = 14;
input over_Bought = 70;
input over_Sold = 30;
input src = close;
input averageType = AverageType.WILDERS;
input showBreakoutSignals = no;

#net volume of positive and negative volume
def obv = if (src - src[1]) > 0 then volume else
if (src - src[1]) < 0 then -volume else 0 * volume;
def cnv = TotalSum(obv); #cumulative net volume
def price = cnv;

def NetChgAvg = MovingAverage(averageType, price - price[1], length);
def TotChgAvg = MovingAverage(averageType, AbsValue(price - price[1]), length);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;

plot RSI = 50 * (ChgRatio + 1);
plot OverSold = over_Sold;
plot OverBought = over_Bought;
plot UpSignal = if RSI crosses above OverSold then OverSold else Double.NaN;
plot DownSignal = if RSI crosses below OverBought then OverBought else Double.NaN;

plot mid = 50;
mid.SetPaintingStrategy(paintingStrategy = PaintingStrategy.DASHES);
mid.AssignValueColor(COLOR.magenta);

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

RSI.DefineColor("OverBought", GetColor(5));
RSI.DefineColor("Normal", GetColor(7));
RSI.DefineColor("OverSold", GetColor(1));
RSI.AssignValueColor(if RSI > over_Bought then RSI.color("OverBought") else
if RSI < over_Sold then RSI.color("OverSold") else RSI.color("Normal"));

OverSold.SetDefaultColor(GetColor(8));
OverBought.SetDefaultColor(GetColor(8));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

#AddCloud(over_Bought, Over_Sold, Color.Light_Gray, Color.Light_Gray);

#END OF OBV RSI

=#+#+#+#+#+##+#+#+#+#+#+#+#+#+#+#+#+#+#+#+#+#+#+#+#+#+#+#+#+#+#+#+#+#+#+#
Code:
#SlowRSIMA
declare lower;

#Intended to use on 600 tick chart of /MES
input emaLength = 5;
input rsiLength = 10;
input over_bought = 80;
input over_sold = 20;

#breakout signals
input averageType = AverageType.WILDERS;
input showBreakoutSignals = no;

def ema = ExpAverage(close, emaLength);
def netChgAvg = WildersAverage(close - ema, rsiLength);
def totChgAvg = WildersAverage(AbsValue(close - ema), rsiLength);
def chgRatio = if totChgAvg != 0 then netChgAvg / totChgAvg else 0;

plot SlowRSI = 50 * (chgRatio + 1);
plot OverBought = over_bought;
plot MiddleLine = 50;
plot OverSold = over_sold;

#plot breakout signals

#Moving Averages
input MALength = 8;
input AverageType1 = AverageType.SIMPLE;
input MALength2 = 26;
input AverageType2 = AverageType.SIMPLE;
input MALength3 = 57;
input AverageType3 = AverageType.SIMPLE;
input MALength4 = 180;
input AverageType4 = AverageType.SIMPLE;

# plot the Moving Averages
def MA = MovingAverage(AverageType1, SlowRSI, MALength);
plot pMA = MA;
def MA2 = MovingAverage(AverageType2, SlowRSI, MALength2);
plot pMA2 = MA2;
def MA3 = MovingAverage (AverageType3, SlowRSI, MALength3);
plot pMA3 = MA3;
def MA4 = MovingAverage(AverageType4, SlowRSI, MALength4);
plot pMA4 = MA4;

plot UpSignal = if PMA3 crosses above MIDDLELINE then MIDDLELINE else Double.NaN;

SlowRSI.SetDefaultColor(GetColor(8));
OverBought.SetDefaultColor(GetColor(5));
MiddleLine.SetDefaultColor(GetColor(5));
MiddleLine.SetStyle(Curve.LONG_DASH);
OverSold.SetDefaultColor(GetColor(5));

UpSignal.SetHiding(!showBreakoutSignals);

SlowRSI.DefineColor("OverBought", GetColor(5));
SlowRSI.DefineColor("Normal", GetColor(7));
SlowRSI.DefineColor("OverSold", GetColor(1));
SlowRSI.AssignValueColor(if SlowRSI > over_bought then SlowRSI.Color("OverBought") else if SlowRSI < over_sold then SlowRSI.Color("OverSold") else SlowRSI.Color("Normal"));
OverSold.SetDefaultColor(GetColor(8));
OverBought.SetDefaultColor(GetColor(8));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
 

MerryDay

Administrative
Staff member
Staff
VIP
Lifetime
Hello traders, I have a request for anyone who would like to help me combine the OBV RSI with the Slow RSI MA. The final code would be a RSI calculated using the OBV, the slow RSI smoothing and MAs. These indicators work really well using tick charts.
This thread was created so non-programmers / non-thinkscripters could combine their own studies. Give it a try, and come back with any problems that you run into.
 

WallStScope

New member
Hello evemryone, newbie here. I've been trying to combine the AccumDisTrVol study with the MovingAveExp to create a new study to trigger an up arrow in the chart when the former crosses the latter. Anyone has any ideas how I could make it work? They both use the Close constant so I'm not sure if that's the problem here. I can plot them separately fine on the chart but when I combine them, one or the other goes way off chart or totally flat. Thank you!
 

MerryDay

Administrative
Staff member
Staff
VIP
Lifetime
Hello evemryone, newbie here. I've been trying to combine the AccumDisTrVol study with the MovingAveExp to create a new study to trigger an up arrow in the chart when the former crosses the latter. Anyone has any ideas how I could make it work? They both use the Close constant so I'm not sure if that's the problem here. I can plot them separately fine on the chart but when I combine them, one or the other goes way off chart or totally flat. Thank you!
you can take the moving average of a indicator but you can't plop an MA on a lower chart. They don't scale together.
Code:
# ########################################################
plot Indicator        = reference AccumDistPrVol() ;
plot Indicator_ma     = MovingAverage(AverageType.SIMPLE, Indicator, 30);
# ########################################################
 

champa911

New member
ty, @MerryDay.....my question is, is it POSSIBLE to write a STRATEGY that uses multiple studies(indicators) that will issue AddOrder buys and sells independently of each other, so that your net position is the net long number (longs-shorts) or net short number......for instance, if 4 of the 5 indicators reflect a long position and one indicator is a short position, the net long position would be 3 long (4 longs minus 1 short).....just want to know if this is possible and if so, I will go to work on it or have someone work on it. Thanks.
 
Last edited by a moderator:

MerryDay

Administrative
Staff member
Staff
VIP
Lifetime
ty, @MerryDay.....my question is, is it POSSIBLE to write a STRATEGY that uses multiple studies(indicators) that will issue AddOrder buys and sells independently of each other, so that your net position is the net long number (longs-shorts) or net short number......for instance, if 4 of the 5 indicators reflect a long position and one indicator is a short position, the net long position would be 3 long (4 longs minus 1 short).....just want to know if this is possible and if so, I will go to work on it or have someone work on it. Thanks.
Yes it is possible
 

PAYtience

New member
VIP
Hi @MerryDay is it possible for me to combine 2 studies with signals where they don't have to trigger at the same exact time? I want them to have +-2 bar window in order form them to alert. Is doesn't have to be on the same exact bar...
 

BenTen

Administrative
Staff member
Staff
VIP
Lifetime
Hi @MerryDay is it possible for me to combine 2 studies with signals where they don't have to trigger at the same exact time? I want them to have +-2 bar window in order form them to alert. Is doesn't have to be on the same exact bar...

Possible, but it still depends. You need to create a separate request with both scripts and describe what needs to be done.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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