'How to draw a line.new from start and end of month and week in pinescript
Need Help in pine script to draw a line.new from start to end of the month as well as week. My calculation for the start of the month week is not working well. Help me here.
enter code here
todaystart = timestamp(y, m, d, 00, 00)
start = if pivottimeframe == 'D'
todaystart
else if pivottimeframe == 'W'
todaystart - (dayofweek(time)) * 86400000
else if pivottimeframe == 'M'
todaystart - (dayofmonth(time)) * 86400000
else
todaystart
end = if pivottimeframe == 'D'
start + 86400000
else if pivottimeframe == 'W'
start + 86400000 * 7
else if pivottimeframe == 'M'
start + 86400000 * 30
else
start + 86400000
Solution 1:[1]
Instead of adding fixed amount of milliseconds, which could vary from month to month, use the request.security() function call with the time and time_close variables from the monthly chart.
The script below plots a horizontal line at the level of 10(you haven't specified y-axis location) from the beginning of the current month till the next one, it updates on the monthly change and moves the line. Same logic for the weekly, daily etc.
//@version=5
indicator("Monthly horizontal line", overlay = false)
[monthStart, monthEnd] = request.security(syminfo.tickerid, 'M', [time, time_close])
var line myLine = line.new(monthStart, 10, monthEnd, 10, xloc = xloc.bar_time)
if ta.change(monthStart)
line.set_x1(myLine, monthStart)
line.set_x2(myLine, monthEnd)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | e2e4 |
