Class: Iro::Strategy

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, Mongoid::Paranoia, Mongoid::Timestamps
Defined in:
app/models/iro/strategy.rb

Constant Summary collapse

LONG =
'long'
SHORT =
'short'
CREDIT =
'credit'
DEBIT =
'debit'
KIND_COVERED_CALL =
'covered_call'
KIND_IRON_CONDOR =
'iron_condor'
KIND_LONG_CREDIT_PUT_SPREAD =
'long_credit_put_spread'
KIND_LONG_DEBIT_CALL_SPREAD =
'long_debit_call_spread'
KIND_SHORT_CREDIT_CALL_SPREAD =
'short_credit_call_spread'
KIND_SHORT_DEBIT_PUT_SPREAD =
'short_debit_put_spread'
KIND_UNINVESTABLE =
'uninvestable'
KIND_LONG_ONLY =
'long-only'
KIND_SPREAD =

these are too simple and deprecated:

'spread'
KIND_WHEEL =

@deprecated, be specific

'wheel'
KINDS =

@deprecated, use covered_call

[ nil,
  KIND_COVERED_CALL,
  KIND_IRON_CONDOR,
  KIND_LONG_CREDIT_PUT_SPREAD,
  KIND_LONG_DEBIT_CALL_SPREAD,
  KIND_SHORT_CREDIT_CALL_SPREAD,
  KIND_SHORT_DEBIT_PUT_SPREAD,
  KIND_UNINVESTABLE,
  KIND_LONG_ONLY,
]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.for_ticker(ticker) ⇒ Object

scopes



362
363
364
# File 'app/models/iro/strategy.rb', line 362

def self.for_ticker ticker
  where( ticker: ticker )
end

.list(long_or_short = nil) ⇒ Object



370
371
372
373
# File 'app/models/iro/strategy.rb', line 370

def self.list long_or_short = nil
  these = long_or_short ? where( long_or_short: long_or_short ) : all
  [[nil,nil]] + these.map { |st| [ st, st.id ] }
end

Instance Method Details

#_begin_delta_spread(p) ⇒ Object

def begin_delta_wheel p

p.inner.begin_delta

end



103
104
105
# File 'app/models/iro/strategy.rb', line 103

def _begin_delta_spread p
  p.inner.begin_delta - p.outer.begin_delta
end

#_end_delta_spread(p) ⇒ Object

def end_delta_wheel p

p.inner.end_delta

end



119
120
121
# File 'app/models/iro/strategy.rb', line 119

def _end_delta_spread p
  p.inner.end_delta - p.outer.end_delta
end

#begin_delta_covered_call(p) ⇒ Object



97
98
99
# File 'app/models/iro/strategy.rb', line 97

def begin_delta_covered_call p
  p.inner.begin_delta
end

#begin_delta_long_credit_put_spread(p) ⇒ Object



106
107
108
# File 'app/models/iro/strategy.rb', line 106

def begin_delta_long_credit_put_spread p
  _begin_delta_spread p
end

#begin_delta_short_credit_call_spread(p) ⇒ Object



109
110
111
# File 'app/models/iro/strategy.rb', line 109

def begin_delta_short_credit_call_spread p
  _begin_delta_spread p
end

#calc_rollp_covered_call(p) ⇒ Object

do not use!



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'app/models/iro/strategy.rb', line 209

def calc_rollp_covered_call p
  stock.reload

  if ( p.expires_on.to_date - Time.now.to_date ).to_i < 1
    return [ 0.99, '0 DTE, must exit' ]
  end

  if ( stock.last - threshold_usd_above_mark ) < p.inner.strike
    return [ 0.98, "Last #{'%.2f' % stock.last} is " +
      "#{'%.2f' % [p.inner.strike + threshold_usd_above_mark - stock.last]} " +
      "below #{'%.2f' % [p.inner.strike + threshold_usd_above_mark]} water" ]
  end

  if p.inner.end_delta < threshold_pos_delta
    return [ 0.61, "Delta #{p.inner.end_delta} is lower than #{threshold_pos_delta} threshold." ]
  end

  if 1 - p.inner.end_price/p.inner.begin_price > threshold_netp
    return [ 0.51, "made enough #{'%.02f' % [(1.0 - p.inner.end_price/p.inner.begin_price )*100]}% profit." ]
  end

  return [ 0.33, '-' ]
