If I want to say the following: if A=true AND B=true then X else Y. How do I handle double conditionality of if-then statement in thinkscript?
def bn=BarNumber();
def XR;
#def XR = CompoundValue(1, if AbsValue(low - XR[1]) > AbsValue(high - XR[1]) then low else high, high);
if (bn==1) then {
XR = high;
} else {
if AbsValue(low - XR[1]) > AbsValue(high - XR[1]) then XR=low else XR=high
}
def XR = CompoundValue(1, if AbsValue(low - XR[1]) > AbsValue(high - XR[1]) then low else high, high);
plot ZIGGY = XR;
def XR;
def highdiff;
def lowdiff;
if BarNumber() == 1
{XR = hl2;
highdiff = 0;
lowdiff = 0;
}
else
{highdiff = AbsValue(high - XR[1]);
lowdiff = AbsValue(low - XR[1]);
if lowdiff < highdiff
{XR = low;}
else
{XR = high;}
}
plot XRI = XR;
def bn = BarNumber();
def XR;
if bn == 1
{XR = high;}
else
{if AbsValue(low - XR[1]) > AbsValue(high - XR[1])
{XR = low;}
else
{XR = high;}
}
plot XRI = XR;
declare lower;
declare once_per_bar;
def bn = BarNumber();
def vlow = low;
def vhigh = high;
def XR_C = CompoundValue(1, if AbsValue(low - XR_C[1]) > AbsValue(high - XR_C[1]) then low else high, high);
def XR;
def lowdiff;
def highdiff;
def c1; # condition 1
if (bn == 1) then {
lowdiff = high-low;
highdiff = 0;
XR = high;
c1 = 0;
} else {
lowdiff = AbsValue(low - XR[1]);
highdiff = AbsValue(high - XR[1]);
c1 = lowdiff > highdiff;
# strange BUG in thinkscript!
# if you use low/high below, XR is always HIGH
# using vlow and vhigh, works as expected!
if c1==1 then {
XR = vlow;
} else {
XR = vhigh;
}
}
plot ZIGGY = XR;
plot ZIGGY_C = XR_C;
AddChartBubble(
yes,
XR,
"c1:"+c1+"\nld:"+lowdiff+"\nhd:"+highdiff+"\nxr:"+XR+"\nxr_c:"+XR_C+"\nh:"+high+"\nl:"+low,
color.Gray,
yes);
This is totally messed up, and there is a serious bug in thinkscript.
if cond1 > 0 && cond2 > 0 && cond3 > 0 && cond4 > 0 && cond5 > 0 {
# plot green arrow on chart
} else {}
something like this:Hi everyone! This is probably an easy question. I'm trying to figure out how to do a multiple condition if statement. If you look at the following, I'm trying to do something like this:
Code:if cond1 > 0 && cond2 > 0 && cond3 > 0 && cond4 > 0 && cond5 > 0 { # plot green arrow on chart } else {}
How would I do this?
plot buy = if condition_1 > 0 AND condition_2 >= 0 and condition_3 > 0 then LOW else double.nan;
def a = if condition_1 > 0 then 1 else 0;
def b = if condition_2 > 0 then 1 else 0;
def c = if condition_3 > 0 then 1 else 0;
plot buy = if a + b + c == 3 then LOW else double.nan;
plot x = k1 >=(k2-10) or k1 <=(K2+10) ;I want to set up a scan study so that if a plot "k" is within -10 or +10 of another plot "k" it will return those stocks
ie
if k1 >=(k2-10) or k1 <=(K2+10), True, otherwise false
Any help is appreciated.
# Define the MACD
def macd = MACD(close, 12, 26, 9);
def macdSignal = macd.MACDSignal;
def macdHist = macd.MACDHist;
# Define the current bar
def currentBar = barNumber();
# Define the previous bar
def prevBar = currentBar - 1;
# Define the current and previous highs
def currentHigh = high[currentBar];
def prevHigh = high[prevBar];
# Define the current and previous closes
def currentClose = close[currentBar];
def prevClose = close[prevBar];
# Check if the MACD is bullish and the current high is above the previous high
if (macdHist > 0 and currentHigh > prevHigh) {
# Check if the current close is above the previous high
if (currentClose > prevHigh) {
# Plot an upward arrow and set the text to "BUY"
plotArrowUp(high + 1);
label.new(bar_index, high + 2, "BUY", color = color.green);
}
}
# Check if the MACD is bearish and the current high is below the previous high
else if (macdHist < 0 and currentHigh < prevHigh) {
# Check if the current close is below the previous high
if (currentClose < prevHigh) {
# Plot a downward arrow and set the text to "SELL"
plotArrowDown(high - 1);
label.new(bar_index, high - 2, "SELL", color = color.red);
}
}
Syntax error: An 'else' block expected at 23:3
Syntax error: Semicolon expected at 21:1
Invalid statement: } at 36:3
Hello,
I am trying to paste this script in TOS, but I am getting syntax error
Code:# Define the MACD def macd = MACD(close, 12, 26, 9); def macdSignal = macd.MACDSignal; def macdHist = macd.MACDHist; # Define the current bar def currentBar = barNumber(); # Define the previous bar def prevBar = currentBar - 1; # Define the current and previous highs def currentHigh = high[currentBar]; def prevHigh = high[prevBar]; # Define the current and previous closes def currentClose = close[currentBar]; def prevClose = close[prevBar]; # Check if the MACD is bullish and the current high is above the previous high if (macdHist > 0 and currentHigh > prevHigh) { # Check if the current close is above the previous high if (currentClose > prevHigh) { # Plot an upward arrow and set the text to "BUY" plotArrowUp(high + 1); label.new(bar_index, high + 2, "BUY", color = color.green); } } # Check if the MACD is bearish and the current high is below the previous high else if (macdHist < 0 and currentHigh < prevHigh) { # Check if the current close is below the previous high if (currentClose < prevHigh) { # Plot a downward arrow and set the text to "SELL" plotArrowDown(high - 1); label.new(bar_index, high - 2, "SELL", color = color.red); } }
Error Says
Code:Syntax error: An 'else' block expected at 23:3 Syntax error: Semicolon expected at 21:1 Invalid statement: } at 36:3
Thank you for your help.
# macd_errors_01
#https://usethinkscript.com/threads/syntax-error.13851/
#Syntax Error
# krsheath Start dateYesterday at 9:57 PM
#I am trying to paste this script in TOS, but I am getting syntax error
# Define the MACD
#---------------------
def na = double.nan;
def bn = barnumber();
# def macd = MACD(close, 12, 26, 9);
# def macdSignal = macd.macdSignal;
# def macdHist = macd.macdHist;
# macd doesn't have a price parameter , close is invalid
# better to set up inputs, if you want to pass parameters to a study
# functions and studies need () after the name , macd()
# referencing a function without the plot name will read the first plot from that study. if you want to read other plots, you need to append the plot name to the end of the study.
# underline is the only special character that can be used in variable names. i like to use it to separate words, because i think it makes it easier to read.
# if you want to read a different plot from a study, you have to supply the parameters again, to make sure you are getting the correct output.
input fast_length = 12;
input slow_length = 26;
input length = 9;
# 2 plot values aren't used, so i commented them out
#def macd_value = MACD(fast_length, slow_length, length).value;
#def macd_avg = MACD(fast_length, slow_length, length).avg;
def macd_hist = MACD(fast_length, slow_length, length).diff;
#---------------------
# Define the current bar
def currentBar = BarNumber();
# Define the previous bar
def prevBar = currentBar - 1;
#---------------------
# your offsets are wrong
# when a number is appended to a variable, in brackets, it is a relative offset to another value at another bar.
# offsets are relative, not absolute.
# abc[1] does not read from bar #1, it means go 1 bar back in time, to the previous bar and read the value.
# abc[-1] means look at the next future bar and read a value.
# an offset of 0 isn't needed, but i used it to stay consistant.
# Define the current and previous highs
#def currentHigh = high[currentBar];
#def prevHigh = high[prevBar];
# Define the current and previous closes
#def currentClose = close[currentBar];
#def prevClose = close[prevBar];
def currentHigh = high[0];
def prevHigh = high[1];
def currentClose = close[0];
def prevClose = close[1];
#---------------------
# can only assign a number or generate a true/false within an if then.
# can't create any outputs within an if then.
# can't draw shapes, or lines, or bubbles, or labels,..
# invalid functions , plotArrowUp() label.new() ,
# it looks like you are trying to use several conditions to draw a bubble and an arrow.
# since you want 2 things to happen when all the conditions are true, i would set up a variable to be equal to a formula containing the conditions. then use the variable in a plot formula and as a time parameter in a bubble.
# if a formula will be true or false, a boolean, it doesn't need to use an if then to define a value. true = 1 and false will be = to 0. i like to wrap it in parenthesis.
# can't display text on an arrow. have to use a bubble
#-------------------------
## Check if the MACD is bullish and the current high is above the previous high
#if (macdHist > 0 and currentHigh > prevHigh) {
# # Check if the current close is above the previous high
# if (currentClose > prevHigh) {
# # Plot an upward arrow and set the text to "BUY"
# plotArrowUp(high + 1);
# label.new(bar_index, high + 2, "BUY", color = Color.GREEN);
# }
#}
input show_arrows = yes;
input show_bubbles = yes;
# create a true/false formula
def up = (macd_hist > 0 and currentHigh > prevHigh and currentClose > prevHigh);
# plot an up arrow below the candle
# use a formula with a price level or nan value
# i tend to add z to a variable i am plotting.
plot zup = if show_arrows and up then (low*0.999) else na;
zup.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
zup.SetDefaultColor(Color.green);
# can pick a weight size , 1 to 5
zup.setlineweight(3);
# stop a price bubble from showing up on y axis
zup.hidebubble();
addchartbubble(show_bubbles and up, (low*0.996),
"BUY"
, color.green, no);
#-------------------------
## Check if the MACD is bearish and the current high is below the previous high
#else if (macdHist < 0 and currentHigh < prevHigh) {
# # Check if the current close is below the previous high
# if (currentClose < prevHigh) {
# # Plot a downward arrow and set the text to "SELL"
# plotArrowDown(high - 1);
# label.new(bar_index, high - 2, "SELL", color = Color.RED);
# }
#}
def dwn = (macd_hist < 0 and currentHigh < prevHigh and currentClose < prevHigh);
plot zdwn = if show_arrows and dwn then (high*1.001) else na;
zdwn.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
zdwn.SetDefaultColor(Color.red);
zdwn.setlineweight(3);
zdwn.hidebubble();
addchartbubble(show_bubbles and dwn, (high*1.004),
"SELL"
, color.red, yes);
#-------------------------
#
Join useThinkScript to post your question to a community of 21,000+ developers and traders.
Thread starter | Similar threads | Forum | Replies | Date |
---|---|---|---|---|
Simple bar color script using AddChart() not working or am I overthinking it? | Questions | 3 | ||
S | How to get the "Take Profit" script | Questions | 0 | |
G | Using upper chart from TOS script | Questions | 0 | |
D | Custom Bar Direction Script | Questions | 1 | |
Option Chain Script: Delta/Market Price displayed as percentage | Questions | 1 |
Start a new thread and receive assistance from our community.
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.
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.