I would like to achieve the following:
- Check last fully closed 5min candle and compare "close" value with multiple SMA values at that time.
- Check last 1min candle from the fully closed 5min candle and compare "close" value with multiple SMA values at that time.
- Add this indicator in the 5min timeframe.
Example: it's 9:37am. Check "close" value of the 9:30am 5min candle and compare with several SMA values from the 9:30am 5min candle. Check "close" value of the 9:34 1min candle and compare with several SMA values from the 9:34am 1min candle.
I've created this code but it has the following issues that i've detected so far:
- It check the last 1min and 5min candle, not the last fully closed. In the example above, it checks the 9:35am 5min candle and the 9:37am 1min candle.
- Sometimes it doesn't update the result and as soon as I reload the chart/tab, it shows the result it should. Not sure why.
Could anybody help me transform my code into what I need? Thanks
// © AdrianInf
//@version=5
indicator("Uptrend/Downtrend", "Uptrend/Downtrend", true, scale = scale.none, max_bars_back=5000)
var string GP1 = "Display"
txt_sizeopt = input(title="Text Size", defval="large", inline = "10")
txt_size = (txt_sizeopt == "auto") ? size.auto : (txt_sizeopt == "tiny") ? size.tiny : (txt_sizeopt == "small") ? size.small : (txt_sizeopt == "normal") ? size.normal : (txt_sizeopt == "large") ? size.large : size.huge
// Table bottom left
string i_tableYpos = input("bottom", "Panel position", inline = "11")
string i_tableXpos = input("right", "", inline = "11")
var table TableDisplay = table.new(i_tableYpos + "_" + i_tableXpos, 2, 2, border_width = 0, border_color=color.white)
uptrend = false
uptrend_1min = false
uptrend_5min = false
text_to_show_1min = string(na)
text_to_show_5min = string(na)
// 1 minute
_9SMA = ta.sma(close, 9)
_9EMA = ta.ema(close, 9)
_13EMA = ta.ema(close, 13)
_20SMA = ta.sma(close, 20)
_50SMA = ta.sma(close, 50)
_VWAP = ta.vwap(close)
array<float> last_1_array = request.security_lower_tf(syminfo.tickerid, "1", close)
var float last_1_close = na
array_size = array.size(last_1_array)
last_1_close := array_size ? array.get(last_1_array, array_size-1) : na
array<float> last_1_array_9SMA = request.security_lower_tf(syminfo.tickerid, "1", _9SMA)
var float last_1_9SMA = na
array_size_9SMA = array.size(last_1_array_9SMA)
last_1_9SMA := array_size_9SMA ? array.get(last_1_array_9SMA, array_size_9SMA-1) : na
array<float> last_1_array_9EMA = request.security_lower_tf(syminfo.tickerid, "1", _9EMA)
var float last_1_9EMA = na
array_size_9EMA = array.size(last_1_array_9EMA)
last_1_9EMA := array_size_9EMA ? array.get(last_1_array_9EMA, array_size_9EMA-1) : na
array<float> last_1_array_13EMA = request.security_lower_tf(syminfo.tickerid, "1", _13EMA)
var float last_1_13EMA = na
array_size_13EMA = array.size(last_1_array_13EMA)
last_1_13EMA := array_size_13EMA ? array.get(last_1_array_13EMA, array_size_13EMA-1) : na
array<float> last_1_array_20SMA = request.security_lower_tf(syminfo.tickerid, "1", _20SMA)
var float last_1_20SMA = na
array_size_20SMA = array.size(last_1_array_20SMA)
last_1_20SMA := array_size_20SMA ? array.get(last_1_array_20SMA, array_size_20SMA-1) : na
array<float> last_1_array_50SMA = request.security_lower_tf(syminfo.tickerid, "1", _50SMA)
var float last_1_50SMA = na
array_size_50SMA = array.size(last_1_array_50SMA)
last_1_50SMA := array_size_50SMA ? array.get(last_1_array_50SMA, array_size_50SMA-1) : na
array<float> last_1_array_VWAP = request.security_lower_tf(syminfo.tickerid, "1", _VWAP)
var float last_1_VWAP = na
array_size_VWAP = array.size(last_1_array_VWAP)
last_1_VWAP := array_size_VWAP ? array.get(last_1_array_VWAP, array_size_VWAP-1) : na
if barstate.islast and (last_1_close > last_1_9SMA and last_1_close > last_1_9EMA and last_1_close > last_1_13EMA and last_1_close > last_1_20SMA and last_1_close > last_1_50SMA)
uptrend_1min := true
text_to_show_1min := "Uptrend 1min"
if (last_1_close > last_1_VWAP)
text_to_show_1min := "Uptrend 1min > VWAP"
if uptrend_1min
table.cell(TableDisplay, 0, 0, text_to_show_1min, bgcolor=color.white, text_size=txt_size)
//plot ((last_1_close > last_1_9SMA and last_1_close > last_1_50SMA) ? last_1_close : na, style=plot.style_circles, linewidth=2)
// 5 minute
_close_5min = close
_9SMA_5min = ta.sma(close, 9)
Calculated_9SMA = request.security(syminfo.tickerid, "5", _9SMA_5min)
_20SMA_5min = ta.sma(close, 20)
Calculated_20SMA = request.security(syminfo.tickerid, "5", _20SMA_5min)
_VWAP_5min = ta.vwap(close)
Calculated_VWAP = request.security(syminfo.tickerid, "5", _VWAP_5min)
if barstate.islast and (_close_5min > Calculated_9SMA and _close_5min > Calculated_20SMA)
uptrend_5min := true
text_to_show_5min := "Uptrend 5min"
if (_close_5min > _VWAP_5min)
text_to_show_5min := "Uptrend 5min > VWAP"
if uptrend_5min
table.cell(TableDisplay, 0, 1, text_to_show_5min, bgcolor=color.white, text_size=txt_size)