Class: Em::Nordnet::Order

Inherits:
Object
  • Object
show all
Defined in:
lib/em-nordnet/order.rb

Constant Summary collapse

BUY =
'BUY'.freeze
SELL =
'SELL'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Order

Returns a new instance of Order.



26
27
28
29
30
31
32
33
# File 'lib/em-nordnet/order.rb', line 26

def initialize attributes={}
  self.id         = attributes[:id] || attributes[:orderID]
  self.    = attributes[:account]
  self.instrument = attributes[:instrument]
  self.price      = attributes[:price]
  self.volume     = attributes[:volume]
  self.status     = attributes[:status]
end

Instance Attribute Details

#accountObject

Returns the value of attribute account.



7
8
9
# File 'lib/em-nordnet/order.rb', line 7

def 
  @account
end

#idObject

Returns the value of attribute id.



7
8
9
# File 'lib/em-nordnet/order.rb', line 7

def id
  @id
end

#instrumentObject

Returns the value of attribute instrument.



7
8
9
# File 'lib/em-nordnet/order.rb', line 7

def instrument
  @instrument
end

#priceObject

Returns the value of attribute price.



7
8
9
# File 'lib/em-nordnet/order.rb', line 7

def price
  @price
end

#statusObject

Returns the value of attribute status.



7
8
9
# File 'lib/em-nordnet/order.rb', line 7

def status
  @status
end

#volumeObject

Returns the value of attribute volume.



7
8
9
# File 'lib/em-nordnet/order.rb', line 7

def volume
  @volume
end

Class Method Details

.all(account_id) ⇒ Object



10
11
12
13
# File 'lib/em-nordnet/order.rb', line 10

def all 
  orders = Nordnet.api.orders()
  orders.map { |attributes| new attributes }
end

.buy(attributes = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/em-nordnet/order.rb', line 15

def buy attributes={}
  symbol, price, volume = attributes[:symbol], attributes[:price], attributes[:volume]

  instrument = Instrument.search(symbol).first
   = attributes[:account] || Account.default

  order = Order.new account: , instrument: instrument, price: price, volume: volume
  order.buy!
end

Instance Method Details

#buy!Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/em-nordnet/order.rb', line 35

def buy!
  self.status = Nordnet.api.create_order .id,
    identifier: instrument.id,
    marketID: instrument.market_id,
    price: price,
    volume: volume,
    side: BUY,
    currency: instrument.currency

  status
end