Class: Excoin::Market::Exchange

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

Defined Under Namespace

Classes: CandlestickChart, Order, OrderDepthChart, Orders, Trade, Trades

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(exchange_data) ⇒ Exchange

Returns a new instance of Exchange.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/exchange/exchange.rb', line 6

def initialize(exchange_data)
  begin
    @name = exchange_data['currency'] + exchange_data['commodity']
    @currency = exchange_data['currency']
    @commodity = exchange_data['commodity']

    self.update_summary(exchange_data)
    @orders = Orders.new(self)
    @trades = Trades.new(self)
  rescue
    puts "Error in Excoin::Market::Exchange.initialize"
    puts exchange_data
  end
end

Instance Attribute Details

#commodityObject (readonly)

Returns the value of attribute commodity.



2
3
4
# File 'lib/exchange/exchange.rb', line 2

def commodity
  @commodity
end

#currencyObject (readonly)

Returns the value of attribute currency.



2
3
4
# File 'lib/exchange/exchange.rb', line 2

def currency
  @currency
end

#daily_highObject (readonly)

Returns the value of attribute daily_high.



2
3
4
# File 'lib/exchange/exchange.rb', line 2

def daily_high
  @daily_high
end

#daily_lowObject (readonly)

Returns the value of attribute daily_low.



2
3
4
# File 'lib/exchange/exchange.rb', line 2

def daily_low
  @daily_low
end

#daily_volumeObject (readonly)

Returns the value of attribute daily_volume.



2
3
4
# File 'lib/exchange/exchange.rb', line 2

def daily_volume
  @daily_volume
end

#last_priceObject (readonly)

Returns the value of attribute last_price.



2
3
4
# File 'lib/exchange/exchange.rb', line 2

def last_price
  @last_price
end

#lowest_askObject (readonly)

Returns the value of attribute lowest_ask.



2
3
4
# File 'lib/exchange/exchange.rb', line 2

def lowest_ask
  @lowest_ask
end

#nameObject (readonly)

Returns the value of attribute name.



2
3
4
# File 'lib/exchange/exchange.rb', line 2

def name
  @name
end

#ordersObject (readonly)

Returns the value of attribute orders.



2
3
4
# File 'lib/exchange/exchange.rb', line 2

def orders
  @orders
end

#spreadObject (readonly)

Returns the value of attribute spread.



2
3
4
# File 'lib/exchange/exchange.rb', line 2

def spread
  @spread
end

#top_bidObject (readonly)

Returns the value of attribute top_bid.



2
3
4
# File 'lib/exchange/exchange.rb', line 2

def top_bid
  @top_bid
end

#tradesObject (readonly)

Returns the value of attribute trades.



2
3
4
# File 'lib/exchange/exchange.rb', line 2

def trades
  @trades
end

Instance Method Details

#issue_order(type, amount, price) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/exchange/exchange.rb', line 45

def issue_order(type, amount, price)
  decimal_amount = amount
  if amount.class == String
    decimal_amount = BigDecimal.new(amount)
  end
  if type.upcase == "BID" and (Excoin..wallet(self.currency).status == "inactive" or Excoin..wallet(self.currency).available_balance < decimal_amount)
    return "Insufficient funds for this order (#{self.currency})"
  elsif type.upcase == "ASK" and (Excoin..wallet(self.commodity).status == "inactive" or Excoin..wallet(self.commodity).available_balance < decimal_amount)
    return "Insufficient funds for this order (#{self.commodity})"
  else
    begin
      order = Excoin.api.(self.currency, self.commodity, type, amount, price)
      order.merge!({"currency" => self.currency, "commodity" => self.commodity})
      exchange_order = Excoin::Market::Exchange::Order.new(order)
       = Excoin::Account::Order.new(order)
      self.orders.add(exchange_order)
      Excoin..orders.add()
      return .id
    rescue
      puts "Error in Excoin::Market::Exchange.issue_order"
      puts order
    end
  end
end

#update(exchange_data = nil) ⇒ Object



39
40
41
42
43
# File 'lib/exchange/exchange.rb', line 39

def update(exchange_data = nil)
  self.update_summary(exchange_data)
  @orders.update
  @trades.update
end

#update_summary(exchange_data = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/exchange/exchange.rb', line 21

def update_summary(exchange_data = nil)
  begin
    exchange_data ||= self.get_summary

    @last_price = BigDecimal.new(exchange_data['last_price'])
    @daily_high = BigDecimal.new(exchange_data['high'])
    @daily_low = BigDecimal.new(exchange_data['low'])
    @daily_volume = BigDecimal.new(exchange_data['volume'])
    @top_bid = BigDecimal.new(exchange_data['top_bid'])
    @lowest_ask = BigDecimal.new(exchange_data['lowest_ask'])
    spread = self.top_bid - self.lowest_ask
    @spread = (spread > 0 ? spread : (spread * -1))
  rescue
    puts "Error in Excoin::Market::Exchange.update_summary"
    puts exchange_data
  end
end