Security Function pine script to TOS help?

mbarcala

Active member
what could be the equivalent of a pine script security function in thinkscript please?

sample here
Code:
security(tickerid, Resolution, close)

I just trying to learn what could be the equivalents
 
Solution
what could be the equivalent of a pine script security function in thinkscript please?

sample here
Code:
security(tickerid, Resolution, close)

I just trying to learn what could be the equivalents

looks like pinescript security() is used to,
. read a price from a different symbol
. set a different time frame ( 2nd aggregation)
. choose a price parameter


look up the description of TOS close. it is similar.
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Fundamentals/close
close ( Symbol symbol, Any period, String priceType);


symbol...
to pull price data from a different symbol
input sym1= "SPY";
def s = close(sym1);

time...
to pull price data from a different time,
input agg =...
If I remember correctly, that is how you reference secondary symbols in pineScript. The equivalent would probably be;

Close(symbol = tickerid, period = resolution)
 
what could be the equivalent of a pine script security function in thinkscript please?

sample here
Code:
security(tickerid, Resolution, close)

I just trying to learn what could be the equivalents

looks like pinescript security() is used to,
. read a price from a different symbol
. set a different time frame ( 2nd aggregation)
. choose a price parameter


look up the description of TOS close. it is similar.
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Fundamentals/close
close ( Symbol symbol, Any period, String priceType);


symbol...
to pull price data from a different symbol
input sym1= "SPY";
def s = close(sym1);

time...
to pull price data from a different time,
input agg = AggregationPeriod.day;
# current day high
def dayhi = high(period = agg);

type...
input type = PriceType.LAST;
plot LastPrice = close(priceType = type);
https://tlc.thinkorswim.com/center/reference/thinkScript/Constants/PriceType
pricetype: Defines the type of price to be returned: Last, Ask, Bid, or Mark.


combined...
input sym1= "SPY";
input agg = AggregationPeriod.day;
input type = PriceType.LAST;
def s = high(sym1, period = agg, priceType = type);

i don't think the parameter names are needed as long as all 3 data pieces are used and in the correct order. if the first parameter is not specified, then the remaining names need to be used. if not, an agg time would be assigned as a symbol, and cause an error.


https://www.tradingview.com/pine-sc.../Context_switching_the_security_function.html
 
Solution
If I remember correctly, that is how you reference secondary symbols in pineScript. The equivalent would probably be;

Close(symbol = tickerid, period = resolution)
It seems that it doesn't work.

up2 = security(tickerid, bigtf, hl2 - (3 * atr(7)))
dn2 = security(tickerid, bigtf, hl2 + (3 * atr(7)))

which I converted as follows. But it did not work.


def up2 = Close(symbol = tickerid, period = hl2 - (3 * ATR(7)));
def dn2 = Close(symbol = tickerid, period = hl2 + (3 * ATR(7)));
 
It seems that it doesn't work.

up2 = security(tickerid, bigtf, hl2 - (3 * atr(7)))
dn2 = security(tickerid, bigtf, hl2 + (3 * atr(7)))

which I converted as follows. But it did not work.


def up2 = Close(symbol = tickerid, period = hl2 - (3 * ATR(7)));
def dn2 = Close(symbol = tickerid, period = hl2 + (3 * ATR(7)));
How about following expression:

def up2 = close(GetSymbol(bigft), period = hl2 - (3 * ATR(7)));
def dn2 = close(GetSymbol(bigft), period = hl2 + (3 * ATR(7)));
 
Close(symbol = tickerid, period = resolution)
Its not that literal, this is not actual code, that's just how the parameters are reordered with their equivalents.

Tickerid in pine script is the symbol in think script, and resolution in pine script is aggregation period in think script.

Anyway...

You would just need to do this...

def up2 = hl2 - (3 * ATR(7)));
def dn2 = hl2 + (3 * ATR(7)));

But "bigtf" is probably "big time frame" and you would need to set those to "bigtf" - which is what though?

It would basically be:
hl2(period = "bigtf")

But don't just type that, you have to put an aggregation constant where "bigtf" is, which ever one it may be.

For example:
hl2(period = AggregationPeriod.FIVE_MIN);

Unfortunately, ATR doesn't have a period parameter though. I can build you an adjustable time frame average true range from true range, perhaps later on, maybe around lunch time east coast if the market is calm. But only if you promise to stop pinging me, this site sends email notifications to my phone... its 3:00AM here and the market opens for me at 6AM, I gotta go back to sleep.
 
Code:
input BigTF = aggregationPeriod.HOUR;

