I’ve used chatgpt to help develop the following script which evaluates 5 tickers and their respective timeframes to see if they are all bullish or bearish.
If either are true, it’s supposed to plot a green arrow below or red arrow above the candle on the chart.
Additionally, once it plots the arrow, it won’t plot a similar arrow until the opposite arrow is plotted (eg, if a green arrow was plotted, further green arrows are ignored until a red arrow is first plotted).
All the above works except: 1) Live and Replay display different plot locations. The replay shows the correct plots.
//@version=5
indicator("Directional Bias with Candle Coloring", overlay=true)
// Define start and end time in milliseconds
start_time = timestamp("GMT-7", year, month, dayofmonth, 6, 30)
end_time = timestamp("GMT-7", year, month, dayofmonth, 13, 0)
// Check if the current time is within the specified range
in_session = (time >= start_time) and (time <= end_time)
// Checkbox input for candle coloring toggle
color_candles = input.bool(true, "Color Candles Between 6:30 AM and 1:00 PM PT")
// Input for label colors
bullish_label_color = input.color(color.green, "Bullish Label Color")
bearish_label_color = input.color(color.red, "Bearish Label Color")
// Checkbox inputs for each ticker
show_ticker1 = input.bool(true, "", inline="1")
show_ticker2 = input.bool(true, "", inline="2")
show_ticker3 = input.bool(true, "", inline="3")
show_ticker4 = input.bool(true, "", inline="4")
show_ticker5 = input.bool(true, "", inline="5")
// Input tickers and timeframes with default values
ticker1 = input.symbol("SPY", "", inline="1")
ticker2 = input.symbol("SPY", "", inline="2")
ticker3 = input.symbol("NVDA", "", inline="3")
ticker4 = input.symbol("AAPL", "", inline="4")
ticker5 = input.symbol("ADD", "", inline="5")
timeframe1 = input.timeframe("3", "Timeframe:", inline="1")
timeframe2 = input.timeframe("5", "Timeframe:", inline="2")
timeframe3 = input.timeframe("3", "Timeframe:", inline="3")
timeframe4 = input.timeframe("5", "Timeframe:", inline="4")
timeframe5 = input.timeframe("1", "Timeframe:", inline="5")
// Function to check if the close is greater than the open for a given ticker and timeframe
is_bullish(ticker, timeframe) =>
request.security(ticker, timeframe, close) > request.security(ticker, timeframe, open)
// Function to check if the close is less than the open for a given ticker and timeframe
is_bearish(ticker, timeframe) =>
request.security(ticker, timeframe, close) < request.security(ticker, timeframe, open)
// Create a table
var table myTable = table.new(position.top_right, 6, 3, border_width = 1)
// Update the table with ticker information
if (bar_index == 0)
table.cell(myTable, 0, 0, "Ticker", bgcolor=color.gray, text_color=color.white)
table.cell(myTable, 0, 1, "Timeframe", bgcolor=color.gray, text_color=color.white)
table.cell(myTable, 0, 2, "Status", bgcolor=color.gray, text_color=color.white)
if (show_ticker1)
status_ticker1 = is_bullish(ticker1, timeframe1) ? "▲" : "▼"
table.cell(myTable, 1, 0, "SPY", text_color=color.white)
table.cell(myTable, 1, 1, timeframe1, text_color=color.white)
table.cell(myTable, 1, 2, status_ticker1, bgcolor=is_bullish(ticker1, timeframe1) ? color.green : color.red, text_color=color.white)
if (show_ticker2)
status_ticker2 = is_bullish(ticker2, timeframe2) ? "▲" : "▼"
table.cell(myTable, 2, 0, "SPY", text_color=color.white)
table.cell(myTable, 2, 1, timeframe2, text_color=color.white)
table.cell(myTable, 2, 2, status_ticker2, bgcolor=is_bullish(ticker2, timeframe2) ? color.green : color.red, text_color=color.white)
if (show_ticker3)
status_ticker3 = is_bullish(ticker3, timeframe3) ? "▲" : "▼"
table.cell(myTable, 3, 0, "NVDA", text_color=color.white)
table.cell(myTable, 3, 1, timeframe3, text_color=color.white)
table.cell(myTable, 3, 2, status_ticker3, bgcolor=is_bullish(ticker3, timeframe3) ? color.green : color.red, text_color=color.white)
if (show_ticker4)
status_ticker4 = is_bullish(ticker4, timeframe4) ? "▲" : "▼"
table.cell(myTable, 4, 0, "AAPL", text_color=color.white)
table.cell(myTable, 4, 1, timeframe4, text_color=color.white)
table.cell(myTable, 4, 2, status_ticker4, bgcolor=is_bullish(ticker4, timeframe4) ? color.green : color.red, text_color=color.white)
if (show_ticker5)
status_ticker5 = is_bullish(ticker5, timeframe5) ? "▲" : "▼"
table.cell(myTable, 5, 0, "ADD", text_color=color.white)
table.cell(myTable, 5, 1, timeframe5, text_color=color.white)
table.cell(myTable, 5, 2, status_ticker5, bgcolor=is_bullish(ticker5, timeframe5) ? color.green : color.red, text_color=color.white)
// Check if all tickers are bullish or bearish
all_bullish = (show_ticker1 and is_bullish(ticker1, timeframe1)) and (show_ticker2 and is_bullish(ticker2, timeframe2)) and (show_ticker3 and is_bullish(ticker3, timeframe3)) and (show_ticker4 and is_bullish(ticker4, timeframe4)) and (show_ticker5 and is_bullish(ticker5, timeframe5))
all_bearish = (show_ticker1 and is_bearish(ticker1, timeframe1)) and (show_ticker2 and is_bearish(ticker2, timeframe2)) and (show_ticker3 and is_bearish(ticker3, timeframe3)) and (show_ticker4 and is_bearish(ticker4, timeframe4)) and (show_ticker5 and is_bearish(ticker5, timeframe5))
// Variables to track the state
var bool has_bullish_been_plotted = false
var bool has_bearish_been_plotted = false
var color current_candle_color = na
// Reset the states if both conditions have been plotted
if (has_bullish_been_plotted and has_bearish_been_plotted)
has_bullish_been_plotted := false
has_bearish_been_plotted := false
// Plot green arrow below the current candle if all tickers are bullish and this is the first instance
if (all_bullish and not has_bullish_been_plotted and not has_bearish_been_plotted)
label.new(x=bar_index, y=low, yloc=yloc.belowbar, text="", color=bullish_label_color, textcolor=color.white, style=label.style_triangleup, size=size.tiny)
has_bullish_been_plotted := true
current_candle_color := color.new(color.green, 0)
// Plot red arrow above the current candle if all tickers are bearish and this is the first instance
if (all_bearish and has_bullish_been_plotted and not has_bearish_been_plotted)
label.new(x=bar_index, y=high, yloc=yloc.abovebar, text="", color=bearish_label_color, textcolor=color.white, style=label.style_triangledown, size=size.tiny)
has_bearish_been_plotted := true
current_candle_color := color.new(color.red, 0)
// Color the candles only within the specified time range and if the color toggle is enabled
plotcandle(color_candles and in_session ? open : na, color_candles and in_session ? high : na, color_candles and in_session ? low : na, color_candles and in_session ? close : na, color=color_candles and in_session ? current_candle_color : na, bordercolor=color_candles and in_session ? current_candle_color : na, wickcolor=color_candles and in_session ? current_candle_color : na)
Plot correctly so it matches in replay and live
Del Laird is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1