thinkScript Reference Other Data or Studies


I'm building my own indicator but I need to reference other custom studies in thinkscript. What would be the function for that? I know it is super easy to define an alert based on condition of 2 or more indicators.

For example, Alert is true if:
  • indicatorA is true AND
  • indicatorB > x AND
  • indicatorC < y
But instead of notification from alerts, I would like to write and indicator/study that will visually place a mark (an arrow for example) on chart while the conditions of my 3 indicators are met. I try writing it but it seems that TOS does not recognized my custom indicators when I want to "call" them. Any idea how to fix it?

Thank you
You will have to integrate all three different indicators into one, assuming they play nicely with each other. You can't call for data from separate indicators by referencing them unless prebuilt and provided by ThinkorSwim.

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

@rad14733 , @BenTen , thanks. I think Ben undertood what I need. I am trying to call studies that I have custom developed and it seems that is not possible in TOS. I can only called the studies that are prebuilt by TOS. ex: I can call stochastic, EMA...without problem, because they are provided by TOS.
Got it... I was thinking I had called my own custom studies but I see now that I haven't and receive a "No such Function" error when I try... I should have known because they won't work in Conditional Orders either, which should have been a clue... Doh...!!! o_O
Trying to plot a line based on the number of stocks in a watchlist. Is this possible. Is there a way to reference an watchlist instead of entering every stock individually? Thank you

Internal Script Reference: DateAdjuster

# Internal Script Reference: DateAdjuster
script DateAdjuster {
            Input meetingYYYYMMDD = 20190101;
            def adj4Weekly = meetingYYYYMMDD - getdayOfWeek(meetingYYYYMMDD) + 1;
            def adj4Monthly = meetingYYYYMMDD - getdayofMonth(meetingYYYYMMDD) + 1;
            def result = if getAggregationPeriod() == aggregationPeriod.MONTH then adj4Monthly
                         else if getAggregationPeriod() == aggregationPeriod.WEEK then adj4Weekly
                         else  meetingYYYYMMDD;
            plot aggAdjDate = result;
    } # Script will need to be adjusted for Holidays (JQ)

