Reputation: 59
UPdate: I figured out how to create a velocity/ror indicator but I cannot get the rate of change to show negative values when it moves down instead of up. any help with this?? Original post: I am trying to create a pinescript indicator that will have a startdate input to begin a calculation from a certain historical past day of a stock chart. Then it will calculate the ROC from this start date to each new day, and divide that roc by the total number of days that have passed. example where start date=x /// (ROC between x and day2 is 2%)/1 --one day has passed--) //// (ROC between x and day 3 is 4.2%)/2 --2 days have passed-- /// (ROC between x and day 4 is 6.1%)/3 --3 days have passed--. It then plots this as on oscillator. Thanks for any help with this!
//@version=4
study("velocity", shorttitle="vel", overlay=true)
timeYear = input(2022, title="Year", minval=1991, maxval=2100, type=input.integer)
timeMonth = input(1, title="Month", minval=1, maxval=12, type=input.integer)
timeDay = input(04, title="Day", minval=1, maxval=31, type=input.integer)
timeHours = input(9, title="Hours", minval=0, maxval=23, type=input.integer)
timeMinutes = input(30, title="Minutes", minval=0, maxval=59, type=input.integer)
timeSeconds = input(0, title="Seconds", minval=0, maxval=59, type=input.integer)
// Initilization of variables only once
var delta = 0
// start time at 0 from a particular time interval
if(year == timeYear and month == timeMonth and dayofmonth == timeDay and hour == timeHours and minute == timeMinutes and second == timeSeconds)
delta := 0
// Count number of bars
if(year >= timeYear and month >= timeMonth and dayofmonth > timeDay)
delta += 1
plotchar(delta, title="days passed from startdate", color=color.green, char='')
// set to TOP so it doesnt mess up chart scale
delta0 = delta-delta
// calculate Cumulative roc from start date to every continuous new bar
length = input(1, minval=1)
source = input(close, "Source")
roc = 100 * (source[delta] - source[delta0])/source[delta]
plotchar(roc, color=#2962FF, title="cum-ROC", char='')
// divide Cumulative ROC by # of days that have passed
ror = roc/delta
plot(ror, color=color.yellow, title="velocity/ROR")
Upvotes: 0
Views: 460
Reputation: 865
So you're having trouble calculating the number of days between two timestamps?
You needed to know about the timenow variable :) https://www.tradingview.com/pine-script-reference/v5/#var_timenow
//@version=5
indicator("velocity",overlay=true)
i_startdate = input.time(timestamp("04 Oct 2022 09:30 +0000"), "start date")
// (1000*60*60*24) is for getting the nb of days from a timestamp value
delta = (timenow - i_startdate) / (1000*60*60*24)
plot(delta)
Upvotes: 0