/
security_quote.go
146 lines (122 loc) · 3.08 KB
/
security_quote.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
/*
* SPDX-FileCopyrightText: 2022 Brian Welty
*
* SPDX-License-Identifier: MPL-2.0
*/
package model
import (
"log"
"strconv"
"time"
"github.com/piquette/finance-go/datetime"
"github.com/piquette/finance-go/chart"
"github.com/piquette/finance-go/quote"
"github.com/shopspring/decimal"
)
var Months = [14]string{"",
"Jan ",
"Feb ",
"Mar ",
"Apr ",
"May ",
"Jun ",
"Jul ",
"Aug ",
"Sep ",
"Oct ",
"Nov ",
"Dec "}
type SecurityQuote struct {
lastQuoted time.Time
Price decimal.Decimal
}
type SecurityQuoteCache struct {
Quotes map[string]SecurityQuote
}
var quotes *SecurityQuoteCache
func (sqc *SecurityQuoteCache) init() {
sqc.Quotes = make(map[string]SecurityQuote)
}
func init() {
quotes = new(SecurityQuoteCache)
quotes.init()
}
func GetQuoteCache() *SecurityQuoteCache {
return quotes
}
// Decide fetch policy, probably once per hour, if market is open.
// This can be skipped and quotes can alway be forced.
func fetchIsAllowed(last *time.Time, now *time.Time) bool {
if last.IsZero() {
return true
}
// for now, limit quotes to once per (business) day
return (daysBetweenDates(last, now, true) >= 1)
}
func (sqc *SecurityQuoteCache) add(symbol string, quote *SecurityQuote) {
log.Printf("[CACHE] ADD QUOTE FOR SYMBOL(%s)", symbol)
sqc.Quotes[symbol] = *quote
}
func (sqc *SecurityQuoteCache) GetDateOf(symbol string) time.Time {
return sqc.Quotes[symbol].lastQuoted
}
func (sqc *SecurityQuoteCache) Get(symbol string) SecurityQuote {
return sqc.Quotes[symbol]
}
func (s *Security) fetchPrice(force bool) *SecurityQuote {
var last *time.Time = &s.lastQuoteUpdate
securityQuote := new(SecurityQuote)
curTime := time.Now()
if s.Company.Symbol == "" ||
GetQuoteCache() == nil ||
!SecurityTypeIsPriceFetchable[s.SecurityTypeID] {
return nil
}
// determine time of last Quote in cache or database
lastQuoted := GetQuoteCache().GetDateOf(s.Company.Symbol)
if lastQuoted.After(*last) {
last = &lastQuoted
}
if !force && !fetchIsAllowed(last, &curTime) {
return nil
}
q, err := quote.Get(s.Company.Symbol)
if err != nil {
log.Println(err)
return nil
}
if q == nil {
return nil
}
spewModel(q)
//price := q.RegularMarketPreviousClose
price := q.RegularMarketPrice
securityQuote.Price = decimal.NewFromFloatWithExponent(price, -3)
securityQuote.lastQuoted = curTime
log.Printf("[MODEL] SECURITY(%d) QUOTE SYMBOL(%s) PRICE(%f)",
s.ID, s.Company.Symbol, price)
GetQuoteCache().add(s.Company.Symbol, securityQuote)
return securityQuote
}
func (s *Security) fetchPrices(days int) ([]string, []decimal.Decimal) {
labels := []string{}
prices := []decimal.Decimal{}
if s.Company.Symbol == "" {
return labels, prices
}
p := &chart.Params{}
p.Symbol = s.Company.Symbol
p.Interval = datetime.OneDay
t2 := time.Now()
p.End = datetime.New(&t2)
t1 := t2.AddDate(0,0,-days)
p.Start = datetime.New(&t1)
iter := chart.Get(p)
for iter.Next() {
b := iter.Bar()
d := datetime.FromUnix(b.Timestamp)
labels = append(labels, Months[d.Month] + strconv.Itoa(d.Day))
prices = append(prices, b.AdjClose)
}
return labels, prices
}