Class: BitexBot::SellOpeningFlow

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

Overview

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

When created, a SellOpeningFlow places an Ask on Bitex for the calculated quantity and price, when the Ask is matched on Bitex an OpenSell is created to buy the same quantity for a lower price on the other exchange.

A SellOpeningFlow can be cancelled at any point, which will cancel the Bitex order and any orders on the remote exchange created from its OpenSell’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 SellOpeningFlow does is placing an Ask on Bitex, this is its unique id.

Returns:

  • (Object)

    the current value of order_id



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

def order_id
  @order_id
end

Class Method Details

.create_for_market(usd_balance, order_book, transactions, bitex_fee, other_fee, store) ⇒ SellOpeningFlow

Start a workflow for selling bitcoin on bitex and buying on the other exchange. The quantity to be sold on bitex is retrieved from Settings, if there is not enough BTC on bitex or USD on the other exchange then no order will be placed and an exception will be raised instead. The amount a SellOpeningFlow will try to sell and the price it will try to charge are derived from these parameters:

Parameters:

  • usd_balance (BigDecimal)

    amount of usd available in the other exchange that can be spent to balance this sale.

  • order_book ([price, quantity])

    a list of lists representing an ask 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.

  • 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 BTC on bitex or out of USD on the other exchange.



41
42
43
44
# File 'lib/bitex_bot/models/sell_opening_flow.rb', line 41

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

.get_bitex_price(btc_to_sell, usd_to_spend_re_buying) ⇒ Object



79
80
81
# File 'lib/bitex_bot/models/sell_opening_flow.rb', line 79

def self.get_bitex_price(btc_to_sell, usd_to_spend_re_buying) 
 (usd_to_spend_re_buying / btc_to_sell) * (1 + profit / 100.0)
end

.get_remote_value_to_use(value_to_use_needed, safest_price) ⇒ Object



71
72
73
# File 'lib/bitex_bot/models/sell_opening_flow.rb', line 71

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, bitcoins_to_use) ⇒ Object



66
67
68
69
# File 'lib/bitex_bot/models/sell_opening_flow.rb', line 66

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

.open_position_classObject



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

def self.open_position_class
  OpenSell
end

.order_classObject



58
59
60
# File 'lib/bitex_bot/models/sell_opening_flow.rb', line 58

def self.order_class
  Bitex::Ask
end

.profitObject



75
76
77
# File 'lib/bitex_bot/models/sell_opening_flow.rb', line 75

def self.profit
  store.selling_profit || Settings.selling.profit
end

.transaction_classObject



50
51
52
# File 'lib/bitex_bot/models/sell_opening_flow.rb', line 50

def self.transaction_class
  Bitex::Sell
end

.transaction_order_id(transaction) ⇒ Object



54
55
56
# File 'lib/bitex_bot/models/sell_opening_flow.rb', line 54

def self.transaction_order_id(transaction)
  transaction.ask_id
end

.value_to_useObject



62
63
64
# File 'lib/bitex_bot/models/sell_opening_flow.rb', line 62

def self.value_to_use
  store.selling_quantity_to_sell_per_order || Settings.selling.quantity_to_sell_per_order
end