bitflyer-cli 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/bitflyer-cli.gemspec +1 -1
- data/lib/bitflyer/cli.rb +6 -0
- data/lib/bitflyer/cli/authorization.rb +9 -0
- data/lib/bitflyer/cli/command/cancel_all_command.rb +9 -0
- data/lib/bitflyer/cli/command/counter_trade_command.rb +4 -16
- data/lib/bitflyer/cli/command/order_by_best_command.rb +5 -17
- data/lib/bitflyer/cli/command/summary_command.rb +9 -15
- data/lib/bitflyer/cli/has_http_client.rb +14 -0
- data/lib/bitflyer/cli/has_realtime_client.rb +7 -0
- data/lib/bitflyer/cli/model/position.rb +13 -6
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 361b2441ac3dd18926a9058a98ba0c873ac4b121
|
4
|
+
data.tar.gz: 84f1630b3e802554db67297d6f43503b789c362d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2de2c18dc14311034f603ebf5680128ddca0e66f302807305cdef612fa957c0a55ec9421345a447e0efcd203f4de5331832be4e13276b80733cff33f9adbdf8a
|
7
|
+
data.tar.gz: ba346a5b99a94c78416ee4e504a1ed6806edcf8e45aa433c06953d77576bc474c01b65c6a6071caa644c523ff9065f102a6db89081fbc0da73f9348d205b9395
|
data/Gemfile.lock
CHANGED
data/bitflyer-cli.gemspec
CHANGED
data/lib/bitflyer/cli.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'thor'
|
2
|
+
require 'bitflyer/cli/command/cancel_all_command'
|
2
3
|
require 'bitflyer/cli/command/counter_trade_command'
|
3
4
|
require 'bitflyer/cli/command/summary_command'
|
4
5
|
require 'bitflyer/cli/command/order_by_best_command'
|
@@ -21,5 +22,10 @@ module Bitflyer
|
|
21
22
|
def counter_trade
|
22
23
|
CounterTradeCommand.new.run
|
23
24
|
end
|
25
|
+
|
26
|
+
desc 'cancel_all', 'cancel all of orders'
|
27
|
+
def cancel_all
|
28
|
+
CancelAllCommand.new.run
|
29
|
+
end
|
24
30
|
end
|
25
31
|
end
|
@@ -1,15 +1,13 @@
|
|
1
|
-
require 'bitflyer'
|
1
|
+
require 'bitflyer/cli/has_http_client'
|
2
2
|
|
3
3
|
class CounterTradeCommand
|
4
|
-
|
5
|
-
@http_client = Bitflyer.http_private_client(api_key, api_secret)
|
6
|
-
end
|
4
|
+
include HasHTTPClient
|
7
5
|
|
8
6
|
def run
|
9
|
-
position = Position.new(
|
7
|
+
position = Position.new(http_private_client.positions)
|
10
8
|
size = position.size.abs
|
11
9
|
type = position.size > 0 ? 'SELL' : 'BUY'
|
12
|
-
response =
|
10
|
+
response = http_private_client.send_child_order(
|
13
11
|
product_code: 'FX_BTC_JPY',
|
14
12
|
child_order_type: 'MARKET',
|
15
13
|
side: type,
|
@@ -21,14 +19,4 @@ class CounterTradeCommand
|
|
21
19
|
puts "Clear position #{type} / #{size}"
|
22
20
|
end
|
23
21
|
end
|
24
|
-
|
25
|
-
private
|
26
|
-
|
27
|
-
def api_key
|
28
|
-
ENV['BITFLYER_API_KEY']
|
29
|
-
end
|
30
|
-
|
31
|
-
def api_secret
|
32
|
-
ENV['BITFLYER_API_SECRET']
|
33
|
-
end
|
34
22
|
end
|
@@ -1,10 +1,7 @@
|
|
1
|
-
require 'bitflyer'
|
1
|
+
require 'bitflyer/cli/has_http_client'
|
2
2
|
|
3
3
|
class OrderByBestCommand
|
4
|
-
|
5
|
-
@http_public_client = Bitflyer.http_public_client
|
6
|
-
@http_private_client = Bitflyer.http_private_client(api_key, api_secret)
|
7
|
-
end
|
4
|
+
include HasHTTPClient
|
8
5
|
|
9
6
|
def run(options)
|
10
7
|
amount = options.amount
|
@@ -15,30 +12,21 @@ class OrderByBestCommand
|
|
15
12
|
return
|
16
13
|
end
|
17
14
|
|
18
|
-
ticker =
|
15
|
+
ticker = http_public_client.ticker('FX_BTC_JPY')
|
19
16
|
price = type == 'buy' ? ticker['best_bid'] : ticker['best_ask']
|
20
17
|
|
21
|
-
response =
|
18
|
+
response = http_private_client.send_child_order(
|
22
19
|
product_code: 'FX_BTC_JPY',
|
23
20
|
child_order_type: 'LIMIT',
|
24
21
|
side: type.upcase,
|
25
22
|
price: price,
|
26
23
|
size: amount
|
27
24
|
)
|
25
|
+
|
28
26
|
if response['child_order_acceptance_id'].nil?
|
29
27
|
puts 'An error has occurred' + response.to_s
|
30
28
|
else
|
31
29
|
puts "An order is created #{type} / #{price} / #{amount}"
|
32
30
|
end
|
33
31
|
end
|
34
|
-
|
35
|
-
private
|
36
|
-
|
37
|
-
def api_key
|
38
|
-
ENV['BITFLYER_API_KEY']
|
39
|
-
end
|
40
|
-
|
41
|
-
def api_secret
|
42
|
-
ENV['BITFLYER_API_SECRET']
|
43
|
-
end
|
44
32
|
end
|
@@ -1,16 +1,18 @@
|
|
1
|
-
require 'bitflyer'
|
1
|
+
require 'bitflyer/cli/has_http_client'
|
2
|
+
require 'bitflyer/cli/has_realtime_client'
|
2
3
|
require 'bitflyer/cli/ext/string'
|
3
4
|
require 'bitflyer/cli/model/position'
|
4
5
|
|
5
6
|
class SummaryCommand
|
7
|
+
include HasHTTPClient
|
8
|
+
include HasRealtimeClient
|
9
|
+
|
6
10
|
BUFFER_SIZE = 30.freeze
|
7
11
|
|
8
12
|
def initialize
|
9
13
|
@current_price = 0.0
|
10
|
-
@http_clinet = Bitflyer.http_private_client(api_key, api_secret)
|
11
|
-
@realtime_client = Bitflyer.realtime_client
|
12
14
|
|
13
|
-
|
15
|
+
realtime_client.executions_fx_btc_jpy = ->(message) {
|
14
16
|
message.each { |m| @current_price = m['price'].to_f }
|
15
17
|
}
|
16
18
|
|
@@ -30,7 +32,7 @@ class SummaryCommand
|
|
30
32
|
while true
|
31
33
|
print "\e[3F\e[0J"
|
32
34
|
print "Current: " + @current_price.to_i.to_s.split_by_comma
|
33
|
-
print "\nPosition: " + "#{@position.average} * #{@position.size}".to_s.split_by_comma
|
35
|
+
print "\nPosition: " + "#{@position.average} * #{@position.size.to_f}".to_s.split_by_comma
|
34
36
|
print "\nSpread: " + spread.to_s.split_by_comma.color_with_number(spread)
|
35
37
|
print "\nBalance: " + (@balance + profit).to_s.split_by_comma.ljust(15, ' ') + "(#{profit.to_s.split_by_comma.color_with_number(profit)})"
|
36
38
|
sleep 0.1
|
@@ -39,17 +41,9 @@ class SummaryCommand
|
|
39
41
|
|
40
42
|
private
|
41
43
|
|
42
|
-
def api_key
|
43
|
-
ENV['BITFLYER_API_KEY']
|
44
|
-
end
|
45
|
-
|
46
|
-
def api_secret
|
47
|
-
ENV['BITFLYER_API_SECRET']
|
48
|
-
end
|
49
|
-
|
50
44
|
def update_balance
|
51
|
-
@position = Position.new(
|
52
|
-
@balance =
|
45
|
+
@position = Position.new(http_private_client.positions)
|
46
|
+
@balance = http_private_client.collateral['collateral'].to_i
|
53
47
|
end
|
54
48
|
|
55
49
|
def profit
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'bitflyer'
|
2
|
+
require 'bitflyer/cli/authorization'
|
3
|
+
|
4
|
+
module HasHTTPClient
|
5
|
+
include Authorization
|
6
|
+
|
7
|
+
def http_public_client
|
8
|
+
@http_public_client ||= Bitflyer.http_public_client
|
9
|
+
end
|
10
|
+
|
11
|
+
def http_private_client
|
12
|
+
@http_private_client ||= Bitflyer.http_private_client(api_key, api_secret)
|
13
|
+
end
|
14
|
+
end
|
@@ -1,11 +1,14 @@
|
|
1
|
+
require 'bigdecimal'
|
2
|
+
require 'bigdecimal/util'
|
3
|
+
|
1
4
|
class Position
|
2
5
|
def initialize(positions)
|
3
6
|
@positions = positions
|
4
7
|
end
|
5
8
|
|
6
9
|
def profit(current_price)
|
7
|
-
@positions.inject(0.
|
8
|
-
sum + (current_price - position['price'].
|
10
|
+
@positions.inject('0'.to_d) do |sum, position|
|
11
|
+
sum + (current_price - position['price'].to_s.to_d) * position['size'].to_s.to_d * coefficient(position)
|
9
12
|
end
|
10
13
|
end
|
11
14
|
|
@@ -14,12 +17,16 @@ class Position
|
|
14
17
|
end
|
15
18
|
|
16
19
|
def sum
|
17
|
-
@positions.inject(0.
|
18
|
-
sum + position['price'].to_f * position['size'].to_f
|
19
|
-
end
|
20
|
+
@positions.inject('0'.to_d) { |sum, position| sum + position['price'].to_s.to_d * position['size'].to_s.to_d }
|
20
21
|
end
|
21
22
|
|
22
23
|
def size
|
23
|
-
@positions.inject(0.
|
24
|
+
@positions.inject('0'.to_d) { |sum, position| sum + position['size'].to_s.to_d * coefficient(position) }
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def coefficient(position)
|
30
|
+
position['side'] == 'BUY' ? 1.0 : -1.0
|
24
31
|
end
|
25
32
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bitflyer-cli
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yuji Ueki
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-08-
|
11
|
+
date: 2017-08-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: thor
|
@@ -111,10 +111,14 @@ files:
|
|
111
111
|
- bitflyer-cli.gemspec
|
112
112
|
- exe/bitflyer
|
113
113
|
- lib/bitflyer/cli.rb
|
114
|
+
- lib/bitflyer/cli/authorization.rb
|
115
|
+
- lib/bitflyer/cli/command/cancel_all_command.rb
|
114
116
|
- lib/bitflyer/cli/command/counter_trade_command.rb
|
115
117
|
- lib/bitflyer/cli/command/order_by_best_command.rb
|
116
118
|
- lib/bitflyer/cli/command/summary_command.rb
|
117
119
|
- lib/bitflyer/cli/ext/string.rb
|
120
|
+
- lib/bitflyer/cli/has_http_client.rb
|
121
|
+
- lib/bitflyer/cli/has_realtime_client.rb
|
118
122
|
- lib/bitflyer/cli/model/position.rb
|
119
123
|
homepage: https://github.com/unhappychoice/bitflyer-cli
|
120
124
|
licenses:
|