Skip to content

Commit

Permalink
fix #7: remove trading history type
Browse files Browse the repository at this point in the history
  • Loading branch information
btmura committed Aug 29, 2015
1 parent 19ddf99 commit e9ba958
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 40 deletions.
16 changes: 8 additions & 8 deletions ponzi.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func refreshStockData(sd *stockData) {
start := end.Add(-time.Hour * 24 * 30)

// Map from symbol to tradingHistory channel.
scm := map[string]chan tradingHistory{}
scm := map[string]chan []tradingSession{}

// Map from symbol to realTimeTradingData channel.
rcm := map[string]chan realTimeTradingData{}
Expand All @@ -185,14 +185,14 @@ func refreshStockData(sd *stockData) {
}

// Launch a go routine that will stuff the tradingHistory into the channel.
ch := make(chan tradingHistory)
ch := make(chan []tradingSession)
scm[s.symbol] = ch
go func(symbol string, ch chan tradingHistory) {
th, err := getTradingHistory(symbol, start, end)
go func(symbol string, ch chan []tradingSession) {
tss, err := getTradingSessions(symbol, start, end)
if err != nil {
log.Printf("getTradingHistory(%s): %v", symbol, err)
log.Printf("getTradingSessions(%s): %v", symbol, err)
}
ch <- th
ch <- tss
}(s.symbol, ch)

// Launch a go routine that will stuff the realTimeTradingData into the channel.
Expand Down Expand Up @@ -266,9 +266,9 @@ func refreshStockData(sd *stockData) {
sd.Unlock()
}

func convertTradingHistory(th tradingHistory) []stockTradingSession {
func convertTradingHistory(tss []tradingSession) []stockTradingSession {
var sts []stockTradingSession
for _, ts := range th {
for _, ts := range tss {
sts = append(sts, stockTradingSession{
date: ts.date,
close: ts.close,
Expand Down
64 changes: 32 additions & 32 deletions tradinghistory.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,6 @@ import (
"time"
)

// tradingHistory is a sorted tradingSession slice with the most recent at the front.
type tradingHistory []tradingSession

// Len implements sort.Interface.
func (th tradingHistory) Len() int {
return len(th)
}

// Less implements sort.Interface.
func (th tradingHistory) Less(i, j int) bool {
return th[i].date.Before(th[j].date)
}

// Swap implements sort.Interface.
func (th tradingHistory) Swap(i, j int) {
th[i], th[j] = th[j], th[i]
}

// tradingSession contains stats from a single trading session.
type tradingSession struct {
date time.Time
Expand All @@ -51,10 +33,10 @@ type realTimeTradingData struct {
percentChange float64
}

func getTradingHistory(symbol string, startDate, endDate time.Time) (tradingHistory, error) {
tradingFuncs := []func(symbol string, startDate, endDate time.Time) (tradingHistory, error){
getTradingHistoryFromGoogle,
getTradingHistoryFromYahoo,
func getTradingSessions(symbol string, startDate, endDate time.Time) ([]tradingSession, error) {
tradingFuncs := []func(symbol string, startDate, endDate time.Time) ([]tradingSession, error){
getTradingSessionsFromGoogle,
getTradingSessionsFromYahoo,
}
for _, n := range rand.Perm(len(tradingFuncs)) {
th, err := tradingFuncs[n](symbol, startDate, endDate)
Expand All @@ -67,7 +49,7 @@ func getTradingHistory(symbol string, startDate, endDate time.Time) (tradingHist
return nil, fmt.Errorf("all %d tradingFuncs failed", len(tradingFuncs))
}

func getTradingHistoryFromGoogle(symbol string, startDate, endDate time.Time) (tradingHistory, error) {
func getTradingSessionsFromGoogle(symbol string, startDate, endDate time.Time) ([]tradingSession, error) {
formatTime := func(date time.Time) string {
return date.Format("Jan 02, 2006")
}
Expand All @@ -91,7 +73,7 @@ func getTradingHistoryFromGoogle(symbol string, startDate, endDate time.Time) (t
}
defer resp.Body.Close()

var th tradingHistory
var tss []tradingSession
r := csv.NewReader(resp.Body)
for i := 0; ; i++ {
record, err := r.Read()
Expand Down Expand Up @@ -151,7 +133,7 @@ func getTradingHistoryFromGoogle(symbol string, startDate, endDate time.Time) (t
return nil, err
}

th = append(th, tradingSession{
tss = append(tss, tradingSession{
date: date,
open: open,
high: high,
Expand All @@ -163,12 +145,12 @@ func getTradingHistoryFromGoogle(symbol string, startDate, endDate time.Time) (t
}

// Most recent trading sessions at the front.
sort.Reverse(th)
sort.Reverse(sortableTradingSessions(tss))

return th, nil
return tss, nil
}

func getTradingHistoryFromYahoo(symbol string, startDate, endDate time.Time) (tradingHistory, error) {
func getTradingSessionsFromYahoo(symbol string, startDate, endDate time.Time) ([]tradingSession, error) {
v := url.Values{}
v.Set("s", symbol)
v.Set("a", strconv.Itoa(int(startDate.Month())-1))
Expand All @@ -193,7 +175,7 @@ func getTradingHistoryFromYahoo(symbol string, startDate, endDate time.Time) (tr
}
defer resp.Body.Close()

var th tradingHistory
var tss []tradingSession
r := csv.NewReader(resp.Body)
for i := 0; ; i++ {
record, err := r.Read()
Expand Down Expand Up @@ -255,7 +237,7 @@ func getTradingHistoryFromYahoo(symbol string, startDate, endDate time.Time) (tr

// Ignore adjusted close value to keep Google and Yahoo APIs the same.

th = append(th, tradingSession{
tss = append(tss, tradingSession{
date: date,
open: open,
high: high,
Expand All @@ -267,9 +249,9 @@ func getTradingHistoryFromYahoo(symbol string, startDate, endDate time.Time) (tr
}

// Most recent trading sessions at the front.
sort.Reverse(th)
sort.Reverse(sortableTradingSessions(tss))

return th, nil
return tss, nil
}

func getRealTimeTradingData(symbol string) (realTimeTradingData, error) {
Expand Down Expand Up @@ -343,3 +325,21 @@ func getRealTimeTradingData(symbol string) (realTimeTradingData, error) {
percentChange: percentChange,
}, nil
}

// sortableTradingSessions is a sortable tradingSession slice.
type sortableTradingSessions []tradingSession

// Len implements sort.Interface.
func (sts sortableTradingSessions) Len() int {
return len(sts)
}

// Less implements sort.Interface.
func (sts sortableTradingSessions) Less(i, j int) bool {
return sts[i].date.Before(sts[j].date)
}

// Swap implements sort.Interface.
func (sts sortableTradingSessions) Swap(i, j int) {
sts[i], sts[j] = sts[j], sts[i]
}

0 comments on commit e9ba958

Please sign in to comment.