end

#calc_rollp_long_credit_put_spread(p) ⇒ Object

2025-10-12 continue 2026-04-05 continue



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
# File 'app/models/iro/strategy.rb', line 263

def calc_rollp_long_credit_put_spread p
  stock.reload

  # puts! p, '#calc_rollp_long_credit_put_spread'
  # puts! p.inner, 'p.inner'
  # puts! stock, 'stock'
  # puts! attributes, 'strategy attributes'

  if ( p.expires_on.to_date - Time.now.to_date ).to_i < 1
    return [ 0.99, '0 DTE, must exit' ]
  end
  if ( p.expires_on.to_date - Time.now.to_date ).to_i < 2
    return [ 0.99, '1 DTE, must exit' ]
  end

  if ( stock.last - threshold_usd_above_mark ) < p.inner.strike
    return [ 0.95, ":threshold_usd_above_mark <br />Last $#{'%.2f' % stock.last} is " +
      "#{'%.2f' % [stock.last - p.inner.strike]} near #{p.inner.strike} but should be greater than #{threshold_usd_above_mark} ." ]
  end

  if p.inner.end_delta.abs < threshold_pos_delta
    return [ 0.79, ":threshold_pos_delta <br />Offensive roll: delta #{p.inner.end_delta} is lower than #{threshold_pos_delta} ." ]
  end
  if p.inner.end_delta.abs > threshold_neg_delta
    return [ 0.79, ":threshold_neg_delta <br />Defensive roll: delta #{p.inner.end_delta} is higher than #{threshold_neg_delta} ." ]
  end

  if threshold_netp.present?
    if 1 - p.inner.end_price/p.inner.begin_price > threshold_netp
      return [ 0.51, ":threshold_netp <br />made enough #{'%.02f' % [(1.0 - p.inner.end_price/p.inner.begin_price )*100]}% profit^" ]
    end
  end

  return [ 0.33, '-' ]
end

#calc_rollp_long_debit_call_spread(p) ⇒ Object

do not use!



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
# File 'app/models/iro/strategy.rb', line 234

def calc_rollp_long_debit_call_spread p
  stock.reload

  if ( p.expires_on.to_date - Time.now.to_date ).to_i < 1
    return [ 0.99, '0 DTE, must exit' ]
  end
  if ( p.expires_on.to_date - Time.now.to_date ).to_i < 2
    return [ 0.99, '1 DTE, must exit' ]
  end

  if ( stock.last - threshold_usd_above_mark ) < p.inner.strike
    return [ 0.95, "Last #{'%.2f' % stock.last} is " +
        "#{'%.2f' % [stock.last - p.inner.strike - threshold_usd_above_mark]} " +
        "below #{'%.2f' % [p.inner.strike + threshold_usd_above_mark]} water" ]
  end

  if p.inner.end_delta < threshold_pos_delta
    return [ 0.79, "Delta #{p.inner.end_delta} is lower than #{threshold_pos_delta} threshold." ]
  end

  if 1 - p.inner.end_price/p.inner.begin_price > threshold_netp
    return [ 0.51, "made enough #{'%.02f' % [(1.0 - p.inner.end_price/p.inner.begin_price )*100]}% profit^" ]
  end

  return [ 0.33, '-' ]
end

#calc_rollp_short_credit_call_spread(p) ⇒ Object

2026-02-21 ok 2026-04-05 ok



326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'app/models/iro/strategy.rb', line 326

