ib-extensions 1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -0
  3. data/.rspec +3 -0
  4. data/.travis.yml +6 -0
  5. data/CODE_OF_CONDUCT.md +74 -0
  6. data/Gemfile +9 -0
  7. data/Gemfile.lock +112 -0
  8. data/Guardfile +24 -0
  9. data/README.md +99 -0
  10. data/Rakefile +6 -0
  11. data/bin/console +96 -0
  12. data/bin/console.yml +3 -0
  13. data/bin/gateway.rb +97 -0
  14. data/bin/setup +8 -0
  15. data/changelog.md +31 -0
  16. data/examples/cancel_orders +74 -0
  17. data/examples/eod +35 -0
  18. data/examples/input.rb +475 -0
  19. data/examples/market_price +57 -0
  20. data/examples/option_chain +67 -0
  21. data/examples/place_and_modify_order +162 -0
  22. data/examples/place_bracket_order +62 -0
  23. data/examples/place_butterfly_order +104 -0
  24. data/examples/place_combo_order +70 -0
  25. data/examples/place_limit_order +82 -0
  26. data/examples/place_the_limit_order +145 -0
  27. data/examples/volatility_research +139 -0
  28. data/examples/what_if_order +90 -0
  29. data/ib-extensions.gemspec +37 -0
  30. data/lib/ib-gateway.rb +5 -0
  31. data/lib/ib/alerts/base-alert.rb +128 -0
  32. data/lib/ib/alerts/gateway-alerts.rb +15 -0
  33. data/lib/ib/alerts/order-alerts.rb +68 -0
  34. data/lib/ib/eod.rb +152 -0
  35. data/lib/ib/extensions.rb +9 -0
  36. data/lib/ib/extensions/contract.rb +37 -0
  37. data/lib/ib/extensions/version.rb +5 -0
  38. data/lib/ib/flex.rb +150 -0
  39. data/lib/ib/gateway.rb +425 -0
  40. data/lib/ib/gateway/account-infos.rb +115 -0
  41. data/lib/ib/gateway/order-handling.rb +150 -0
  42. data/lib/ib/market-price.rb +134 -0
  43. data/lib/ib/models/account.rb +329 -0
  44. data/lib/ib/models/spread.rb +159 -0
  45. data/lib/ib/option-chain.rb +198 -0
  46. data/lib/ib/option-greeks.rb +88 -0
  47. data/lib/ib/order-prototypes.rb +110 -0
  48. data/lib/ib/order_prototypes/abstract.rb +67 -0
  49. data/lib/ib/order_prototypes/combo.rb +46 -0
  50. data/lib/ib/order_prototypes/forex.rb +40 -0
  51. data/lib/ib/order_prototypes/limit.rb +177 -0
  52. data/lib/ib/order_prototypes/market.rb +116 -0
  53. data/lib/ib/order_prototypes/pegged.rb +173 -0
  54. data/lib/ib/order_prototypes/premarket.rb +31 -0
  55. data/lib/ib/order_prototypes/stop.rb +202 -0
  56. data/lib/ib/order_prototypes/volatility.rb +39 -0
  57. data/lib/ib/spread-prototypes.rb +62 -0
  58. data/lib/ib/spread_prototypes/butterfly.rb +79 -0
  59. data/lib/ib/spread_prototypes/calendar.rb +85 -0
  60. data/lib/ib/spread_prototypes/stock-spread.rb +48 -0
  61. data/lib/ib/spread_prototypes/straddle.rb +75 -0
  62. data/lib/ib/spread_prototypes/strangle.rb +96 -0
  63. data/lib/ib/spread_prototypes/vertical.rb +84 -0
  64. data/lib/ib/verify.rb +226 -0
  65. metadata +206 -0
