Class: Excoin::Market::Exchange
- Inherits:
-
Object
- Object
- Excoin::Market::Exchange
- Defined in:
- lib/exchange/exchange.rb
Defined Under Namespace
Classes: CandlestickChart, Order, OrderDepthChart, Orders, Trade, Trades
Instance Attribute Summary collapse
-
#commodity ⇒ Object
readonly
Returns the value of attribute commodity.
-
#currency ⇒ Object
readonly
Returns the value of attribute currency.
-
#daily_high ⇒ Object
readonly
Returns the value of attribute daily_high.
-
#daily_low ⇒ Object
readonly
Returns the value of attribute daily_low.
-
#daily_volume ⇒ Object
readonly
Returns the value of attribute daily_volume.
-
#last_price ⇒ Object
readonly
Returns the value of attribute last_price.
-
#lowest_ask ⇒ Object
readonly
Returns the value of attribute lowest_ask.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#orders ⇒ Object
readonly
Returns the value of attribute orders.
-
#spread ⇒ Object
readonly
Returns the value of attribute spread.
-
#top_bid ⇒ Object
readonly
Returns the value of attribute top_bid.
-
#trades ⇒ Object
readonly
Returns the value of attribute trades.
Instance Method Summary collapse
-
#initialize(exchange_data) ⇒ Exchange
constructor
A new instance of Exchange.
- #issue_order(type, amount, price) ⇒ Object
- #update(exchange_data = nil) ⇒ Object
- #update_summary(exchange_data = nil) ⇒ Object
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
#commodity ⇒ Object (readonly)
Returns the value of attribute commodity.
2 3 4 |
# File 'lib/exchange/exchange.rb', line 2 def commodity @commodity end |
#currency ⇒ Object (readonly)
Returns the value of attribute currency.
2 3 4 |
# File 'lib/exchange/exchange.rb', line 2 def currency @currency end |
#daily_high ⇒ Object (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_low ⇒ Object (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_volume ⇒ Object (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_price ⇒ Object (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_ask ⇒ Object (readonly)
Returns the value of attribute lowest_ask.
2 3 4 |
# File 'lib/exchange/exchange.rb', line 2 def lowest_ask @lowest_ask end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
2 3 4 |
# File 'lib/exchange/exchange.rb', line 2 def name @name end |
#orders ⇒ Object (readonly)
Returns the value of attribute orders.
2 3 4 |
# File 'lib/exchange/exchange.rb', line 2 def orders @orders end |
#spread ⇒ Object (readonly)
Returns the value of attribute spread.
2 3 4 |
# File 'lib/exchange/exchange.rb', line 2 def spread @spread end |
#top_bid ⇒ Object (readonly)
Returns the value of attribute top_bid.
2 3 4 |
# File 'lib/exchange/exchange.rb', line 2 def top_bid @top_bid end |
#trades ⇒ Object (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.account.wallet(self.currency).status == "inactive" or Excoin.account.wallet(self.currency).available_balance < decimal_amount) return "Insufficient funds for this order (#{self.currency})" elsif type.upcase == "ASK" and (Excoin.account.wallet(self.commodity).status == "inactive" or Excoin.account.wallet(self.commodity).available_balance < decimal_amount) return "Insufficient funds for this order (#{self.commodity})" else begin order = Excoin.api.account_issue_order(self.currency, self.commodity, type, amount, price) order.merge!({"currency" => self.currency, "commodity" => self.commodity}) exchange_order = Excoin::Market::Exchange::Order.new(order) account_order = Excoin::Account::Order.new(order) self.orders.add(exchange_order) Excoin.account.orders.add(account_order) return account_order.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 |