If, (When), Then statement

TigerWares

New member
VIP
Need some help with an IF, WHEN, THEN statement...

If LINE crosses above 0 and (When) LINE > 1 then 0 else Double.Nan;

...so that when the histogram crosses above 0, an arrow plots but not UNTIL the histogram is greater than 1. It may be the second or third histogram bar

I've done the below but what is happening is if the first plot of the histogram is not > 1, then no arrow plots even if the next bar is >1. An arrow only plots if THE FIRST bar is greater than 1

= if Line crosses above 0 and Line > 1 then 0 else Double.NaN ;

I've tried
if LINE > 1 then 0 else Double.NAN;

but that plots arrows on ALL bars >1. I just want the first bar >1 to have an arrow regardless of how many after are > 1.
 
Last edited:
Solution
Need some help with an IF, WHEN, THEN statement...

If LINE crosses above 0 and (When) LINE > 1 then 0 else Double.Nan;

...so that when the histogram crosses above 0, an arrow plots but not UNTIL the histogram is greater than 1. It may be the second or third histogram bar

I've done the below but what is happening is if the first plot of the histogram is not > 1, then no arrow plots even if the next bar is >1. An arrow only plots if THE FIRST bar is greater than 1

= if Line crosses above 0 and Line > 1 then 0 else Double.NaN ;

I've tried
if LINE > 1 then 0 else Double.NAN;

but that plots arrows on ALL bars >1. I just want the first bar >1 to have an arrow regardless of how many after are > 1. hal_seq


you didn't...
Need some help with an IF, WHEN, THEN statement...

If LINE crosses above 0 and (When) LINE > 1 then 0 else Double.Nan;

...so that when the histogram crosses above 0, an arrow plots but not UNTIL the histogram is greater than 1. It may be the second or third histogram bar

I've done the below but what is happening is if the first plot of the histogram is not > 1, then no arrow plots even if the next bar is >1. An arrow only plots if THE FIRST bar is greater than 1

= if Line crosses above 0 and Line > 1 then 0 else Double.NaN ;

I've tried
if LINE > 1 then 0 else Double.NAN;

but that plots arrows on ALL bars >1. I just want the first bar >1 to have an arrow regardless of how many after are > 1. hal_seq


you didn't provide a full study , no description of what defines line.
so i had to create test data.

this uses 2 averages, and plots the difference of them.
when they cross is 1 condition.
when their difference is >= some number, is another condition.

this study waits for one condition.
after it happens, then it waits for another condition.
when the 2nd happens, a white histogram bar is plotted, and an arrow.

Code:
#cross_above_more_than1_lower_00

#https://usethinkscript.com/threads/if-when-then-statement.14511/
#...so that when the histogram crosses above 0, an arrow plots but not UNTIL the histogram is greater than 1. It may be the second or third histogram bar

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

declare lower;

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

#/////////////////////////////////
# test data
# difference of 2 avgs

input avg1_type = AverageType.SIMPLE;
input avg1_price = close;
input avg1_len = 9;
def ma1 = MovingAverage(avg1_type, avg1_price, avg1_len);

input avg2_type = AverageType.SIMPLE;
input avg2_price = close;
input avg2_len = 50;
def ma2 = MovingAverage(avg2_type, avg2_price, avg2_len);

def diff = ma1 - ma2;

#input show_test_averages = no;
#plot t1 = if show_test_averages then ma1 else na;
#plot t2 = if show_test_averages then ma2 else na;

# end test data
#/////////////////////////////////


#def signal1 = line;
def signal1 = diff;
#def signal2 = ....
#def signal3 = ...
#------------------------

input min_difference = 1.0;

# conditions , sequencial advancing , one after the other
def cond1 = signal1 crosses above 0;
def cond2 = signal1 crosses below 0;

def cond3 = signal1 > min_difference;
def cond4 = signal1 < -min_difference;

# conditions , cancel
#def cancel = signal1 crosses below 0;
def cancel1 = cond2;
def cancel2 = cond1;

# create vars, that count up, with each new condition (0,1,2)
def tup =
 if bn == 1 then 0
 else if cancel1 then 0
 else if tup[1] == 0 and cond1 and cond3 then 2
 else if tup[1] == 0 and cond1 then 1
 else if tup[1] == 1 and cond3 then 2
 else tup[1];
def trigger1 = (tup == 2 and tup[1] != 2);


def tdwn =
 if bn == 1 then 0
 else if cancel2 then 0
 else if tdwn[1] == 0 and cond2 and cond4 then 2
 else if tdwn[1] == 0 and cond2 then 1
 else if tdwn[1] == 1 and cond4 then 2
 else tdwn[1];
def trigger2 = (tdwn == 2 and tdwn[1] != 2);

DefineGlobalColor(color = Color.white, name = "trig_bar");

plot z = if bn == 1 then na else diff;
z.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
z.AssignValueColor(if bn == 1 then color.gray else if trigger1 or trigger2 then GlobalColor("trig_bar") else if diff > 0 then color.green else color.red);
z.SetLineWeight(3);
z.HideBubble();

plot zup = if trigger1 then -0.1 else na;
zup.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
zup.SetDefaultColor(Color.CYAN);
zup.SetLineWeight(2);
zup.HideBubble();

plot zdwn = if trigger2 then 0.1 else na;
zdwn.SetPaintingStrategy(PaintingStrategy.ARROW_down);
zdwn.SetDefaultColor(Color.CYAN);
zdwn.SetLineWeight(2);
zdwn.HideBubble();


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

input test_vert_lines = no;
addverticalline(test_vert_lines and (cond1 or cond3), "cond 1", Color.CYAN);
addverticalline(test_vert_lines and (cond2 or cond4), "cond 2", Color.yellow);
#

Vy9ElUC.jpg
 
Last edited:
Solution

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

Ah...That is something like the MACD, yes? I should have thought of that. I guess I could have taken that MACD code and instead of saying when it crosses above 0, could have put crosses 1. Something like that

Thank you very much
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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