Class: Ish::IronCondor

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, Mongoid::Timestamps
Defined in:
lib/ish/iron_condor.rb

Overview

c.update_attributes( call_sell_strike: 242, call_buy_strike: 243, put_sell_strike: 229, put_buy_strike: 228 )

Constant Summary collapse

STATUSES =
[ :queued, :placed, :filled, :rolling_up, :rolling_down, :rolled_up, :rolled_down, :expired ]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all_filledObject


28
29
30
# File 'lib/ish/iron_condor.rb', line 28

def self.all_filled
  where( status: :filled )
end

Instance Method Details

#buysell_spreadObject


56
57
58
# File 'lib/ish/iron_condor.rb', line 56

def buysell_spread
  1 # $1 for QQQ
end

#created_onObject

Internal, below


26
# File 'lib/ish/iron_condor.rb', line 26

def created_on; created_at.to_date; end

#get_call_buy_strikeObject


64
65
66
# File 'lib/ish/iron_condor.rb', line 64

def get_call_buy_strike
  call_sell_strike + buysell_spread
end

#get_call_sell_strikeObject


60
61
62
63
# File 'lib/ish/iron_condor.rb', line 60

def get_call_sell_strike
  result = enter_price * ( 1 - period_iv/100 )
  result = result.ceil
end

#get_put_buy_strikeObject


72
73
74
# File 'lib/ish/iron_condor.rb', line 72

def get_put_buy_strike
  put_sell_strike - buysell_spread
end

#get_put_sell_strikeObject


68
69
70
71
# File 'lib/ish/iron_condor.rb', line 68

def get_put_sell_strike
  result = enter_price * ( 1 - period_iv/100 )
  result = result.floor
end

#iv_periodObject Also known as: period_iv


45
46
47
48
# File 'lib/ish/iron_condor.rb', line 45

def iv_period
  n_days = created_on.business_days_until expires_on
  result = iv_annual.to_f / Math.sqrt(252/n_days)
end

#lower_panic_thresholdObject


80
81
82
# File 'lib/ish/iron_condor.rb', line 80

def lower_panic_threshold
  result = put_sell_strike * ( 1 + panic_percentage )
end

#new_multileg_order_example_doneObject


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/ish/iron_condor.rb', line 84

def new_multileg_order_example_done
  ticker = 'QQQ'
  px = 0.08
   = ALLY_CREDS[:account_id]
  n_contracts = 1
  sell_strike = 237.0
  buy_strike = 237.5
  expiration = '2020-02-21'.to_date

  tmpl = <<~AOL
  <FIXML xmlns="http://www.fixprotocol.org/FIXML-5-0-SP2">
    <NewOrdMleg TmInForce="0" Px="#{px}" OrdTyp="2" Acct="#{}">
      <Ord OrdQty="#{n_contracts}" PosEfct="O">
        <Leg Side="2" Strk="#{sell_strike}" 
          Mat="#{expiration.strftime('%Y-%m-%d')}T00:00:00.000-05:00" MMY="#{expiration.strftime('%y%m')}" 
          SecTyp="OPT" CFI="OC" Sym="#{ticker}"/>
      </Ord>
      <Ord OrdQty="#{n_contracts}" PosEfct="O">
        <Leg Side="1" Strk="#{buy_strike}" 
          Mat="#{expiration.strftime('%Y-%m-%d')}T00:00:00.000-05:00" MMY="#{expiration.strftime('%y%m')}" 
          SecTyp="OPT" CFI="OC" Sym="#{ticker}"/>
      </Ord>
    </NewOrdMleg>
  </FIXML>
  AOL
end

#new_purchase_trashObject


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/ish/iron_condor.rb', line 111

def new_purchase_trash
  ticker = 'AXU'
  px = 2.06
   = ALLY_CREDS[:account_id]
  n_contracts = 1
  strike = 2.06

  xml = <<~AOL
    <FIXML xmlns="http://www.fixprotocol.org/FIXML-5-0-SP2">
      <Order TmInForce="0" Typ="1" Side="1" Acct="#{}">
        <Instrmt SecTyp="CS" Sym="#{ticker}"/>
        <OrdQty Qty="1"/>
      </Order>
    </FIXML>
  AOL
end

#panic_percentageObject

how close to a sell leg I need to be to take followup action


52
53
54
# File 'lib/ish/iron_condor.rb', line 52

def panic_percentage
  0.01 # 1% for QQQ
end

#rolldown_xml(access_token = nil, natural = nil) ⇒ Object


230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/ish/iron_condor.rb', line 230

