Class: Fyb::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/fyb/client.rb

Overview

Client class containing all the methods needed to buy and sell btc.

Instance Method Summary collapse

Instance Method Details

#askObject

Returns the current ask price.



5
6
7
# File 'lib/fyb/client.rb', line 5

def ask
  BigDecimal Fyb.public.ticker.perform.parse['ask'], 2
end

#balanceObject

Returns your currenct balance and the currency you have configured.



61
62
63
64
65
66
67
68
69
70
# File 'lib/fyb/client.rb', line 61

def balance
  wallet = Fyb.private.getaccinfo.perform.parse
  btc_label = 'btcBal'
  money_label = Fyb::Configuration.currency.to_s + 'Bal'

  btc = BigDecimal.new wallet[btc_label]
  real_money = BigDecimal.new wallet[money_label]

  { :btc => btc, Fyb::Configuration.currency => real_money }
end

#bidObject

Returns the current bid price.



10
11
12
# File 'lib/fyb/client.rb', line 10

def bid
  BigDecimal Fyb.public.ticker.perform.parse['bid'], 2
end

#buy!(qty, price) ⇒ Object

Creates and performs a buy order.

Returns the order.



47
48
49
50
# File 'lib/fyb/client.rb', line 47

def buy!(qty, price)
  order = Fyb::Order.new qty, price, :buy
  order.perform
end

#order_history(limit = 10) ⇒ Object

Returns your order history.



73
74
75
76
77
78
79
80
81
82
# File 'lib/fyb/client.rb', line 73

def order_history(limit = 10)
  plain_orders = Fyb.private.getorderhistory(limit: limit).perform.parse
  error = plain_orders['error']

  fail Exception, error unless error == 0

  plain_orders['orders'].map do |data|
    Order.new data['qty'], data['price'], data['type'] == 'B' ? :buy : :sell, data['ticket']
  end
end

#orderbookObject

Returns a couple of the last asks and bids.



15
16
17
# File 'lib/fyb/client.rb', line 15

def orderbook
  Fyb.public.orderbook.perform.parse
end

#sell!(qty, price) ⇒ Object

Creates and performs a sell order.

Returns the order.



55
56
57
58
# File 'lib/fyb/client.rb', line 55

def sell!(qty, price)
  order = Fyb::Order.new qty, price, :sell
  order.perform
end

#testObject

Returns true if the authorization key and signature was correct.



39
40
41
42
# File 'lib/fyb/client.rb', line 39

def test
  data = Fyb.private.test.perform.parse
  data['msg'] == 'success'
end

#trades(tid = nil) ⇒ Object

Returns trades.

Fyb.trades
Fyb.trades Time.now.to_i
  • tid tradeid to begin history from



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/fyb/client.rb', line 25

def trades(tid = nil)
  params = { since: tid } unless tid.nil?
  params ||= {}

  plain_orders = Fyb.public.trades(params).perform.parse

  return [] if plain_orders.empty?

  plain_orders.map do |data|
    Order.new data['amount'], data['price'], :undefined, data['tid']
  end
end