trade-o-matic 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/trade-o-matic/adapters/base/raw_account_order.rb +7 -0
- data/lib/trade-o-matic/adapters/base/raw_balance.rb +7 -0
- data/lib/trade-o-matic/adapters/base/raw_resource.rb +31 -0
- data/lib/trade-o-matic/adapters/bitstamp_backend.rb +166 -0
- data/lib/trade-o-matic/adapters/fake_backend.rb +235 -0
- data/lib/trade-o-matic/adapters/itbit_backend.rb +57 -0
- data/lib/trade-o-matic/adapters/surbtc_backend.rb +149 -0
- data/lib/trade-o-matic/cli.rb +6 -22
- data/lib/trade-o-matic/converters/compound_converter.rb +13 -0
- data/lib/trade-o-matic/converters/fixed_converter.rb +0 -7
- data/lib/trade-o-matic/converters/inverse_converter.rb +13 -0
- data/lib/trade-o-matic/converters/json_api_converter.rb +20 -0
- data/lib/trade-o-matic/converters/sync_converter.rb +1 -11
- data/lib/trade-o-matic/core/account.rb +75 -0
- data/lib/trade-o-matic/core/account_order.rb +99 -0
- data/lib/trade-o-matic/core/account_proxy.rb +48 -0
- data/lib/trade-o-matic/core/balance.rb +57 -0
- data/lib/trade-o-matic/core/exchange.rb +19 -0
- data/lib/trade-o-matic/core/market.rb +29 -0
- data/lib/trade-o-matic/core/market_loader.rb +29 -0
- data/lib/trade-o-matic/services/backend_factory.rb +46 -0
- data/lib/trade-o-matic/structs/ask_slope.rb +4 -1
- data/lib/trade-o-matic/structs/bid_slope.rb +4 -1
- data/lib/trade-o-matic/structs/book.rb +52 -0
- data/lib/trade-o-matic/structs/converter.rb +2 -3
- data/lib/trade-o-matic/structs/currency.rb +126 -21
- data/lib/trade-o-matic/structs/currency_pair.rb +31 -5
- data/lib/trade-o-matic/structs/order.rb +53 -1
- data/lib/trade-o-matic/structs/price.rb +9 -8
- data/lib/trade-o-matic/structs/slope.rb +11 -34
- data/lib/trade-o-matic/support/converter_configurator.rb +35 -0
- data/lib/trade-o-matic/version.rb +1 -1
- data/lib/trade-o-matic.rb +29 -1
- metadata +59 -43
- data/lib/trade-o-matic/adapters/bitstamp_account.rb +0 -63
- data/lib/trade-o-matic/flows/ask_replicator_flow.rb +0 -50
- data/lib/trade-o-matic/generators/linear_generator.rb +0 -37
- data/lib/trade-o-matic/structs/market.rb +0 -28
@@ -1,63 +0,0 @@
|
|
1
|
-
require 'rest-client'
|
2
|
-
require 'json'
|
3
|
-
|
4
|
-
module Trader
|
5
|
-
|
6
|
-
# TODO: separate market information from account
|
7
|
-
|
8
|
-
class BitstampAccount
|
9
|
-
|
10
|
-
def initialize
|
11
|
-
end
|
12
|
-
|
13
|
-
def market_snapshot
|
14
|
-
market = Market.new :BTC, :Bitstamp_USD
|
15
|
-
unpack_order_book market, fetch_raw_order_book
|
16
|
-
unpack_transactions market, fetch_raw_transactions
|
17
|
-
market
|
18
|
-
end
|
19
|
-
|
20
|
-
def refresh
|
21
|
-
end
|
22
|
-
|
23
|
-
def base_balance # XBT in an XBT/USD pair
|
24
|
-
Price.new :BTC, 0.0
|
25
|
-
end
|
26
|
-
|
27
|
-
def quote_balance # USD in an XBT/USD pair
|
28
|
-
Price.new :Bitstamp_USD, 20000.0
|
29
|
-
end
|
30
|
-
|
31
|
-
def create_order
|
32
|
-
end
|
33
|
-
|
34
|
-
def cancel_order
|
35
|
-
end
|
36
|
-
|
37
|
-
def orders
|
38
|
-
end
|
39
|
-
|
40
|
-
private
|
41
|
-
|
42
|
-
def fetch_raw_order_book
|
43
|
-
JSON.parse(RestClient.get "https://www.bitstamp.net/api/order_book/")
|
44
|
-
end
|
45
|
-
|
46
|
-
def fetch_raw_transactions
|
47
|
-
JSON.parse(RestClient.get "https://www.bitstamp.net/api/transactions/")
|
48
|
-
end
|
49
|
-
|
50
|
-
def unpack_order_book(_market, _ob)
|
51
|
-
_ob['bids'].each { |o| _market.add_bid(o[0].to_f, o[1].to_f) }
|
52
|
-
_ob['asks'].each { |o| _market.add_ask(o[0].to_f, o[1].to_f) }
|
53
|
-
end
|
54
|
-
|
55
|
-
def unpack_transactions(_market, _tx)
|
56
|
-
_tx.each do |t|
|
57
|
-
time = Time.at(t['date'].to_i)
|
58
|
-
market.add_buy(t['price'].to_f, t['amount'].to_f, time) if ['type'] == 0
|
59
|
-
market.add_sell(t['price'].to_f, t['amount'].to_f, time) if ['type'] == 1
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
@@ -1,50 +0,0 @@
|
|
1
|
-
module Trader
|
2
|
-
|
3
|
-
class AskReplicatorFlow
|
4
|
-
|
5
|
-
def initialize(_source, _target, _generator)
|
6
|
-
@source = _source
|
7
|
-
@target = _target
|
8
|
-
@generator = _generator
|
9
|
-
end
|
10
|
-
|
11
|
-
def run(_dry)
|
12
|
-
|
13
|
-
# TODO: assert that both source and target base currencies are the same.
|
14
|
-
|
15
|
-
market = source.market_snapshot
|
16
|
-
|
17
|
-
skip_volume = market.ask_slope.volume 60
|
18
|
-
|
19
|
-
start_price = market.ask_slope.price skip_volume
|
20
|
-
|
21
|
-
available_volume = market.ask_slope.assess source.quote_balance, skip_volume # TODO: check
|
22
|
-
|
23
|
-
# available_volume = target.base_balance if target.base_balance < available_volume
|
24
|
-
|
25
|
-
orders = generator.ask start_price, available_volume
|
26
|
-
|
27
|
-
orders = adjust_orders_to_slope orders, market.ask_slope
|
28
|
-
|
29
|
-
# orders.each { |o| o.price.convert(target.quote_currency) }
|
30
|
-
|
31
|
-
# executed = target_position.sync orders
|
32
|
-
# executed.each do |order|
|
33
|
-
|
34
|
-
# end
|
35
|
-
|
36
|
-
# TODO: transform orders price to target currency
|
37
|
-
|
38
|
-
# TODO: sync orders
|
39
|
-
|
40
|
-
end
|
41
|
-
|
42
|
-
private
|
43
|
-
|
44
|
-
attr_reader :source, :target, :generator
|
45
|
-
|
46
|
-
def adjust_orders_to_slope(_orders, _slope)
|
47
|
-
_orders
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
@@ -1,37 +0,0 @@
|
|
1
|
-
module Trader
|
2
|
-
class LinearGenerator
|
3
|
-
|
4
|
-
def initialize(_options)
|
5
|
-
@price_spread = _options[:spread]
|
6
|
-
@price_step = _options[:step]
|
7
|
-
end
|
8
|
-
|
9
|
-
def bid(_start_price, _volume)
|
10
|
-
generate _start_price, _volume, -1
|
11
|
-
end
|
12
|
-
|
13
|
-
def ask(_start_price, _volume)
|
14
|
-
generate _start_price, _volume, 1
|
15
|
-
end
|
16
|
-
|
17
|
-
private
|
18
|
-
|
19
|
-
def generate(_start_price, _volume, _mult)
|
20
|
-
buckets = (@price_spread.amount / @price_step.amount).floor
|
21
|
-
|
22
|
-
# make sure price step is using the same currency as generated prices
|
23
|
-
conv_price_step = @price_step.convert(_start_price.currency).amount
|
24
|
-
|
25
|
-
buckets.floor.times.map do |i|
|
26
|
-
price = _start_price.amount + _mult * conv_price_step * i
|
27
|
-
volume = _volume.amount / buckets
|
28
|
-
|
29
|
-
Order.new(
|
30
|
-
Price.new(_start_price.currency, price),
|
31
|
-
Price.new(_volume.currency, volume)
|
32
|
-
)
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
end
|
37
|
-
end
|
@@ -1,28 +0,0 @@
|
|
1
|
-
module Trader
|
2
|
-
class Market
|
3
|
-
|
4
|
-
attr_reader :pair, :ask_slope, :bid_slope
|
5
|
-
|
6
|
-
def initialize(_base, _quote)
|
7
|
-
@pair = CurrencyPair.new _base, _quote
|
8
|
-
@ask_slope = AskSlope.new @pair
|
9
|
-
@bid_slope = BidSlope.new @pair
|
10
|
-
end
|
11
|
-
|
12
|
-
def add_bid(_price, _volume)
|
13
|
-
@bid_slope.add_order(_price, _volume)
|
14
|
-
end
|
15
|
-
|
16
|
-
def add_ask(_price, _volume)
|
17
|
-
@ask_slope.add_order(_price, _volume)
|
18
|
-
end
|
19
|
-
|
20
|
-
def add_sell(_price, _volume, _time)
|
21
|
-
@bid_slope.add_transaction(_price, _volume, _time)
|
22
|
-
end
|
23
|
-
|
24
|
-
def add_buy(_price, _volume, _time)
|
25
|
-
@bid_slope.add_transaction(_price, _volume, _time)
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|