forked from saniales/golang-crypto-trading-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bittrex_v2.go
182 lines (154 loc) · 5.95 KB
/
bittrex_v2.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
// Copyright © 2017 Alessandro Sanino <saninoale@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package exchanges
import (
"errors"
"github.com/saniales/golang-crypto-trading-bot/environment"
"github.com/shopspring/decimal"
"github.com/thebotguys/golang-bittrex-api/bittrex"
)
// BittrexWrapperV2 wraps Bittrex API v2.0
type BittrexWrapperV2 struct {
PublicKey string
SecretKey string
summaries *SummaryCache
depositAddresses map[string]string
}
// NewBittrexV2Wrapper creates a generic wrapper of the bittrex API v2.0.
func NewBittrexV2Wrapper(publicKey string, secretKey string, depositAddresses map[string]string) ExchangeWrapper {
return &BittrexWrapperV2{
PublicKey: publicKey,
SecretKey: secretKey,
summaries: NewSummaryCache(),
depositAddresses: depositAddresses,
}
}
// Name returns the name of the wrapped exchange.
func (wrapper *BittrexWrapperV2) Name() string {
return "bittrex"
}
func (wrapper *BittrexWrapperV2) String() string {
return wrapper.Name()
}
// GetMarkets gets all the markets info.
func (wrapper *BittrexWrapperV2) GetMarkets() ([]*environment.Market, error) {
bittrexMarkets, err := bittrex.GetMarkets()
if err != nil {
return nil, err
}
wrappedMarkets := make([]*environment.Market, 0, len(bittrexMarkets))
for _, market := range bittrexMarkets {
if market.IsActive {
wrappedMarkets = append(wrappedMarkets, &environment.Market{
Name: market.MarketName,
BaseCurrency: market.BaseCurrency,
MarketCurrency: market.MarketCurrency,
})
}
}
return wrappedMarkets, nil
}
// GetOrderBook gets the order(ASK + BID) book of a market.
func (wrapper *BittrexWrapperV2) GetOrderBook(market *environment.Market) (*environment.OrderBook, error) {
panic("GetOrderBook not implemented")
}
// BuyLimit performs a limit buy action.
func (wrapper *BittrexWrapperV2) BuyLimit(market *environment.Market, amount float64, limit float64) (string, error) {
return "", errors.New("BuyLimit not implemented")
}
// BuyMarket performs a market buy action.
func (wrapper *BittrexWrapperV2) BuyMarket(market *environment.Market, amount float64) (string, error) {
return "", errors.New("BuyMarket not implemented")
}
// SellLimit performs a limit sell action.
func (wrapper *BittrexWrapperV2) SellLimit(market *environment.Market, amount float64, limit float64) (string, error) {
return "", errors.New("SellLimit not implemented")
}
// SellMarket performs a market sell action.
func (wrapper *BittrexWrapperV2) SellMarket(market *environment.Market, amount float64) (string, error) {
return "", errors.New("SellMarket not implemented")
}
// GetMarketSummary gets the current market summary.
func (wrapper *BittrexWrapperV2) GetMarketSummary(market *environment.Market) (*environment.MarketSummary, error) {
summary, err := bittrex.GetMarketSummary(market.Name)
if err != nil {
return nil, err
}
ret := &environment.MarketSummary{
High: summary.High,
Low: summary.Low,
Volume: summary.Volume,
Bid: summary.Bid,
Ask: summary.Ask,
Last: summary.Last,
}
wrapper.summaries.Set(market, ret)
return ret, nil
}
// GetCandles gets the candle data from the exchange.
func (wrapper *BittrexWrapperV2) GetCandles(market *environment.Market) ([]environment.CandleStick, error) {
bittrexCandles, err := bittrex.GetTicks(MarketNameFor(market, wrapper), "30m")
if err != nil {
return nil, err
}
ret := make([]environment.CandleStick, len(bittrexCandles))
for i, bittrexCandle := range bittrexCandles {
ret[i] = environment.CandleStick{
High: bittrexCandle.High,
Open: bittrexCandle.Open,
Close: bittrexCandle.Close,
Low: bittrexCandle.Low,
Volume: bittrexCandle.BaseVolume,
}
}
return ret, nil
}
// GetBalance gets the balance of the user of the specified currency.
func (wrapper *BittrexWrapperV2) GetBalance(symbol string) (*decimal.Decimal, error) {
panic("Not Implemented")
}
// GetDepositAddress gets the deposit address for the specified coin on the exchange.
func (wrapper *BittrexWrapperV2) GetDepositAddress(coinTicker string) (string, bool) {
addr, exists := wrapper.depositAddresses[coinTicker]
return addr, exists
}
// CalculateTradingFees calculates the trading fees for an order on a specified market.
//
// NOTE: In Bittrex fees are hardcoded due to the inability to obtain them via API before placing an order.
func (wrapper *BittrexWrapperV2) CalculateTradingFees(market *environment.Market, amount float64, limit float64, orderType TradeType) float64 {
var feePercentage float64
if orderType == MakerTrade {
feePercentage = 0.0025
} else if orderType == TakerTrade {
feePercentage = 0.0025
} else {
panic("Unknown trade type")
}
return amount * limit * feePercentage
}
// CalculateWithdrawFees calculates the withdrawal fees on a specified market.
func (wrapper *BittrexWrapperV2) CalculateWithdrawFees(market *environment.Market, amount float64) float64 {
panic("Not Implemented")
}
// FeedConnect connects to the feed of the exchange.
//
// NOTE: Not supported on Bittrex v1 API, use *BittrexWrapperV2.
func (wrapper *BittrexWrapperV2) FeedConnect(markets []*environment.Market) error {
return ErrWebsocketNotSupported
}
// Withdraw performs a withdraw operation from the exchange to a destination address.
func (wrapper *BittrexWrapperV2) Withdraw(destinationAddress string, coinTicker string, amount float64) error {
panic("Not Implemented")
}