• LIMITED TIME OFFER: use coupon code PRIMEDAY at checkout to save $50 off VIP membership (down to $149 / year). Valid through Tuesday 6/22.

thinkScript Reference Other Data or Studies

SugarTrader

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

BenTen

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

rad14733

Well-known member
VIP
You will have to integrate all 3 different indicators into one, assuming they play nicely with each other. You can't call for data from one study to the other unless it's something that was prebuilt and provided by ToS.
Hmmm... I call multiple studies all the time... In fact I'm working on a dashboard study that does just that as I type this... I already have references to RSICrossover, multiple different moving averages with crossovers, and more in one study... Or am I misunderstanding something here...???
 

SugarTrader

Member
VIP
@rad14733 I think what @moutic777 is trying to do is call for data from Study 1 while developing Study 2. Both indicators will be separated rather being combined into one. Not sure if I understand that correctly.
@rad14733 , @BenTen , thanks. I think Ben understood 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.
 

rad14733

Well-known member
VIP
@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
 

MindPortal

New member
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

Code:
# 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;

    plot
    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);
 

rad14733

Well-known member
VIP
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...
 

rad14733

Well-known member
VIP
@Saylarr Unfortunately, we cannot reference custom Studies or Strategies from within another Study/Strategy - only licensed studies... You must include the entire custom script within your new script... Some of the studies here have several complete studies or more in them...
 

rad14733

Well-known member
VIP
Ok... So are you calling the script from within the code...??? Think of a script as a subroutine, function, procedure, whatever... It is a re-entrant piece of code - a script... We can use the default variable settings, even the inputs, or send them in the script call... Scripts are very under-used within Studies and Strategies, in my humble opinion...With them we could create libraries, shareable for future development...
 
Top