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 orderbook 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, #executing?, #finalise!, #finalised?, old_active, #settling?, 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



16
17
18
# File 'lib/bitex_bot/models/buy_opening_flow.rb', line 16

def order_id
  @order_id
end

Class Method Details

.create_for_market(btc_balance, order_book, transactions, bitex_fee, other_fee) ⇒ 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.

  • bitex_fee (BigDecimal)

    the transaction fee to pay on bitex.

  • other_fee (BigDecimal)

    the transaction fee to pay on the other exchange.

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.



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

def self.create_for_market(btc_balance, order_book, transactions,
  bitex_fee, other_fee)
  super
end

.get_bitex_price(usd_to_spend, bitcoin_to_resell) ⇒ Object



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

def self.get_bitex_price(usd_to_spend, bitcoin_to_resell) 
 (usd_to_spend / bitcoin_to_resell) * (1 - Settings.buying.profit / 100.0)
end

.get_remote_value_to_use(value_to_use_needed, safest_price) ⇒ Object



74
75
76
# File 'lib/bitex_bot/models/buy_opening_flow.rb', line 74

def self.get_remote_value_to_use(value_to_use_needed, safest_price)
  value_to_use_needed / safest_price
end

.get_safest_price(transactions, order_book, dollars_to_use) ⇒ Object



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

def self.get_safest_price(transactions, order_book, dollars_to_use)
  OrderBookSimulator.run(Settings.time_to_live, transactions,
    order_book, dollars_to_use, nil)
end

.open_position_classObject



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

def self.open_position_class
  OpenBuy
end

.order_classObject



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

def self.order_class
  Bitex::Bid
end

.profitObject



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

def self.profit
  Settings.buying.profit
end

.transaction_classObject



49
50
51
# File 'lib/bitex_bot/models/buy_opening_flow.rb', line 49

def self.transaction_class
  Bitex::Buy
end

.transaction_order_id(transaction) ⇒ Object



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

def self.transaction_order_id(transaction)
  transaction.bid_id
end

.value_to_useObject



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

def self.value_to_use
  Settings.buying.amount_to_spend_per_order
end