def rolldown_xml access_token=nil, natural=nil
  @access_token ||= access_token

  new_put_sell_strike = ( natural * ( 1 - period_iv ) ).floor
  new_put_buy_strike = new_put_sell_strike - buysell_spread

  # get the costs of the option first, to compute `Px`
  ymd = expires_on.strftime('%y%m%d')
  price8 = (new_put_sell_strike*1000).to_i.to_s.rjust(8, '0')
  path = "/v1/market/ext/quotes.json?symbols=#{ticker}#{ymd}C#{price8}"
  puts! path, 'path sell'
  response = @access_token.post(path, {'Accept' => 'application/json'})
  json_sell = JSON.parse( response.body ).deep_symbolize_keys
  json_sell_bid = json_sell[:response][:quotes][:quote][:bid].to_f
  json_sell_ask = json_sell[:response][:quotes][:quote][:ask].to_f
  json_puts! json_sell, 'json_sell'

  price8 = (new_put_buy_strike*1000).to_s.rjust(8, '0')
  path = "/v1/market/ext/quotes.json?symbols=#{ticker}#{ymd}C#{price8}"
  puts! path, 'path buy'
  response = @access_token.post(path, {'Accept' => 'application/json'})
  json_buy = JSON.parse( response.body ).deep_symbolize_keys
  json_buy_bid = json_buy[:response][:quotes][:quote][:bid].to_f
  json_buy_ask = json_buy[:response][:quotes][:quote][:ask].to_f
  json_puts! json_buy, 'json_buy'

  px_sell = ( json_sell_bid.to_f + json_sell_ask ) / 2
  px_sell = px_sell # .round 2
  px_buy = ( json_buy_bid + json_buy_ask )/ 2
  px_buy = px_buy # .round 2
  px = px_sell - px_buy
  px = ( px * 20 ).round.to_f / 20 # down to nearest 0.05
  puts! px, 'px'

=begin
  update( status: :rolling_down, 
    new_put_sell_strike: new_put_sell_strike, 
    new_put_buy_strike: new_put_buy_strike )
=end

  rollup_tmpl =<<~AOL
    <?xml version="1.0" encoding="UTF-8"?>
    <FIXML xmlns="http://www.fixprotocol.org/FIXML-5-0-SP2">
      <NewOrdMleg
        OrdTyp="2"
        Px="#{px}"
        Acct="#{ALLY_CREDS[:]}" 
        TmInForce="0"
      >
        <Ord OrdQty="#{n_contracts}" PosEfct="C" >
          <Leg 
            AcctTyp="5" 
            Side="1" 
            Strk="#{put_sell_strike}" 
            Mat="#{expires_on.strftime('%Y-%m-%d')}T00:00:00.000‐05:00"
            MMY="#{expires_on.strftime('%Y%m')}"
            SecTyp="OPT"
            CFI="OP"
            Sym="#{ticker}" />
        </Ord>
        <Ord OrdQty="#{n_contracts}" PosEfct="C" >
          <Leg 
            Side="2" 
            Strk="#{put_buy_strike}" 
            Mat="#{expires_on.strftime('%Y-%m-%d')}T00:00:00.000‐05:00" 
            MMY="#{expires_on.strftime('%Y%m')}" 
            SecTyp="OPT" 
            CFI="OP" 
            Sym="#{ticker}" />
        </Ord>
        <Ord OrdQty="#{n_contracts}" PosEfct="O" >
          <Leg 
            Side="2" 
            Strk="#{new_put_sell_strike}" 
            Mat="#{expires_on.strftime('%Y-%m-%d')}T00:00:00.000‐05:00" 
            MMY="#{expires_on.strftime('%Y%m')}" 
            SecTyp="OPT" 
            CFI="OP" 
            Sym="#{ticker}" />
        </Ord>
        <Ord OrdQty="#{n_contracts}" PosEfct="O" >
          <Leg 
            Side="1" 
            Strk="#{new_put_buy_strike}" 
            Mat="#{expires_on.strftime('%Y-%m-%d')}T00:00:00.000‐05:00" 
            MMY="#{expires_on.strftime('%Y%m')}" 
            SecTyp="OPT" 
            CFI="OP" 
            Sym="#{ticker}" />
        </Ord>
      </NewOrdMleg>
    </FIXML>
  AOL
end

#rollup_xml(access_token = nil, natural = nil) ⇒ Object

www.ally.com/api/invest/documentation/fixml/ www.ally.com/api/invest/documentation/trading/ follow up, roll up buy call to close sell call to close sell call to open buy call to open


