I am confuse with [1] else 0 in if and else statement with my VWAP Code.

Stoynks

Member
Code:
def x = if close crosses above VWAP then 1 else x[1];
plot alert = if x == 1 then 1 else 0;

This code is great when finding out the stock is is crossing vwap. My only problem is instead of showing 0, it is showing NaN. How do I add a false statement and x[1] together?
is it like this?

Code:
if close crosses above VWAP then 1 else 0 and x[1]

that way my plot alert triggers the 0 instead of NaN

I also tried:

Code:
plot alert = if x == 1 then 1 else if x!= 1 then 0 else 0;
Still not Working

Please Help. Very Much Appreciated.
 
Solution
Code:
def x = if close crosses above VWAP then 1 else x[1];
plot alert = if x == 1 then 1 else 0;

This code is great when finding out the stock is is crossing vwap. My only problem is instead of showing 0, it is showing NaN. How do I add a false statement and x[1] together?
is it like this?

Code:
if close crosses above VWAP then 1 else 0 and x[1]

that way my plot alert triggers the 0 instead of NaN

I also tried:

Code:
plot alert = if x == 1 then 1 else if x!= 1 then 0 else 0;
Still not Working

Please Help. Very Much Appreciated.

I am not sure why you want to use x[1]. The following code will plot 1/0 for all bars. You could have just eliminated def x and just used plot alert = if close crosses above reference VWAP...
Code:
def x = if close crosses above VWAP then 1 else x[1];
plot alert = if x == 1 then 1 else 0;

This code is great when finding out the stock is is crossing vwap. My only problem is instead of showing 0, it is showing NaN. How do I add a false statement and x[1] together?
is it like this?

Code:
if close crosses above VWAP then 1 else 0 and x[1]

that way my plot alert triggers the 0 instead of NaN

I also tried:

Code:
plot alert = if x == 1 then 1 else if x!= 1 then 0 else 0;
Still not Working

Please Help. Very Much Appreciated.

I am not sure why you want to use x[1]. The following code will plot 1/0 for all bars. You could have just eliminated def x and just used plot alert = if close crosses above reference VWAP then 1 else 0;

Also, if you want the VWAP indicator you need to use 'reference VWAP', instead of just VWAP, which is a price type, like high/low/close/etc.

Code:
def x = if close crosses above reference VWAP then 1 else 0;
plot v= reference vwap;
#plot alert = if x == 1 then 1 else if x!= 1 then 0 else 0;
plot alert = x;
alert.setpaintingStrategy(paintingStrategy.VALUES_BELOW);
 
Solution
I am not sure why you want to use x[1]. The following code will plot 1/0 for all bars. You could have just eliminated def x and just used plot alert = if close crosses above reference VWAP then 1 else 0;

Also, if you want the VWAP indicator you need to use 'reference VWAP', instead of just VWAP, which is a price type, like high/low/close/etc.

Thank you but I am not trying indicate or referencing vwap. The reason for the x[1] is because it stays true when close crosses vwap but if there is no x[1] then close crosses vwap again it's going to be going from 1 to 0 back and forth. I want it to stay once for the first time it crosses vwap. It's my indication of bullish in my column watchlist. I just want to know how to combine [1] and 0 with this code:

Code:
if close crosses above VWAP then 1 else x[1];
if close crosses above VWAP then 1 else 0;

How do I combine this together? how do I make it true the first time it crosses which will be 1 in my plot and at the same time if I want it still false, instead of having NaN in the plot, I would have it to 0? I hope I am explaining this correctly but basically I want it stay 0 until close crosses vwap then it will stay 1 indefinitely. Right now before it crosses vwap, it's NaN instead of 0. how do I change it to 0? Sorry if I can't fully explain it.
 
Thank you but I am not trying indicate or referencing vwap. The reason for the x[1] is because it stays true when close crosses vwap but if there is no x[1] then close crosses vwap again it's going to be going from 1 to 0 back and forth. I want it to stay once for the first time it crosses vwap. It's my indication of bullish in my column watchlist. I just want to know how to combine [1] and 0 with this code:

Code:
if close crosses above VWAP then 1 else x[1];
if close crosses above VWAP then 1 else 0;

