This repository has been archived by the owner on Oct 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
strategy_balances.go
161 lines (128 loc) · 3.79 KB
/
strategy_balances.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
package dola
import (
"context"
"errors"
"fmt"
"strings"
"sync"
"time"
"github.com/rs/zerolog/log"
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
"github.com/thrasher-corp/gocryptotrader/exchanges/account"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-corp/gocryptotrader/exchanges/stream"
"github.com/thrasher-corp/gocryptotrader/exchanges/ticker"
)
// +------------------+
// | BalancesStrategy |
// +------------------+
var (
ErrAccountIndexOutOfRange = errors.New("no account with this index exists")
ErrCurrencyNotFound = errors.New("currency not found in holdings")
ErrHoldingsNotFound = errors.New("holdings not found for exchange")
)
type BalancesStrategy struct {
balances sync.Map
ticker TickerStrategy
}
func NewBalancesStrategy(refreshRate time.Duration) Strategy {
b := &BalancesStrategy{
balances: sync.Map{},
ticker: TickerStrategy{
Interval: refreshRate,
TickFunc: nil,
tickers: sync.Map{},
},
}
b.ticker.TickFunc = b.tick
return b
}
func (b *BalancesStrategy) Store(holdings account.Holdings) {
key := strings.ToLower(holdings.Exchange)
b.balances.Store(key, holdings)
}
func (b *BalancesStrategy) Load(exchangeName string) (holdings account.Holdings, loaded bool) {
key := strings.ToLower(exchangeName)
pointer, loaded := b.balances.Load(key)
if loaded {
var ok bool
holdings, ok = pointer.(account.Holdings)
if !ok {
panic(fmt.Sprintf("have %T, want account.Holdings", pointer))
}
}
return
}
func (b *BalancesStrategy) Currency(exchangeName string, code string, accountID string) (account.Balance, error) {
holdings, loaded := b.Load(exchangeName)
if !loaded {
var empty account.Balance
return empty, ErrHoldingsNotFound
}
for _, sub := range holdings.Accounts {
if sub.ID == accountID {
for _, balance := range sub.Currencies {
if balance.CurrencyName.String() == code {
return balance, nil
}
}
}
}
var empty account.Balance
return empty, ErrCurrencyNotFound
}
func (b *BalancesStrategy) tick(k *Keep, e exchange.IBotExchange) {
holdings, err := e.UpdateAccountInfo(context.Background(), asset.Spot)
if err != nil {
Msg(log.Error().Str("exchange", e.GetName()).Err(err))
}
b.Store(holdings)
}
// +--------------------+
// | Strategy interface |
// +--------------------+
func (b *BalancesStrategy) Init(k *Keep, e exchange.IBotExchange) error {
return b.ticker.Init(k, e)
}
func (b *BalancesStrategy) OnFunding(k *Keep, e exchange.IBotExchange, x stream.FundingData) error {
return nil
}
func (b *BalancesStrategy) OnPrice(k *Keep, e exchange.IBotExchange, x ticker.Price) error {
return nil
}
func (b *BalancesStrategy) OnKline(k *Keep, e exchange.IBotExchange, x stream.KlineData) error {
return nil
}
func (b *BalancesStrategy) OnOrderBook(k *Keep, e exchange.IBotExchange, x orderbook.Base) error {
return nil
}
func (b *BalancesStrategy) OnOrder(k *Keep, e exchange.IBotExchange, x order.Detail) error {
return nil
}
func (b *BalancesStrategy) OnModify(k *Keep, e exchange.IBotExchange, x order.Modify) error {
return nil
}
func (b *BalancesStrategy) OnBalanceChange(k *Keep, e exchange.IBotExchange, x account.Change) error {
return nil
}
func (b *BalancesStrategy) OnUnrecognized(k *Keep, e exchange.IBotExchange, x interface{}) error {
return nil
}
func (b *BalancesStrategy) Deinit(k *Keep, e exchange.IBotExchange) error {
return b.ticker.Deinit(k, e)
}
// func zeroHoldings(holdings account.Holdings) bool {
// if holdings.Exchange == "" {
// return true
// }
// for _, a := range holdings.Accounts {
// for _, c := range a.Currencies {
// if c.TotalValue > 0 {
// return false
// }
// }
// }
// return true
// }