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 =

has_and_belongs_to_many :purses, class_name: ‘Iro::Purse’, inverse_of: :strategies

'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_SPREAD =

these are too simple and deprecated:

'spread'
KIND_WHEEL =

@deprecated, be specific

'wheel'
KINDS =

@deprecated, be specific

[ 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_SPREAD,
  KIND_WHEEL,
]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.for_ticker(ticker) ⇒ Object

scopes



309
310
311
# File 'app/models/iro/strategy.rb', line 309

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

.list(long_or_short = nil) ⇒ Object



320
321
322
323
# File 'app/models/iro/strategy.rb', line 320

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

Instance Method Details

#begin_delta_long_credit_put_spread(p) ⇒ Object



100
101
102
# File 'app/models/iro/strategy.rb', line 100

def begin_delta_long_credit_put_spread p
  begin_delta_spread p
end

#begin_delta_spread(p) ⇒ Object



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

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

#begin_delta_wheel(p) ⇒ Object



94
95
96
# File 'app/models/iro/strategy.rb', line 94

def begin_delta_wheel p
  p.inner.begin_delta
end

#breakeven_covered_call(p) ⇒ Object



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

def breakeven_covered_call p
  p.inner.strike + p.inner.begin_price
end

#breakeven_long_debit_call_spread(p) ⇒ Object Also known as: breakeven_short_debit_put_spread



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

def breakeven_long_debit_call_spread p
  p.inner.strike - p.max_gain
end

#calc_rollp_covered_call(p) ⇒ Object

decisions



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'app/models/iro/strategy.rb', line 201

def calc_rollp_covered_call p

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

  if ( stock.last - buffer_above_water ) < p.inner.strike
    return [ 0.98, "Last #{'%.2f' % stock.last} is " +
        "#{'%.2f' % [p.inner.strike + buffer_above_water - stock.last]} " +
        "below #{'%.2f' % [p.inner.strike + buffer_above_water]} 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 _TODO



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

def calc_rollp_long_credit_put_spread p
  # 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 - buffer_above_water ) < p.inner.strike
    return [ 0.95, "Last #{'%.2f' % stock.last} is " +
        "#{'%.2f' % [stock.last - p.inner.strike - buffer_above_water]} " +
        "below #{'%.2f' % [p.inner.strike + buffer_above_water]} 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_long_debit_call_spread(p) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'app/models/iro/strategy.rb', line 225

def calc_rollp_long_debit_call_spread p

  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 - buffer_above_water ) < p.inner.strike
    return [ 0.95, "Last #{'%.2f' % stock.last} is " +
        "#{'%.2f' % [stock.last - p.inner.strike - buffer_above_water]} " +
        "below #{'%.2f' % [p.inner.strike + buffer_above_water]} 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_debit_put_spread(p) ⇒ Object



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'app/models/iro/strategy.rb', line 283

def calc_rollp_short_debit_put_spread p

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

  if stock.last + buffer_above_water > p.inner.strike
    return [ 0.98, "Last #{'%.2f' % stock.last} is " +
        "#{'%.2f' % [stock.last + buffer_above_water - p.inner.strike]} " +
        "above #{'%.2f' % [p.inner.strike - buffer_above_water]} 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_long_credit_put_spread(p) ⇒ Object



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

def end_delta_long_credit_put_spread p
  end_delta_spread p
end

#end_delta_spread(p) ⇒ Object



117
118
119
# File 'app/models/iro/strategy.rb', line 117

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

#end_delta_wheel(p) ⇒ Object



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

def end_delta_wheel p
  p.inner.end_delta
end

#max_gain_covered_call(p) ⇒ Object



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

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

#max_gain_long_credit_put_spread(p) ⇒ Object



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

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



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

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



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

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



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

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



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

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



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

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

#max_loss_covered_call(p) ⇒ Object



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

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

#max_loss_long_credit_put_spread(p) ⇒ Object



155
156
157
# File 'app/models/iro/strategy.rb', line 155

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

#max_loss_long_debit_call_spread(p) ⇒ Object



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

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

#max_loss_short_credit_call_spread(p) ⇒ Object



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

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



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

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

#max_loss_spread(p) ⇒ Object



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

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

#max_loss_wheel(p) ⇒ Object



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

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

#net_amount_long_credit_put_spread(p) ⇒ Object



179
180
181
# File 'app/models/iro/strategy.rb', line 179

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

#net_amount_spread(p) ⇒ Object



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

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

#next_inner_strike_on(expires_on) ⇒ Object

2024-05-09 @TODO 2025-10-11 _TODO



186
187
188
189
190
191
192
193
# File 'app/models/iro/strategy.rb', line 186

def next_inner_strike_on expires_on
  outs = ::Tda::Option.get_quotes({
    contractType: put_call,
    expirationDate: expires_on,
    ticker: stock.ticker,
  })
  puts! outs, 'next_inner_strike_on -> outs'
end

#next_positionObject

_TODO: makes no sense…



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

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
78
# 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
    # put_call = 'zq9-ERROR'
    throw 'zq9 - this should never happen'
  end
end

#slugObject



314
315
316
# File 'app/models/iro/strategy.rb', line 314

def slug
  "#{long_or_short} #{credit_or_debit} #{kind} #{stock}"
end

#to_sObject



317
318
319
# File 'app/models/iro/strategy.rb', line 317

def to_s
  slug
end