Skip to content

Commit

Permalink
Bar, Exec models rewritten with prop macro
Browse files Browse the repository at this point in the history
  • Loading branch information
arvicco committed Mar 12, 2012
1 parent 6ecb8bb commit 07fff7b
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 49 deletions.
22 changes: 11 additions & 11 deletions lib/ib-ruby/models/bar.rb
Expand Up @@ -5,17 +5,17 @@ module Models
# This is a single data point delivered by HistoricData messages.
# Instantiate with a Hash of attributes, to be auto-set via initialize in Model.
class Bar < Model
attr_accessor :time, # The date-time stamp of the start of the bar. The format is
# determined by the reqHistoricalData() formatDate parameter.
:open, # The bar opening price.
:high, # The high price during the time covered by the bar.
:low, # The low price during the time covered by the bar.
:close, # The bar closing price.
:volume, # The bar opening price.
:wap, # Weighted average price during the time covered by the bar.
:has_gaps, # Whether or not there are gaps in the data.
:trades # int: When TRADES data history is returned, represents number
# of trades that occurred during the time period the bar covers
prop :time, # The date-time stamp of the start of the bar. The format is
# determined by the reqHistoricalData() formatDate parameter.
:open, # The bar opening price.
:high, # The high price during the time covered by the bar.
:low, # The low price during the time covered by the bar.
:close, # The bar closing price.
:volume, # The bar opening price.
:wap, # Weighted average price during the time covered by the bar.
:has_gaps, # Whether or not there are gaps in the data.
:trades # int: When TRADES data history is returned, represents number
# of trades that occurred during the time period the bar covers

def to_s
"<Bar #{time}: wap: #{wap}, OHLC: #{open}, #{high}, #{low}, #{close}, " +
Expand Down
58 changes: 24 additions & 34 deletions lib/ib-ruby/models/execution.rb
Expand Up @@ -5,41 +5,31 @@ module Models
# This is IB Order execution report.
# Instantiate with a Hash of attributes, to be auto-set via initialize in Model.
class Execution < Model
attr_accessor :order_id, # int: order id. TWS orders have a fixed order id of 0.
:client_id, # int: id of the client that placed the order.
# TWS orders have a fixed client id of 0.
:perm_id, # int: TWS id used to identify orders, remains
:exec_id, # String: Unique order execution id.
# the same over TWS sessions.
:time, # String: The order execution time.
:exchange, # String: Exchange that executed the order.
:side, # String: Was the transaction a buy or a sale: BOT|SLD
:account_name, # String: The customer account number.
:price, # double: The order execution price.
:average_price, # double: Average price. Used in regular trades, combo
# trades and legs of the combo.
:shares, # int: The number of shares filled.
:cumulative_quantity, # int: Cumulative quantity. Used in regular
# trades, combo trades and legs of the combo
:liquidation # int: This position is liquidated last should the need arise.
prop :order_id, # int: order id. TWS orders have a fixed order id of 0.
:client_id, # int: id of the client that placed the order.
# TWS orders have a fixed client id of 0.
:perm_id, # int: TWS id used to identify orders, remains
:exec_id, # String: Unique order execution id.
# the same over TWS sessions.
:time, # String: The order execution time.
:exchange, # String: Exchange that executed the order.
:price, # double: The order execution price.
:average_price, # double: Average price. Used in regular trades, combo
# trades and legs of the combo.
:shares, # int: The number of shares filled.
:cumulative_quantity, # int: Cumulative quantity. Used in regular
# trades, combo trades and legs of the combo
:liquidation, # int: This position is liquidated last should the need arise.
[:account_name, :account_number], # String: The customer account number.
:side => # String: Was the transaction a buy or a sale: BOT|SLD
{:set => proc { |val| self[:side] = val.to_s.upcase[0..0] == 'B' ? :buy : :sell }}

alias account_number account_name # Legacy
alias account_number= account_name= # Legacy

