I created a short script that plots Moving Averages on all timeframes - specifically it draws Daily Moving Averages on intraday charts. I want to add to the script vertical lines in the same color as the Moving Averages - the vertical lines should be offset by number of days as the MAs are calculated with. I wrote such a code to show the MAs:
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// Shows the Daily Simple Moving Averages on all timeframes.
// This will include the 200, 150, 50, 20 and 10 DMAs automatically on the intraday charts.
// Daily SMA time periods can be adjusted in the settings.
// Based on the following script: https://www.tradingview.com/script/lzOi0ws5-Daily-Moving-Average-to-Intraday-Chart/
// and https://www.tradingview.com/script/NxVJbviK-Daily-Moving-Averages-on-Intraday-Chart/
// and https://www.tradingview.com/script/Xvf0HO0w-5-Moving-Averages-SMA-EMA/
//@version=4
study("5 Daily Moving Averages on all Timeframes", shorttitle="5 DMAs on all TFs", overlay=true)
// Take input for which daily SMAs to use
ma1Len = input(200, "MA1 length", minval=1)
ma1Type = input(title="MA1 Type", defval="SMA", options=["SMA", "EMA"])
ma2Len = input(150, "MA2 length", minval=1)
ma2Type = input(title="MA2 Type", defval="SMA", options=["SMA", "EMA"])
ma3Len = input(50, "MA3 length", minval=1)
ma3Type = input(title="MA3 Type", defval="SMA", options=["SMA", "EMA"])
ma4Len = input(20, "MA4 length", minval=1)
ma4Type = input(title="MA4 Type", defval="SMA", options=["SMA", "EMA"])
ma5Len = input(10, "MA5 length", minval=1)
ma5Type = input(title="MA5 Type", defval="SMA", options=["SMA", "EMA"])
// Evaluate SMAs
ma1 = ma1Type == "SMA" ? sma(close, ma1Len) : ema(close, ma1Len)
ma2 = ma2Type == "SMA" ? sma(close, ma2Len) : ema(close, ma2Len)
ma3 = ma3Type == "SMA" ? sma(close, ma3Len) : ema(close, ma3Len)
ma4 = ma4Type == "SMA" ? sma(close, ma4Len) : ema(close, ma4Len)
ma5 = ma5Type == "SMA" ? sma(close, ma5Len) : ema(close, ma5Len)
// Grab the Daily SMAs for the ticker
plotMA1 = security(syminfo.tickerid, 'D', ma1)
plotMA2 = security(syminfo.tickerid, 'D', ma2)
plotMA3 = security(syminfo.tickerid, 'D', ma3)
plotMA4 = security(syminfo.tickerid, 'D', ma4)
plotMA5 = security(syminfo.tickerid, 'D', ma5)
// Plot the SMAs
plot(plotMA1, linewidth=1, color=#9e8036)
plot(plotMA2, linewidth=1, color=#434651)
plot(plotMA3, linewidth=1, color=#c9333f)
plot(plotMA4, linewidth=1, color=#f793b1)
plot(plotMA5, linewidth=1, color=#6a00ff)
I asked ChatGPT to add the vertical lines - it succeeded with adding vertical lines, but they are offset by number of bars and it's not correct on intraday charts. The code it proposed was:
// Function to plot vertical lines
draw_vertical_line(offset, color) =>
var line vline = na
if bar_index >= offset
line.delete(vline)
vline := line.new(bar_index[offset], low, bar_index[offset], high, color=color, width=1)
// Plot vertical lines for each SMA
draw_vertical_line(ma1Len, #9e8036)
draw_vertical_line(ma2Len, #434651)
draw_vertical_line(ma3Len, #c9333f)
draw_vertical_line(ma4Len, #f793b1)
draw_vertical_line(ma5Len, #6a00ff)
I asked him to offset it by days and not bars but the code it returned did not compile correctly:
// Function to plot vertical lines
draw_vertical_line(offset, color) =>
var line vline = na
if bar_index >= offset
line.delete(vline)
vline := line.new(bar_index[offset], low, bar_index[offset], high, color=color, width=1)
// Calculate bars offset for each moving average length
ma1_bars_offset = bars_offset(ma1Len)
ma2_bars_offset = bars_offset(ma2Len)
ma3_bars_offset = bars_offset(ma3Len)
ma4_bars_offset = bars_offset(ma4Len)
ma5_bars_offset = bars_offset(ma5Len)
// Plot vertical lines for each SMA with days offset
draw_vertical_line(ma1_bars_offset, #9e8036)
draw_vertical_line(ma2_bars_offset, #434651)
draw_vertical_line(ma3_bars_offset, #c9333f)
draw_vertical_line(ma4_bars_offset, #f793b1)
draw_vertical_line(ma5_bars_offset, #6a00ff)