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.
Files changed (161) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +52 -0
  3. data/.rspec +3 -0
  4. data/.travis.yml +7 -0
  5. data/CLAUDE.md +131 -0
  6. data/CODE_OF_CONDUCT.md +74 -0
  7. data/Gemfile +17 -0
  8. data/Gemfile.lock +120 -0
  9. data/Guardfile +24 -0
  10. data/LICENSE +674 -0
  11. data/LLM_GUIDE.md +388 -0
  12. data/README.md +114 -0
  13. data/Rakefile +11 -0
  14. data/VERSION +1 -0
  15. data/api.gemspec +50 -0
  16. data/bin/console +96 -0
  17. data/bin/console.yml +3 -0
  18. data/bin/setup +8 -0
  19. data/bin/simple +91 -0
  20. data/changelog.md +32 -0
  21. data/conditions/ib/execution_condition.rb +31 -0
  22. data/conditions/ib/margin_condition.rb +28 -0
  23. data/conditions/ib/order_condition.rb +29 -0
  24. data/conditions/ib/percent_change_condition.rb +34 -0
  25. data/conditions/ib/price_condition.rb +44 -0
  26. data/conditions/ib/time_condition.rb +42 -0
  27. data/conditions/ib/volume_condition.rb +36 -0
  28. data/lib/class_extensions.rb +167 -0
  29. data/lib/ib/base.rb +109 -0
  30. data/lib/ib/base_properties.rb +178 -0
  31. data/lib/ib/connection.rb +573 -0
  32. data/lib/ib/constants.rb +402 -0
  33. data/lib/ib/contract.rb +30 -0
  34. data/lib/ib/errors.rb +52 -0
  35. data/lib/ib/messages/abstract_message.rb +68 -0
  36. data/lib/ib/messages/incoming/abstract_message.rb +116 -0
  37. data/lib/ib/messages/incoming/abstract_tick.rb +25 -0
  38. data/lib/ib/messages/incoming/account_message.rb +26 -0
  39. data/lib/ib/messages/incoming/alert.rb +34 -0
  40. data/lib/ib/messages/incoming/contract_data.rb +105 -0
  41. data/lib/ib/messages/incoming/contract_message.rb +13 -0
  42. data/lib/ib/messages/incoming/delta_neutral_validation.rb +23 -0
  43. data/lib/ib/messages/incoming/execution_data.rb +50 -0
  44. data/lib/ib/messages/incoming/histogram_data.rb +30 -0
  45. data/lib/ib/messages/incoming/historical_data.rb +65 -0
  46. data/lib/ib/messages/incoming/historical_data_update.rb +50 -0
  47. data/lib/ib/messages/incoming/managed_accounts.rb +21 -0
  48. data/lib/ib/messages/incoming/market_depth.rb +34 -0
  49. data/lib/ib/messages/incoming/market_depth_l2.rb +15 -0
  50. data/lib/ib/messages/incoming/next_valid_id.rb +19 -0
  51. data/lib/ib/messages/incoming/open_order.rb +290 -0
  52. data/lib/ib/messages/incoming/order_status.rb +85 -0
  53. data/lib/ib/messages/incoming/portfolio_value.rb +47 -0
  54. data/lib/ib/messages/incoming/position_data.rb +21 -0
  55. data/lib/ib/messages/incoming/positions_multi.rb +15 -0
  56. data/lib/ib/messages/incoming/real_time_bar.rb +32 -0
  57. data/lib/ib/messages/incoming/receive_fa.rb +30 -0
  58. data/lib/ib/messages/incoming/scanner_data.rb +54 -0
  59. data/lib/ib/messages/incoming/tick_by_tick.rb +77 -0
  60. data/lib/ib/messages/incoming/tick_efp.rb +18 -0
  61. data/lib/ib/messages/incoming/tick_generic.rb +12 -0
  62. data/lib/ib/messages/incoming/tick_option.rb +60 -0
  63. data/lib/ib/messages/incoming/tick_price.rb +60 -0
  64. data/lib/ib/messages/incoming/tick_size.rb +55 -0
  65. data/lib/ib/messages/incoming/tick_string.rb +13 -0
  66. data/lib/ib/messages/incoming.rb +292 -0
  67. data/lib/ib/messages/outgoing/abstract_message.rb +84 -0
  68. data/lib/ib/messages/outgoing/bar_request_message.rb +247 -0
  69. data/lib/ib/messages/outgoing/new-place-order.rb +193 -0
  70. data/lib/ib/messages/outgoing/old-place-order.rb +147 -0
  71. data/lib/ib/messages/outgoing/place_order.rb +149 -0
  72. data/lib/ib/messages/outgoing/request_account_summary.rb +79 -0
  73. data/lib/ib/messages/outgoing/request_historical_data.rb +182 -0
  74. data/lib/ib/messages/outgoing/request_market_data.rb +102 -0
  75. data/lib/ib/messages/outgoing/request_market_depth.rb +57 -0
  76. data/lib/ib/messages/outgoing/request_real_time_bars.rb +48 -0
  77. data/lib/ib/messages/outgoing/request_scanner_subscription.rb +73 -0
  78. data/lib/ib/messages/outgoing/request_tick_by_tick_data.rb +21 -0
  79. data/lib/ib/messages/outgoing.rb +410 -0
  80. data/lib/ib/messages.rb +139 -0
  81. data/lib/ib/order_condition.rb +26 -0
  82. data/lib/ib/plugins.rb +27 -0
  83. data/lib/ib/prepare_data.rb +61 -0
  84. data/lib/ib/raw_message_parser.rb +99 -0
  85. data/lib/ib/socket.rb +83 -0
  86. data/lib/ib/support.rb +236 -0
  87. data/lib/ib/version.rb +6 -0
  88. data/lib/ib-api.rb +44 -0
  89. data/lib/server_versions.rb +145 -0
  90. data/lib/support/array_function.rb +28 -0
  91. data/lib/support/logging.rb +45 -0
  92. data/models/ib/account.rb +72 -0
  93. data/models/ib/account_value.rb +33 -0
  94. data/models/ib/bag.rb +55 -0
  95. data/models/ib/bar.rb +31 -0
  96. data/models/ib/combo_leg.rb +127 -0
  97. data/models/ib/contract.rb +411 -0
  98. data/models/ib/contract_detail.rb +118 -0
  99. data/models/ib/execution.rb +67 -0
  100. data/models/ib/forex.rb +12 -0
  101. data/models/ib/future.rb +64 -0
  102. data/models/ib/index.rb +14 -0
  103. data/models/ib/option.rb +149 -0
  104. data/models/ib/option_detail.rb +84 -0
  105. data/models/ib/order.rb +720 -0
  106. data/models/ib/order_state.rb +155 -0
  107. data/models/ib/portfolio_value.rb +86 -0
  108. data/models/ib/spread.rb +176 -0
  109. data/models/ib/stock.rb +25 -0
  110. data/models/ib/underlying.rb +32 -0
  111. data/plugins/ib/advanced-account.rb +442 -0
  112. data/plugins/ib/alerts/base-alert.rb +125 -0
  113. data/plugins/ib/alerts/gateway-alerts.rb +15 -0
  114. data/plugins/ib/alerts/order-alerts.rb +73 -0
  115. data/plugins/ib/auto-adjust.rb +0 -0
  116. data/plugins/ib/connection-tools.rb +122 -0
  117. data/plugins/ib/eod.rb +326 -0
  118. data/plugins/ib/greeks.rb +102 -0
  119. data/plugins/ib/managed-accounts.rb +274 -0
  120. data/plugins/ib/market-price.rb +150 -0
  121. data/plugins/ib/option-chain.rb +167 -0
  122. data/plugins/ib/order-flow.rb +157 -0
  123. data/plugins/ib/order-prototypes/abstract.rb +67 -0
  124. data/plugins/ib/order-prototypes/adaptive.rb +40 -0
  125. data/plugins/ib/order-prototypes/all-in-one.rb +46 -0
  126. data/plugins/ib/order-prototypes/combo.rb +46 -0
  127. data/plugins/ib/order-prototypes/forex.rb +40 -0
  128. data/plugins/ib/order-prototypes/limit.rb +193 -0
  129. data/plugins/ib/order-prototypes/market.rb +116 -0
  130. data/plugins/ib/order-prototypes/pegged.rb +169 -0
  131. data/plugins/ib/order-prototypes/premarket.rb +31 -0
  132. data/plugins/ib/order-prototypes/stop.rb +202 -0
  133. data/plugins/ib/order-prototypes/volatility.rb +39 -0
  134. data/plugins/ib/order-prototypes.rb +118 -0
  135. data/plugins/ib/probability-of-expiring.rb +109 -0
  136. data/plugins/ib/process-orders.rb +155 -0
  137. data/plugins/ib/roll.rb +86 -0
  138. data/plugins/ib/spread-prototypes/butterfly.rb +77 -0
  139. data/plugins/ib/spread-prototypes/calendar.rb +97 -0
  140. data/plugins/ib/spread-prototypes/stock-spread.rb +56 -0
  141. data/plugins/ib/spread-prototypes/straddle.rb +70 -0
  142. data/plugins/ib/spread-prototypes/strangle.rb +93 -0
  143. data/plugins/ib/spread-prototypes/vertical.rb +83 -0
  144. data/plugins/ib/spread-prototypes.rb +70 -0
  145. data/plugins/ib/symbols/abstract.rb +136 -0
  146. data/plugins/ib/symbols/bonds.rb +28 -0
  147. data/plugins/ib/symbols/cfd.rb +19 -0
  148. data/plugins/ib/symbols/combo.rb +46 -0
  149. data/plugins/ib/symbols/commodity.rb +17 -0
  150. data/plugins/ib/symbols/forex.rb +41 -0
  151. data/plugins/ib/symbols/futures.rb +127 -0
  152. data/plugins/ib/symbols/index.rb +43 -0
  153. data/plugins/ib/symbols/options.rb +99 -0
  154. data/plugins/ib/symbols/stocks.rb +44 -0
  155. data/plugins/ib/symbols/version.rb +5 -0
  156. data/plugins/ib/symbols.rb +118 -0
  157. data/plugins/ib/verify.rb +226 -0
  158. data/symbols/w20.yml +210 -0
  159. data/t.txt +20 -0
  160. data/update.md +71 -0
  161. metadata +327 -0