def side= value
self[:side] = value == 'BOT' ? :BUY : :SELL
end

def initialize opts = {}
self[:order_id] = 0
self[:client_id] = 0
self[:shares] = 0
self[:price] = 0
self[:perm_id]= 0
self[:liquidation] = 0

super opts
end
DEFAULT_PROPS = {:order_id => 0,
:client_id => 0,
:shares => 0,
:price => 0,
:perm_id => 0,
:liquidation => 0, }

def to_s
"<Execution #{time}: #{side} #{shares} @ #{price} on #{exchange}, " +
Expand Down
73 changes: 73 additions & 0 deletions spec/ib-ruby/models/execution_spec.rb
@@ -0,0 +1,73 @@
require 'spec_helper'

describe IB::Models::Execution do

let(:properties) do
{:account_name => "DU111110",
:average_price => 1.31075,
:client_id => 1111,
:cumulative_quantity => 20000,
:exchange => "IDEALPRO",
:exec_id => "0001f4e8.4f5d48f1.01.01",
:liquidation => 0,
:order_id => 373,
:perm_id => 1695693613,
:price => 1.31075,
:shares => 20000,
:side => :buy,
:time => "20120312 15:41:09"
}
end

context "instantiation" do
context 'empty without properties' do
subject { IB::Models::Execution.new }

it { should_not be_nil }
its(:order_id) { should == 0 }
its(:client_id) { should == 0 }
its(:perm_id) { should == 0 }
its(:shares) { should == 0 }
its(:price) { should == 0 }
its(:liquidation) { should == 0 }
its(:created_at) { should be_a Time }
end

context 'with properties' do
subject { IB::Models::Execution.new properties }

it 'sets properties right' do
properties.each do |name, value|
subject.send(name).should == value
end
end
end
end #instantiation

context "properties" do

it 'allows setting properties' do
expect {
x = IB::Models::Execution.new
properties.each do |name, value|
subject.send("#{name}=", value)
subject.send(name).should == value
end
}.to_not raise_error
end

it 'sets side as directed by its setter' do
@x = IB::Models::Execution.new
['BOT', 'BUY', 'Buy', 'buy', :BUY, :BOT, :Buy, :buy, 'B', :b].each do |val|
expect { @x.side = val }.to_not raise_error
@x.side.should == :buy
end

['SELL', 'SLD', 'Sel', 'sell', :SELL, :SLD, :Sell, :sell, 'S', :S].each do |val|
expect { @x.side = val }.to_not raise_error
@x.side.should == :sell
end
end
end # properties

end # describe IB::Models::Contract
6 changes: 3 additions & 3 deletions spec/integration/orders/execution_spec.rb
Expand Up @@ -49,7 +49,7 @@
end

it 'receives Execution Data' do
execution_should_be 'BUY'
execution_should_be :buy
end

it 'receives OrderStatus with fill details' do
Expand Down Expand Up @@ -91,7 +91,7 @@
end

it 'receives Execution Data' do
execution_should_be 'SELL'
execution_should_be :sell
end

it 'receives OrderStatus with fill details' do
Expand Down Expand Up @@ -122,7 +122,7 @@
end

it 'receives Execution Data' do
execution_should_be 'SELL', :request_id => 456
execution_should_be :sell, :request_id => 456
end
end # Request executions
end # Forex order
Expand Down
2 changes: 1 addition & 1 deletion spec/integration_helper.rb
Expand Up @@ -114,7 +114,7 @@ def execution_should_be side, opts={}
exec.time.should =~ /\d\d:\d\d:\d\d/
exec.account_name.should == OPTS[:connection][:account_name]
exec.exchange.should == 'IDEALPRO'
exec.side.to_s.should == side
exec.side.should == side
exec.shares.should == 20000
exec.cumulative_quantity.should == 20000
exec.price.should be > 1
Expand Down

0 comments on commit 07fff7b

Please sign in to comment.