Script MTF_ATR {
    Input TF = aggregationPeriod.HOUR;
    Input Length = 14;
    input averageType = AverageType.WILDERS;
    plot ATR =
        MovingAverage(
            averageType,
            TrueRange(
                high(period = TF),
                close(period = TF),
                low(period = TF)
            ),
            length
        )
    ;
};
def up2 = hl2(Period = BigTF) - (3 * MTF_ATR(BigTF,7));
def dn2 = hl2(Period = BigTF) + (3 * MTF_ATR(BigTF,7));

Here is the sample. You will need to modify it to fit in with what ever other code you're working with of course.
 
Code:
input BigTF = aggregationPeriod.HOUR;

Script MTF_ATR {
    Input TF = aggregationPeriod.HOUR;
    Input Length = 14;
    input averageType = AverageType.WILDERS;
    plot ATR =
        MovingAverage(
            averageType,
            TrueRange(
                high(period = TF),
                close(period = TF),
                low(period = TF)
            ),
            length
        )
    ;
};
def up2 = hl2(Period = BigTF) - (3 * MTF_ATR(BigTF,7));
def dn2 = hl2(Period = BigTF) + (3 * MTF_ATR(BigTF,7));

Here is the sample. You will need to modify it to fit in with what ever other code you're working with of course.
Thank you very much for your help. After I make bigtf = 50, it is partially working mainly in daily timeframes, but not in small timeframes. The counterpart indicator shows small clouds inside bigger clouds in any small timeframes such as 10-min, 30-min, and one hour. My converted indicator only shows a pair of clouds with one upper cloud and one lower cloud in any timeframes. The second issue is cloud color change issue. My converted indicator doesn't change cloud colors in downtrends or uptrends as the counterpart indicator does. I assign two different cloud colors, but it only shows one pair of colors. I changed bigtf values, the charts did not change at al, I think that the main problem may come from up =up2, dn=dn2, resulting in one pair of clouds instead of two pairs of clouds. Could you please help me to check where I make mistakes.
https://usethinkscript.com/threads/convert-noros-overcloud-from-tradingview.9253/


Ruby:
input Cloud = 25;
input needcenter = no;
input needcenterbig  = yes;
input bigtf = 50;
input border = no;
input trendcolor = yes;
def hl2 = (high + low) / 2;

script MTF_ATR {
    input TF = AggregationPeriod.HOUR;
    input Length = 14;
    input averageType = AverageType.WILDERS;
    plot ATR =
        MovingAverage(
            averageType,
            TrueRange(
                high(period = TF),
                close(period = TF),
                low(period = TF)
            ),
            Length
        )
    ;
}
;

script nz {
    input data = 0;
    def ret_val = if IsNaN(data) then 0 else data;
    plot return = ret_val;
}

def up = hl2 - (3 * ATR(7));
def dn = hl2 + (3 * ATR(7));
def tup = if close[1] > tup[1] then Max(up, tup[1]) else up;
def tdown = if close[1] < tdown[1] then Min(dn, tdown[1]) else dn;
def center = (tup + tdown) / 2;
def limit = (tup - tdown) / 100 * Cloud;

def upcloud = tup - limit;
def dncloud = tdown + limit;
def trend = if close > tdown[1] then 1 else if close < tup[1] then -1 else nz(trend[1]);

plot centercolor = needcenter;
centercolor.AssignValueColor (if needcenter == no then Color.DARK_GREEN else Color.BLUE);
plot bordercolor = border;
bordercolor.AssignValueColor (if border == yes then Color.BLACK else Color.DARK_GREEN);
plot cloudcolor = trend;
cloudcolor.AssignValueColor ( if trend == 1 then Color.LIME else Color.RED);
plot p11 = tup;
plot p12 = tdown;
plot p13 = center;
plot p14 = upcloud;
plot p15 = dncloud;
AddCloud(p11, p14, color.DARK_GREEN, color.DARK_RED, yes);
AddCloud(p12, p15, color.DARK_GREEN, color.DARK_RED, yes);

def up2 = hl2(Period = bigtf) - (3 * MTF_ATR(bigtf, 7));
def dn2 = hl2(Period = bigtf) + (3 * MTF_ATR(bigtf, 7));
def tup2 = if close[1] > tup2[1] then Max(up2, tup2[1]) else up2;
def tdown2 = if close[1] < tdown2[1] then Min(dn2, tdown2[1]) else dn2;
def center2 = (tup2 + tdown2) / 2;
def limit2 = (tup2 - tdown2) / 100 * Cloud;
def upcloud2 = tup2 - limit2;
def dncloud2 = tdown2 + limit2;
def trend2 = if close > tdown2[1] then 1 else if  close < tup2[1] then -1 else nz(trend2[1]);

