Okay. So you meant is I use some kind of scheduler that runs at the end of time interval and it appends the new data to an already existing data?
Depends if you want the bar to be closed on time or existing ticks.
On existing ticks you can just use sth like below. Whereas I prefer to have the current bar also already in the existing array/data structure and not separate it
Where do I store this data?
Memory or some database?
Depends. Whether you want to request the data over and over from your provider or have it on your system.
I store data in plain files because I can make better use of resources.
1 Minute Example in Golang, pseudo, without consideration if tick data might cross with historical data:
type Tick struct {
Time int64
C int64
V int64
}
type OHLCVBar struct {
Tick
O int64
H int64
L int64
}
type Ctx struct {
Bars []OHLCVBar
BarCur OHLCVBar
LenBarTime int64
}
func (self *Ctx)OnRTData(tick *Tick) {
isNextBar := OHLCVTimeBasedAddTick(&self.BarCur, tick)
if isNextBar {
self.Bars = append(self.Bars, self.BarCur)
StoreBarToDB(self.BarCur)
barNext := OHLCVBar{}
CalculateNextBarTime(&barNext, LenBarTime)
barNext.O = tick.C
barNext.H = tick.C
barNext.L = tick.C
barNext.C = tick.C
barNext.V = tick.V
self.BarCur = barNext
}
}
// assumes that OHLCVBar Time is on end of period
func OHLCVTimeBasedAddTick(barCur *OHLCVBar, tick *Tick) (isNextBar bool) {
if tick.Time >= barCur.Time {
return true
}
barCur.C = tick.C
barCur.V += tick.V
barCur.H = MAX(barCur.H, tick.C)
barCur.L = MIN(barCur.L, tick.C)
return false
}