sey eeet
sey eeet

Reputation: 258

how to get time of a bar_index for each bar_index?

Is there a way to get the time of each bar_index? I am doing

if peakLow
    array.push(VarArrayBarIndexLows, bar_index-1)
    array.push(VarArrayLowsValues, low[1])

I am wondering how I can also add the time of the bar_index-1 to an array? Something like array.push(VarArrayTimeofBarIndexLows,time_of_bar_index), but I am not sure how to get the time_of_bar_index for each bar_index-1?

TLDL; Specifically, here is what I am trying to do and I dont know how to find the ???

// High and Low Functions to find the peaks
fHigh(hi) =>
    pHigh = (hi[1] > hi[2]) and (hi <= hi[1])
fLow(lo) =>
    pLow = (lo[1] < lo[2]) and (lo >= lo[1])
peakLow = fLow(low)
peakHigh = fHigh(high)
plotshape(peakLow,    style=shape.triangleup,   location=location.belowbar, color=color.new(color.red,50),   offset = -1 , size=size.auto)
plotshape(peakHigh,   style=shape.triangledown, location=location.abovebar, color=color.new(color.blue,50),  offset = -1 , size=size.auto)
// =================== initializing the internal params ================
var VarArrayBarIndexLows = array.new_int()
var VarArrayTimeofBarIndexLows = array.new_float()
var VarArrayBarIndexHighs = array.new_int()
// init Array that holds the value for lows/highs
var VarArrayLowsValues = array.new_float()
var VarArrayHighsValues = array.new_float()



// if there is a peakLow then give the index and value to arrays
// I want to also get its time so later I use it in drawing tools
if peakLow
    array.push(VarArrayTimeofBarIndexLows, ???)
    array.push(VarArrayBarIndexLows, bar_index-1)
    array.push(VarArrayLowsValues, low[1])

Upvotes: 0

Views: 1407

Answers (1)

vitruvius
vitruvius

Reputation: 21121

You can simply get the time of bar_index - 1 with time[1].

Note: That will be in milliseconds.

You can also do hour[1], minute[1], second[1] etc.

You can use the below code from the faq to convert a time to a date-string format.

f_timeToString(_t) =>
  tostring(year(_t), "0000") + "." + tostring(month(_t), "00") + "." + tostring(dayofmonth(_t), "00") + " " +
  tostring(hour(_t), "00") + ":" + tostring(minute(_t), "00") + ":" + tostring(second(_t), "00")

Upvotes: 2

Related Questions