plot centercolor2 = needcenterbig;
centercolor2.AssignValueColor (if needcenterbig == no then color.BLUE else Color.BLACK);
plot bordercolor2 = border;
bordercolor2.AssignValueColor (if border == yes then Color.BLACK else Color.CYAN);
plot cloudcolor2 = trend2;
cloudcolor2.AssignValueColor (if trend2 == 1 then Color.LIME else Color.RED);
plot p21 = tup2;
plot p22 = tdown2;
plot p23 = center2;
plot p24 = upcloud2;
plot p25 = dncloud2;
AddCloud(p21, p24, color.dARK_ORANGE, color.DARK_GRAY, yes);
AddCloud(p22, p25, color.dARK_ORANGE, color.DARK_GRAY, yes);
 
Last edited:
Try this and let me know if the root functionality is as it should be. Once we get the main stuff out of the way, then we can work on the color changes and more decorative aspects of it.

Code:
input cloud = 25;
input needcenter = no;
input needcenterbig = no;
input bigtf = aggregationPeriod.DAY;
input border = yes;
input trendcolor = yes;

script nz {
    input data = 0;
    input data2 = Double.NaN;
    def ret_val =
            if IsNaN(data) and !isNan(data2) then data2
            else if IsNaN(data) and isNan(data2) then 0
            else data;
    plot return = ret_val;
}
script MTF_ATR {
    input Length = 14;
    input TF = AggregationPeriod.HOUR;
    input averageType = AverageType.WILDERS;
    plot ATR =
        MovingAverage(
            averageType,
            TrueRange(
                high(period = TF),
                close(period = TF),
                low(period = TF)
            ),
            Length
        )
    ;
}

def up = hl2 - (3 * atr(7));
def dn = hl2 + (3 * atr(7));
def tup = if close[1] > tup[1] then max(up, tup[1]) else up;
def tdown = if close[1] < tdown[1] then min(dn, tdown[1]) else dn;
def center = (tup + tdown) / 2;
def limit = (tup - tdown) / 100 * cloud;
def upcloud = tup - limit;
def dncloud = tdown + limit;
def trend = if close > tdown[1] then 1 else if close < tup[1] then -1 else nz(trend[1], 1);

#centercolor = needcenter == false ? na : blue
#bordercolor = border == true ? black : na
#cloudcolor = trend == 1 ? lime : red

plot p11 = tup;
plot p12 = tdown;
plot p13 = center;
plot p14 = upcloud;
plot p15 = dncloud;
addcloud(p11, p14,color.green,color.green);
addcloud(p12, p15,color.red,color.red);

def up2 = hl2(period = bigtf) - (3 * mtf_atr(7,bigtf));
def dn2 = hl2(period = bigtf) + (3 * mtf_atr(7,bigtf));
def tup2 = if close[1] > tup2[1] then max(up2, tup2[1]) else up2;
def tdown2 = if close[1] < tdown2[1] then min(dn2, tdown2[1]) else dn2;
def center2 = (tup2 + tdown2) / 2;
def limit2 = (tup2 - tdown2) / 100 * cloud;
def upcloud2 = tup2 - limit2;
def dncloud2 = tdown2 + limit2;
def trend2 = if close > tdown2[1] then 1 else if close < tup2[1] then -1 else nz(trend2[1], 1);

#centercolor2 = needcenterbig == false ? na : black
#bordercolor2 = border == true ? black : na
#cloudcolor2 = trend2 == 1 ? lime : red

plot p21 = tup2;
plot p22 = tdown2;
plot p23 = center2;
plot p24 = upcloud2;
plot p25 = dncloud2;
addcloud(p21, p24,color.green,color.green);
addcloud(p22, p25,color.red,color.red);
 
Try this and let me know if the root functionality is as it should be. Once we get the main stuff out of the way, then we can work on the color changes and more decorative aspects of it.

Code:
input cloud = 25;
input needcenter = no;
input needcenterbig = no;
input bigtf = aggregationPeriod.DAY;
input border = yes;
input trendcolor = yes;

script nz {
    input data = 0;
    input data2 = Double.NaN;
    def ret_val =
            if IsNaN(data) and !isNan(data2) then data2
            else if IsNaN(data) and isNan(data2) then 0
            else data;
    plot return = ret_val;
}
script MTF_ATR {
    input Length = 14;
    input TF = AggregationPeriod.HOUR;
    input averageType = AverageType.WILDERS;
    plot ATR =
        MovingAverage(
            averageType,
            TrueRange(
                high(period = TF),
                close(period = TF),
                low(period = TF)
            ),
            Length
        )
    ;
}