# Internal Script Reference: DateAdjuster
Script Prior {
        input of = close;
        def priorOf = if of != of[1] then of[1] else priorOf[1];
        plot  prior = priorOf;

# Internal Script Reference: RegressionBetweenBars
Script RegressionBetweenBars {
        input     y = close;
        input start = 10;
        input   end = 10;

        def na = Double.NaN;
        def bn = BarNumber();
        def currentBar = HighestAll(if !IsNaN(y) then bn else na);
        def   startBar = HighestAll(start);
        def     endBar = if startBar > HighestAll(end)
                     then currentBar
                     else HighestAll(end);
    def n = endBar - startBar;
    def x = CompoundValue(1,
            if Between(bn, startBar, endBar)
            then x[1] + 1
            else x[1], 0);
    def  Ex = fold ix  = 0 to n with px  do px  + GetValue(x, ix);
    def  Ey = fold iy  = 0 to n with py  do py  + GetValue(y, iy);
    def Exy = fold ixy = 0 to n with pxy do pxy + GetValue(x * y, ixy);
    def Ex2 = fold ix2 = 0 to n with px2 do px2 + GetValue(x * x, ix2);
    def    b = (n * Exy - (Ex * Ey)) / (n * Ex2 - (Ex * Ex));
    def getB = if Between(bn, startBar, endBar)
               then GetValue(b, bn - endBar)
               else na;
    def a = (GetValue(Ey, bn - endBar) - getB *
             GetValue(Ex, bn - endBar)) / n;

    LinearRegression =  a + getB * x;

input debugBubbles = { default "hide", display};

def o = open;
def x = BarNumber();
        # plot xDebug = x; xDebug.setPaintingStrategy(paintingStrategy.VALUES_ABOVE); xDebug.setdefaultColor(color.LIGHT_GRAY);
        addchartbubble(1, lowestall(low),x, color.lime,no);
def hX = highestAll(if !IsNaN(o) then x else Double.NaN);
        #addchartbubble(1, lowestall(low),hX, color.lime,no);
def nan = Double.NaN;

def beginDateDefaultSeason1 = 20191015; 
def beginDateDefaultSeason2 = 20190716;
def beginDateDefaultSeason3 = 20190412;
def beginDateDefaultSeason4 = 20190111;
def beginDateDefaultSeason5 = 20181012;
def beginDateDefaultSeason6 = 20180713;
def beginDateDefaultSeason7 = 20180413;
def beginDateDefaultSeason8 = 20180112;
def beginDateDefaultSeason9 = 20171012;
def beginDateDefaultSeason10 = 20170714;
def beginDateDefaultSeason11 = 20170413;
def beginDateDefaultSeason12 = 20170113;

def endDateDefaultSeason1 = hX;
def endDateDefaultSeason2 = 20191014; 
def endDateDefaultSeason3 = 20190715;
def endDateDefaultSeason4 = 20190411;
def endDateDefaultSeason5 = 20190110;
def endDateDefaultSeason6 = 20181011;
def endDateDefaultSeason7 = 20180712;
def endDateDefaultSeason8 = 20180412;
def endDateDefaultSeason9 = 20180111;
def endDateDefaultSeason10 = 20171011;
def endDateDefaultSeason11 = 20170713;
def endDateDefaultSeason12 = 20170412;
def endDateDefaultSeason13 = 20170112;

def beginDefaultSeason1x_ = if DaysTillDate(beginDateDefaultSeason1) == 0 then x else beginDefaultSeason1x_[1];   
def beginDefaultSeason2x_ = if DaysTillDate(beginDateDefaultSeason2) == 0 then x else beginDefaultSeason2x_[1];   
def beginDefaultSeason3x_ = if DaysTillDate(beginDateDefaultSeason3) == 0 then x else beginDefaultSeason3x_[1];
def beginDefaultSeason4x_ = if DaysTillDate(beginDateDefaultSeason4) == 0 then x else beginDefaultSeason4x_[1];
def beginDefaultSeason5x_ = if DaysTillDate(beginDateDefaultSeason5) == 0 then x else beginDefaultSeason5x_[1];
def beginDefaultSeason6x_ = if DaysTillDate(beginDateDefaultSeason6) == 0 then x else beginDefaultSeason6x_[1];
def beginDefaultSeason7x_ = if DaysTillDate(beginDateDefaultSeason7) == 0 then x else beginDefaultSeason7x_[1];
def beginDefaultSeason8x_ = if DaysTillDate(beginDateDefaultSeason8) == 0 then x else beginDefaultSeason8x_[1];
def beginDefaultSeason9x_ = if DaysTillDate(beginDateDefaultSeason9) == 0 then x else beginDefaultSeason9x_[1];
def beginDefaultSeason10x_ = if DaysTillDate(beginDateDefaultSeason10) == 0 then x else beginDefaultSeason10x_[1];
def beginDefaultSeason11x_ = if DaysTillDate(beginDateDefaultSeason11) == 0 then x else beginDefaultSeason11x_[1];
def beginDefaultSeason12x_ = if DaysTillDate(beginDateDefaultSeason12) == 0 then x else beginDefaultSeason12x_[1];
#addchartbubble(debugBubbles, lowestall(low),"5x__\n"+beginDefaultSeason5x_, color.lime,no);
#addchartbubble(debugBubbles, lowestall(low),"6x__\n"+beginDefaultSeason6x_, color.lime,no);
#addchartbubble(debugBubbles, lowestall(low),"7x__\n"+beginDefaultSeason7x_, color.lime,no);

def endDefaultSeasonx__ = hX;
def endDefaultSeason2x__ = if getYYYYMMDD() == endDateDefaultSeason2 then x else endDefaultSeason2x__[1];
def endDefaultSeason3x__ = if getYYYYMMDD() == endDateDefaultSeason3 then x else endDefaultSeason3x__[1];
def endDefaultSeason4x__ = if getYYYYMMDD() == endDateDefaultSeason4 then x else endDefaultSeason4x__[1];
def endDefaultSeason5x__ = if getYYYYMMDD() == endDateDefaultSeason5 then x else endDefaultSeason5x__[1];
def endDefaultSeason6x__ = if getYYYYMMDD() == endDateDefaultSeason6 then x else endDefaultSeason6x__[1];
def endDefaultSeason7x__ = if getYYYYMMDD() == endDateDefaultSeason7 then x else endDefaultSeason7x__[1];
def endDefaultSeason8x__ = if getYYYYMMDD() == endDateDefaultSeason8 then x else endDefaultSeason8x__[1];
def endDefaultSeason9x__ = if getYYYYMMDD() == endDateDefaultSeason9 then x else endDefaultSeason9x__[1];
def endDefaultSeason10x__ = if getYYYYMMDD() == endDateDefaultSeason10 then x else endDefaultSeason10x__[1];
def endDefaultSeason11x__ = if getYYYYMMDD() == endDateDefaultSeason11 then x else endDefaultSeason11x__[1];
def endDefaultSeason12x__ = if getYYYYMMDD() == endDateDefaultSeason12 then x else endDefaultSeason12x__[1];
# addchartbubble(debugBubbles, lowestall(low),"6x__\n"+endDefaultSeason6x__, color.violet,no);
# addchartbubble(debugBubbles, lowestall(low),"7x__\n"+endDefaultSeason7x__, color.violet,no);
# addchartbubble(debugBubbles, lowestall(low),"8x__\n"+endDefaultSeason8x__, color.violet,no);

#  Actual Earnings <<<<<<<<<< <<<<<<<<<< <<<<<<<<<< <<<<<<<<<<<
def actualEarningsYesNo = !IsNaN(o) and HasEarnings();
        #addchartbubble(1, lowestall(low),actualEarningsYesNo, color.lime,no);

def noEarningsYesNo = !HighestAll(TotalSum(actualEarningsYesNo));
        #addchartbubble(1, lowestall(low),noEarnings, color.lime,no);

def morningEarningsYesNo__ = if actualEarningsYesNo
                      then HasEarnings(EarningTime.BEFORE_MARKET)
                      else morningEarningsYesNo__[1];

def afternoonEarningsYesNo__ = if actualEarningsYesNo
                      then HasEarnings(EarningTime.AFTER_MARKET)
                      else afternoonEarningsYesNo__[1];

# def actualEarningsBars__ = if actualEarningsYesNo then x else actualEarningsBar__[1];  # nubes code

def earningsBarsX__ = if actualEarningsYesNo and morningEarningsYesNo__
                   then x
                   else if actualEarningsYesNo and afternoonEarningsYesNo__
                   then x[-1]
                   else if actualEarningsYesNo
                   then x[-1]                   
                   else earningsBarsX__[1];
             addchartbubble(1, lowestall(low), "eb1\n" + earningsBarsX__, color.lime, no);

#def earningsBar2 = if hasEarnings and morningEarnings
#                   then x
#                   else if hasEarnings and afternoonEarnings
#                   then getvalue(x, -1)
#                   else earningsBar2[1];
# addchartbubble(1, lowestall(low), "t2\n" + earningsBar1, color.lime, no);

# beginning with the forming bar and counting to the left
def beginEarningsdPeriod1X__ = earningsBarsX__;
             addchartbubble(1, lowestall(low), "bEP1\n" + beginEarningsdPeriod1X__, color.violet, no);
def beginEarningsdPeriod2X__ = prior(beginEarningsdPeriod1X__);
def beginEarningsdPeriod3X__ = prior(beginEarningsdPeriod2X__);
def beginEarningsdPeriod4X__ = prior(beginEarningsdPeriod3X__);
def beginEarningsdPeriod5X__ = prior(beginEarningsdPeriod4X__);
def beginEarningsdPeriod6X__ = prior(beginEarningsdPeriod5X__);
def eb7 = prior(beginEarningsdPeriod6X__);
def eb8 = prior(eb7);
def eb9 = prior(eb8);
def eb10 = prior(eb9);
def eb11 = prior(eb10);
def eb12 = prior(eb11);

def endEarningsdPeriod2X__ = if actualEarningsYesNo[-1] then x else endEarningsdPeriod2X__[1];
def endEarningsdPeriod3X__ = prior(endEarningsdPeriod2X__);
def endEarningsdPeriod4X__ = prior(endEarningsdPeriod3X__);
def endEarningsdPeriod5X__ = prior(endEarningsdPeriod4X__);
def endEarningsdPeriod6X__ = prior(endEarningsdPeriod5X__);
def db5 = prior(endEarningsdPeriod6X__);
def db6 = prior(db5);
def db7 = prior(db6);
def db8 = prior(db7);
def db9 = prior(db8);
def db10 = prior(db9);
def db11 = prior(db10);
def db12 = prior(db11);


def earningsLRL1 = regressionBetweenBars(o,
                                        if noEarningsYesNo then beginDefaultSeason1X_ else HighestAll(beginEarningsdPeriod1X__),
                                        if noEarningsYesNo then hX else hX);

def earningsLRL2 = regressionBetweenBars(o,
                                         if noEarningsYesNo then beginDefaultSeason2x_ else HighestAll(beginEarningsdPeriod2X__),
                                         if noEarningsYesNo then endDefaultSeason2x__ else HighestAll(endEarningsdPeriod2X__));

def earningsLRL3 = regressionBetweenBars(o,
                                         if noEarningsYesNo then beginDefaultSeason3x_ else HighestAll(beginEarningsdPeriod3X__),
                                         if noEarningsYesNo then endDefaultSeason3x__ else HighestAll(endEarningsdPeriod3X__));

def earningsLRL4 = regressionBetweenBars(o,
                                         if noEarningsYesNo then beginDefaultSeason4x_ else HighestAll(beginEarningsdPeriod4X__),
                                         if noEarningsYesNo then endDefaultSeason4x__ else HighestAll(endEarningsdPeriod4X__));

def earningsLRL5 = regressionBetweenBars(o,
                                         if noEarningsYesNo then beginDefaultSeason5x_ else HighestAll(beginEarningsdPeriod5X__),
                                         if noEarningsYesNo then endDefaultSeason5x__ else HighestAll(endEarningsdPeriod5X__));

def earningsLRL6 = regressionBetweenBars(o,
                                         if noEarningsYesNo then beginDefaultSeason6x_ else HighestAll(beginEarningsdPeriod6X__),
                                         if noEarningsYesNo then endDefaultSeason6x__ else HighestAll(endEarningsdPeriod6X__));

#def earningsLRL7 = regressionBetweenBars(o,
#                                         if noEarningsYesNo then beginDefaultSeason7x_ else HighestAll(eb6),
#                                         if noEarningsYesNo then endDefaultSeason7x__ else HighestAll(db5));

#def earningsLRL8 = regressionBetweenBars(o,
#                                         if noEarningsYesNo then beginDefaultSeason8x_ else HighestAll(eb7),
#                                         if noEarningsYesNo then endDefaultSeason8x__ else HighestAll(db6));

#def earningsLRL9 = regressionBetweenBars(o,
#                                         if noEarningsYesNo then beginDefaultSeason9x_ else HighestAll(eb8),
#                                         if noEarningsYesNo then endDefaultSeason9x__ else HighestAll(db7));

#def earningsLRL10 = regressionBetweenBars(o,
#                                         if noEarningsYesNo then beginDefaultSeason10x_ else HighestAll(eb9),
#                                         if noEarningsYesNo then endDefaultSeason10x__ else HighestAll(db8));

#def earningsLRL11 = regressionBetweenBars(o,
#                                         if noEarningsYesNo then beginDefaultSeason11x_ else HighestAll(eb10),
#                                         if noEarningsYesNo then endDefaultSeason11x__ else HighestAll(db9));

#def earningsLRL12 = regressionBetweenBars(o,
#                                         if noEarningsYesNo then beginDefaultSeason12x_ else HighestAll(eb11),
#                                         if noEarningsYesNo then endDefaultSeason12x__ else HighestAll(db10));

    # Plots
# Ticker regression plots
plot EaringsPeriodRegression1 = earningsLRL1;
plot EaringsPeriodRegression2 = earningsLRL2;
plot EaringsPeriodRegression3 = earningsLRL3;
plot EaringsPeriodRegression4 = earningsLRL4;
plot EaringsPeriodRegression5 = earningsLRL5;
plot EaringsPeriodRegression6 = earningsLRL6;
#plot EaringsPeriodRegression7 = earningsLRL7;
#plot EaringsPeriodRegression8 = earningsLRL8;
#plot EaringsPeriodRegression9 = earningsLRL9;
#plot EaringsPeriodRegression10 = earningsLRL10;
#plot EaringsPeriodRegression11 = earningsLRL11;
#plot EaringsPeriodRegression12 = earningsLRL12;

input displayEarningsSeasonStart = yes;
addverticalLine(displayEarningsSeasonStart == yes and getyYYYMMDD()== beginDateDefaultSeason1 and SecondsTillTime(0930) == 0," 3Q2019 Earnings Season",color.gray, curve.firm);
addverticalLine(displayEarningsSeasonStart == yes and getyYYYMMDD()== beginDateDefaultSeason2 and SecondsTillTime(0930) == 0," 2Q2019 Earnings Season",color.gray, curve.firm);
addverticalLine(displayEarningsSeasonStart == yes and getyYYYMMDD()== beginDateDefaultSeason3 and SecondsTillTime(0930) == 0," 1Q2019 Earnings Season",color.gray, curve.firm);
addverticalLine(displayEarningsSeasonStart == yes and getyYYYMMDD()== beginDateDefaultSeason4 and SecondsTillTime(0930) == 0," 4Q2018 Earnings Season",color.gray, curve.firm);
addverticalLine(displayEarningsSeasonStart == yes and getyYYYMMDD()== beginDateDefaultSeason5 and SecondsTillTime(0930) == 0," 3Q2018 Earnings Season",color.gray, curve.firm);
addverticalLine(displayEarningsSeasonStart == yes and getyYYYMMDD()== beginDateDefaultSeason6 and SecondsTillTime(0930) == 0," 2Q2018 Earnings Season",color.gray, curve.firm);
addverticalLine(displayEarningsSeasonStart == yes and getyYYYMMDD()== beginDateDefaultSeason7 and SecondsTillTime(0930) == 0," 1Q2018 Earnings Season",color.gray, curve.firm);
addverticalLine(displayEarningsSeasonStart == yes and getyYYYMMDD()== beginDateDefaultSeason8 and SecondsTillTime(0930) == 0," 4Q2017 Earnings Season",color.gray, curve.firm);
addverticalLine(displayEarningsSeasonStart == yes and getyYYYMMDD()== beginDateDefaultSeason9 and SecondsTillTime(0930) == 0," 3Q2017 Earnings Season",color.gray, curve.firm);
addverticalLine(displayEarningsSeasonStart == yes and getyYYYMMDD()== beginDateDefaultSeason10 and SecondsTillTime(0930) == 0," 2Q2017 Earnings Season",color.gray, curve.firm);
addverticalLine(displayEarningsSeasonStart == yes and getyYYYMMDD()== beginDateDefaultSeason11 and SecondsTillTime(0930) == 0," 1Q2017 Earnings Season",color.gray, curve.firm);
addverticalLine(displayEarningsSeasonStart == yes and getyYYYMMDD()== beginDateDefaultSeason12 and SecondsTillTime(0930) == 0," 4Q2016 Earnings Season",color.gray, curve.firm);
Yes, I do it all the time... However, you can only reference licensed Studies, Strategies, and Patterns... It is not currently possible to reference custom Thinkscripts of any type...

For example, I often make a reference call to ATR(), RSI(), ZigZagHighLow(), RayBullPower(), BalanceOfMarketPower(), Moving Averages, etc...
I have written and saved a study with some def/plot ad have saved with a name say "StudyXYZ". I am now trying to write a different study, where I am want to reference the previously saved custom study "StudyXYZ".

When I reference as

plot x = reference StudyXYZ();

It doesn't seem to work.

But if I try a built in Study, it works. For example the following works.

plot X=reference OnBalanceVolume();

Another case of thinkscript study referencing.

09:18 JohnnyQuotron: I have lifed the required code from Mobius' RSI-Laguerre Self Adjusting With Fractal Energy Gaussian Price Filter script and placed it directly into a scan.
09:21 Mobius: Johnny - It's much easier to just reference the study. The scan would be 1 line of code.

Example: Assuming you named the study RSI_Laguerre_Gaussian to scan for a cross above .2 would be

RSI_Laguerre_Gaussian()."RSI" crosses above .2
I've been learning/coding thinkscript for a short time now. Thanks to this site and the many talented and generous traders here, I have been doing what I always do, that is, reading a lot and most important of all, studying the scripts here line by line to learn what everything does and how it all works together. I know that many of you have a list of all the built-in studies at the ready, but for anyone that needs this list (which I do) here is a link to the list of built-in studies.
@sarvi You can only reference licensed built-in studies from within studies... You cannot reference custom studies, their code must be incorporated into your new study...
Last edited by a moderator:
1) You can take the source code from both studies, combine them, and change the input for MACD from close to Ratio or w/e you like.
2) Don't really understand what you mean by data series. Like different variables? If so, then yes you can def new variables to do calculations and plot them or use them as inputs for studies.
@generic Thanks. I don't know how to code, so it was more a question whether you can do this easily on ToS. As to 2), yes I was referring to defining new variables. Can you do this without having to code stuff in Thinkscript?
@BrathBB Add the ticker line and replace the two def o and def c lines. No need to take jabs at Ben, he's just posting the reference so that you can try figure it out for yourself.

