Class: Ubea::Exchange::KrakenBase

Inherits:
Base
  • Object
show all
Defined in:
lib/ubea/exchanges/kraken_base.rb

Direct Known Subclasses

KrakenEur, KrakenUsd

Constant Summary collapse

BadResponseError =
Class.new(Exception)
RateError =
Class.new(Exception)

Instance Attribute Summary

Attributes inherited from Base

#last_rtt, #order_book, #updated_at

Instance Method Summary collapse

Methods inherited from Base

#id, #short_name

Instance Method Details

#balanceObject



38
39
40
41
42
43
44
45
# File 'lib/ubea/exchanges/kraken_base.rb', line 38

def balance
  balance = post_private("Balance")

  OpenStruct.new(
    fiat: Money.new(balance["Z#{fiat_currency}"], fiat_currency),
    xbt: BigDecimal.new(balance["XXBT"]),
  ).freeze
end

#exchange_settingsObject

Raises:

  • (NotImplementedError)


20
21
22
# File 'lib/ubea/exchanges/kraken_base.rb', line 20

def exchange_settings
  raise NotImplementedError
end

#fiat_currencyObject

Raises:

  • (NotImplementedError)


16
17
18
# File 'lib/ubea/exchanges/kraken_base.rb', line 16

def fiat_currency
  raise NotImplementedError
end

#nameObject



12
13
14
# File 'lib/ubea/exchanges/kraken_base.rb', line 12

def name
  "Kraken (#{fiat_currency})"
end

#open_ordersObject



81
82
83
# File 'lib/ubea/exchanges/kraken_base.rb', line 81

def open_orders
  post_private("OpenOrders")["open"]
end

#open_orders?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/ubea/exchanges/kraken_base.rb', line 85

def open_orders?
  !open_orders.empty?
end

#refresh_order_book!Object



28
29
30
31
32
33
34
35
36
# File 'lib/ubea/exchanges/kraken_base.rb', line 28

def refresh_order_book!
  json = get_json("https://api.kraken.com/0/public/Depth?pair=XBT#{fiat_currency}") or return

  asks = format_asks_bids(json["result"]["XXBTZ#{fiat_currency}"]["asks"])
  bids = format_asks_bids(json["result"]["XXBTZ#{fiat_currency}"]["bids"])

  mark_as_refreshed
  @order_book = OrderBook.new(asks: asks, bids: bids)
end

#trade!(args, simulate: false) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/ubea/exchanges/kraken_base.rb', line 47

def trade!(args, simulate: false)
  params = {
    pair: "XXBTZ#{fiat_currency}",
    type: args.fetch(:type),
    volume: args.fetch(:volume),
  }

  ordertype = args.fetch(:ordertype, "limit")

  case ordertype
  when "market"
    params.merge!(
      ordertype: "market",
    )
  when "limit"
    params.merge!(
      price: args.fetch(:price),
      ordertype: "limit",
    )
  else
    raise "Unknown ordertype"
  end

  if simulate
    params.merge!(
      validate: true
    )
  end

  Log.debug params
  trade = post_private("AddOrder", params)
  Log.info trade
end

#trade_feeObject



24
25
26
# File 'lib/ubea/exchanges/kraken_base.rb', line 24

def trade_fee
  BigDecimal.new("0.0035").freeze # 0.35%
end