@@ -0,0 +1,173 @@
1
+ module IB
2
+ module Pegged2Primary
3
+ extend OrderPrototype
4
+ class << self
5
+
6
+ def defaults
7
+ super.merge order_type: 'REL' , tif: :day
8
+ end
9
+
10
+ def aliases
11
+ super.merge limit_price: :price_cap, aux_price: :offset_amount
12
+ end
13
+
14
+ def requirements
15
+ super.merge aux_price: 'also aliased as :offset_amount',
16
+ limit_price: 'aliased as :price_cap'
17
+ end
18
+
19
+ def optional
20
+ super
21
+ end
22
+
23
+ def summary
24
+ <<-HERE
25
+ Relative (a.k.a. Pegged-to-Primary) orders provide a means for traders
26
+ to seek a more aggressive price than the National Best Bid and Offer
27
+ (NBBO). By acting as liquidity providers, and placing more aggressive
28
+ bids and offers than the current best bids and offers, traders increase
29
+ their odds of filling their order. Quotes are automatically adjusted as
30
+ the markets move, to remain aggressive. For a buy order, your bid is
31
+ pegged to the NBB by a more aggressive offset, and if the NBB moves up,
32
+ your bid will also move up. If the NBB moves down, there will be no
33
+ adjustment because your bid will become even more aggressive and
34
+ execute. For sales, your offer is pegged to the NBO by a more
35
+ aggressive offset, and if the NBO moves down, your offer will also move
36
+ down. If the NBO moves up, there will be no adjustment because your
37
+ offer will become more aggressive and execute. In addition to the
38
+ offset, you can define an absolute cap, which works like a limit price,
39
+ and will prevent your order from being executed above or below a
40
+ specified level.
41
+ Supported Products: Stocks, Options and Futures
42
+ ------
43
+ not available on paper trading
44
+ HERE
45
+ end
46
+ end
47
+ end
48
+ module Pegged2Market
49
+ extend OrderPrototype
50
+ class << self
51
+
52
+ def defaults
53
+ super.merge order_type: 'PEG MKT' , tif: :day
54
+ end
55
+
56
+ def aliases
57
+ Limit.aliases.merge aux_price: :market_offset
58
+ end
59
+
60
+ def requirements
61
+ super.merge aux_price: :decimal
62
+ end
63
+
64
+ def optional
65
+ super
66
+ end
67
+
68
+ def summary
69
+ <<-HERE
70
+ A pegged-to-market order is designed to maintain a purchase price relative to the
71
+ national best offer (NBO) or a sale price relative to the national best bid (NBB).
72
+ Depending on the width of the quote, this order may be passive or aggressive.
73
+ The trader creates the order by entering a limit price which defines the worst limit
74
+ price that they are willing to accept.
75
+ Next, the trader enters an offset amount which computes the active limit price as follows:
76
+ Sell order price = Bid price + offset amount
77
+ Buy order price = Ask price - offset amount
78
+ HERE
79
+ end
80
+ end
81
+ end
82
+
83
+ module Pegged2Stock
84
+ extend OrderPrototype
85
+ class << self
86
+
87
+ def defaults
88
+ super.merge order_type: 'PEG STK'
89
+ end
90
+
91
+ def aliases
92
+ Limit.aliases.merge limit_price: :stock_reference_price
93
+ end
94
+
95
+ def requirements
96
+ super.merge total_quantity: :decimal,
97
+ delta: 'required Delta of the Option',
98
+ starting_price: 'initial Limit-Price for the Option'
99
+ end
100
+
101
+ def optional
102
+ super.merge limit_price: 'Stock Reference Price',
103
+ stock_ref_price: '',
104
+ stock_range_lower: 'Lowest acceptable Stock Price',
105
+ stock_range_upper: 'Highest accepable Stock Price'
106
+ end
107
+
108
+ def summary
109
+ <<-HERE
110
+ Options ONLY
111
+ ------------
112
+ A Pegged to Stock order continually adjusts the option order price by the product of a signed user-
113
+ defined delta and the change of the option's underlying stock price.
114
+ The delta is entered as an absolute and assumed to be positive for calls and negative for puts.
115
+ A buy or sell call order price is determined by adding the delta times a change in an underlying stock
116
+ price to a specified starting price for the call.
117
+ To determine the change in price, the stock reference price is subtracted from the current NBBO
118
+ midpoint. The Stock Reference Price can be defined by the user, or defaults to the
119
+ the NBBO midpoint at the time of the order if no reference price is entered.
120
+ You may also enter a high/low stock price range which cancels the order when reached. The
121
+ delta times the change in stock price will be rounded to the nearest penny in favor of the order.
122
+ ------------
123
+ Supported Exchanges: (as of Jan 2018): BOX, NASDAQOM, PHLX
124
+ HERE
125
+ end
126
+ end
127
+ end
128
+
129
+ module Pegged2Benchmark
130
+ extend OrderPrototype
131
+ class << self
132
+
133
+ def defaults
134
+ super.merge order_type: 'PEG BENCH'
135
+ end
136
+
137
+
138
+
139
+
140
+
141
+ def requirements
142
+ super.merge total_quantity: :decimal,
143
+ delta: 'required Delta of the Option',
144
+ starting_price: 'initial Limit-Price for the Option' ,
145
+ is_pegged_change_amount_decrease: 'increase(true) / decrease(false) Price',
146
+ pegged_change_amount: ' (increase/decrceas) by... (and likewise for price moving in opposite direction)',
147
+ reference_change_amount: ' ... whenever there is a price change of...',
148
+ reference_contract_id: 'the conid of the reference contract',
149
+ reference_exchange: "Exchange of the reference contract"
150
+
151
+
152
+
153
+
154
+
155
+ end
156
+
157
+ def optional
158
+ super.merge stock_ref_price: 'starting price of the reference contract',
159
+ stock_range_lower: 'Lowest acceptable Price of the reference contract',
160
+ stock_range_upper: 'Highest accepable Price of the reference contract'
161
+ end
162
+
163
+ def summary
164
+ <<-HERE
165
+ The Pegged to Benchmark order is similar to the Pegged to Stock order for options,
166
+ except that the Pegged to Benchmark allows you to specify any asset type as the
167
+ reference (benchmark) contract for a stock or option order. Both the primary and
168
+ reference contracts must use the same currency.
169
+ HERE
170
+ end
171
+ end
172
+ end
173
+ 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,62 @@
1
+ require 'ib/models/spread'
2
+ require 'ib/verify'
3
+ # These modules are used to facilitate referencing of most common Spreads
4
+
5
+ # Spreads are created in two ways:
6
+ #
7
+ # (1) IB::Spread::{prototype}.build from: {underlying},
8
+ # trading_class: (optional)
9
+ # {other specific attributes}
10
+ #
11
+ # (2) IB::Spread::{prototype}.fabcricate master: [one leg},
12
+ # {other specific attributes}
13
+ #
14
+ # They return a freshly instantiated Spread-Object
15
+ #
16
+ module IB
17
+ module SpreadPrototype
18
+
19
+
20
+ def build from: , **fields
21
+
22
+ end
23
+
24
+
25
+ def initialize_spread ref_contract = nil, **attributes
26
+ error "Initializing of Spread failed – contract is missing" unless ref_contract.is_a?(IB::Contract)
27
+ the_contract = ref_contract.merge( attributes ).verify.first
28
+ error "Underlying for Spread is not valid: #{ref_contract.to_human}" if the_contract.nil?
29
+ the_spread= IB::Spread.new the_contract.attributes.slice( :exchange, :symbol, :currency )
30
+ error "Initializing of Spread failed – Underling is no Contract" if the_spread.nil?
31
+ yield the_spread if block_given? # yield outside mutex controlled verify-environment
32
+ the_spread # return_value
33
+ end
34
+
35
+ def requirements
36
+ {}
37
+ end
38
+
39
+ def defaults
40
+ {}
41
+ end
42
+
43
+ def optional
44
+ { }
45
+ end
46
+
47
+ def parameters
48
+ the_output = ->(var){ var.empty? ? "none" : var.map{|x| x.join(" --> ") }.join("\n\t: ")}
49
+
50
+ "Required : " + the_output[requirements] + "\n --------------- \n" +
51
+ "Optional : " + the_output[optional] + "\n --------------- \n"
52
+
53
+ end
54
+ end
55
+
56
+ end
57
+ require 'ib/spread_prototypes/straddle'
58
+ require 'ib/spread_prototypes/strangle'
59
+ require 'ib/spread_prototypes/vertical'
60
+ require 'ib/spread_prototypes/calendar'
61
+ require 'ib/spread_prototypes/stock-spread'
62
+ require 'ib/spread_prototypes/butterfly'