input ticker = "SPY";

def o = open(symbol = ticker, period = agg);
def c = close(symbol = ticker, period = agg);
@BrathBB Combining many indicators into one strategy or study is one way which does require a bit of coding.

Another way is to the scanner to filter for the indicator's criteria.

Below is an example. It should be noted that no stocks were returned using these particular combinations of indicators at least not using the arbitrary aggregation periods, I inputted.

Is thinkscript capable of black-box accessing thinkorswim indicators?
For example, consider the FW_SOAP indicator and suppose I want to program a strategy that subscribes to its events and takes some action each time the indicator emits an arrow. How can I subscribe to its events?
I would expect this to be possible in thinkscript. It is possible in ninjascript. In fact, ninjascript even allows access beyond ninjatrader indicators to third party indicators, see section Educational Resources / Using 3rd Party Indicators at I would imagine thinkscript can provide similar capabilities.
Thank you
Sorry, so then what's the solution, if any, apart from a market scan, to compile multiple custom indicators into one?

In ThinkScript, we can't reference custom studies, is there any way to make a code where multiple conditions are met from other custom indicators?

For instance, in this image, I would like to have just one signal (Arrow, colored line, etc.) when these indicators, all together, confirm the alert. I know that the alternative is to add a market scan, but it will be great to see if we can do that into a compiled study or Indicator...

@BenTen, I don't know if you have any suggestions that might solve this. Thank you
Last edited by a moderator:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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