135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/ish/iron_condor.rb', line 135

def rollup_xml access_token=nil, natural=nil
  @access_token ||= access_token

  new_call_sell_strike = ( natural * ( 1 + period_iv ) ).ceil
  new_call_buy_strike = new_call_sell_strike + buysell_spread

  # get the costs of the option first, to compute `Px`
  ymd = expires_on.strftime('%y%m%d')
  price8 = (new_call_sell_strike*1000).to_i.to_s.rjust(8, '0')
  path = "/v1/market/ext/quotes.json?symbols=#{ticker}#{ymd}C#{price8}"
  puts! path, 'path sell'
  response = @access_token.post(path, {'Accept' => 'application/json'})
  json_sell = JSON.parse( response.body ).deep_symbolize_keys
  json_sell_bid = json_sell[:response][:quotes][:quote][:bid].to_f
  json_sell_ask = json_sell[:response][:quotes][:quote][:ask].to_f
  
  price8 = (new_call_buy_strike*1000).to_s.rjust(8, '0')
  path = "/v1/market/ext/quotes.json?symbols=#{ticker}#{ymd}C#{price8}"
  response = @access_token.post(path, {'Accept' => 'application/json'})
  json_buy = JSON.parse( response.body ).deep_symbolize_keys
  json_buy_bid = json_buy[:response][:quotes][:quote][:bid].to_f
  json_buy_ask = json_buy[:response][:quotes][:quote][:ask].to_f

  px_sell = ( json_sell_bid.to_f + json_sell_ask ) / 2
  px_sell = px_sell # .round 2
  px_buy = ( json_buy_bid + json_buy_ask )/ 2
  px_buy = px_buy # .round 2
  px = px_sell - px_buy
  px = ( px * 20 ).round.to_f / 20 # down to nearest 0.05
  
  json_puts! json_sell, 'json_sell'
  json_puts! json_buy, 'json_buy'
  puts! px, '^00 - px'

=begin
  update( status: :rolling_up, 
    new_call_sell_strike: new_call_sell_strike, 
    new_call_buy_strike: new_call_buy_strike )
=end

  rollup_tmpl =<<~AOL
    <?xml version="1.0" encoding="UTF-8"?>
    <FIXML xmlns="http://www.fixprotocol.org/FIXML-5-0-SP2">
      <NewOrdMleg
        OrdTyp="2"
        Px="#{px}"
        Acct="#{ALLY_CREDS[:]}" 
        TmInForce="0"
      >
        <Ord OrdQty="#{n_contracts}" PosEfct="C" >
          <Leg 
            AcctTyp="5" 
            Side="1" 
            Strk="#{call_sell_strike}" 
            Mat="#{expires_on.strftime('%Y-%m-%d')}T00:00:00.000‐05:00"
            MMY="#{expires_on.strftime('%Y%m')}"
            SecTyp="OPT"
            CFI="OC"
            Sym="#{ticker}" />
        </Ord>
        <Ord OrdQty="#{n_contracts}" PosEfct="C" >
          <Leg 
            Side="2" 
            Strk="#{call_buy_strike}" 
            Mat="#{expires_on.strftime('%Y-%m-%d')}T00:00:00.000‐05:00" 
            MMY="#{expires_on.strftime('%Y%m')}" 
            SecTyp="OPT" 
            CFI="OC" 
            Sym="#{ticker}" />
        </Ord>
        <Ord OrdQty="#{n_contracts}" PosEfct="O" >
          <Leg 
            Side="2" 
            Strk="#{new_call_sell_strike}" 
            Mat="#{expires_on.strftime('%Y-%m-%d')}T00:00:00.000‐05:00" 
            MMY="#{expires_on.strftime('%Y%m')}" 
            SecTyp="OPT" 
            CFI="OC" 
            Sym="#{ticker}" />
        </Ord>
        <Ord OrdQty="#{n_contracts}" PosEfct="O" >
          <Leg 
            Side="1" 
            Strk="#{new_call_buy_strike}" 
            Mat="#{expires_on.strftime('%Y-%m-%d')}T00:00:00.000‐05:00" 
            MMY="#{expires_on.strftime('%Y%m')}" 
            SecTyp="OPT" 
            CFI="OC" 
            Sym="#{ticker}" />
        </Ord>
      </NewOrdMleg>
    </FIXML>
  AOL
end

#upper_panic_thresholdObject


76
77
78
# File 'lib/ish/iron_condor.rb', line 76

def upper_panic_threshold
  result = call_sell_strike * ( 1 - panic_percentage )
end