Class: Iro::Position

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

Constant Summary collapse

STATUS_ACTIVE =
'active'
STATUS_CLOSED =
'closed'
STATUS_PROPOSED =
'proposed'
STATUS_PENDING =

one more, ‘selected’ after proposed?

'pending'
STATUSES =

‘working’

[ nil, STATUS_CLOSED, STATUS_ACTIVE, STATUS_PROPOSED, STATUS_PENDING ]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#next_gain_loss_amountObject

Returns the value of attribute next_gain_loss_amount.



9
10
11
# File 'app/models/iro/position.rb', line 9

def next_gain_loss_amount
  @next_gain_loss_amount
end

Class Method Details

.longObject

ok



289
290
291
# File 'app/models/iro/position.rb', line 289

def self.long
  where( long_or_short: Iro::Strategy::LONG )
end

.shortObject

ok



294
295
296
# File 'app/models/iro/position.rb', line 294

def self.short
  where( long_or_short: Iro::Strategy::SHORT )
end

Instance Method Details

#begin_deltaObject



78
79
80
# File 'app/models/iro/position.rb', line 78

def begin_delta
  strategy.send("begin_delta_#{strategy.kind}", self)
end

#breakevenObject



85
86
87
# File 'app/models/iro/position.rb', line 85

def breakeven
  strategy.send("breakeven_#{strategy.kind}", self)
end

#calc_nxtObject



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
229
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
# File 'app/models/iro/position.rb', line 152

def calc_nxt
  pos = self

  ## 7 days ahead - not configurable so far
  outs = Tda::Option.get_quotes({
    contractType: pos.put_call,
    expirationDate: next_expires_on,
    ticker: ticker,
  })
  outs_bk = outs.dup

  outs = outs.select do |out|
    out[:bidSize] + out[:askSize] > 0
  end

  if 'CALL' == pos.put_call
    ;
  elsif 'PUT' == pos.put_call
    outs = outs.reverse
  end
  puts! outs, '#calc_nxt.outs -> 2'

  ## next_inner_strike
  outs = outs.select do |out|
    if Iro::Strategy::CREDIT == pos.credit_or_debit
      if Iro::Strategy::SHORT == pos.long_or_short
        ## short credit call
        out[:strikePrice] >= strategy.next_inner_strike
      elsif Iro::Strategy::LONG == pos.long_or_short
        ## long credit put
        out[:strikePrice] <= strategy.next_inner_strike
      end
    else
      raise 'zt3 - @TODO: implement, debit spreads'
    end
  end
  puts! outs[0][:strikePrice], 'after calc next_inner_strike'
  puts! outs, 'outs'

  ## next_buffer_above_water
  outs = outs.select do |out|
    if Iro::Strategy::SHORT == pos.long_or_short
      out[:strikePrice] > strategy.next_buffer_above_water + strategy.stock.last
    elsif Iro::Strategy::LONG == pos.long_or_short
      out[:strikePrice] < strategy.stock.last - strategy.next_buffer_above_water
    else
      raise 'zt4 - this cannot happen'
    end
  end
  puts! outs[0][:strikePrice], 'after calc next_buffer_above_water'
  puts! outs, 'outs'

  ## next_inner_delta
  outs = outs.select do |out|
    if 'CALL' == pos.put_call
      out_delta  = out[:delta] rescue 1
      out_delta <= strategy.next_inner_delta
    elsif 'PUT' == pos.put_call
      out_delta  = out[:delta] rescue 0
      out_delta <= strategy.next_inner_delta
    else
      raise 'zt5 - this cannot happen'
    end
  end
  puts! outs[0][:strikePrice], 'after calc next_inner_delta'
  puts! outs, 'outs'

  inner = outs[0]
  outs = outs.select do |out|
    if 'CALL' == pos.put_call
      out[:strikePrice] >= inner[:strikePrice].to_f + strategy.next_spread_amount
    elsif 'PUT' == pos.put_call
      out[:strikePrice] <= inner[:strikePrice].to_f - strategy.next_spread_amount
    end
  end
  outer = outs[0]

  if inner && outer
    o_attrs = {
      expires_on: next_expires_on,
      put_call:   pos.put_call,
      stock_id:   pos.stock_id,
    }
    inner_ = Iro::Option.new(o_attrs.merge({
      strike:        inner[:strikePrice],
      begin_price: ( inner[:bid] + inner[:ask] )/2,
      begin_delta:   inner[:delta],
      end_price:   ( inner[:bid] + inner[:ask] )/2,
      end_delta:     inner[:delta],
    }))
    outer_ = Iro::Option.new(o_attrs.merge({
      strike:        outer[:strikePrice],
      begin_price: ( outer[:bid] + outer[:ask] )/2,
      begin_delta:   outer[:delta],
      end_price:   ( outer[:bid] + outer[:ask] )/2,
      end_delta:     outer[:delta],
    }))
    pos.autonxt ||= Iro::Position.new
    pos.autonxt.update({
      prev_gain_loss_amount: 'a',
      put_call:     pos.put_call,
      status:      'proposed',
      stock:        strategy.stock,
      inner:        inner_,
      outer:        outer_,
      inner_strike: inner_.strike,
      outer_strike: outer_.strike,
      begin_on:     Time.now.to_date,
      expires_on:   next_expires_on,
      purse:        purse,
      strategy:     strategy,
      quantity:     1,
      autoprev:     pos,
    })

    pos.autonxt.sync
    pos.autonxt.save!
    pos.save
    return pos

  else
    throw 'zmq - should not happen'
  end
