What is the ValueWhen (TradingView) equivalent in ThinkScript?

MindPortal

New member
What is the ValueWhen (TradingView) equivalent in ThinkScript?
so I can get a value during a certain condition.
Say if I wanted to plot an arrow when an ema cross happens. Thank you!
 
Solution
TV opensource reference:

study(title="[RS]Bollinger Bands Breakout Candles V0", shorttitle="[RS]BBBc.V0", overlay=true)
length = input(8)
src = input(hl2)
deviations = input(2)
ma = sma(src, length)
oma = ma > ma[1] ? valuewhen(ma < ma[1], ma[1], 0) : ma < ma[1] ? valuewhen(ma > ma[1], ma[1], 0) : nz(oma[1])
upper = oma + stdev(src, length) * deviations
lower = oma - stdev(src, length) * deviations

plotcandle(oma, upper, lower, ma, color=ma>=upper?#9ce0b2:ma<=lower?#e0b29c:ma>=ma[1]?green:maroon)
https://www.tradingview.com/pine-script-reference/#fun_valuewhen
oma = ma > ma[1] ? valuewhen(ma < ma[1], ma[1], 0) : ma < ma[1] ? valuewhen(ma > ma[1], ma[1], 0) : nz(oma[1])

I know I have to use conditionals and a plot but not sure how to translate the valuewhen and the nz function to thinkscript. The top is from pinescript in tradingview. Any help is appreciated!
 
TV opensource reference:

study(title="[RS]Bollinger Bands Breakout Candles V0", shorttitle="[RS]BBBc.V0", overlay=true)
length = input(8)
src = input(hl2)
deviations = input(2)
ma = sma(src, length)
oma = ma > ma[1] ? valuewhen(ma < ma[1], ma[1], 0) : ma < ma[1] ? valuewhen(ma > ma[1], ma[1], 0) : nz(oma[1])
upper = oma + stdev(src, length) * deviations
lower = oma - stdev(src, length) * deviations

plotcandle(oma, upper, lower, ma, color=ma>=upper?#9ce0b2:ma<=lower?#e0b29c:ma>=ma[1]?green:maroon)
https://www.tradingview.com/pine-script-reference/#fun_valuewhen
 
Solution
What is the ValueWhen (TradingView) equivalent in ThinkScript?
so I can get a value during a certain condition.
Say if I wanted to plot an arrow when an ema cross happens. Thank you!

you are asking for 2 different things

this will do both
read the comments to see how it works

this will mimics what valuewhen() does
a user picks,
..a condition
..and which condition number it is to find in the past, first, 2nd, 3rd,...

this looks for the crossing of 2 averages, and draws 2 vertical lines around the desired bar.
all of the average formulas would need to be replaced with your formulas that define your condition.

one possible issue would be if you have a chart with over 400 bars. there is a formula with a constant, 400, that may need to be changed.
# find offset to desired condition
def t = GetMaxValueOffset(( if revcntx == prevnum then revcntx else 0) , 400);


Ruby:
# valueWhen_convert_01

input nth_previous_condition = 3;
input show_vertical_lines_on_condition = yes;

def bn = barnumber();
def na = double.nan;

def lastbarbn = HighestAll(If(IsNaN(close), 0, bn));
def lastbar = if (bn == lastbarbn) then 1 else 0;

#=================================
#=================================
# test data , condition is when 2 emas cross

input avg1_len = 10;
input avg1_type =  AverageType.simple;
def ma1 = MovingAverage(avg1_type, close, avg1_len);

input avg2_len = 80;
input avg2_type =  AverageType.simple;
def ma2 = MovingAverage(avg2_type, close, avg2_len);

input show_ma_lines = yes;
plot z1 = if show_ma_lines then ma1 else na;
z1.setdefaultcolor(color.cyan);
plot z2 = if show_ma_lines then ma2 else na;
z2.setdefaultcolor(color.yellow);

def ma_cross = if ma1 crosses ma2 then 1 else 0;

plot x1 = if ma_cross then min(min(z1,z2),low)*0.997 else na;
x1.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
x1.SetDefaultColor(Color.cyan);
x1.setlineweight(3);
x1.hidebubble();

#=================================
#=================================

def cond1 = ma_cross;

# count conditions, create reverse count
def cntx = if bn == 1 then 0 else if cond1 then cntx[1] + 1 else cntx[1];
def cntx_max = highestall(cntx);
def revcntx = if cond1 then cntx_max - cntx + 1 else cntx_max - cntx + 0;

# --------------------------------

# check if desired condition number is valid
def prevnum = if nth_previous_condition > cntx_max then cntx_max else if nth_previous_condition < 1 then 1 else nth_previous_condition;

addlabel(1, "find the nth previous condition " + prevnum, color.yellow);

# --------------------------------

# find offset to desired condition
def t = GetMaxValueOffset(( if revcntx == prevnum then revcntx else 0) , 400);
def cond_cls = getvalue(close, t);

addlabel(1, "close of signal, " + t + " bars back " + cond_cls, color.yellow);

# vertical lines around condition bar
def x = if (revcntx[0] == prevnum and revcntx[-1] <> prevnum ) then 1 else 0;

# vertical lines around desired condition
addverticalline(show_vertical_lines_on_condition and (x or x[1]), "-", color.cyan);

# --------------------------------
# test stuff

input test1_cond_counts = no;
addchartbubble(test1_cond_counts, low*0.998,
cntx + "\n" +
revcntx
, color.cyan, no);

input test2_cond_bubbles = no;
addchartbubble(test2_cond_bubbles and cond1, high,
bn + "\n" +
cntx + "\n" +
revcntx
, color.cyan, yes);

input test3_cond_cls = no;
addchartbubble(test3_cond_cls, low*0.998,
 t + "\n" +
 cond_cls
, color.yellow, no);
#

when set to 3, it finds the 3rd condition in the past, back from the last bar

2U3lxXh.jpg
 
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
425 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