Class: IB::OrderState

Inherits:
Model show all
Includes:
BaseProperties
Defined in:
lib/models/ib/order_state.rb

Overview

OrderState represents dynamic (changeable) info about a single Order, isolating these changes and making Order essentially immutable

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BaseProperties

#as_table, #content_attributes, #default_attributes, #invariant_attributes, #set_attribute_defaults, #table_header, #table_row, #update_missing

Class Method Details

.valid_status?(the_message) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
76
# File 'lib/models/ib/order_state.rb', line 72

def self.valid_status? the_message
  valid_stati =  %w( ApiCancelled PreSubmitted PendingCancel Cancelled Submitted Filled
Inactive PendingSubmit Unknown ApiPending)
valid_stati.include?( the_message )
end

Instance Method Details

#==(other) ⇒ Object

Comparison



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/models/ib/order_state.rb', line 108

def == other

  super(other) ||
    other.is_a?(self.class) &&
    status == other.status &&
    local_id == other.local_id &&
    perm_id == other.perm_id &&
    client_id == other.client_id &&
    filled == other.filled &&
    remaining == other.remaining &&
    last_fill_price == other.last_fill_price &&
    init_margin == other.init_margin &&
    maint_margin == other.maint_margin &&
    equity_with_loan == other.equity_with_loan &&
    why_held == other.why_held &&
    warning_text == other.warning_text &&
    commission == other.commission
end

#active?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/models/ib/order_state.rb', line 99

def active?
  !inactive? # status == 'Inactive'
end

#complete_fill?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/models/ib/order_state.rb', line 103

def complete_fill?
  status == 'Filled' && remaining == 0 # filled >= total_quantity # Manually corrected
end

#forcastObject

If an Order is submitted with the :what_if-Flag set, commission and margin are returned via the order_state-Object.



143
144
145
146
147
148
149
150
# File 'lib/models/ib/order_state.rb', line 143

def forcast
	{ :init_margin => init_margin, 
		:maint_margin => maint_margin,
		:equity_with_loan => equity_with_loan ,
	  :commission => commission, 
		:commission_currency=> commission_currency,
		:warning => warning_text }
end

#inactive?Boolean

Order is in invalid state

Returns:

  • (Boolean)


95
96
97
# File 'lib/models/ib/order_state.rb', line 95

def inactive?
  new? || pending? || status =~ /Cancel/
end

#new?Boolean

Testing Order state:

Returns:

  • (Boolean)


80
81
82
# File 'lib/models/ib/order_state.rb', line 80

def new?
  status.empty? || status == 'New'
end

#pending?Boolean

Order is in a valid, working state on TWS side

Returns:

  • (Boolean)


90
91
92
# File 'lib/models/ib/order_state.rb', line 90

def pending?
   || status =~ /Pending/
end

#submitted?Boolean

Order is in a valid, working state on TWS side

Returns:

  • (Boolean)


85
86
87
# File 'lib/models/ib/order_state.rb', line 85

def 
  status =~ /Submit/
end

#to_humanObject Also known as: to_s



127
128
129
130
131
132
133
134
135
136
# File 'lib/models/ib/order_state.rb', line 127

def to_human
  "<OrderState: #{status} ##{local_id}/#{perm_id} from #{client_id}" +
    (filled ? " filled #{filled}/#{remaining}" : '') +
    (last_fill_price ? " at #{last_fill_price}/#{average_fill_price}" : '') +
    (init_margin ? " margin #{init_margin}/#{maint_margin}" : '') +
    (equity_with_loan ? " equity #{equity_with_loan}" : '') +
    (commission && commission > 0 ? " fee #{commission}" : "") +
    (why_held ? " why_held #{why_held}" : '') +
    ((warning_text && warning_text != '') ? " warning #{warning_text}" : '') + ">"
end