end

#calc_rollpObject

should_roll?



140
141
142
143
144
145
146
147
148
149
150
# File 'app/models/iro/position.rb', line 140

def calc_rollp
  self.next_reasons = []
  # self.next_symbol  = nil
  # self.next_delta   = nil

  out = strategy.send( "calc_rollp_#{strategy.kind}", self )

  self.rollp = out[0]
  self.next_reasons.push out[1]
  save
end

#current_underlying_strikeObject



89
90
91
# File 'app/models/iro/position.rb', line 89

def current_underlying_strike
  Iro::Stock.find_by( ticker: ticker ).last
end

#end_deltaObject



81
82
83
# File 'app/models/iro/position.rb', line 81

def end_delta
  strategy.send("end_delta_#{strategy.kind}", self)
end

#innerObject

Options



53
# File 'app/models/iro/position.rb', line 53

belongs_to :inner, class_name: 'Iro::Option', inverse_of: :inner

#max_gainObject

each



118
119
120
# File 'app/models/iro/position.rb', line 118

def max_gain # each
  strategy.send("max_gain_#{strategy.kind}", self)
end

#max_lossObject

each



121
122
123
# File 'app/models/iro/position.rb', line 121

def max_loss # each
  strategy.send("max_loss_#{strategy.kind}", self)
end

#net_amountObject

each



110
111
112
# File 'app/models/iro/position.rb', line 110

def net_amount # each
  self.send("net_amount_#{strategy.kind}")
end

#net_amount_long_credit_put_spreadObject

2025-10-14 tested



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

def net_amount_long_credit_put_spread ## each
  inner.begin_price - outer.begin_price + outer.end_price - inner.end_price
end

#net_percentObject



107
108
109
# File 'app/models/iro/position.rb', line 107

def net_percent
  net_amount / max_gain
end

#next_expires_onObject

ok



280
281
282
283
284
285
286
# File 'app/models/iro/position.rb', line 280

def next_expires_on
  out = expires_on.to_datetime.next_occurring(:monday).next_occurring(:friday)
  if !out.workday?
    out = Time.previous_business_day(out)
  end
  return out
end

#next_reasonsObject

decisions



136
# File 'app/models/iro/position.rb', line 136

field :next_reasons, type: :array, default: []

#nxtsObject

there are many of these, for viewing on the ‘roll’ view



48
# File 'app/models/iro/position.rb', line 48

has_many :nxts,     class_name: 'Iro::Position', inverse_of: :prev

#prev_gain_loss_amountObject



10
11
12
13
# File 'app/models/iro/position.rb', line 10

def prev_gain_loss_amount
  out  = autoprev.outer.end_price - autoprev.inner.end_price
  out += inner.begin_price - outer.begin_price
end

#put_callObject

no: the strategy can be wheel, and position is put-spread. delegate :put_call, to: :strategy



36
# File 'app/models/iro/position.rb', line 36

field :put_call, type: :string

#qObject



72
# File 'app/models/iro/position.rb', line 72

def q; quantity; end

#refreshObject



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/models/iro/position.rb', line 93

def refresh
  out = Tda::Option.get_quote({
    contractType:   'CALL',
    strike:         strike,
    expirationDate: expires_on,
    ticker:         ticker,
  })
  update({
    end_delta: out[:delta],
    end_price: out[:last],
  })
  print '^'
end

#syncObject



126
127
128
129
# File 'app/models/iro/position.rb', line 126

def sync
  inner.sync
  outer.sync
end

#to_sObject



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'app/models/iro/position.rb', line 298

def to_s
  out = "#{stock} (#{q}) #{expires_on.to_datetime.strftime('%b %d')} #{strategy.long_or_short} ["
  if Iro::Strategy::LONG == long_or_short
    if outer.strike
      out = out + "$#{outer.strike}->"
    end
    out = out + "$#{inner.strike}"
  else
    out = out + "$#{inner.strike}"
    if outer.strike
      out = out + "<-$#{outer.strike}"
    end
  end
  out += "] "
  return out
end