-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.go
809 lines (720 loc) · 20.6 KB
/
schema.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
// Copyright 2022 Stock Parfait
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package db
import (
"encoding/json"
"fmt"
"math"
"regexp"
"strconv"
"strings"
"time"
"github.com/stockparfait/errors"
"github.com/stockparfait/stockparfait/message"
"github.com/stockparfait/stockparfait/table"
)
// lessLex is a lexicographic ordering on the slices of int.
func lessLex(x, y []int) bool {
l := len(x)
if len(y) < l {
l = len(y)
}
for i := 0; i < l; i++ {
if x[i] < y[i] {
return true
}
if x[i] > y[i] {
return false
}
}
return len(x) < len(y)
}
func parseTime(s string) (time.Time, error) {
if s == "0000-00-00" || s == "0000-00-00T00:00:00.000" {
return time.Time{}, nil
}
formats := []string{
"2006-01-02 15:04:05.999",
"2006-01-02T15:04:05.999",
"2006-01-02T15:04:05.999Z",
"2006-01-02 15:04:05",
"2006-01-02T15:04:05",
"2006-01-02",
}
var err error
for _, fmt := range formats {
var tm time.Time
tm, err := time.Parse(fmt, s)
if err == nil {
return tm, nil
}
}
return time.Time{}, err
}
// TimeOfDay as milliseconds from midnight.
type TimeOfDay uint32
var _ message.Message = new(TimeOfDay)
func (t *TimeOfDay) InitMessage(js any) error {
switch s := js.(type) {
case string:
tm, err := NewTimeOfDayFromString(s)
if err != nil {
return errors.Annotate(err, "failed to parse Time string")
}
*t = tm
case map[string]any:
*t = TimeOfDay(0)
default:
return errors.Reason("expected a string or {}, got %v", js)
}
return nil
}
// NewTimeOfDay creates TimeOfDay out of components. It panics if any of the
// arguments is out of range: hour>23, minute, second>59 or msec>999.
func NewTimeOfDay(hour, minute, second uint8, msec uint32) TimeOfDay {
if hour > 23 {
panic(errors.Reason("hour=%d > 23", hour))
}
if minute > 59 {
panic(errors.Reason("minute=%d > 59", minute))
}
if second > 59 {
panic(errors.Reason("second=%d > 59", second))
}
if msec > 999 {
panic(errors.Reason("msec=%d > 999", msec))
}
return TimeOfDay(((uint32(hour)*60+uint32(minute))*60+uint32(second))*1000 + msec)
}
// todRegexp matches "hh:mm", "hh:mm:ss" or "hh:mm:ss.xxx" formats.
var todRegexp = regexp.MustCompile(
`^(?P<h>(\s|\d)?\d):(?P<m>\d\d)(:(?P<s>\d\d)(\.(?P<ms>\d\d\d))?)?$`)
// NewTimeOfDayFromString parses a string "hh:mm:ss.xxx" with optional seconds
// and milliseconds.
func NewTimeOfDayFromString(s string) (TimeOfDay, error) {
submatches := todRegexp.FindSubmatch([]byte(s))
if len(submatches) == 0 {
return TimeOfDay(0), errors.Reason("expected hh:mm[:ss[.xxx]]: '%s'", s)
}
hs := strings.Trim(string(submatches[todRegexp.SubexpIndex("h")]), " \t")
hour, err := strconv.ParseUint(hs, 10, 8)
if err != nil {
return TimeOfDay(0), errors.Reason("cannot parse hour in '%s'", s)
}
minute, err := strconv.ParseUint(string(submatches[todRegexp.SubexpIndex("m")]), 10, 8)
if err != nil {
return TimeOfDay(0), errors.Reason("cannot parse minute in '%s'", s)
}
var second, msec uint64
if str := string(submatches[todRegexp.SubexpIndex("s")]); str != "" {
second, err = strconv.ParseUint(str, 10, 8)
if err != nil {
return TimeOfDay(0), errors.Reason("cannot parse second in '%s'", s)
}
}
if str := string(submatches[todRegexp.SubexpIndex("ms")]); str != "" {
msec, err = strconv.ParseUint(str, 10, 16)
if err != nil {
return TimeOfDay(0), errors.Reason("cannot parse milliseconds in '%s'", s)
}
}
return NewTimeOfDay(uint8(hour), uint8(minute), uint8(second), uint32(msec)), nil
}
func (t TimeOfDay) Hour() uint8 {
return uint8(t / 3_600_000)
}
func (t TimeOfDay) Minute() uint8 {
return uint8((t % 3_600_000) / 60_000)
}
func (t TimeOfDay) Second() uint8 {
return uint8((t % 60_000) / 1000)
}
func (t TimeOfDay) Millisecond() uint32 {
return uint32(t) % 1000
}
func (t TimeOfDay) String() string {
return fmt.Sprintf("%02d:%02d:%02d.%03d",
t.Hour(), t.Minute(), t.Second(), t.Millisecond())
}
func (t TimeOfDay) Before(t2 TimeOfDay) bool {
return t < t2
}
func (t TimeOfDay) After(t2 TimeOfDay) bool {
return t > t2
}
func (t TimeOfDay) IsZero() bool {
return t == TimeOfDay(0)
}
// Date records a calendar date and time as year, month, day and millisecond
// from the midnight. The struct is designed to fit into 8 bytes.
type Date struct {
YearVal uint16
MonthVal uint8
DayVal uint8
Time TimeOfDay
}
var _ json.Marshaler = Date{}
var _ json.Unmarshaler = &Date{}
var _ message.Message = &Date{}
// NewDate is a Date constructor without time of the day.
func NewDate(year uint16, month, day uint8) Date {
return Date{YearVal: year, MonthVal: month, DayVal: day}
}
// NewDatetime is a Date constructor with time of the day.
func NewDatetime(year uint16, month, day, hour, minute, second uint8, msec uint32) Date {
return Date{
YearVal: year,
MonthVal: month,
DayVal: day,
Time: NewTimeOfDay(hour, minute, second, msec),
}
}
// NewDateFromTime creates a Date instance from a time.Time value in UTC.
func NewDateFromTime(t time.Time) Date {
var zero time.Time
if t == zero {
return Date{}
}
return Date{
YearVal: uint16(t.Year()),
MonthVal: uint8(t.Month()),
DayVal: uint8(t.Day()),
Time: NewTimeOfDay(uint8(t.Hour()), uint8(t.Minute()), uint8(t.Second()), uint32(t.Nanosecond()/1_000_000)),
}
}
// NewDateFromString creates a Date instance from a string representation.
func NewDateFromString(s string) (Date, error) {
t, err := parseTime(s)
if err != nil {
return Date{}, errors.Annotate(err, "failed to parse a Date string: '%s'", s)
}
return NewDateFromTime(t), nil
}
// DateInNY returns today's date in New York timezone.
func DateInNY(now time.Time) Date {
tz := "America/New_York"
location, err := time.LoadLocation(tz)
if err != nil {
panic(errors.Annotate(err, "failed to load timezone %s", tz))
}
t := now.In(location)
return NewDateFromTime(t)
}
func (d Date) Year() uint16 { return d.YearVal }
func (d Date) Month() uint8 { return d.MonthVal }
func (d Date) Day() uint8 { return d.DayVal }
func (d Date) Hour() uint8 { return d.Time.Hour() }
func (d Date) Minute() uint8 { return d.Time.Minute() }
func (d Date) Second() uint8 { return d.Time.Second() }
func (d Date) Millisecond() uint32 { return d.Time.Millisecond() }
// String representation of the value.
func (d Date) String() string {
if d.Time == TimeOfDay(0) {
return fmt.Sprintf("%04d-%02d-%02d", d.Year(), d.Month(), d.Day())
}
return fmt.Sprintf("%04d-%02d-%02dT%02d:%02d:%02d.%03d",
d.Year(), d.Month(), d.Day(), d.Hour(), d.Minute(), d.Second(), d.Millisecond())
}
// MarshalJSON implements json.Marshaler.
func (d Date) MarshalJSON() ([]byte, error) {
return []byte(`"` + d.String() + `"`), nil
}
// UnmarshalJSON implements json.Unmarshaler. NOTE: unlike other methods, this
// is a pointer method.
func (d *Date) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return errors.Annotate(err, "Date JSON must be a string")
}
date, err := NewDateFromString(s)
if err != nil {
return errors.Annotate(err, "failed to parse Date string")
}
*d = date
return nil
}
// InitMessage implements message.Message.
func (d *Date) InitMessage(js any) error {
switch s := js.(type) {
case string:
date, err := NewDateFromString(s)
if err != nil {
return errors.Annotate(err, "failed to parse Date string")
}
*d = date
case map[string]any:
*d = Date{}
default:
return errors.Reason("expected a string or {}, got %v", js)
}
return nil
}
// ToTime converts Date to Time in UTC.
func (d Date) ToTime() time.Time {
return time.Date(int(d.Year()), time.Month(d.Month()), int(d.Day()),
int(d.Hour()), int(d.Minute()), int(d.Second()),
int(d.Millisecond()*1_000_000), time.UTC)
}
// Date returns the date value without time of day (Time = 0).
func (d Date) Date() Date {
return NewDate(d.Year(), d.Month(), d.Day())
}
// Monday returns a new Date of the Monday midnight of the current date's
// week. Note: week is assumed to start on Sunday, so Monday(d=Sunday) returns
// the next day.
func (d Date) Monday() Date {
t := d.ToTime()
t = t.AddDate(0, 0, 1-int(t.Weekday()))
return NewDate(uint16(t.Year()), uint8(t.Month()), uint8(t.Day()))
}
// MonthStart returns the 1st of the month of the current date.
func (d Date) MonthStart() Date {
return NewDate(d.Year(), d.Month(), 1)
}
// QuarterStart returns the first day of the quarter of the current date.
func (d Date) QuarterStart() Date {
return NewDate(d.Year(), (d.Month()-1)/3*3+1, 1)
}
// Before compares two Date objects for strict inequality (self < d2).
func (d Date) Before(d2 Date) bool {
return lessLex([]int{int(d.Year()), int(d.Month()), int(d.Day()), int(d.Time)},
[]int{int(d2.Year()), int(d2.Month()), int(d2.Day()), int(d2.Time)})
}
// After compares two Date objects for strict inequality, self > d2.
func (d Date) After(d2 Date) bool {
return d2.Before(d)
}
// IsZero checks whether the date has a zero value.
func (d Date) IsZero() bool {
return d.Year() == 0 && d.Month() == 0 && d.Day() == 0 && d.Time == TimeOfDay(0)
}
// MinDate returns the earliest date from the list, or zero value.
func MinDate(dates ...Date) Date {
var min Date
for _, d := range dates {
if min.IsZero() || (!d.IsZero() && min.After(d)) {
min = d
}
}
return min
}
// MaxDate returns the latest date from the list, or zero value.
func MaxDate(dates ...Date) Date {
var max Date
for _, d := range dates {
if max.IsZero() || (!d.IsZero() && max.Before(d)) {
max = d
}
}
return max
}
func (d Date) IsLeapYear() bool {
if d.Year()%400 == 0 {
return true
}
if d.Year()%100 == 0 {
return false
}
if d.Year()%4 == 0 {
return true
}
return false
}
// DaysInMonth is the numder of days in the current month, which for February
// depends on the year.
func (d Date) DaysInMonth() uint8 {
if d.IsZero() {
return 0
}
switch d.Month() {
case 1, 3, 5, 7, 8, 10, 12:
return 31
case 4, 6, 9, 11:
return 30
case 2:
if d.IsLeapYear() {
return 29
} else {
return 28
}
}
return 0
}
// YearsTill returns possibly fractional number of years between the two dates.
func (d Date) YearsTill(d2 Date) float64 {
if d.IsZero() || d2.IsZero() {
return 0.0
}
years := float64(d2.Year()) - float64(d.Year())
years += (float64(d2.Month()) - float64(d.Month())) / 12.0
years += (float64(d2.Day())/float64(d2.DaysInMonth()) - float64(d.Day())/float64(d.DaysInMonth())) / 12.0
years += (float64(d2.Time) - float64(d.Time)) / (365.25 * 1000)
return years
}
// InRange checks if d is in the inclusive date range. Any of the bounds may be
// zero value, in which case it's ignored.
func (d Date) InRange(start, end Date) bool {
if d.IsZero() {
return false
}
if !start.IsZero() && start.After(d) {
return false
}
if !end.IsZero() && end.Before(d) {
return false
}
return true
}
// TickerRow is a row in the tickers table.
type TickerRow struct {
Source string // where the ticker was downloaded, e.g. NDL table name
Exchange string // the primary exchange trading this ticker
Name string // the company name
Category string
Sector string
Industry string
Location string
SECFilings string // URL
CompanySite string // URL
Active bool // ticker is listed at the last price date
}
// Row converts TickerRow to table.Row which includes the ticker name.
func (t TickerRow) Row(ticker string) table.Row {
return TickerTableRow{
TickerRow: t,
Ticker: ticker,
}
}
// TickerTableRow implements table.Row by adding ticker name to TickerRow.
type TickerTableRow struct {
TickerRow
Ticker string
}
var _ table.Row = TickerTableRow{}
func (r TickerTableRow) CSV() []string {
return []string{
r.Ticker,
r.Source,
r.Exchange,
r.Name,
r.Category,
r.Sector,
r.Industry,
r.Location,
r.SECFilings,
r.CompanySite,
bool2str(r.Active),
}
}
// TickerRowHeader for CSV table.
func TickerRowHeader() []string {
return []string{
"Ticker",
"Source",
"Exchange",
"Name",
"Category",
"Sector",
"Industry",
"Location",
"SEC Filings",
"Company Site",
"Active",
}
}
func bool2str(x bool) string {
if x {
return "TRUE"
}
return "FALSE"
}
func float2str(x float32) string {
return fmt.Sprintf("%g", x)
}
func uint2str(x uint16) string {
return fmt.Sprintf("%d", x)
}
// PriceRow is a row in the prices table. It is intended for daily price points.
// Size: 32 bytes.
//
// Note, that the sign of the (unadjusted) Close price indicates whether the
// ticker is listed at the close of the day (negate = delisted). Therefore, use
// CloseUnadjusted() and Active() methods to get the corresponding values.
//
// Price and cash volume are assumed to be in the stock's native currency,
// e.g. dollar for the US stocks.
type PriceRow struct {
Date Date
Open float32 // all other prices are unadjusted
High float32
Low float32
Close float32 // unadjusted; negative means delisted
CloseSplitAdjusted float32 // adjusted only for splits
CloseFullyAdjusted float32 // adjusted for splits, dividends, spinoffs
CashVolume float32 // shares volume * closing price
}
var _ table.Row = PriceRow{}
func PriceRowHeader() []string {
return []string{
"Date",
"Open",
"High",
"Low",
"Close",
"Close split adj",
"Close fully adj",
"Cash Volume",
"Active",
}
}
func (p PriceRow) CSV() []string {
return []string{
p.Date.String(),
float2str(p.Open),
float2str(p.High),
float2str(p.Low),
float2str(p.CloseUnadjusted()),
float2str(p.CloseSplitAdjusted),
float2str(p.CloseFullyAdjusted),
float2str(p.CashVolume),
bool2str(p.Active()),
}
}
func (p PriceRow) OpenFullyAdjusted() float32 {
if p.Close == 0 {
return 0
}
return p.Open / p.CloseUnadjusted() * p.CloseFullyAdjusted
}
func (p PriceRow) OpenSplitAdjusted() float32 {
if p.Close == 0 {
return 0
}
return p.Open / p.CloseUnadjusted() * p.CloseSplitAdjusted
}
func (p PriceRow) HighFullyAdjusted() float32 {
if p.Close == 0 {
return 0
}
return p.High / p.CloseUnadjusted() * p.CloseFullyAdjusted
}
func (p PriceRow) HighSplitAdjusted() float32 {
if p.Close == 0 {
return 0
}
return p.High / p.CloseUnadjusted() * p.CloseSplitAdjusted
}
func (p PriceRow) LowFullyAdjusted() float32 {
if p.Close == 0 {
return 0
}
return p.Low / p.CloseUnadjusted() * p.CloseFullyAdjusted
}
func (p PriceRow) LowSplitAdjusted() float32 {
if p.Close == 0 {
return 0
}
return p.Low / p.CloseUnadjusted() * p.CloseSplitAdjusted
}
// CloseUnadjusted price, separated from the activity status.
func (p PriceRow) CloseUnadjusted() float32 {
if p.Close < 0.0 {
return -p.Close
} else {
return p.Close
}
}
// Active indicates whether the ticker is currently listed.
func (p PriceRow) Active() bool {
return !math.Signbit(float64(p.Close))
}
// SetActive bit for the price point.
func (p *PriceRow) SetActive(active bool) {
if p.Active() != active {
p.Close = -p.Close
}
}
// TestPrice creates a PriceRow instance for use in tests. It uses the closing
// price to assign the other OHL prices.
func TestPrice(date Date, close, splitAdj, fullyAdj, dv float32, active bool) PriceRow {
return TestPriceRow(date, close, close, close, close, splitAdj, fullyAdj, dv, active)
}
// TestPriceRow is a complete version of TestPrice which allows to set all OHLC
// prices directly. The OHL prices are always fully adjusted.
func TestPriceRow(date Date, open, high, low, close, closeSplitAdj, closeFullyAdj, dv float32, active bool) PriceRow {
p := PriceRow{
Date: date,
Open: open,
High: high,
Low: low,
Close: close,
CloseSplitAdjusted: closeSplitAdj,
CloseFullyAdjusted: closeFullyAdj,
CashVolume: dv,
}
p.SetActive(active)
return p
}
// ResampledRow is a multi-day bar with some additional daily statistics. Size:
// 44 bytes.
type ResampledRow struct {
Open float32
OpenSplitAdjusted float32
OpenFullyAdjusted float32
Close float32 // unadjusted
CloseSplitAdjusted float32 // adjusted only for splits
CloseFullyAdjusted float32 // adjusted for splits, dividends, spinoffs
CashVolume float32
DateOpen Date
DateClose Date
// Sum of absolute values of daily log-profits within the bar:
// sum(abs(log(p(t+1))-log(p(t)))). Note, that it always has NumSamples-1
// samples. When summing over multiple resampled bars, recover the missing
// sample as abs(log(open(t))-log(close(t-1))).
SumAbsLogProfits float32
NumSamples uint16
Active bool // if ticker is active at bar's close
}
var _ table.Row = &ResampledRow{}
func (r ResampledRow) CSV() []string {
return []string{
float2str(r.Open),
float2str(r.OpenSplitAdjusted),
float2str(r.OpenFullyAdjusted),
float2str(r.Close),
float2str(r.CloseSplitAdjusted),
float2str(r.CloseFullyAdjusted),
float2str(r.CashVolume),
r.DateOpen.String(),
r.DateClose.String(),
float2str(r.SumAbsLogProfits),
uint2str(r.NumSamples),
bool2str(r.Active),
}
}
func ResampledRowHeader() []string {
return []string{
"Open",
"Open split adj",
"Open fully adj",
"Close",
"Close split adj",
"Close fully adj",
"Cash Volume",
"Date Open",
"Date Close",
"Sum Abs Log Profits",
"Samples",
"Active",
}
}
// TestResampled creates a new ResampledRow for use in tests.
func TestResampled(dateOpen, dateClose Date, open, close, adj, dv float32, active bool) ResampledRow {
return ResampledRow{
Open: open,
OpenSplitAdjusted: open,
OpenFullyAdjusted: open,
Close: close,
CloseSplitAdjusted: adj,
CloseFullyAdjusted: adj,
CashVolume: dv,
DateOpen: dateOpen,
DateClose: dateClose,
SumAbsLogProfits: 0.2,
NumSamples: 20,
Active: active,
}
}
// DailyVolatility computes the average daily absolute log-profit from the list
// of consecutive monthly bars.
func DailyVolatility(rows []ResampledRow) (volatility float64, samples uint16) {
absLogProfit := func(x, y float32) float64 {
diff := math.Log(float64(x)) - math.Log(float64(y))
if diff < 0.0 {
return -diff
}
return diff
}
var total float64
var prevRow ResampledRow
for i, r := range rows {
if i > 0 && r.OpenFullyAdjusted > 0.0 && prevRow.CloseFullyAdjusted > 0.0 {
total += absLogProfit(r.OpenFullyAdjusted, prevRow.CloseFullyAdjusted)
samples++
}
total += float64(r.SumAbsLogProfits)
samples += r.NumSamples - 1
prevRow = r
}
if samples == 0 {
return 0.0, 0
}
volatility = total / float64(samples)
return
}
// Metadata is the schema for the metadata.json file.
type Metadata struct {
Start Date `json:"start"` // the earliest available price date
End Date `json:"end"` // the latest available price date
NumTickers int `json:"num_tickers"`
NumPrices int `json:"num_prices"` // daily price samples
NumMonthly int `json:"num_monthly"` // monthly price samples
}
func (m *Metadata) UpdateTickers(tickers map[string]TickerRow) {
m.NumTickers = len(tickers)
}
func (m *Metadata) UpdatePrices(prices []PriceRow) {
m.NumPrices += len(prices)
for _, p := range prices {
if m.Start.IsZero() || m.Start.After(p.Date) {
m.Start = p.Date
}
if m.End.IsZero() || m.End.Before(p.Date) {
m.End = p.Date
}
}
}
func (m *Metadata) UpdateMonthly(monthly map[string][]ResampledRow) {
m.NumMonthly = 0
for _, ms := range monthly {
m.NumMonthly += len(ms)
}
}
// Time is a wrapper around time.Time with JSON methods.
type Time time.Time
var _ json.Marshaler = &Time{}
var _ json.Unmarshaler = &Time{}
func NewTime(year, month, day, hour, minute, second int) *Time {
t := time.Date(year, time.Month(month), day, hour, minute, second, 0, time.UTC)
return (*Time)(&t)
}
// String representation of Time.
func (t *Time) String() string {
return time.Time(*t).Format("2006-01-02 15:04:05")
}
// MarshalJSON implements json.Marshaler.
func (t *Time) MarshalJSON() ([]byte, error) {
return []byte(`"` + t.String() + `"`), nil
}
// UnmarshalJSON implements json.Unmarshaler.
func (t *Time) UnmarshalJSON(data []byte) error {
var s string
var err error
if err = json.Unmarshal(data, &s); err != nil {
return errors.Annotate(err, "Time JSON must be a string")
}
tm, err := parseTime(s)
if err != nil {
return errors.Annotate(err, "failed to parse time string: '%s'", s)
}
*t = Time(tm)
return nil
}