ib-api 10.33.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +52 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/CLAUDE.md +131 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +17 -0
- data/Gemfile.lock +120 -0
- data/Guardfile +24 -0
- data/LICENSE +674 -0
- data/LLM_GUIDE.md +388 -0
- data/README.md +114 -0
- data/Rakefile +11 -0
- data/VERSION +1 -0
- data/api.gemspec +50 -0
- data/bin/console +96 -0
- data/bin/console.yml +3 -0
- data/bin/setup +8 -0
- data/bin/simple +91 -0
- data/changelog.md +32 -0
- data/conditions/ib/execution_condition.rb +31 -0
- data/conditions/ib/margin_condition.rb +28 -0
- data/conditions/ib/order_condition.rb +29 -0
- data/conditions/ib/percent_change_condition.rb +34 -0
- data/conditions/ib/price_condition.rb +44 -0
- data/conditions/ib/time_condition.rb +42 -0
- data/conditions/ib/volume_condition.rb +36 -0
- data/lib/class_extensions.rb +167 -0
- data/lib/ib/base.rb +109 -0
- data/lib/ib/base_properties.rb +178 -0
- data/lib/ib/connection.rb +573 -0
- data/lib/ib/constants.rb +402 -0
- data/lib/ib/contract.rb +30 -0
- data/lib/ib/errors.rb +52 -0
- data/lib/ib/messages/abstract_message.rb +68 -0
- data/lib/ib/messages/incoming/abstract_message.rb +116 -0
- data/lib/ib/messages/incoming/abstract_tick.rb +25 -0
- data/lib/ib/messages/incoming/account_message.rb +26 -0
- data/lib/ib/messages/incoming/alert.rb +34 -0
- data/lib/ib/messages/incoming/contract_data.rb +105 -0
- data/lib/ib/messages/incoming/contract_message.rb +13 -0
- data/lib/ib/messages/incoming/delta_neutral_validation.rb +23 -0
- data/lib/ib/messages/incoming/execution_data.rb +50 -0
- data/lib/ib/messages/incoming/histogram_data.rb +30 -0
- data/lib/ib/messages/incoming/historical_data.rb +65 -0
- data/lib/ib/messages/incoming/historical_data_update.rb +50 -0
- data/lib/ib/messages/incoming/managed_accounts.rb +21 -0
- data/lib/ib/messages/incoming/market_depth.rb +34 -0
- data/lib/ib/messages/incoming/market_depth_l2.rb +15 -0
- data/lib/ib/messages/incoming/next_valid_id.rb +19 -0
- data/lib/ib/messages/incoming/open_order.rb +290 -0
- data/lib/ib/messages/incoming/order_status.rb +85 -0
- data/lib/ib/messages/incoming/portfolio_value.rb +47 -0
- data/lib/ib/messages/incoming/position_data.rb +21 -0
- data/lib/ib/messages/incoming/positions_multi.rb +15 -0
- data/lib/ib/messages/incoming/real_time_bar.rb +32 -0
- data/lib/ib/messages/incoming/receive_fa.rb +30 -0
- data/lib/ib/messages/incoming/scanner_data.rb +54 -0
- data/lib/ib/messages/incoming/tick_by_tick.rb +77 -0
- data/lib/ib/messages/incoming/tick_efp.rb +18 -0
- data/lib/ib/messages/incoming/tick_generic.rb +12 -0
- data/lib/ib/messages/incoming/tick_option.rb +60 -0
- data/lib/ib/messages/incoming/tick_price.rb +60 -0
- data/lib/ib/messages/incoming/tick_size.rb +55 -0
- data/lib/ib/messages/incoming/tick_string.rb +13 -0
- data/lib/ib/messages/incoming.rb +292 -0
- data/lib/ib/messages/outgoing/abstract_message.rb +84 -0
- data/lib/ib/messages/outgoing/bar_request_message.rb +247 -0
- data/lib/ib/messages/outgoing/new-place-order.rb +193 -0
- data/lib/ib/messages/outgoing/old-place-order.rb +147 -0
- data/lib/ib/messages/outgoing/place_order.rb +149 -0
- data/lib/ib/messages/outgoing/request_account_summary.rb +79 -0
- data/lib/ib/messages/outgoing/request_historical_data.rb +182 -0
- data/lib/ib/messages/outgoing/request_market_data.rb +102 -0
- data/lib/ib/messages/outgoing/request_market_depth.rb +57 -0
- data/lib/ib/messages/outgoing/request_real_time_bars.rb +48 -0
- data/lib/ib/messages/outgoing/request_scanner_subscription.rb +73 -0
- data/lib/ib/messages/outgoing/request_tick_by_tick_data.rb +21 -0
- data/lib/ib/messages/outgoing.rb +410 -0
- data/lib/ib/messages.rb +139 -0
- data/lib/ib/order_condition.rb +26 -0
- data/lib/ib/plugins.rb +27 -0
- data/lib/ib/prepare_data.rb +61 -0
- data/lib/ib/raw_message_parser.rb +99 -0
- data/lib/ib/socket.rb +83 -0
- data/lib/ib/support.rb +236 -0
- data/lib/ib/version.rb +6 -0
- data/lib/ib-api.rb +44 -0
- data/lib/server_versions.rb +145 -0
- data/lib/support/array_function.rb +28 -0
- data/lib/support/logging.rb +45 -0
- data/models/ib/account.rb +72 -0
- data/models/ib/account_value.rb +33 -0
- data/models/ib/bag.rb +55 -0
- data/models/ib/bar.rb +31 -0
- data/models/ib/combo_leg.rb +127 -0
- data/models/ib/contract.rb +411 -0
- data/models/ib/contract_detail.rb +118 -0
- data/models/ib/execution.rb +67 -0
- data/models/ib/forex.rb +12 -0
- data/models/ib/future.rb +64 -0
- data/models/ib/index.rb +14 -0
- data/models/ib/option.rb +149 -0
- data/models/ib/option_detail.rb +84 -0
- data/models/ib/order.rb +720 -0
- data/models/ib/order_state.rb +155 -0
- data/models/ib/portfolio_value.rb +86 -0
- data/models/ib/spread.rb +176 -0
- data/models/ib/stock.rb +25 -0
- data/models/ib/underlying.rb +32 -0
- data/plugins/ib/advanced-account.rb +442 -0
- data/plugins/ib/alerts/base-alert.rb +125 -0
- data/plugins/ib/alerts/gateway-alerts.rb +15 -0
- data/plugins/ib/alerts/order-alerts.rb +73 -0
- data/plugins/ib/auto-adjust.rb +0 -0
- data/plugins/ib/connection-tools.rb +122 -0
- data/plugins/ib/eod.rb +326 -0
- data/plugins/ib/greeks.rb +102 -0
- data/plugins/ib/managed-accounts.rb +274 -0
- data/plugins/ib/market-price.rb +150 -0
- data/plugins/ib/option-chain.rb +167 -0
- data/plugins/ib/order-flow.rb +157 -0
- data/plugins/ib/order-prototypes/abstract.rb +67 -0
- data/plugins/ib/order-prototypes/adaptive.rb +40 -0
- data/plugins/ib/order-prototypes/all-in-one.rb +46 -0
- data/plugins/ib/order-prototypes/combo.rb +46 -0
- data/plugins/ib/order-prototypes/forex.rb +40 -0
- data/plugins/ib/order-prototypes/limit.rb +193 -0
- data/plugins/ib/order-prototypes/market.rb +116 -0
- data/plugins/ib/order-prototypes/pegged.rb +169 -0
- data/plugins/ib/order-prototypes/premarket.rb +31 -0
- data/plugins/ib/order-prototypes/stop.rb +202 -0
- data/plugins/ib/order-prototypes/volatility.rb +39 -0
- data/plugins/ib/order-prototypes.rb +118 -0
- data/plugins/ib/probability-of-expiring.rb +109 -0
- data/plugins/ib/process-orders.rb +155 -0
- data/plugins/ib/roll.rb +86 -0
- data/plugins/ib/spread-prototypes/butterfly.rb +77 -0
- data/plugins/ib/spread-prototypes/calendar.rb +97 -0
- data/plugins/ib/spread-prototypes/stock-spread.rb +56 -0
- data/plugins/ib/spread-prototypes/straddle.rb +70 -0
- data/plugins/ib/spread-prototypes/strangle.rb +93 -0
- data/plugins/ib/spread-prototypes/vertical.rb +83 -0
- data/plugins/ib/spread-prototypes.rb +70 -0
- data/plugins/ib/symbols/abstract.rb +136 -0
- data/plugins/ib/symbols/bonds.rb +28 -0
- data/plugins/ib/symbols/cfd.rb +19 -0
- data/plugins/ib/symbols/combo.rb +46 -0
- data/plugins/ib/symbols/commodity.rb +17 -0
- data/plugins/ib/symbols/forex.rb +41 -0
- data/plugins/ib/symbols/futures.rb +127 -0
- data/plugins/ib/symbols/index.rb +43 -0
- data/plugins/ib/symbols/options.rb +99 -0
- data/plugins/ib/symbols/stocks.rb +44 -0
- data/plugins/ib/symbols/version.rb +5 -0
- data/plugins/ib/symbols.rb +118 -0
- data/plugins/ib/verify.rb +226 -0
- data/symbols/w20.yml +210 -0
- data/t.txt +20 -0
- data/update.md +71 -0
- metadata +327 -0
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
|
|
2
|
+
module IB
|
|
3
|
+
# module OrderPrototype
|
|
4
|
+
module Market
|
|
5
|
+
extend OrderPrototype
|
|
6
|
+
class << self
|
|
7
|
+
|
|
8
|
+
def defaults
|
|
9
|
+
super.merge order_type: 'MKT' , tif: :day
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def aliases
|
|
13
|
+
super
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def requirements
|
|
17
|
+
super
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def summary
|
|
22
|
+
<<-HERE
|
|
23
|
+
A Market order is an order to buy or sell at the market bid or offer price.
|
|
24
|
+
A market order may increase the likelihood of a fill and the speed of execution,
|
|
25
|
+
but unlike the Limit order a Market order provides no price protection and
|
|
26
|
+
may fill at a price far lower/higher than the current displayed bid/ask.
|
|
27
|
+
HERE
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
module MarketIfTouched
|
|
32
|
+
extend OrderPrototype
|
|
33
|
+
class << self
|
|
34
|
+
|
|
35
|
+
def defaults
|
|
36
|
+
super.merge order_type: 'MIT' , tif: :day
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def aliases
|
|
40
|
+
super
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def requirements
|
|
44
|
+
super
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def summary
|
|
49
|
+
<<-HERE
|
|
50
|
+
A Market if Touched (MIT) is an order to buy (or sell) a contract below (or above) the market.
|
|
51
|
+
Its purpose is to take advantage of sudden or unexpected changes in share or other prices and
|
|
52
|
+
rovides investors with a trigger price to set an order in motion.
|
|
53
|
+
Investors may be waiting for excessive strength (or weakness) to cease, which might be represented
|
|
54
|
+
by a specific price point.
|
|
55
|
+
MIT orders can be used to determine whether or not to enter the market once a specific price level
|
|
56
|
+
has been achieved. This order is held in the system until the trigger price is touched, and
|
|
57
|
+
is then submitted as a market order. An MIT order is similar to a stop order, except that an MIT
|
|
58
|
+
sell order is placed above the current market price, and a stop sell order is placed below.
|
|
59
|
+
HERE
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
module MarketOnClose
|
|
66
|
+
extend OrderPrototype
|
|
67
|
+
class << self
|
|
68
|
+
|
|
69
|
+
def defaults
|
|
70
|
+
super.merge order_type: 'MOC' , tif: :day
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def aliases
|
|
74
|
+
super
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def requirements
|
|
78
|
+
super
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def summary
|
|
83
|
+
<<-HERE
|
|
84
|
+
A Market-on-Close (MOC) order is a market order that is submitted to execute as close
|
|
85
|
+
to the closing price as possible.
|
|
86
|
+
HERE
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
module MarketOnOpen
|
|
92
|
+
extend OrderPrototype
|
|
93
|
+
class << self
|
|
94
|
+
|
|
95
|
+
def defaults
|
|
96
|
+
super.merge order_type: 'MOC' , tif: :opening_price
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def aliases
|
|
100
|
+
super
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def requirements
|
|
104
|
+
super
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def summary
|
|
109
|
+
<<-HERE
|
|
110
|
+
A Market-on-Close (MOC) order is a market order that is submitted to execute as close
|
|
111
|
+
to the closing price as possible.
|
|
112
|
+
HERE
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
module IB
|
|
2
|
+
module Pegged2Primary
|
|
3
|
+
extend OrderPrototype
|
|
4
|
+
class << self
|
|
5
|
+
|
|
6
|
+
def defaults
|
|
7
|
+
super.merge order_type: :pegged_to_primary , tif: :day
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def aliases
|
|
11
|
+
super.merge limit_price: :price_cap,
|
|
12
|
+
aux_price: :offset_amount
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def requirements
|
|
16
|
+
super.merge aux_price: 'also aliased as :offset_amount',
|
|
17
|
+
limit_price: 'aliased as :price_cap'
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def optional
|
|
21
|
+
super
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def summary
|
|
25
|
+
<<-HERE
|
|
26
|
+
Relative (a.k.a. Pegged-to-Primary) orders provide a means for traders
|
|
27
|
+
to seek a more aggressive price than the National Best Bid and Offer
|
|
28
|
+
(NBBO). By acting as liquidity providers, and placing more aggressive
|
|
29
|
+
bids and offers than the current best bids and offers, traders increase
|
|
30
|
+
their odds of filling their order. Quotes are automatically adjusted as
|
|
31
|
+
the markets move, to remain aggressive. For a buy order, your bid is
|
|
32
|
+
pegged to the NBB by a more aggressive offset, and if the NBB moves up,
|
|
33
|
+
your bid will also move up. If the NBB moves down, there will be no
|
|
34
|
+
adjustment because your bid will become even more aggressive and
|
|
35
|
+
execute. For sales, your offer is pegged to the NBO by a more
|
|
36
|
+
aggressive offset, and if the NBO moves down, your offer will also move
|
|
37
|
+
down. If the NBO moves up, there will be no adjustment because your
|
|
38
|
+
offer will become more aggressive and execute. In addition to the
|
|
39
|
+
offset, you can define an absolute cap, which works like a limit price,
|
|
40
|
+
and will prevent your order from being executed above or below a
|
|
41
|
+
specified level.
|
|
42
|
+
Supported Products: Stocks, Options and Futures
|
|
43
|
+
------
|
|
44
|
+
not available on paper trading
|
|
45
|
+
HERE
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
module Pegged2Market
|
|
50
|
+
extend OrderPrototype
|
|
51
|
+
class << self
|
|
52
|
+
|
|
53
|
+
def defaults
|
|
54
|
+
super.merge order_type: 'PEG MKT' , tif: :day
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def aliases
|
|
58
|
+
Limit.aliases.merge aux_price: :market_offset
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def requirements
|
|
62
|
+
super.merge aux_price: :decimal
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def optional
|
|
66
|
+
super
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def summary
|
|
70
|
+
<<-HERE
|
|
71
|
+
A pegged-to-market order is designed to maintain a purchase price relative to the
|
|
72
|
+
national best offer (NBO) or a sale price relative to the national best bid (NBB).
|
|
73
|
+
Depending on the width of the quote, this order may be passive or aggressive.
|
|
74
|
+
The trader creates the order by entering a limit price which defines the worst limit
|
|
75
|
+
price that they are willing to accept.
|
|
76
|
+
Next, the trader enters an offset amount which computes the active limit price as follows:
|
|
77
|
+
Sell order price = Bid price + offset amount
|
|
78
|
+
Buy order price = Ask price - offset amount
|
|
79
|
+
HERE
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
module Pegged2Stock
|
|
85
|
+
extend OrderPrototype
|
|
86
|
+
class << self
|
|
87
|
+
|
|
88
|
+
def defaults
|
|
89
|
+
super.merge order_type: 'PEG STK', tif: :day
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def requirements
|
|
93
|
+
super.merge total_quantity: :decimal,
|
|
94
|
+
delta: 'required Delta of the Option',
|
|
95
|
+
starting_price: 'initial Limit-Price for the Option'
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def optional
|
|
99
|
+
super.merge stock_ref_price: 'Stock Reference Price',
|
|
100
|
+
stock_range_lower: 'Lowest acceptable Stock Price',
|
|
101
|
+
stock_range_upper: 'Highest accepable Stock Price'
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def summary
|
|
105
|
+
<<-HERE
|
|
106
|
+
Options ONLY
|
|
107
|
+
------------
|
|
108
|
+
A Pegged to Stock order continually adjusts the option order price by the product of a signed user-
|
|
109
|
+
defined delta and the change of the option's underlying stock price.
|
|
110
|
+
The delta is entered as an absolute and assumed to be positive for calls and negative for puts.
|
|
111
|
+
A buy or sell call order price is determined by adding the delta times a change in an underlying stock
|
|
112
|
+
price to a specified starting price for the call.
|
|
113
|
+
To determine the change in price, the stock reference price is subtracted from the current NBBO
|
|
114
|
+
midpoint. The Stock Reference Price can be defined by the user, or defaults to the
|
|
115
|
+
the NBBO midpoint at the time of the order if no reference price is entered.
|
|
116
|
+
You may also enter a high/low stock price range which cancels the order when reached. The
|
|
117
|
+
delta times the change in stock price will be rounded to the nearest penny in favor of the order.
|
|
118
|
+
------------
|
|
119
|
+
HERE
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
module Pegged2Benchmark
|
|
125
|
+
extend OrderPrototype
|
|
126
|
+
class << self
|
|
127
|
+
|
|
128
|
+
def defaults
|
|
129
|
+
super.merge order_type: :pegged_to_benchmark,
|
|
130
|
+
is_pegged_change_amount_decrease: false,
|
|
131
|
+
tif: :day
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def requirements
|
|
135
|
+
super.merge total_quantity: :decimal,
|
|
136
|
+
starting_price: 'initial Limit-Price for the contract to trade' ,
|
|
137
|
+
pegged_change_amount: ' (increase/decrease) by... (and likewise for price moving in opposite direction)',
|
|
138
|
+
reference_change_amount: ' ... whenever there is a price change of...',
|
|
139
|
+
reference_contract_id: 'the conid of the reference contract'
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def aliases
|
|
143
|
+
super.merge reference_change_amount: :reference_change_by,
|
|
144
|
+
pegged_change_amount: :change_by,
|
|
145
|
+
is_pegged_change_amount_decrease: :decrease,
|
|
146
|
+
reference_contract_id: :reference
|
|
147
|
+
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def optional
|
|
152
|
+
super.merge stock_ref_price: 'starting price of the reference contract',
|
|
153
|
+
stock_range_lower: 'Lowest acceptable Price of the reference contract',
|
|
154
|
+
stock_range_upper: 'Highest accepable Price of the reference contract',
|
|
155
|
+
is_pegged_change_amount_decrease: 'increase(true) / decrease(false) Price (default: false)',
|
|
156
|
+
reference_exchange_id: "Exchange of the reference contract"
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def summary
|
|
160
|
+
<<-HERE
|
|
161
|
+
The Pegged to Benchmark order is similar to the Pegged to Stock order for options,
|
|
162
|
+
except that the Pegged to Benchmark allows you to specify any asset type as the
|
|
163
|
+
reference (benchmark) contract for a stock or option order. Both the primary and
|
|
164
|
+
reference contracts must use the same currency.
|
|
165
|
+
HERE
|
|
166
|
+
end
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
module IB
|
|
2
|
+
# module OrderPrototype
|
|
3
|
+
module AtAuction
|
|
4
|
+
extend OrderPrototype
|
|
5
|
+
class << self
|
|
6
|
+
|
|
7
|
+
def defaults
|
|
8
|
+
{ order_type: 'MTL' , tif: "AUC"}
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def aliases
|
|
12
|
+
super.merge limit_price: :price
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def requirements
|
|
16
|
+
super.merge limit_price: :decimal
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def summary
|
|
21
|
+
<<-HERE
|
|
22
|
+
An auction order is entered into the electronic trading system during the pre-market
|
|
23
|
+
opening period for execution at the Calculated Opening Price (COP).
|
|
24
|
+
If your order is not filled on the open, the order is re-submitted as a
|
|
25
|
+
limit order with the limit price set to the COP or the best bid/ask after the market opens.
|
|
26
|
+
Products: FUT, STK
|
|
27
|
+
HERE
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
module IB
|
|
3
|
+
module SimpleStop
|
|
4
|
+
extend OrderPrototype
|
|
5
|
+
class << self
|
|
6
|
+
|
|
7
|
+
def defaults
|
|
8
|
+
super.merge order_type: :stop
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def aliases
|
|
12
|
+
super.merge aux_price: :price
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def requirements
|
|
16
|
+
super.merge aux_price: 'Price where the action is triggert. Aliased as :price'
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def summary
|
|
21
|
+
<<-HERE
|
|
22
|
+
A Stop order is an instruction to submit a buy or sell market order if and when the
|
|
23
|
+
user-specified stop trigger price is attained or penetrated. A Stop order is not guaranteed
|
|
24
|
+
a specific execution price and may execute significantly away from its stop price.
|
|
25
|
+
|
|
26
|
+
A Sell Stop order is always placed below the current market price and is typically used
|
|
27
|
+
to limit a loss or protect a profit on a long stock position.
|
|
28
|
+
|
|
29
|
+
A Buy Stop order is always placed above the current market price. It is typically used
|
|
30
|
+
to limit a loss or help protect a profit on a short sale.
|
|
31
|
+
HERE
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
module StopLimit
|
|
36
|
+
extend OrderPrototype
|
|
37
|
+
class << self
|
|
38
|
+
|
|
39
|
+
def defaults
|
|
40
|
+
super.merge order_type: :stop_limit
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def aliases
|
|
44
|
+
Limit.aliases.merge aux_price: :stop_price
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def requirements
|
|
48
|
+
Limit.requirements.merge aux_price: 'Price where the action is triggert. Aliased as :stop_price'
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def summary
|
|
53
|
+
<<-HERE
|
|
54
|
+
A Stop-Limit order is an instruction to submit a buy or sell limit order when
|
|
55
|
+
the user-specified stop trigger price is attained or penetrated. The order has
|
|
56
|
+
two basic components: the stop price and the limit price. When a trade has occurred
|
|
57
|
+
at or through the stop price, the order becomes executable and enters the market
|
|
58
|
+
as a limit order, which is an order to buy or sell at a specified price or better.
|
|
59
|
+
HERE
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
module StopProtected
|
|
64
|
+
extend OrderPrototype
|
|
65
|
+
class << self
|
|
66
|
+
|
|
67
|
+
def defaults
|
|
68
|
+
super.merge order_type: :stop_protected
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def aliases
|
|
72
|
+
SimpleStop.aliases
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def requirements
|
|
76
|
+
SimpleStop.requirements
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def summary
|
|
81
|
+
<<-HERE
|
|
82
|
+
US-Futures only
|
|
83
|
+
----------------------------
|
|
84
|
+
A Stop with Protection order combines the functionality of a stop limit order
|
|
85
|
+
with a market with protection order. The order is set to trigger at a specified
|
|
86
|
+
stop price. When the stop price is penetrated, the order is triggered as a
|
|
87
|
+
market with protection order, which means that it will fill within a specified
|
|
88
|
+
protected price range equal to the trigger price +/- the exchange-defined protection
|
|
89
|
+
point range. Any portion of the order that does not fill within this protected
|
|
90
|
+
range is submitted as a limit order at the exchange-defined trigger price +/-
|
|
91
|
+
the protection points.
|
|
92
|
+
HERE
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
# module OrderPrototype
|
|
97
|
+
module TrailingStop
|
|
98
|
+
extend OrderPrototype
|
|
99
|
+
class << self
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def defaults
|
|
103
|
+
super.merge order_type: :trailing_stop , tif: :day
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def aliases
|
|
107
|
+
super.merge trail_stop_price: :price,
|
|
108
|
+
aux_price: :trailing_amount
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def requirements
|
|
112
|
+
## usualy the trail_stop_price is the market-price minus(plus) the trailing_amount
|
|
113
|
+
super.merge trail_stop_price: 'Price to trigger the action, aliased as :price'
|
|
114
|
+
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def alternative_parameters
|
|
118
|
+
{ aux_price: 'Trailing distance in absolute terms, aliased as :trailing_amount',
|
|
119
|
+
trailing_percent: 'Trailing distance in relative terms'}
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def summary
|
|
123
|
+
<<-HERE
|
|
124
|
+
A "Sell" trailing stop order sets the stop price at a fixed amount below the market
|
|
125
|
+
price with an attached "trailing" amount. As the market price rises, the stop price
|
|
126
|
+
rises by the trail amount, but if the stock price falls, the stop loss price doesn't
|
|
127
|
+
change, and a market order is submitted when the stop price is hit. This technique
|
|
128
|
+
is designed to allow an investor to specify a limit on the maximum possible loss,
|
|
129
|
+
without setting a limit on the maximum possible gain.
|
|
130
|
+
|
|
131
|
+
"Buy" trailing stop orders are the mirror image of sell trailing stop orders, and
|
|
132
|
+
are most appropriate for use in falling markets.
|
|
133
|
+
|
|
134
|
+
Note that Trailing Stop orders can have the trailing amount specified as a percent,
|
|
135
|
+
or as an absolute amount which is specified in the auxPrice field.
|
|
136
|
+
|
|
137
|
+
HERE
|
|
138
|
+
end # summary
|
|
139
|
+
end # class self
|
|
140
|
+
end # module
|
|
141
|
+
|
|
142
|
+
module TrailingStopLimit
|
|
143
|
+
extend OrderPrototype
|
|
144
|
+
class << self
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def defaults
|
|
148
|
+
super.merge order_type: :trailing_limit , tif: :day
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def aliases
|
|
152
|
+
Limit.aliases
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def requirements
|
|
156
|
+
super.merge trail_stop_price: 'Price to trigger the action',
|
|
157
|
+
limit_price_offset: 'a pRICE'
|
|
158
|
+
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def alternative_parameters
|
|
162
|
+
{ aux_price: 'Trailing distance in absolute terms',
|
|
163
|
+
trailing_percent: 'Trailing distance in relative terms'}
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def summary
|
|
167
|
+
<<-HERE
|
|
168
|
+
A trailing stop limit order is designed to allow an investor to specify a
|
|
169
|
+
limit on the maximum possible loss, without setting a limit on the maximum
|
|
170
|
+
possible gain. A SELL trailing stop limit moves with the market price, and
|
|
171
|
+
continually recalculates the stop trigger price at a fixed amount below
|
|
172
|
+
the market price, based on the user-defined "trailing" amount. The limit
|
|
173
|
+
order price is also continually recalculated based on the limit offset. As
|
|
174
|
+
the market price rises, both the stop price and the limit price rise by
|
|
175
|
+
the trail amount and limit offset respectively, but if the stock price
|
|
176
|
+
falls, the stop price remains unchanged, and when the stop price is hit a
|
|
177
|
+
limit order is submitted at the last calculated limit price. A "Buy"
|
|
178
|
+
trailing stop limit order is the mirror image of a sell trailing stop
|
|
179
|
+
limit, and is generally used in falling markets.
|
|
180
|
+
|
|
181
|
+
Products: BOND, CFD, CASH, FUT, FOP, OPT, STK, WAR
|
|
182
|
+
HERE
|
|
183
|
+
end
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
# def TrailingStopLimit(action:str, quantity:float, lmtPriceOffset:float,
|
|
187
|
+
# trailingAmount:float, trailStopPrice:float):
|
|
188
|
+
#
|
|
189
|
+
# # ! [trailingstoplimit]
|
|
190
|
+
# order = Order()
|
|
191
|
+
# order.action = action
|
|
192
|
+
# order.orderType = "TRAIL LIMIT"
|
|
193
|
+
# order.totalQuantity = quantity
|
|
194
|
+
# order.trailStopPrice = trailStopPrice
|
|
195
|
+
# order.lmtPriceOffset = lmtPriceOffset
|
|
196
|
+
# order.auxPrice = trailingAmount
|
|
197
|
+
# # ! [trailingstoplimit]
|
|
198
|
+
# return order
|
|
199
|
+
#
|
|
200
|
+
#
|
|
201
|
+
end
|
|
202
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
module IB
|
|
2
|
+
# module OrderPrototype
|
|
3
|
+
module Volatility
|
|
4
|
+
### todo : check again. Order is siently accepted, but not acknowledged
|
|
5
|
+
extend OrderPrototype
|
|
6
|
+
class << self
|
|
7
|
+
|
|
8
|
+
def defaults
|
|
9
|
+
{ order_type: :volatility, volatility_type: 2 } #default is annual volatility
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def requirements
|
|
14
|
+
super.merge volatility: "the desired Option implied Vola (in %)"
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def aliases
|
|
18
|
+
super.merge volatility: :volatility_percent
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def summary
|
|
22
|
+
<<-HERE
|
|
23
|
+
Investors are able to create and enter Volatility-type orders for options and combinations
|
|
24
|
+
rather than price orders. Option traders may wish to trade and position for movements in the
|
|
25
|
+
price of the option determined by its implied volatility. Because implied volatility is a key
|
|
26
|
+
determinant of the premium on an option, traders position in specific contract months in an
|
|
27
|
+
effort to take advantage of perceived changes in implied volatility arising before, during or
|
|
28
|
+
after earnings or when company specific or broad market volatility is predicted to change.
|
|
29
|
+
In order to create a Volatility order, clients must first create a Volatility Trader page from
|
|
30
|
+
the Trading Tools menu and as they enter option contracts, premiums will display in percentage
|
|
31
|
+
terms rather than premium. The buy/sell process is the same as for regular orders priced in
|
|
32
|
+
premium terms except that the client can limit the volatility level they are willing to pay or receive.
|
|
33
|
+
--------
|
|
34
|
+
Products: FOP, OPT
|
|
35
|
+
HERE
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
module IB
|
|
2
|
+
=begin rdoc
|
|
3
|
+
|
|
4
|
+
Plugin to build IB::Order objects through singletons
|
|
5
|
+
|
|
6
|
+
Public API
|
|
7
|
+
==========
|
|
8
|
+
|
|
9
|
+
Creates IB::Order objects
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
* IB::<OrderPrototye>.order params
|
|
13
|
+
* IB::<OrderPrototye>.summary
|
|
14
|
+
* IB::<OrderPrototye>.parameters
|
|
15
|
+
|
|
16
|
+
=end
|
|
17
|
+
|
|
18
|
+
module OrderPrototype
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
#The Module OrderPrototypes provides a wrapper to define even complex ordertypes.
|
|
22
|
+
#
|
|
23
|
+
#The Order is build by
|
|
24
|
+
#
|
|
25
|
+
# IB::<OrderPrototye>.order
|
|
26
|
+
#
|
|
27
|
+
#A description is available through
|
|
28
|
+
#
|
|
29
|
+
# puts IB::<OrderPrototype>.summary
|
|
30
|
+
#
|
|
31
|
+
#Nessesary and optional arguments are printed by
|
|
32
|
+
#
|
|
33
|
+
# puts IB::<OrderPrototype>.parameters
|
|
34
|
+
#
|
|
35
|
+
#Orders can be setup interactively
|
|
36
|
+
#
|
|
37
|
+
# > d = Discretionary.order
|
|
38
|
+
# Traceback (most recent call last): (..)
|
|
39
|
+
# IB::ArgumentError (IB::Discretionary.order -> A necessary field is missing:
|
|
40
|
+
# action: --> {"B"=>:buy, "S"=>:sell, "T"=>:short, "X"=>:short_exempt})
|
|
41
|
+
# > d = Discretionary.order action: :buy
|
|
42
|
+
# IB::ArgumentError (IB::Discretionary.order -> A necessary field is missing:
|
|
43
|
+
# total_quantity: --> also aliased as :size)
|
|
44
|
+
# > d = Discretionary.order action: :buy, size: 100
|
|
45
|
+
# Traceback (most recent call last):
|
|
46
|
+
# IB::ArgumentError (IB::Discretionary.order -> A necessary field is missing: limit_price: --> decimal)
|
|
47
|
+
#
|
|
48
|
+
#
|
|
49
|
+
#
|
|
50
|
+
#Prototypes are defined as module. They extend OrderPrototype and establish singleton methods, which
|
|
51
|
+
#can adress and extend similar methods from OrderPrototype.
|
|
52
|
+
#
|
|
53
|
+
#
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def order **fields
|
|
58
|
+
|
|
59
|
+
# special treatment of size: positive numbers --> buy order, negative: sell
|
|
60
|
+
if fields[:size].present? && fields[:action].blank?
|
|
61
|
+
error "Size = 0 is not possible" if fields[:size].zero?
|
|
62
|
+
fields[:action] = fields[:size] >0 ? :buy : :sell
|
|
63
|
+
fields[:size] = fields[:size].abs
|
|
64
|
+
end
|
|
65
|
+
# change aliases to the original. We are modifying the fields-hash.
|
|
66
|
+
fields.keys.each{|x| fields[aliases.key(x)] = fields.delete(x) if aliases.has_value?(x)}
|
|
67
|
+
# inlcude defaults (arguments override defaults)
|
|
68
|
+
the_arguments = defaults.merge fields
|
|
69
|
+
# check if requirements are fullfilled
|
|
70
|
+
necessary = requirements.keys.detect{|y| the_arguments[y].nil?}
|
|
71
|
+
if necessary.present?
|
|
72
|
+
msg =self.name + ".order -> A necessary field is missing: #{necessary}: --> #{requirements[necessary]}"
|
|
73
|
+
error msg, :args, nil
|
|
74
|
+
end
|
|
75
|
+
if alternative_parameters.present?
|
|
76
|
+
unless ( alternative_parameters.keys & the_arguments.keys ).size == 1
|
|
77
|
+
msg =self.name + ".order -> One of the alternative fields needs to be specified: \n\t:" +
|
|
78
|
+
"#{alternative_parameters.map{|x| x.join ' => '}.join(" or \n\t:")}"
|
|
79
|
+
error msg, :args, nil
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# initialise order with given attributes
|
|
84
|
+
IB::Order.new **the_arguments
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def alternative_parameters
|
|
88
|
+
{}
|
|
89
|
+
end
|
|
90
|
+
def requirements
|
|
91
|
+
{ action: IB::VALUES[:side], total_quantity: 'also aliased as :size' }
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def defaults
|
|
95
|
+
{ tif: :good_till_cancelled }
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def optional
|
|
99
|
+
{ account: 'Account(number) to trade on' }
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def aliases
|
|
103
|
+
{ total_quantity: :size }
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def parameters
|
|
107
|
+
the_output = ->(var){ var.map{|x| x.join(" --> ") }.join("\n\t: ")}
|
|
108
|
+
|
|
109
|
+
"Required : " + the_output[requirements] + "\n --------------- \n" +
|
|
110
|
+
"Optional : " + the_output[optional] + "\n --------------- \n"
|
|
111
|
+
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
end
|
|
115
|
+
[ :forex, :market, :limit, :stop, :volatility, :premarket, :pegged, :combo, :adaptive ].each do | pt |
|
|
116
|
+
Connection.current.activate_plugin "order_prototypes/#{pt.to_s}"
|
|
117
|
+
end
|
|
118
|
+
end
|