How do I combine this together? how do I make it true the first time it crosses which will be 1 in my plot and at the same time if I want it still false, instead of having NaN in the plot, I would have it to 0? I hope I am explaining this correctly but basically I want it stay 0 until close crosses vwap then it will stay 1 indefinitely. Right now before it crosses vwap, it's NaN instead of 0. how do I change it to 0? Sorry if I can't fully explain it.

Here is both pricetype vwap in white and indicator type vwap in cyan. By default x is looking for a cross of the pricetype where x becomes 1. At the start of the next day x is 0 until it is crossed.

Screenshot-2022-10-27-124854.png
Ruby:
plot vwap_indicator = reference vwap;
vwap_indicator.setdefaultColor(color.cyan);
plot vwap_price = vwap;
vwap_price.setdefaultColor(color.white);
vwap_price.setlineWeight(4);

def x = if getday()!= getday()[1]
        then 0
        else if close crosses above vwap_price
        then 1
        else x[1];

plot alert = x;
alert.setpaintingStrategy(paintingStrategy.VALUES_BELOW);
 
Okay, wonderful!!! That's the code that I exactly wanted. Thank you so much. I tried this on the chart but on the watchlist, I am still get NaN. Am I doing something wrong?
 
Okay, wonderful!!! That's the code that I exactly wanted. Thank you so much. I tried this on the chart but on the watchlist, I am still get NaN. Am I doing something wrong?

On a watchlist you can only have one plot statement. So the plots of the lines and associated look and feel settings have to be commented out.

Try this. It will plot 1 or 0.

Ruby:
def vwap_indicator = reference vwap;
#vwap_indicator.setdefaultColor(color.cyan);
def vwap_price = vwap;
#vwap_price.setdefaultColor(color.white);
#vwap_price.setlineWeight(4);

def x = if getday()!= getday()[1]
        then 0
        else if close crosses above vwap_price
        then 1
        else x[1];

plot alert = x;
alert.setpaintingStrategy(paintingStrategy.VALUES_BELOW);
.
 
Okay, thank you!! I will let you know if it works tomorrow when market is open. Right now everything is true since it's after hours which is not accurate.

If I want to set an alert with this study I would just do, for example x != 1 and close < 14.00;

Code:
def vwap_indicator = reference vwap;
#vwap_indicator.setdefaultColor(color.cyan);
def vwap_price = vwap;
#vwap_price.setdefaultColor(color.white);
#vwap_price.setlineWeight(4);

def x = if getday()!= getday()[1]
        then 0
        else if close crosses above vwap_price
        then 1
        else x[1];

Plot alert = if x != 1 and close < 14.00 then 1 else 0;

Will this trigger?
x != 1 meaning it still haven't crossed vwap and close is less than $14.00? or do I have to use x == 0 instead of x != 1?
 
Okay, thank you!! I will let you know if it works tomorrow when market is open. Right now everything is true since it's after hours which is not accurate.

If I want to set an alert with this study I would just do, for example x != 1 and close < 14.00;

Code:
def vwap_indicator = reference vwap;
#vwap_indicator.setdefaultColor(color.cyan);
def vwap_price = vwap;
#vwap_price.setdefaultColor(color.white);
#vwap_price.setlineWeight(4);

def x = if getday()!= getday()[1]
        then 0
        else if close crosses above vwap_price
        then 1
        else x[1];

Plot alert = if x != 1 and close < 14.00 then 1 else 0;

Will this trigger?
x != 1 meaning it still haven't crossed vwap and close is less than $14.00? or do I have to use x == 0 instead of x != 1?

This will sound an alert using the alert() function. Since your wish is to use the pricetype VWAP which is closely related to pricetype HLC3, which is closely related to close, the alert will sound in almost every instance I have seen in the early permarket hours. Since you want it to stay 1 thereafter, the alert will likely sound when you are not available. I am not sure this is really what you want.

Ruby:
plot vwap_indicator = reference vwap;
vwap_indicator.setdefaultColor(color.cyan);
plot vwap_price = vwap;
vwap_price.setdefaultColor(color.white);
vwap_price.setlineWeight(4);

def x = if getday()!= getday()[1]
        then 0
        else if close crosses above vwap_price
        then 1
        else x[1];


Plot alert = if x[1]==0 and x == 1 and close < 14 then 1 else 0;
alert.setpaintingStrategy(paintingStrategy.VALUES_BELOW);
alert(alert,"VWAP CROSSED", alert.bar, sound.chimes);
 

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
596 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