# 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);
#-------------------------
#