def calc_rollp_short_credit_call_spread p
  puts! p, 'calc_rollp_short_credit_call_spread...'
  stock.reload

  if ( p.expires_on.to_date - Time.now.to_date ).to_i <= threshold_dte
    return [ 0.99, "< #{threshold_dte}DTE, must exit" ]
  end

  if stock.last + threshold_usd_above_mark > p.inner.strike
    return [ 0.95, ":threshold_usd_above_mark <br />Last $#{'%.2f' % stock.last} is " +
        "#{'%.2f' % [p.inner.strike - stock.last]} near #{p.inner.strike} but should be greater than #{threshold_usd_above_mark} ." ]
  end

  ## defensive
  ## inner short call is negative delta, but I'll deal with absolutes anyway.
  if p.inner.end_delta.abs > threshold_neg_delta.abs
    return [ 0.88, "Delta #{p.inner.end_delta} is larger than #{threshold_neg_delta} defensive threshold." ]
  end

  ## offensive
  if p.inner.end_delta.abs < threshold_pos_delta.abs
    return [ 0.69, "Delta #{p.inner.end_delta} is lower than #{threshold_pos_delta} offensive threshold." ]
  end

  if threshold_netp.present?
    if p.net_percent > threshold_netp
      return [ 0.51, "made enough #{'%.0f' % [p.net_percent*100]}% > #{"%.2f" % [threshold_netp*100]}% profit," ]
    end
  end

  return [ 0.33, '-' ]
end

#calc_rollp_short_debit_put_spread(p) ⇒ Object

do not use!



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'app/models/iro/strategy.rb', line 300

def calc_rollp_short_debit_put_spread p
  stock.reload

  if ( p.expires_on.to_date - Time.now.to_date ).to_i <= threshold_dte
    return [ 0.99, "< #{threshold_dte}DTE, must exit" ]
  end

  if stock.last + threshold_usd_above_mark > p.inner.strike
    return [ 0.98, "Last #{'%.2f' % stock.last} is " +
        "#{'%.2f' % [stock.last + threshold_usd_above_mark - p.inner.strike]} " +
        "above #{'%.2f' % [p.inner.strike - threshold_usd_above_mark]} water" ]
  end

  if p.inner.end_delta.abs < threshold_pos_delta.abs
    return [ 0.79, "Delta #{p.inner.end_delta} is lower than #{threshold_pos_delta} threshold." ]
  end

  if p.net_percent > threshold_netp
    return [ 0.51, "made enough #{'%.0f' % [p.net_percent*100]}% > #{"%.2f" % [threshold_netp*100]}% profit," ]
  end

  return [ 0.33, '-' ]
end

#end_delta_covered_call(p) ⇒ Object



113
114
115
# File 'app/models/iro/strategy.rb', line 113

def end_delta_covered_call p
  p.inner.end_delta
end

#end_delta_long_credit_put_spread(p) ⇒ Object



122
123
124
# File 'app/models/iro/strategy.rb', line 122

def end_delta_long_credit_put_spread p
  _end_delta_spread p
end

#end_delta_short_credit_call_spread(p) ⇒ Object



125
126
127
# File 'app/models/iro/strategy.rb', line 125

def end_delta_short_credit_call_spread p
  _end_delta_spread p
end

#max_gain_covered_call(p) ⇒ Object

each



130
131
132
# File 'app/models/iro/strategy.rb', line 130

def max_gain_covered_call p ## each
  p.inner.begin_price # - 0.66
end

#max_gain_long_credit_put_spread(p) ⇒ Object



133
134
135
136
# File 'app/models/iro/strategy.rb', line 133

def max_gain_long_credit_put_spread p
  ## 100 * disallowed for gameui
  p.inner.begin_price - p.outer.begin_price
end

#max_gain_long_debit_call_spread(p) ⇒ Object



137
138
139
140
# File 'app/models/iro/strategy.rb', line 137

def max_gain_long_debit_call_spread p
  ## 100 * disallowed for gameui
  ( p.inner.strike - p.outer.strike - p.outer.begin_price + p.inner.begin_price ) # - 2*0.66
end

#max_gain_short_credit_call_spread(p) ⇒ Object



141
142
143
# File 'app/models/iro/strategy.rb', line 141

def max_gain_short_credit_call_spread p
  p.inner.begin_price - p.outer.begin_price
end

#max_gain_short_debit_put_spread(p) ⇒ Object



144
145
146
147
# File 'app/models/iro/strategy.rb', line 144