def up = hl2 - (3 * atr(7));
def dn = hl2 + (3 * atr(7));
def tup = if close[1] > tup[1] then max(up, tup[1]) else up;
def tdown = if close[1] < tdown[1] then min(dn, tdown[1]) else dn;
def center = (tup + tdown) / 2;
def limit = (tup - tdown) / 100 * cloud;
def upcloud = tup - limit;
def dncloud = tdown + limit;
def trend = if close > tdown[1] then 1 else if close < tup[1] then -1 else nz(trend[1], 1);

#centercolor = needcenter == false ? na : blue
#bordercolor = border == true ? black : na
#cloudcolor = trend == 1 ? lime : red

plot p11 = tup;
plot p12 = tdown;
plot p13 = center;
plot p14 = upcloud;
plot p15 = dncloud;
addcloud(p11, p14,color.green,color.green);
addcloud(p12, p15,color.red,color.red);

def up2 = hl2(period = bigtf) - (3 * mtf_atr(7,bigtf));
def dn2 = hl2(period = bigtf) + (3 * mtf_atr(7,bigtf));
def tup2 = if close[1] > tup2[1] then max(up2, tup2[1]) else up2;
def tdown2 = if close[1] < tdown2[1] then min(dn2, tdown2[1]) else dn2;
def center2 = (tup2 + tdown2) / 2;
def limit2 = (tup2 - tdown2) / 100 * cloud;
def upcloud2 = tup2 - limit2;
def dncloud2 = tdown2 + limit2;
def trend2 = if close > tdown2[1] then 1 else if close < tup2[1] then -1 else nz(trend2[1], 1);

#centercolor2 = needcenterbig == false ? na : black
#bordercolor2 = border == true ? black : na
#cloudcolor2 = trend2 == 1 ? lime : red

plot p21 = tup2;
plot p22 = tdown2;
plot p23 = center2;
plot p24 = upcloud2;
plot p25 = dncloud2;
addcloud(p21, p24,color.green,color.green);
addcloud(p22, p25,color.red,color.red);
Thank you very much for your great help. It works well and shows two pairs of clouds in lower timeframes such as 4-hour, 2-hour, 1-hour, 30-min, and 10-min timeframes. But it doesn't show two pairs of clouds in higher timeframes such as monthly and weekly timeframes as the counterpart indicator does. In addition, it shows a central line between two pairs of clouds in most of lower timeframes such as 1-hour, 30-min, 10-min and 5-min, but the counterpart doesn't have this central line. Therefore we should remove unnecessary central line between two pairs of clouds. In addition, can we use have an automatic loom switch from day, week to month through input bigtf = aggregationPeriod.DAY, aggregationPeriod.WEEK, or aggregationPeriod.Month? I would also like to know whether it is possible to adjust cloud transparency degree when we use AddCloud function?
 
Last edited:
The big time frame is selectable, and it must be set to something larger than the chart's main time frame. There must also be enough price history on the chart to generate the average true range of the big time frame. If the chart's aggregation is very small, but the big time frame is large, then often the second cloud will simply be out of view. Apply proper settings, extend your price history, and/or scrunch down your y-axis to check these issues. Let me know what you find.

Any of the lines can be disabled by changing their plot to a def.

Cloud transparency is not an option in thinkscript. The cloud is at its most transparent by default. You can, however, make it more opaque by simply duplicating the same could, and overlapping them over each other.

I am not following what you mean by loom switch, do you mean this by any chance?

9mTJfHk.png


If not, please explain in more detail.
 
The big time frame is selectable, and it must be set to something larger than the chart's main time frame. There must also be enough price history on the chart to generate the average true range of the big time frame. If the chart's aggregation is very small, but the big time frame is large, then often the second cloud will simply be out of view. Apply proper settings, extend your price history, and/or scrunch down your y-axis to check these issues. Let me know what you find.

Any of the lines can be disabled by changing their plot to a def.

Cloud transparency is not an option in thinkscript. The cloud is at its most transparent by default. You can, however, make it more opaque by simply duplicating the same could, and overlapping them over each other.

I am not following what you mean by loop switch, do you mean this by any chance?

9mTJfHk.png


If not, please explain in more detail.
Thank you very much for your help and information. In the original pine script, the bigger timeframe changes automatically when you select lower timeframes. I do not know how it is accomplished in the pine script code. I am wondering whether we can have a loop switch so that the big timeframe will automatically switch to month when we choose weekly timeframe. Now I will try to address how to make clouds change green in uptrend and red to downtrend.
 
Last edited:

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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