Class: BitexBot::BuyOpeningFlow

Inherits:
OpeningFlow
  • Object
show all
Defined in:
lib/bitex_bot/models/buy_opening_flow.rb

Overview

A workflow for buying bitcoin in Bitex and selling on another exchange. The BuyOpeningFlow factory function estimates how much you could sell on the other exchange and calculates a reasonable price taking into account the remote order book and the recent operated volume.

When created, a BuyOpeningFlow places a Bid on Bitex for the calculated amount and price, when the Bid is matched on Bitex an OpenBuy is created to sell the matched amount for a higher price on the other exchange.

A BuyOpeningFlow can be cancelled at any point, which will cancel the Bitex order and any orders on the remote exchange created from its OpenBuy’s

Instance Attribute Summary collapse

Class Method Summary collapse

Methods inherited from OpeningFlow

active, active_transaction?, calc_remote_value, create_open_position!, create_order!, enough_funds?, enough_remote_funds?, expected_order_book?, #finalise!, maker_plus, old_active, open_position?, sought_transaction?, #statuses, sync_open_positions

Instance Attribute Details

#order_idObject

The first thing a BuyOpeningFlow does is placing a Bid on Bitex, this is its unique id.

Returns:

  • (Object)

    the current value of order_id



13
14
15
# File 'lib/bitex_bot/models/buy_opening_flow.rb', line 13

def order_id
  @order_id
end

Class Method Details

.create_for_market(btc_balance, order_book, transactions, maker_fee, taker_fee, store) ⇒ BuyOpeningFlow

Start a workflow for buying bitcoin on bitex and selling on the other exchange. The amount to be spent on bitex is retrieved from Settings, if there is not enough USD on bitex or BTC on the other exchange then no order will be placed and an exception will be raised instead.

The amount a BuyOpeningFlow will try to buy and the price it will try to buy at are derived from these parameters:

Parameters:

  • btc_balance (BigDecimal)

    amount of btc available in the other exchange that can be sold to balance this purchase.

  • order_book ([price, quantity])

    a list of lists representing a bid order book in the other exchange.

  • transactions (Hash)

    a list of hashes representing all transactions in the other exchange: Each hash contains ‘date’, ‘tid’, ‘price’ and ‘amount’, where ‘amount’ is the BTC transacted.

  • maker_fee (BigDecimal)

    the transaction fee to pay on maker exchange.

  • taker_fee (BigDecimal)

    the transaction fee to pay on taker exchange.

  • store (Store)

    An updated config for this robot, mainly to use for profit.

Returns:

Raises:

  • (CannotCreateFlow)

    If there’s any problem creating this flow, for example when you run out of USD on bitex or out of BTC on the other exchange.



31
32
33
# File 'lib/bitex_bot/models/buy_opening_flow.rb', line 31

def self.create_for_market(btc_balance, order_book, transactions, maker_fee, taker_fee, store)
  super
end

.fx_rateObject

end: create_for_market helpers



77
78
79
# File 'lib/bitex_bot/models/buy_opening_flow.rb', line 77

def self.fx_rate
  Settings.buying_fx_rate
end

.maker_price(crypto_to_resell) ⇒ Object

create_for_market helpers



52
53
54
# File 'lib/bitex_bot/models/buy_opening_flow.rb', line 52

def self.maker_price(crypto_to_resell)
  value_to_use / crypto_to_resell * (1 - profit / 100)
end

.open_position_classObject



40
41
42
# File 'lib/bitex_bot/models/buy_opening_flow.rb', line 40

def self.open_position_class
  OpenBuy
end

.order_classObject



56
57
58
# File 'lib/bitex_bot/models/buy_opening_flow.rb', line 56

def self.order_class
  Bitex::Bid
end

.profitObject



60
61
62
# File 'lib/bitex_bot/models/buy_opening_flow.rb', line 60

def self.profit
  store.buying_profit || Settings.buying.profit
end

.remote_value_to_use(value_to_use_needed, safest_price) ⇒ Object



64
65
66
# File 'lib/bitex_bot/models/buy_opening_flow.rb', line 64

def self.remote_value_to_use(value_to_use_needed, safest_price)
  (value_to_use_needed / fx_rate) / safest_price
end

.safest_price(transactions, order_book, amount_to_use) ⇒ Object



68
69
70
# File 'lib/bitex_bot/models/buy_opening_flow.rb', line 68

def self.safest_price(transactions, order_book, amount_to_use)
  OrderBookSimulator.run(Settings.time_to_live, transactions, order_book, amount_to_use / fx_rate, nil)
end

.transaction_classObject

sought_transaction helpers



46
47
48
# File 'lib/bitex_bot/models/buy_opening_flow.rb', line 46

def self.transaction_class
  Bitex::Buy
end

.transaction_order_id(transaction) ⇒ Object

sync_open_positions helpers



36
37
38
# File 'lib/bitex_bot/models/buy_opening_flow.rb', line 36

def self.transaction_order_id(transaction)
  transaction.bid_id
end

.value_to_useObject



72
73
74
# File 'lib/bitex_bot/models/buy_opening_flow.rb', line 72

def self.value_to_use
  store.buying_amount_to_spend_per_order || Settings.buying.amount_to_spend_per_order
end