def max_gain_short_debit_put_spread p
  ## 100 * disallowed for gameui
  ( p.outer.strike - p.inner.strike - p.outer.begin_price + p.inner.begin_price ) # - 2*0.66
end

#max_gain_spread(p) ⇒ Object



148
149
150
151
# File 'app/models/iro/strategy.rb', line 148

def max_gain_spread p
  ## 100 * disallowed for gameui
  ( p.outer.strike - p.inner.strike ).abs - p.outer.begin_price + p.inner.begin_price # - 2*0.66
end

#max_gain_wheel(p) ⇒ Object



152
153
154
# File 'app/models/iro/strategy.rb', line 152

def max_gain_wheel p
  p.inner.begin_price * 100 - 0.66 # @TODO: is this *100 really?
end

#max_loss_covered_call(p) ⇒ Object



157
158
159
# File 'app/models/iro/strategy.rb', line 157

def max_loss_covered_call p
  p.inner.begin_price*10 # just suppose 10,000%
end

#max_loss_long_credit_put_spread(p) ⇒ Object



160
161
162
# File 'app/models/iro/strategy.rb', line 160

def max_loss_long_credit_put_spread p
  out = p.inner.strike - p.outer.strike
end

#max_loss_long_debit_call_spread(p) ⇒ Object



163
164
165
# File 'app/models/iro/strategy.rb', line 163

def max_loss_long_debit_call_spread p
  out = p.outer.strike - p.inner.strike
end

#max_loss_short_credit_call_spread(p) ⇒ Object



169
170
171
# File 'app/models/iro/strategy.rb', line 169

def max_loss_short_credit_call_spread p
  out = p.outer.strike - p.inner.strike
end

#max_loss_short_debit_put_spread(p) ⇒ Object

different



166
167
168
# File 'app/models/iro/strategy.rb', line 166

def max_loss_short_debit_put_spread p # different
  out = p.inner.strike - p.outer.strike
end

#max_loss_spread(p) ⇒ Object



172
173
174
# File 'app/models/iro/strategy.rb', line 172

def max_loss_spread p
  ( p.outer.strike - p.inner.strike ).abs
end

#max_loss_wheel(p) ⇒ Object



175
176
177
# File 'app/models/iro/strategy.rb', line 175

def max_loss_wheel p
  p.inner.begin_price*10 # just suppose 10,000%
end

#net_amount_spread(p) ⇒ Object



181
182
183
# File 'app/models/iro/strategy.rb', line 181

def net_amount_spread p
  p.inner.begin_price - p.inner.end_price
end

#next_inner_strikeObject

can be nil, poor design?



89
# File 'app/models/iro/strategy.rb', line 89

field :next_inner_strike,       type: :float

#next_positionObject

_TODO: makes no sense…



22
# File 'app/models/iro/strategy.rb', line 22

has_one  :next_position, class_name: 'Iro::Position', inverse_of: :next_strategy

#put_callObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/models/iro/strategy.rb', line 50

def put_call
  case kind
  when Iro::Strategy::KIND_LONG_CREDIT_PUT_SPREAD
    put_call = 'PUT'
  when Iro::Strategy::KIND_LONG_DEBIT_CALL_SPREAD
    put_call = 'CALL'
  when Iro::Strategy::KIND_SHORT_CREDIT_CALL_SPREAD
    put_call = 'CALL'
  when Iro::Strategy::KIND_SHORT_DEBIT_PUT_SPREAD
    put_call = 'PUT'
  when Iro::Strategy::KIND_COVERED_CALL
    put_call = 'CALL'
  when Iro::Strategy::KIND_SPREAD
    if credit_or_debit == CREDIT
      if long_or_short == LONG
        'PUT'
      elsif long_or_short == SHORT
        'CALL'
      else
        throw 'zq5 - should never happen'
      end
    else
      throw 'zq6 - debit spreads are not implemented'
    end
  else
    throw 'zq9 - this should never happen'
  end
end

#to_sObject



367
368
369
# File 'app/models/iro/strategy.rb', line 367

def to_s
  "#{kind} #{stock} #{descr}"
end