-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstock.go
More file actions
152 lines (132 loc) · 4.58 KB
/
stock.go
File metadata and controls
152 lines (132 loc) · 4.58 KB
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
package main
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/gocolly/colly"
)
var MARKET_COMBINED string = "combined"
var MARKET_MAIN string = "main"
var MARKET_JUNIOR string = "junior"
var MARKET_USD string = "usd-equities"
var MARKET_BOND string = "bond"
type Stock struct {
Name string
Ticker string
ShareCount uint64
ClosingPrice int64
LastTradedPrice int64
LastTradedVolume uint64
JSEUrl string
History []PriceHistory
}
type PriceHistory struct {
OpenPrice uint16
HighPrice uint16
LowPrice uint16
ClosePrice uint16
Volume uint64
Date uint64
}
func newStock(name string, ticker string, sharecount uint64, closingPrice int64, lastTradedPrice int64, lastTradedVolume uint64) *Stock {
stock := Stock{
Name: name,
Ticker: ticker,
ShareCount: sharecount,
ClosingPrice: closingPrice,
LastTradedPrice: lastTradedPrice,
LastTradedVolume: lastTradedVolume,
}
return &stock
}
func (s *Stock) loadStock(history bool) {
dataCollector := colly.NewCollector()
// get name, ticker, close price
dataCollector.OnHTML("h2", func(e *colly.HTMLElement) {
if s.Name == "" {
nameStr := e.Text
s.Name = (nameStr[2:strings.LastIndex(nameStr, " (")])
s.Ticker = (nameStr[(strings.LastIndex(nameStr, " (") + 2):strings.LastIndex(nameStr, ")")])
}
priceStr := e.DOM.Next().Text()
priceStr = priceStr[strings.Index(priceStr, "$")+1 : (strings.Index(priceStr, ".") + 3)]
s.ClosingPrice = getDollarValueAsInt(priceStr)
})
// get share count
dataCollector.OnHTML(".tw-bg-gray-50.tw-rounded-sm:first-child", func(e *colly.HTMLElement) {
if s.ShareCount == 0 {
var err error
shareCountString := e.ChildText(".tw-flex.tw-justify-between.tw-bg-gray-50.tw-p-4:nth-child(5) span:nth-child(2)")
s.ShareCount, _ = strconv.ParseUint(strings.ReplaceAll(shareCountString[0:strings.LastIndex(shareCountString, "\n u")], ",", ""), 10, 64)
if err != nil {
fmt.Println(err)
}
}
})
dataCollector.OnHTML(".tw-container > div:nth-child(2) .tw-justify-between span:nth-child(2)", func(e *colly.HTMLElement) {
switch e.Index {
case 4:
lastTradedVolumeString := e.Text
s.LastTradedVolume, _ = strconv.ParseUint(strings.ReplaceAll(lastTradedVolumeString, ",", ""), 10, 64)
case 5:
s.LastTradedPrice = getDollarValueAsInt(e.Text) //strconv.ParseFloat(e.Text[strings.Index(e.Text, "$")+1:(strings.Index(e.Text, ".")+3)], 64)
// TODO:: continue here
}
})
// load history if flag is true
// TODO: load corporate actions
if history {
dataCollector.OnHTML("script", func(e *colly.HTMLElement) {
if e.Index == 12 {
if !strings.Contains(e.Text, "type: 'candlestick',\n name: ''") {
price_text := e.Text[strings.Index(e.Text, "type: 'candlestick',"):]
volume_text := e.Text[strings.Index(e.Text, "name: 'Volume',"):]
price_text = price_text[strings.Index(price_text, "[[")+2 : strings.Index(price_text, "]],")]
volume_text = volume_text[strings.Index(volume_text, "[[")+2 : strings.Index(volume_text, "]],")]
s.processPriceHistory(&price_text, &volume_text)
}
}
})
}
//attempt to visit url
dataCollector.Visit(s.JSEUrl)
// write_text_file(s.get_json_string(), s.Ticker)
}
// converts price history string to array of price history
func (s *Stock) processPriceHistory(priceHistory *string, volumeeHistory *string) {
priceHist := strings.Split(*priceHistory, "],[")
volHist := strings.Split(*volumeeHistory, "],[")
var lastRecordedHistory uint64 = 0
if len(s.History) > 0 {
lastRecordedHistory = s.History[len(s.History)-1].Date
}
var length int = len(volHist)
for i := 0; i < length; i++ {
priceHistoryData := strings.Split(priceHist[i], ",")
date, _ := strconv.ParseUint(priceHistoryData[0], 10, 64)
if lastRecordedHistory < date {
openprice := uint16(getDollarValueAsInt(priceHistoryData[1]))
highprice := uint16(getDollarValueAsInt(priceHistoryData[2]))
LowPrice := uint16(getDollarValueAsInt(priceHistoryData[3]))
closePrice := uint16(getDollarValueAsInt(priceHistoryData[4]))
volume, _ := strconv.ParseUint(volHist[i][strings.Index(volHist[i], ",")+1:], 10, 64)
var hist PriceHistory
hist.Date = date
hist.OpenPrice = openprice
hist.HighPrice = highprice
hist.LowPrice = LowPrice
hist.ClosePrice = closePrice
hist.Volume = volume
s.History = append(s.History, hist)
}
}
}
func (s *Stock) get_json_string() string {
stock_string, _ := json.Marshal(s)
return string(stock_string)
}
func get_json_list(stocks []Stock) string {
stock_string, _ := json.Marshal(stocks)
return string(stock_string)
}