Skip to content

Commit

Permalink
Merge pull request #1 from unhappychoice/twap-command
Browse files Browse the repository at this point in the history
TWAP ordering
  • Loading branch information
unhappychoice committed Aug 7, 2017
2 parents 117cbc6 + a17edba commit b7b1523
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 19 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ export BITFLYER_API_SECRET=your-bitflyer-api-secret

```
Commands:
bitflyer cancel_all # cancel all of orders
bitflyer counter_trade # clear all positions
bitflyer help [COMMAND] # Describe available commands or one specific command
bitflyer order_by_best # create limit order by best price in the board
bitflyer summary # show current balance information
bitflyer cancel_all # cancel all of orders
bitflyer counter_trade # clear all positions
bitflyer help [COMMAND] # Describe available commands or one specific command
bitflyer order_by_best -a=amount -t=buy/sell # create limit order by best price in the board
bitflyer order_by_twap -a=amount -i=second -n=N -t=buy/sell # trade specified amount N times at specified intervals (TWAP algorithm).
bitflyer summary # show current balance information
```

## Contributing
Expand Down
28 changes: 19 additions & 9 deletions lib/bitflyer/cli.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
require 'thor'
require 'bitflyer/cli/command/cancel_all_command'
require 'bitflyer/cli/command/counter_trade_command'
require 'bitflyer/cli/command/summary_command'
require 'bitflyer/cli/command/order_by_best_command'
require 'bitflyer/cli/command/order_by_twap_command'
require 'bitflyer/cli/command/summary_command'

module Bitflyer
class CLI < Thor
Expand All @@ -11,21 +12,30 @@ def summary
SummaryCommand.new.run
end

desc 'order_by_best', 'create limit order by best price in the board'
method_option :amount, aliases: 'a', type: :numeric, banner: 'amount'
method_option :type, aliases: 't', type: :string, banner: 'buy or sell'
def order_by_best
OrderByBestCommand.new.run(options)
desc 'cancel_all', 'cancel all of orders'
def cancel_all
CancelAllCommand.new.run
end

desc 'counter_trade', 'clear all positions'
def counter_trade
CounterTradeCommand.new.run
end

desc 'cancel_all', 'cancel all of orders'
def cancel_all
CancelAllCommand.new.run
desc 'order_by_best', 'create limit order by best price in the board'
method_option :amount, aliases: 'a', type: :numeric, banner: 'amount', required: true
method_option :type, aliases: 't', type: :string, banner: 'buy/sell', required: true
def order_by_best
OrderByBestCommand.new.run(options)
end

desc 'order_by_twap', 'order by using TWAP algorithm'
method_option :amount, aliases: 'a', type: :numeric, banner: 'amount', required: true
method_option :type, aliases: 't', type: :string, banner: 'buy/sell', required: true
method_option :number_of_times, aliases: 'n', type: :numeric, banner: 'N', required: true
method_option :interval, aliases: 'i', type: :numeric, banner: 'second', required: true
def order_by_twap
OrderByTWAPCommand.new.run(options)
end
end
end
5 changes: 0 additions & 5 deletions lib/bitflyer/cli/command/order_by_best_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ class OrderByBestCommand
def run(options)
amount = options.amount
type = options.type
if amount == nil || type == nil
puts 'You need set amount by -a and type by -t'
puts 'ex: bitflyer order_by_best -a 10 -t buy'
return
end

ticker = http_public_client.ticker('FX_BTC_JPY')
price = type == 'buy' ? ticker['best_bid'] : ticker['best_ask']
Expand Down
34 changes: 34 additions & 0 deletions lib/bitflyer/cli/command/order_by_twap_command.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'bitflyer/cli/has_http_client'

class OrderByTWAPCommand
include HasHTTPClient

def run(options)
type = options.type
amount = options.amount
number = options.number_of_times
interval = options.interval

number.to_i.times do |_|
order(type, amount)
sleep interval
end
end

private

def order(type, amount)
response = http_private_client.send_child_order(
product_code: 'FX_BTC_JPY',
child_order_type: 'MARKET',
side: type.upcase,
size: amount
)

if response['child_order_acceptance_id'].nil?
puts "An error has occurred\n" + response.to_s
else
puts response.to_s
end
end
end

0 comments on commit b7b1523

Please sign in to comment.