bitex_bot 0.6.1 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -3
- data/Gemfile +3 -1
- data/bitex_bot.gemspec +5 -2
- data/lib/bitex_bot/database.rb +2 -2
- data/lib/bitex_bot/models/api_wrappers/api_wrapper.rb +47 -35
- data/lib/bitex_bot/models/api_wrappers/bitex/bitex_api_wrapper.rb +178 -0
- data/lib/bitex_bot/models/api_wrappers/bitstamp/bitstamp_api_wrapper.rb +62 -45
- data/lib/bitex_bot/models/api_wrappers/itbit/itbit_api_wrapper.rb +52 -28
- data/lib/bitex_bot/models/api_wrappers/kraken/kraken_api_wrapper.rb +61 -28
- data/lib/bitex_bot/models/api_wrappers/kraken/kraken_order.rb +12 -6
- data/lib/bitex_bot/models/buy_closing_flow.rb +3 -2
- data/lib/bitex_bot/models/buy_opening_flow.rb +31 -6
- data/lib/bitex_bot/models/closing_flow.rb +37 -22
- data/lib/bitex_bot/models/open_buy.rb +1 -3
- data/lib/bitex_bot/models/open_sell.rb +1 -3
- data/lib/bitex_bot/models/opening_flow.rb +42 -28
- data/lib/bitex_bot/models/order_book_simulator.rb +14 -13
- data/lib/bitex_bot/models/sell_closing_flow.rb +3 -2
- data/lib/bitex_bot/models/sell_opening_flow.rb +29 -4
- data/lib/bitex_bot/robot.rb +28 -43
- data/lib/bitex_bot/settings.rb +2 -0
- data/lib/bitex_bot/version.rb +1 -1
- data/settings.rb.sample +23 -5
- data/spec/bitex_bot/settings_spec.rb +13 -6
- data/spec/factories/bitex_ask.rb +14 -0
- data/spec/factories/bitex_bid.rb +14 -0
- data/spec/factories/bitex_buy.rb +7 -7
- data/spec/factories/bitex_sell.rb +7 -7
- data/spec/factories/buy_opening_flow.rb +10 -10
- data/spec/factories/open_buy.rb +8 -8
- data/spec/factories/open_sell.rb +8 -8
- data/spec/factories/sell_opening_flow.rb +10 -10
- data/spec/fixtures/bitstamp/balance.yml +63 -0
- data/spec/fixtures/bitstamp/order_book.yml +60 -0
- data/spec/fixtures/bitstamp/orders/all.yml +62 -0
- data/spec/fixtures/bitstamp/orders/failure_sell.yml +60 -0
- data/spec/fixtures/bitstamp/orders/successful_buy.yml +62 -0
- data/spec/fixtures/bitstamp/transactions.yml +244 -0
- data/spec/fixtures/bitstamp/user_transactions.yml +223 -0
- data/spec/models/api_wrappers/bitex_api_wrapper_spec.rb +147 -0
- data/spec/models/api_wrappers/bitstamp_api_wrapper_spec.rb +134 -140
- data/spec/models/api_wrappers/itbit_api_wrapper_spec.rb +9 -3
- data/spec/models/api_wrappers/kraken_api_wrapper_spec.rb +142 -73
- data/spec/models/bitex_api_spec.rb +4 -4
- data/spec/models/buy_closing_flow_spec.rb +19 -24
- data/spec/models/buy_opening_flow_spec.rb +102 -83
- data/spec/models/order_book_simulator_spec.rb +5 -0
- data/spec/models/robot_spec.rb +7 -4
- data/spec/models/sell_closing_flow_spec.rb +21 -25
- data/spec/models/sell_opening_flow_spec.rb +100 -80
- data/spec/spec_helper.rb +3 -1
- data/spec/support/bitex_stubs.rb +80 -40
- data/spec/support/bitstamp/bitstamp_api_wrapper_stubs.rb +2 -2
- data/spec/support/bitstamp/bitstamp_stubs.rb +3 -3
- data/spec/support/vcr.rb +8 -0
- data/spec/support/webmock.rb +8 -0
- metadata +77 -10
@@ -1,11 +1,13 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe BitstampApiWrapper do
|
4
|
-
let(:api_wrapper) { described_class }
|
5
4
|
let(:taker_settings) do
|
6
5
|
BitexBot::SettingsClass.new(
|
7
6
|
bitstamp: {
|
8
|
-
api_key: '
|
7
|
+
api_key: 'BITSTAMP_API_KEY',
|
8
|
+
secret: 'BITSTAMP_API_SECRET',
|
9
|
+
client_id: 'BITSTAMP_USERNAME',
|
10
|
+
order_book: 'btcusd'
|
9
11
|
}
|
10
12
|
)
|
11
13
|
end
|
@@ -15,186 +17,178 @@ describe BitstampApiWrapper do
|
|
15
17
|
BitexBot::Robot.setup
|
16
18
|
end
|
17
19
|
|
18
|
-
|
19
|
-
url = 'https://www.bitstamp.net/api/v2/balance/btcusd/'
|
20
|
-
stub_stuff = stub_request(:post, url).with(headers: { 'User-Agent': BitexBot.user_agent })
|
20
|
+
let(:api_wrapper) { BitexBot::Robot.taker }
|
21
21
|
|
22
|
-
|
23
|
-
|
22
|
+
describe 'Sends User-Agent header' do
|
23
|
+
let(:url) { 'https://www.bitstamp.net/api/v2/balance/btcusd/' }
|
24
24
|
|
25
|
-
|
26
|
-
|
25
|
+
it do
|
26
|
+
stub_stuff = stub_request(:post, url).with(headers: { 'User-Agent': BitexBot.user_agent })
|
27
27
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
'btc_reserved' => reserved,
|
33
|
-
'btc_available' => available,
|
34
|
-
'usd_balance' => balance,
|
35
|
-
'usd_reserved' => reserved,
|
36
|
-
'usd_available' => balance,
|
37
|
-
'fee' => fee
|
38
|
-
}
|
28
|
+
# we don't care about the response
|
29
|
+
api_wrapper.balance rescue nil
|
30
|
+
|
31
|
+
expect(stub_stuff).to have_been_requested
|
39
32
|
end
|
40
33
|
end
|
41
34
|
|
42
|
-
|
43
|
-
|
35
|
+
describe '#currency_pair' do
|
36
|
+
it { expect(api_wrapper.currency_pair).to eq({ name: 'btcusd', base: 'btc', quote: 'usd' }) }
|
37
|
+
end
|
38
|
+
|
39
|
+
describe '#base' do
|
40
|
+
it { expect(api_wrapper.base).to eq('btc') }
|
41
|
+
end
|
44
42
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
balance.fiat.should be_a(ApiWrapper::Balance)
|
43
|
+
describe '#quote' do
|
44
|
+
it { expect(api_wrapper.quote).to eq('usd') }
|
45
|
+
end
|
49
46
|
|
50
|
-
|
51
|
-
|
52
|
-
crypto.reserved.should be_a(BigDecimal)
|
53
|
-
crypto.available.should be_a(BigDecimal)
|
47
|
+
describe '#balance', vcr: { cassette_name: 'bitstamp/balance' } do
|
48
|
+
subject { api_wrapper.balance }
|
54
49
|
|
55
|
-
|
56
|
-
fiat.total.should be_a(BigDecimal)
|
57
|
-
fiat.reserved.should be_a(BigDecimal)
|
58
|
-
fiat.available.should be_a(BigDecimal)
|
50
|
+
it { is_expected.to be_a(ApiWrapper::BalanceSummary) }
|
59
51
|
|
60
|
-
|
61
|
-
end
|
52
|
+
its(:members) { is_expected.to eq(%i[crypto fiat fee]) }
|
62
53
|
|
63
|
-
|
64
|
-
|
54
|
+
shared_examples_for 'currency balance' do |currency_type|
|
55
|
+
subject { api_wrapper.balance.send(currency_type) }
|
65
56
|
|
66
|
-
|
67
|
-
end
|
57
|
+
it { is_expected.to be_a(ApiWrapper::Balance) }
|
68
58
|
|
69
|
-
|
70
|
-
|
71
|
-
{
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
59
|
+
its(:members) { is_expected.to eq(%i[total reserved available]) }
|
60
|
+
|
61
|
+
its(:total) { is_expected.to be_a(BigDecimal) }
|
62
|
+
its(:reserved) { is_expected.to be_a(BigDecimal) }
|
63
|
+
its(:available) { is_expected.to be_a(BigDecimal) }
|
64
|
+
end
|
65
|
+
|
66
|
+
it_behaves_like 'currency balance', :crypto
|
67
|
+
it_behaves_like 'currency balance', :fiat
|
68
|
+
|
69
|
+
it 'fee' do
|
70
|
+
expect(api_wrapper.balance.fee).to be_a(BigDecimal)
|
76
71
|
end
|
77
72
|
end
|
78
73
|
|
79
|
-
|
80
|
-
|
74
|
+
describe '.order_book', vcr: { cassette_name: 'bitstamp/order_book' } do
|
75
|
+
subject { api_wrapper.order_book }
|
81
76
|
|
82
|
-
|
83
|
-
order_book.should be_a(ApiWrapper::OrderBook)
|
84
|
-
order_book.bids.all? { |bid| bid.should be_a(ApiWrapper::OrderSummary) }
|
85
|
-
order_book.asks.all? { |ask| ask.should be_a(ApiWrapper::OrderSummary) }
|
86
|
-
order_book.timestamp.should be_a(Integer)
|
77
|
+
it { is_expected.to be_a(ApiWrapper::OrderBook) }
|
87
78
|
|
88
|
-
|
89
|
-
bid.price.should be_a(BigDecimal)
|
90
|
-
bid.quantity.should be_a(BigDecimal)
|
79
|
+
its(:members) { is_expected.to eq(%i[timestamp bids asks]) }
|
91
80
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
end
|
81
|
+
its(:timestamp) { is_expected.to be_a(Integer) }
|
82
|
+
its(:bids) { is_expected.to be_a(Array) }
|
83
|
+
its(:asks) { is_expected.to be_a(Array) }
|
96
84
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
price: (price + 1).to_s,
|
105
|
-
amount: (amount + i).to_s,
|
106
|
-
datetime: 1.seconds.ago.strftime('%Y-%m-%d %H:%m:%S')
|
107
|
-
)
|
108
|
-
end
|
85
|
+
shared_examples_for :orders do |order_type|
|
86
|
+
subject { api_wrapper.order_book.send(order_type).sample }
|
87
|
+
|
88
|
+
it { is_expected.to be_a(ApiWrapper::OrderSummary) }
|
89
|
+
|
90
|
+
its(:price) { is_expected.to be_a(BigDecimal) }
|
91
|
+
its(:quantity) { is_expected.to be_a(BigDecimal) }
|
109
92
|
end
|
93
|
+
|
94
|
+
it_behaves_like :orders, :bids
|
95
|
+
it_behaves_like :orders, :asks
|
110
96
|
end
|
111
97
|
|
112
|
-
|
113
|
-
|
98
|
+
describe '.send_order' do
|
99
|
+
context 'successful buy', vcr: { cassette_name: 'bitstamp/orders/successful_buy' } do
|
100
|
+
subject { api_wrapper.send_order(:buy, 1.01, 1) }
|
114
101
|
|
115
|
-
|
102
|
+
it { is_expected.to be_a(ApiWrapper::Order) }
|
116
103
|
|
117
|
-
|
118
|
-
order.id.should be_a(String)
|
119
|
-
order.type.should be_a(Symbol)
|
120
|
-
order.price.should be_a(BigDecimal)
|
121
|
-
order.amount.should be_a(BigDecimal)
|
122
|
-
order.timestamp.should be_a(Integer)
|
104
|
+
its(:members) { is_expected.to eq(%i[id type price amount timestamp raw]) }
|
123
105
|
|
124
|
-
|
125
|
-
|
106
|
+
its(:id) { is_expected.to be_a(String) }
|
107
|
+
its(:type) { is_expected.to be_a(Symbol) }
|
108
|
+
its(:price) { is_expected.to be_a(BigDecimal) }
|
109
|
+
its(:amount) { is_expected.to be_a(BigDecimal) }
|
110
|
+
its(:timestamp) { is_expected.to be_a(Integer) }
|
111
|
+
its(:raw) { is_expected.to be_a(Bitstamp::Order) }
|
126
112
|
|
127
|
-
|
128
|
-
|
129
|
-
Bitstamp.orders.stub(:buy) do
|
130
|
-
raise OrderNotFound
|
131
|
-
end
|
113
|
+
context 'raw order' do
|
114
|
+
subject { api_wrapper.send_order(:buy, 1.01, 1).raw }
|
132
115
|
|
133
|
-
|
116
|
+
its(:id) { is_expected.to be_a(Integer) }
|
117
|
+
its(:type) { is_expected.to be_a(Integer) }
|
118
|
+
its(:price) { is_expected.to be_a(String) }
|
119
|
+
its(:amount) { is_expected.to be_a(String) }
|
120
|
+
its(:datetime) { is_expected.to be_a(String) }
|
121
|
+
end
|
134
122
|
end
|
135
|
-
end
|
136
123
|
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
double(
|
142
|
-
tid: i,
|
143
|
-
date: 1.seconds.ago.to_i,
|
144
|
-
price: (price + i).to_s,
|
145
|
-
amount: (amount + i).to_s
|
146
|
-
)
|
147
|
-
end
|
124
|
+
context 'failure sell', vcr: { cassette_name: 'bitstamp/orders/failure_sell' } do
|
125
|
+
subject { api_wrapper.send_order(:sell, 1_000, 1) }
|
126
|
+
|
127
|
+
it { is_expected.to be_nil }
|
148
128
|
end
|
149
129
|
end
|
150
130
|
|
151
|
-
|
152
|
-
|
131
|
+
describe '.transactions', vcr: { cassette_name: 'bitstamp/transactions' } do
|
132
|
+
subject { api_wrapper.transactions.sample }
|
133
|
+
|
134
|
+
it { is_expected.to be_a(ApiWrapper::Transaction) }
|
153
135
|
|
154
|
-
|
136
|
+
its(:members) { is_expected.to eq(%i[id price amount timestamp raw]) }
|
155
137
|
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
138
|
+
its(:id) { is_expected.to be_a(Integer) }
|
139
|
+
its(:price) { is_expected.to be_a(BigDecimal) }
|
140
|
+
its(:amount) { is_expected.to be_a(BigDecimal) }
|
141
|
+
its(:timestamp) { is_expected.to be_a(Integer) }
|
142
|
+
its(:raw) { is_expected.to be_a(Bitstamp::Transactions) }
|
161
143
|
end
|
162
144
|
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
fee: fee.to_s,
|
175
|
-
datetime: 1.seconds.ago.strftime('%Y-%m-%d %H:%m:%S')
|
176
|
-
)
|
177
|
-
end
|
145
|
+
describe '.user_transaction', vcr: { cassette_name: 'bitstamp/user_transactions' } do
|
146
|
+
subject { api_wrapper.user_transactions.sample }
|
147
|
+
|
148
|
+
it { is_expected.to be_a(ApiWrapper::UserTransaction) }
|
149
|
+
|
150
|
+
its(:members) { is_expected.to eq(%i[order_id fiat crypto crypto_fiat fee type timestamp]) }
|
151
|
+
|
152
|
+
# same user transactions haven't order_id
|
153
|
+
its(:order_id) do
|
154
|
+
is_expected.to be_a(Integer) if subject.order_id.present?
|
155
|
+
is_expected.to be_nil unless subject.order_id.present?
|
178
156
|
end
|
157
|
+
|
158
|
+
its(:fiat) { is_expected.to be_a(BigDecimal) }
|
159
|
+
its(:crypto) { is_expected.to be_a(BigDecimal) }
|
160
|
+
its(:crypto_fiat) { is_expected.to be_a(BigDecimal) }
|
161
|
+
its(:fee) { is_expected.to be_a(BigDecimal) }
|
162
|
+
its(:type) { is_expected.to be_a(Integer) }
|
163
|
+
its(:timestamp) { is_expected.to be_a(Integer) }
|
179
164
|
end
|
180
165
|
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
166
|
+
describe '.orders', vcr: { cassette_name: 'bitstamp/orders/all' } do
|
167
|
+
subject { api_wrapper.orders.sample }
|
168
|
+
|
169
|
+
it { is_expected.to be_a(ApiWrapper::Order) }
|
170
|
+
|
171
|
+
its(:id) { is_expected.to be_a(String) }
|
172
|
+
its(:type) { is_expected.to be_a(Symbol) }
|
173
|
+
its(:price) { is_expected.to be_a(BigDecimal) }
|
174
|
+
its(:amount) { is_expected.to be_a(BigDecimal) }
|
175
|
+
its(:amount) { is_expected.to be_a(BigDecimal) }
|
176
|
+
its(:timestamp) { is_expected.to be_a(Integer) }
|
177
|
+
end
|
178
|
+
|
179
|
+
describe '.find_lost', vcr: { cassette_name: 'bitstamp/orders/all', allow_playback_repeats: true } do
|
180
|
+
before(:each) { Timecop.freeze(Time.strptime(order.timestamp.to_s, '%s') - 3.minutes.ago) }
|
181
|
+
|
182
|
+
let(:order) { api_wrapper.orders.sample }
|
183
|
+
|
184
|
+
subject { api_wrapper.find_lost(order.type, order.price, order.amount) }
|
185
|
+
|
186
|
+
it { is_expected.to be_present }
|
193
187
|
end
|
194
188
|
|
195
|
-
|
196
|
-
|
189
|
+
describe '.cancel', vcr: { cassette_name: 'bitstamp/orders/all' } do
|
190
|
+
subject { api_wrapper.orders.sample }
|
197
191
|
|
198
|
-
|
192
|
+
it { is_expected.to respond_to(:cancel!) }
|
199
193
|
end
|
200
194
|
end
|
@@ -1,11 +1,15 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe ItbitApiWrapper do
|
4
|
-
let(:api_wrapper) { described_class }
|
5
4
|
let(:taker_settings) do
|
6
5
|
BitexBot::SettingsClass.new(
|
7
6
|
itbit: {
|
8
|
-
client_key: 'client-key',
|
7
|
+
client_key: 'client-key',
|
8
|
+
secret: 'secret',
|
9
|
+
user_id: 'user-id',
|
10
|
+
default_wallet_id: 'wallet-000',
|
11
|
+
sandbox: false,
|
12
|
+
order_book: 'xbtusd'
|
9
13
|
}
|
10
14
|
)
|
11
15
|
end
|
@@ -15,8 +19,10 @@ describe ItbitApiWrapper do
|
|
15
19
|
BitexBot::Robot.setup
|
16
20
|
end
|
17
21
|
|
22
|
+
let(:api_wrapper) { BitexBot::Robot.taker }
|
23
|
+
|
18
24
|
it 'Sends User-Agent header' do
|
19
|
-
url =
|
25
|
+
url = "https://api.itbit.com/v1/markets/#{api_wrapper.currency_pair[:name].upcase}/order_book"
|
20
26
|
stub_stuff = stub_request(:get, url).with(headers: { 'User-Agent': BitexBot.user_agent })
|
21
27
|
|
22
28
|
# We don't care about the response
|