Page 1 of 1

Timeframe format - easy read

Posted: Mon Apr 17, 2023 11:05 am
by theGame
Hello everyone,

This is my first post here and I'm happy that I found this forum :happy2:

My coding knowledge is not that good and what I'm doing mostly is searching for indicators that would have the results that I want and try to combine the lines with other indicators so it results in something that I'm looking for.

I am looking for some help regarding the timeframe format. Anyone know what lines I should have in the following code in order to see the timeframe format similar to the instrument "Measure" (1d 10h or 1d only or 3h only) ?
Currently the format is "7.33" for 7d 4h (if I use Measure instrument) and I don't know how to round it.

Code: Select all

above_since = (time - ta.valuewhen(ta.crossover(close, sma200), time, 0))/1000/3600/24 
	below_since = (time - ta.valuewhen(ta.crossunder(close, sma200), time, 0))/1000/3600/24

	cond =  math.round(close, 5) > math.round(sma200, 5)

	string above_below_string=close < sma200 ? "d/h/  BELOW" : "d/h/  ABOVE"
	
var ma_table = table.new(position.top_right, columns = 10, rows =24, bgcolor =color.new(color.gray, 74), border_width = 1)
table.cell(table_id = ma_table, text_color = color.white,  column = 2, row = 2,text = str.tostring(math.round((cond? above_since:below_since),2)) +'  ' + above_below_string, bgcolor=cond ?  color.green : color.red, text_size = size.normal)

I found a script that has the outcome that I would like but I'm not that good to take the exact lines and add them in my indicator. In the labels of this script we can see the timeframe as "20.5 hours" or "4.6 days"
https://www.tradingview.com/v/9NXf1Bqb/ - Zendog V2 backtest DCA bot 3commas

Whatever solution anyone suggest that could improve the timeframe format I will accept, I just posted an example so maybe it could be easier to understand :happy2:
Thank you in advance!

Re: Timeframe format - easy read

Posted: Tue Apr 18, 2023 7:26 am
by Steve Burman
Here's an example of code you can insert. The first 2 lines are just using example time frames so if you already have a timeframe difference in your code it will be the value of timeDiff in this code.

// Get two Unix times (timestamps) in milliseconds
timestamp1 = timestamp("2022-03-30T00:00:00")
timestamp2 = timestamp("2022-03-30T00:15:00")

// Calculate the difference in milliseconds
timeDiff = timestamp2 - timestamp1

// Convert the difference to days, hours, minutes, and seconds
days = math.floor(timeDiff / (24 * 60 * 60 * 1000))
hours = math.floor((timeDiff % (24 * 60 * 60 * 1000)) / (60 * 60 * 1000))
minutes = math.floor((timeDiff % (60 * 60 * 1000)) / (60 * 1000))
seconds = math.floor((timeDiff % (60 * 1000)) / 1000)

// Format the difference as a readable string
formattedTimeDiff = ""
if daysDiff > 0
formattedTimeDiff := str.tostring(daysDiff) + "D "
if hoursDiff > 0
formattedTimeDiff := formattedTimeDiff + str.tostring(hoursDiff) + "H "
if minutesDiff > 0
formattedTimeDiff := formattedTimeDiff + str.tostring(minutesDiff) + "M "
if secondsDiff > 0
formattedTimeDiff := formattedTimeDiff + str.tostring(secondsDiff) + "S"

Re: Timeframe format - easy read

Posted: Tue Apr 18, 2023 12:52 pm
by theGame
Cool! thank you for this idea!