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 =
'is_long'
SHORT =
'is_short'
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'
KINDS =
[ 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,
]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.for_ticker(ticker) ⇒ Object

scopes



251
252
253
# File 'app/models/iro/strategy.rb', line 251

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

.list(long_or_short = nil) ⇒ Object



259
260
261
262
# File 'app/models/iro/strategy.rb', line 259

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_covered_call(p) ⇒ Object



74
75
76
# File 'app/models/iro/strategy.rb', line 74

def begin_delta_covered_call p
  p.inner.begin_delta
end

#begin_delta_long_credit_put_spread(p) ⇒ Object



77
78
79
# File 'app/models/iro/strategy.rb', line 77

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

#begin_delta_long_debit_call_spread(p) ⇒ Object Also known as: begin_delta_short_debit_put_spread, begin_delta_short_credit_call_spread



80
81
82
# File 'app/models/iro/strategy.rb', line 80

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

#breakeven_covered_call(p) ⇒ Object



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

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



90
91
92
# File 'app/models/iro/strategy.rb', line 90

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

#calc_rollp_covered_call(p) ⇒ Object

decisions



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'app/models/iro/strategy.rb', line 174

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_delta
    return [ 0.61, "Delta #{p.inner.end_delta} is lower than #{threshold_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



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

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_delta
    return [ 0.79, "Delta #{p.inner.end_delta} is lower than #{threshold_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



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

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_delta.abs
    return [ 0.79, "Delta #{p.inner.end_delta} is lower than #{threshold_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



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

def end_delta_covered_call p
  p.inner.end_delta
end

#end_delta_long_credit_put_spread(p) ⇒ Object



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

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

#end_delta_long_debit_call_spread(p) ⇒ Object Also known as: end_delta_short_debit_put_spread, end_delta_short_credit_call_spread



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

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

#max_gain_covered_call(p) ⇒ Object



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

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



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

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



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

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



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

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



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

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_loss_covered_call(p) ⇒ Object



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

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

#max_loss_long_credit_put_spread(p) ⇒ Object



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

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

#max_loss_long_debit_call_spread(p) ⇒ Object



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

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

#max_loss_short_credit_call_spread(p) ⇒ Object



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

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



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

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

#net_amount_covered_call(p) ⇒ Object



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

def net_amount_covered_call p
  ( p.inner.begin_price - p.inner.end_price )
end

#net_amount_long_debit_call_spread(p) ⇒ Object Also known as: net_amount_long_credit_put_spread, net_amount_short_credit_call_spread, net_amount_short_debit_put_spread



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

def net_amount_long_debit_call_spread p
  outer = p.outer.end_price   - p.outer.begin_price
  inner = p.inner.begin_price - p.inner.end_price
  out = ( outer + inner )
end

#next_inner_strike_on(expires_on) ⇒ Object

2024-05-09 @TODO



160
161
162
163
164
165
166
# File 'app/models/iro/strategy.rb', line 160

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

#put_callObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/models/iro/strategy.rb', line 40

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'
  else
    throw 'zz9 - this should never happen'
  end
end

#to_sObject



256
257
258
# File 'app/models/iro/strategy.rb', line 256

def to_s
  slug
end