@@ -0,0 +1,149 @@
1
+ module IB
2
+ module Messages
3
+ module Outgoing
4
+ extend Messages # def_message macros
5
+
6
+ PlaceOrder = def_message [ 3,0 ]
7
+
8
+ class PlaceOrder
9
+ def encode
10
+ order = @data[:order]
11
+ contract = @data[:contract]
12
+ contract = order.contract unless contract.is_a? IB::Contract
13
+
14
+ error 'contract has to be specified' unless contract.is_a? IB::Contract
15
+
16
+ # send place order msg
17
+ fields = [ super ,
18
+ contract.serialize_short(:primary_exchange, :sec_id_type),
19
+ order.serialize_main_order_fields,
20
+ order.serialize_extended_order_fields,
21
+ order.serialize_combo_legs(contract),
22
+ order.serialize_auxilery_order_fields # including advisory order fields
23
+ ]
24
+
25
+ # if server_version >= KNOWN_SERVERS[:min_server_ver_models_support] # 103
26
+ fields.push(order.model_code )
27
+ # end
28
+
29
+ fields += [
30
+ order[:short_sale_slot] , # 0 only for retail, 1 or 2 for institution (Institutional)
31
+ order.designated_location # only populate when short_sale_slot == 2 (Institutional)
32
+ ]
33
+
34
+ fields.push(order.exempt_code) #if server_version >= KNOWN_SERVERS[:min_server_ver_sshortx_old]
35
+
36
+ fields.push(order[:oca_type])
37
+ fields += [
38
+ order[:rule_80a], # .to_sup[0..0],
39
+ order.settling_firm,
40
+ order.all_or_none,
41
+ order.min_quantity,
42
+ order.percent_offset,
43
+ false, # etrade_only , desupported in TWS > 981
44
+ false, # firm_quote_only , desupported in TWS > 981
45
+ '', ## desupported in TWS > 981, too.
46
+ order[:auction_strategy], # one of: AUCTION_MATCH, AUCTION_IMPROVEMENT, AUCTION_TRANSPARENT
47
+ order.serialize_advanced_option_order_fields,
48
+ order.override_percentage_constraints,
49
+ order.serialize_volatility_order_fields,
50
+ order.serialize_delta_neutral_order_fields
51
+ ]
52
+
53
+ fields += [
54
+ order.continuous_update,
55
+ order[:reference_price_type] ,
56
+ order.trail_stop_price,
57
+ order.trailing_percent
58
+ ]
59
+
60
+ fields << order.serialize_scale_order_fields
61
+
62
+ fields.push order.hedge_type
63
+ fields.push order.hedge_param # default is [] --> omitted if left default
64
+ fields.push order.opt_out_smart_routing
65
+
66
+ fields.push order.clearing_account
67
+ fields.push order.clearing_intent
68
+
69
+ fields.push(order.not_held)
70
+
71
+ fields << contract.serialize_under_comp
72
+ fields << order.serialize_algo
73
+
74
+ fields.push(order.algo_id)
75
+ fields.push(order.what_if)
76
+ fields.push(order.serialize_misc_options)
77
+ fields.push(order.solicided)
78
+ fields << [ order.random_size, order.random_price ]
79
+
80
+ fields << order.serialize_pegged_order_fields
81
+ fields << order.serialize_conditions
82
+ fields << [
83
+ order.adjusted_order_type,
84
+ order.trigger_price,
85
+ order.limit_price_offset,
86
+ order.adjusted_stop_price,
87
+ order.adjusted_stop_limit_price,
88
+ order.adjusted_trailing_amount,
89
+ order.adjustable_trailing_unit
90
+ ]
91
+
92
+ fields.push(order.ext_operator) # if server_version >= KNOWN_SERVERS[:min_server_ver_ext_operator] # 105
93
+
94
+ fields << order.serialize_soft_dollar_tier
95
+
96
+ fields.push(order.cash_qty) # if server_version >= KNOWN_SERVERS[:min_server_ver_cash_qty] # 111
97
+
98
+ fields << order.serialize_mifid_order_fields
99
+
100
+ # if server_version >= KNOWN_SERVERS[:min_server_ver_auto_price_for_hedge] # 141
101
+ fields.push(order.dont_use_auto_price_for_hedge)
102
+ # end
103
+
104
+ fields.push(order.is_O_ms_container) #if server_version >= KNOWN_SERVERS[:min_server_ver_order_container] # 145
105
+
106
+ if server_version >= KNOWN_SERVERS[:min_server_ver_d_peg_orders] # 148
107
+ fields.push(order.discretionary_up_to_limit_price)
108
+ end
109
+
110
+ if server_version >= KNOWN_SERVERS[:min_server_ver_price_mgmt_algo] # 151
111
+ fields.push(order.use_price_management_algo)
112
+ end
113
+
114
+ if server_version >= KNOWN_SERVERS[:min_server_ver_duration] # 158
115
+ fields.push(order.duration)
116
+ end
117
+
118
+ if server_version >= KNOWN_SERVERS[:min_server_ver_post_to_ats] # 160
119
+ fields.push(order.post_to_ats)
120
+ end
121
+
122
+ if server_version >= KNOWN_SERVERS[:min_server_ver_auto_cancel_parent] # 162
123
+ fields.push(order.auto_cancel_parent)
124
+ end
125
+
126
+ if server_version >= KNOWN_SERVERS[:min_server_ver_advanced_order_reject] # 166
127
+ fields.push(order.advanced_order_reject)
128
+ end
129
+
130
+ if server_version >= KNOWN_SERVERS[:min_server_ver_manual_order_time] # 169
131
+ fields.push(order.manual_order_time)
132
+ end
133
+
134
+ fields << order.serialize_peg_best_and_mid
135
+
136
+ if server_version >= KNOWN_SERVERS[:min_server_ver_customer_account] # 183
137
+ fields.push(order.customer_account)
138
+ end
139
+
140
+ if server_version >= KNOWN_SERVERS[:min_server_ver_professional_customer] # 184
141
+ fields.push(order.professional_account)
142
+ end
143
+
144
+ fields
145
+ end
146
+ end
147
+ end
148
+ end
149
+ end
@@ -0,0 +1,79 @@
1
+ module IB
2
+ module Messages
3
+ module Outgoing
4
+ extend Messages # def_message macros
5
+
6
+ =begin
7
+ Call this method to request and keep up to date the data that appears
8
+ on the TWS Account Window Summary tab. The data is returned by
9
+ accountSummary().
10
+
11
+ Note: This request is designed for an FA managed account but can be
12
+ used for any multi-account structure.
13
+
14
+ reqId:int - The ID of the data request. Ensures that responses are matched
15
+ to requests If several requests are in process.
16
+ groupName:str - Set to All to returnrn account summary data for all
17
+ accounts, or set to a specific Advisor Account Group name that has
18
+ already been created in TWS Global Configuration.
19
+ tags:str - A comma-separated list of account tags. Available tags are:
20
+ accountountType
21
+ NetLiquidation,
22
+ TotalCashValue - Total cash including futures pnl
23
+ SettledCash - For cash accounts, this is the same as
24
+ TotalCashValue
25
+ AccruedCash - Net accrued interest
26
+ BuyingPower - The maximum amount of marginable US stocks the
27
+ account can buy
28
+ EquityWithLoanValue - Cash + stocks + bonds + mutual funds
29
+ PreviousDayEquityWithLoanValue,
30
+ GrossPositionValue - The sum of the absolute value of all stock
31
+ and equity option positions
32
+ RegTEquity,
33
+ RegTMargin,
34
+ SMA - Special Memorandum Account
35
+ InitMarginReq,
36
+ MaintMarginReq,
37
+ AvailableFunds,
38
+ ExcessLiquidity,
39
+ Cushion - Excess liquidity as a percentage of net liquidation value
40
+ FullInitMarginReq,
41
+ FullMaintMarginReq,
42
+ FullAvailableFunds,
43
+ FullExcessLiquidity,
44
+ LookAheadNextChange - Time when look-ahead values take effect
45
+ LookAheadInitMarginReq,
46
+ LookAheadMaintMarginReq,
47
+ LookAheadAvailableFunds,
48
+ LookAheadExcessLiquidity,
49
+ HighestSeverity - A measure of how close the account is to liquidation
50
+ DayTradesRemaining - The Number of Open/Close trades a user
51
+ could put on before Pattern Day Trading is detected. A value of "-1"
52
+ means that the user can put on unlimited day trades.
53
+ Leverage - GrossPositionValue / NetLiquidation
54
+ $LEDGER - Single flag to relay all cash balance tags*, only in base
55
+ currency.
56
+ $LEDGER:CURRENCY - Single flag to relay all cash balance tags*, only in
57
+ the specified currency.
58
+ $LEDGER:ALL - Single flag to relay all cash balance tags* in all
59
+ currencies.
60
+
61
+ =end
62
+ RequestAccountSummary = def_message( 62,
63
+ :request_id, # autogenerated if not specified
64
+ [:group, 'All'],
65
+ :tags )
66
+
67
+
68
+
69
+ end # module outgoing
70
+ end # module messages
71
+ end # module ib
72
+
73
+ # REQ_POSITIONS = 61
74
+ # REQ_ACCOUNT_SUMMARY = 62
75
+ # CANCEL_ACCOUNT_SUMMARY = 63
76
+ # CANCEL_POSITIONS = 64
77
+
78
+ # REQ_ACCOUNT_UPDATES_MULTI = 76
79
+ # CANCEL_ACCOUNT_UPDATES_MULTI = 77
@@ -0,0 +1,182 @@
1
+ module IB
2
+ module Messages
3
+ module Outgoing
4
+ extend Messages # def_message macros
5
+
6
+
7
+ RequestHistoricalData = def_message [20, 0], BarRequestMessage,
8
+ :request_id # autogenerated if not specified
9
+
10
+ # - data = {
11
+ # :contract => Contract: requested ticker description
12
+ # :end_date_time => String: "yyyymmdd HH:mm:ss", with optional time zone
13
+ # allowed after a space: "20050701 18:26:44 GMT"
14
+ # :duration => String, time span the request will cover, and is specified
15
+ # using the format: <integer> <unit>, eg: '1 D', valid units are:
16
+ # '1 S' (seconds, default if no unit is specified)
17
+ # '1 D' (days)
18
+ # '1 W' (weeks)
19
+ # '1 M' (months)
20
+ # '1 Y' (years, currently limited to one)
21
+ # :bar_size => String: Specifies the size of the bars that will be returned
22
+ # (within IB/TWS limits). Valid values include:
23
+ # '1 sec'
24
+ # '5 secs'
25
+ # '15 secs'
26
+ # '30 secs'
27
+ # '1 min'
28
+ # '2 mins'
29
+ # '3 mins'
30
+ # '5 mins'
31
+ # '15 mins'
32
+ # '30 min'
33
+ # '1 hour'
34
+ # '1 day'
35
+ # :what_to_show => Symbol: Determines the nature of data being extracted.
36
+ # Valid values:
37
+ # :trades, :midpoint, :bid, :ask, :bid_ask,
38
+ # :historical_volatility, :option_implied_volatility,
39
+ # :option_volume, :option_open_interest
40
+ # - converts to "TRADES," "MIDPOINT," "BID," etc...
41
+ # :use_rth => int: 0 - all data available during the time span requested
42
+ # is returned, even data bars covering time intervals where the
43
+ # market in question was illiquid. 1 - only data within the
44
+ # "Regular Trading Hours" of the product in question is returned,
45
+ # even if the time span requested falls partially or completely
46
+ # outside of them.
47
+ # :format_date => int: 1 - text format, like "20050307 11:32:16".
48
+ # 2 - offset from 1970-01-01 in sec (UNIX epoch)
49
+ # }
50
+ #
51
+ # - NB: using the D :duration only returns bars in whole days, so requesting "1 D"
52
+ # for contract ending at 08:05 will only return 1 bar, for 08:00 on that day.
53
+ # But requesting "86400 S" gives 86400/barlengthsecs bars before the end Time.
54
+ #
55
+ # - Note also that the :duration for any request must be such that the start Time is not
56
+ # more than one year before the CURRENT-Time-less-one-day (not 1 year before the end
57
+ # Time in the Request)
58
+ #
59
+ # Bar Size Max Duration
60
+ # -------- ------------
61
+ # 1 sec 2000 S
62
+ # 5 sec 10000 S
63
+ # 15 sec 30000 S
64
+ # 30 sec 86400 S
65
+ # 1 minute 86400 S, 6 D
66
+ # 2 minutes 86400 S, 6 D
67
+ # 5 minutes 86400 S, 6 D
68
+ # 15 minutes 86400 S, 6 D, 20 D, 2 W
69
+ # 30 minutes 86400 S, 34 D, 4 W, 1 M
70
+ # 1 hour 86400 S, 34 D, 4 w, 1 M
71
+ # 1 day 60 D, 12 M, 52 W, 1 Y
72
+ #
73
+ # - NB: as of 4/07 there is no historical data available for forex spot.
74
+ #
75
+ # - data[:contract] may either be a Contract object or a String. A String should be
76
+ # in serialize_ib_ruby format; that is, it should be a colon-delimited string in
77
+ # the format (e.g. for Globex British pound futures contract expiring in Sep-2008):
78
+ #
79
+ #
80
+ # - Fields not needed for a particular security should be left blank (e.g. strike
81
+ # and right are only relevant for options.)
82
+ #
83
+ # - A Contract object will be automatically serialized into the required format.
84
+ #
85
+ # - See also http://chuckcaplan.com/twsapi/index.php/void%20reqIntradayData%28%29
86
+ # for general information about how TWS handles historic data requests, whence
87
+ # the following has been adapted:
88
+ #
89
+ # - The server providing historical prices appears to not always be
90
+ # available outside of market hours. If you call it outside of its
91
+ # supported time period, or if there is otherwise a problem with
92
+ # it, you will receive error #162 "Historical Market Data Service
93
+ # query failed.:HMDS query returned no data."
94
+ #
95
+ # - For backfill on futures data, you may need to leave the Primary
96
+ # Exchange field of the Contract structure blank; see
97
+ # http://www.interactivebrokers.com/discus/messages/2/28477.html?1114646754
98
+ #
99
+ # - Version 6 implemented --> the version is not transmitted anymore
100
+ class RequestHistoricalData
101
+ def parse data
102
+ data_type, bar_size, contract = super data
103
+
104
+ size = data[:bar_size] || data[:size]
105
+ bar_size = BAR_SIZES.invert[size] || size
106
+ unless BAR_SIZES.keys.include?(bar_size)
107
+ error ":bar_size must be one of #{BAR_SIZES.inspect}", :args
108
+ end
109
+ [data_type, bar_size, contract]
110
+ end
111
+
112
+ def encode
113
+ data_type, bar_size, contract = parse @data
114
+
115
+ [super.flatten,
116
+ contract.serialize_long[0..-1], # omit sec_id_type and sec_id
117
+ @data[:end_date_time],
118
+ bar_size,
119
+ @data[:duration],
120
+ @data[:use_rth],
121
+ data_type.to_s.upcase,
122
+ 2 , # @data[:format_date], format-date is hard-coded as int_date in incoming/historicalData
123
+ contract.serialize_legs ,
124
+ @data[:keep_up_todate], # 0 / 1
125
+ '' # chartOptions:TagValueList - For internal use only. Use default value XYZ.
126
+ ]
127
+ end
128
+ end # RequestHistoricalData
129
+
130
+ end # module Outgoing
131
+ end # module Messages
132
+ end # module IB
133
+
134
+ ## python documentaion
135
+ # """Requests contracts' historical data. When requesting historical data, a
136
+ # finishing time and date is required along with a duration string. The
137
+ # resulting bars will be returned in EWrapper.historicalData()
138
+ # reqId:TickerId - The id of the request. Must be a unique value. When the
139
+ # market data returns, it whatToShowill be identified by this tag. This is also
140
+ # used when canceling the market data.
141
+ # contract:Contract - This object contains a description of the contract for which
142
+ # market data is being requested.
143
+ # endDateTime:str - Defines a query end date and time at any point during the past 6 mos.
144
+ # Valid values include any date/time within the past six months in the format:
145
+ # yyyymmdd HH:mm:ss ttt
146
+ # where "ttt" is the optional time zone.
147
+ # durationStr:str - Set the query duration up to one week, using a time unit
148
+ # of seconds, days or weeks. Valid values include any integer followed by a space
149
+ # and then S (seconds), D (days) or W (week). If no unit is specified, seconds is used.
150
+ # barSizeSetting:str - Specifies the size of the bars that will be returned (within IB/TWS listimits).
151
+ # Valid values include:
152
+ # 1 sec
153
+ # 5 secs
154
+ # 15 secs
155
+ # 30 secs
156
+ # 1 min
157
+ # 2 mins
158
+ # 3 mins
159
+ # 5 mins
160
+ # 15 mins
161
+ # 30 mins
162
+ # 1 hour
163
+ # 1 day
164
+ # whatToShow:str - Determines the nature of data beinging extracted. Valid values include:
165
+ # TRADES
166
+ # MIDPOINT
167
+ # BID
168
+ # ASK
169
+ # BID_ASK
170
+ # HISTORICAL_VOLATILITY
171
+ # OPTION_IMPLIED_VOLATILITY
172
+ # useRTH:int - Determines whether to return all data available during the requested time span,
173
+ # or only data that falls within regular trading hours. Valid values include:
174
+ # 0 - all data is returned even where the market in question was outside of its
175
+ # regular trading hours.
176
+ # 1 - only data within the regular trading hours is returned, even if the
177
+ # requested time span falls partially or completely outside of the RTH.
178
+ # formatDate: int - Determines the date format applied to returned bars. validd values include:
179
+ # 1 - dates applying to bars returned in the format: yyyymmdd{space}{space}hh:mm:dd
180
+ # 2 - dates are returned as a long integer specifying the number of seconds since
181
+ # 1/1/1970 GMT.
182
+ # chartOptions:TagValueList - For internal use only. Use default value XYZ. """
@@ -0,0 +1,102 @@
1
+ module IB
2
+ module Messages
3
+ module Outgoing
4
+ extend Messages # def_message macros
5
+
6
+ RequestMarketData =
7
+ def_message [ 1, 11 ], :request_id,
8
+ [ :contract, :serialize_short ],
9
+ [ :contract, :serialize_legs, :market_data ],
10
+ [ :delta_neutral, false ], # delta neutral: we do not support that
11
+ [ :tick_list, ->(tick_list){ tick_list.is_a?(Array) ? tick_list.join(',') : (tick_list || '')}, [] ],
12
+ [ :snapshot, false ],
13
+ [ :regulatory_snapshot, false ],
14
+ [ :mkt_data_options, "" ] # changed to enable requests in V 10.19 ff
15
+ end
16
+
17
+ # ==> details: https://interactivebrokers.github.io/tws-api/tick_types.html
18
+ #
19
+ # @data={:id => int: ticker_id - Must be a unique value. When the market data
20
+ # returns, it will be identified by this tag,
21
+ # if omitted its autogenerated
22
+ # :contract => IB::Contract, requested contract.
23
+ # :tick_list => String: comma delimited list of requested tick groups:
24
+ # Group ID - Description - Requested Tick Types
25
+ # 100 - Option Volume (currently for stocks) - 29, 30
26
+ # 101 - Option Open Interest (currently for stocks) - 27, 28
27
+ # 104 - Historical Volatility (currently for stocks) - 23
28
+ # 105 - Average Opt Volume, # new 971
29
+ # 106 - Option Implied Volatility (impvolat) - 24
30
+ # 107 (climpvlt) # new 971
31
+ # 125 (Bond analytic data) # new 971
32
+ # 162 - Index Future Premium - 31
33
+ # 165 - Miscellaneous Stats - 15, 16, 17, 18, 19, 20, 21
34
+ # 166 (CScreen) # new 971,
35
+ # 221/220 - Creditman, Mark Price (used in TWS P&L computations) - 37
36
+ # 225 - Auction values (volume, price and imbalance) - 34, 35, 36
37
+ # 232/221(Pl-price ) # new 971
38
+ # 233 - RTVolume - 48
39
+ # 236 - Shortable (inventory) - 46
40
+ # 256 - Inventory - ?
41
+ # 258 - Fundamental Ratios - 47
42
+ # 291 - (ivclose)
43
+ # 292 - (Wide News)
44
+ # 293 - (TradeCount)
45
+ # 295 - (VolumeRate)
46
+ # 318 - (LastRTHT-Trade)
47
+ # 370 - (Participation Monitor)
48
+ # 375 - RTTrdVolumne
49
+ # 377 - CttTickTag
50
+ # 381 - IB-Rate
51
+ # 384 - RfdTickRespTag
52
+ # 387 - DMM
53
+ # 388 - Issuer Fundamentals
54
+ # 391 - IBWarrantImplVolCompeteTick
55
+ # 405 - Index Capabilities
56
+ # 407 - Futures Margins
57
+ # 411 - Realtime Historical Volatility - 58
58
+ # 428 - Monetary Close
59
+ # 439 - MonitorTicTag
60
+ # 456/59 - IB Dividends, 4 comma separated values: 12 Month dividend,
61
+ # projected 12 Month dividend,
62
+ # next dividend date,
63
+ # next dividend value
64
+ # (use primary exchange instead of smart)
65
+ # 459 - RTCLOSE
66
+ # 460 - Bond Factor Multiplier
67
+ # 499 - Fee and Rebate Ratge
68
+ # 506 - midptiv
69
+ #
70
+ # 511(hvolrt10 (per-underlying)),
71
+ # 512(hvolrt30 (per-underlying)),
72
+ # 513(hvolrt50 (per-underlying)),
73
+ # 514(hvolrt75 (per-underlying)),
74
+ # 515(hvolrt100 (per-underlying)),
75
+ # 516(hvolrt150 (per-underlying)),
76
+ # 517(hvolrt200 (per-underlying)),
77
+ # 521(fzmidptiv),
78
+ # 545(vsiv),
79
+ # 576(EtfNavBidAsk(navbidask)),
80
+ # 577(EtfNavLast(navlast)),
81
+ # 578(EtfNavClose(navclose)),
82
+ # 584(Average Opening Vol.),
83
+ # 585(Average Closing Vol.),
84
+ # 587(Pl Price Delayed),
85
+ # 588(Futures Open Interest),
86
+ # 595(Short-Term Volume X Mins),
87
+ # 608(EMA N),
88
+ # 614(EtfNavMisc(hight/low)),
89
+ # 619(Creditman Slow Mark Price),
90
+ # 623(EtfFrozenNavLast(fznavlast) ## updated 2018/1/21
91
+ #
92
+ # :snapshot => bool: Check to return a single snapshot of market data and
93
+ # have the market data subscription canceled. Do not enter any
94
+ # :tick_list values if you use snapshot.
95
+ #
96
+ # :regulatory_snapshot => bool - With the US Value Snapshot Bundle for stocks,
97
+ # regulatory snapshots are available for 0.01 USD each. (applies on demo accounts as well)
98
+ # :mktDataOptions => (TagValueList) For internal use only.
99
+ # Use default value XYZ.
100
+ #
101
+ end
102
+ end
@@ -0,0 +1,57 @@
1
+
2
+ module IB
3
+ module Messages
4
+ module Outgoing
5
+ extend Messages # def_message macros
6
+
7
+ ## actual Version supported is: 137
8
+ ## changes: MIN_SERVER_VER_SMART_DEPTH: 146 --> insert 'is_smarth_depth' after 'num_rows'
9
+ ## then: 'is_smart_depth' (bool) has to be specified in CancelMarketDepth, too
10
+ #
11
+ #
12
+
13
+ RequestMarketDepth = def_message(
14
+ [10, 5],
15
+ :request_id, # autogenerated if not specified
16
+ [:contract, :serialize, :option, :trading_class], # serialize_supershort],
17
+ :num_rows,
18
+ :is_smart_depth,
19
+ ""
20
+ ) # mktDataOptionsStr. ## not supported by api
21
+
22
+ class RequestMarketDepth
23
+ def encode
24
+ ## create a proper request_id and erase :id and :ticker_id if nessesary
25
+ if self.class.properties?.include?(:request_id)
26
+ @data[:request_id] = @data[:id] || @data[:ticker_id] || @data[:request_id] || rand(9999)
27
+ @data[:id] = @data[:ticker_id] = nil
28
+ end
29
+ contract = @data[:contract]
30
+ # error "RequestMarketDepth requires a valid con-id" if contract.con_id.empty?
31
+
32
+ [
33
+ self.class.message_id,
34
+ self.class.version,
35
+ @data[:request_id],
36
+ contract.con_id,
37
+ contract.symbol,
38
+ contract[:sec_type],
39
+ contract.expiry, #last_trade_date_or_contract_month,
40
+ contract.strike,
41
+ contract.right == :none ? '' : contract.right,
42
+ contract.multiplier,
43
+ contract.exchange,
44
+ contract.primary_exchange,
45
+ contract.currency,
46
+ contract.local_symbol,
47
+ contract.trading_class,
48
+ @data[:num_rows],
49
+ @data[:is_smart_depth],
50
+ "",
51
+ ""
52
+ ]
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,48 @@
1
+ module IB
2
+ module Messages
3
+ module Outgoing
4
+ extend Messages # def_message macros
5
+
6
+
7
+ # data = { :id => ticker_id (int),
8
+ # :contract => Contract ,
9
+ # :bar_size => int/Symbol? Currently only 5 second bars are supported,
10
+ # if any other value is used, an exception will be thrown.,
11
+ # :data_type => Symbol: Determines the nature of data being extracted.
12
+ # :trades, :midpoint, :bid, :ask, :bid_ask,
13
+ # :historical_volatility, :option_implied_volatility,
14
+ # :option_volume, :option_open_interest
15
+ # - converts to "TRADES," "MIDPOINT," "BID," etc...
16
+ # :use_rth => int: 0 - all data available during the time span requested
17
+ # is returned, even data bars covering time intervals where the
18
+ # market in question was illiquid. 1 - only data within the
19
+ # "Regular Trading Hours" of the product in question is returned,
20
+ # even if the time span requested falls partially or completely
21
+ # outside of them.
22
+ #
23
+ # Version 3
24
+ RequestRealTimeBars = def_message [ 50, 3 ], BarRequestMessage,
25
+ :request_id # autogenerated if not specified
26
+
27
+ class RequestRealTimeBars
28
+ def parse data
29
+ data_type, bar_size, contract = super data
30
+
31
+ size = data[:bar_size] || data[:size]
32
+ bar_size = 5 # only 5 sec bars are supported --> for future use ::> size.to_i
33
+ [data_type, bar_size, contract]
34
+ end
35
+
36
+ def encode
37
+ data_type, bar_size, contract = parse @data
38
+
39
+ [super,
40
+ contract.serialize_short(:primary_exchange), # include primary exchange in request
41
+ bar_size,
42
+ data_type.to_s.upcase,
43
+ @data[:use_rth] ,
44
+ "" # not suported realtimebars option string
45
+ ]
46
+ end
47
+ end # RequestRealTimeBars
48
+