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_PREPARE =
'prepare'
STATUS_PROPOSED =
'proposed'
STATUS_PENDING =

one more, 'selected' after proposed?

'pending'
STATUSES =

'working'

[ nil, STATUS_CLOSED, STATUS_ACTIVE, STATUS_PREPARE, STATUS_PROPOSED, STATUS_PENDING ]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.longObject

ok



477
478
479
# File 'app/models/iro/position.rb', line 477

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

.shortObject

ok



482
483
484
# File 'app/models/iro/position.rb', line 482

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

.sync_allObject



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

def self.sync_all
  @positions = Iro::Position.where( :status.in => [ 'active', 'pending' ] )
  expiration_dates = @positions.map { |p| p.expires_on.to_s }.sort
  # puts! expiration_dates, 'expiration_dates'

  count = 1
  @positions.each do |pos|
    # puts! pos.id.to_s, '#sync_all.pos'

    quotes_h = Tda::Option.get_quotes_h({
      contractType: 'ALL',
      ticker:  pos.ticker,
      fromDate: expiration_dates.first,
      toDate: expiration_dates.last,
    })

    pos.inner.end_price = quotes_h[pos.expires_on.to_date.to_s][pos.put_call][pos.inner.strike][:price]
    pos.inner.end_delta = quotes_h[pos.expires_on.to_date.to_s][pos.put_call][pos.inner.strike][:delta]
    pos.inner.save ? print("#{count}^") : print("#{count}X")

    # if [ Iro::Strategy::KIND_LONG_CREDIT_PUT_SPREAD,
    #      Iro::Strategy::KIND_SHORT_CREDIT_CALL_SPREAD,
    #      Iro::Strategy::KIND_DIAG_LONG_CALL_SPREAD,
    #      Iro::Strategy::KIND_DIAG_SHORT_PUT_SPREAD ].include?( pos.strategy.kind )
    if pos.outer
      pos.outer.end_price = quotes_h[pos.expires_on.to_date.to_s][pos.put_call][pos.outer.strike][:price]
      pos.outer.end_delta = quotes_h[pos.expires_on.to_date.to_s][pos.put_call][pos.outer.strike][:delta]
      pos.outer.save ? print('^') : print('X')
    end
    count = count+1
  end

  print 'synced-all.'
end

Instance Method Details

#autonxtObject

2026-02-26 using this one.



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

belongs_to :autonxt,  class_name: 'Iro::Position', inverse_of: :autoprev, optional: true

#begin_deltaObject



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

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

#breakevenObject



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

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

#breakeven_covered_callObject



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

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

#breakeven_diag_long_call_spreadObject



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

def breakeven_diag_long_call_spread
  p = self
  realized_gl + p.outer.strike + p.outer.begin_price - p.inner.begin_price
end

#breakeven_diag_short_put_spreadObject



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

def breakeven_diag_short_put_spread
  p = self
  p.inner.strike + p.max_gain + p.realized_gl
end

#breakeven_long_credit_put_spreadObject

2026-02-23



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

def breakeven_long_credit_put_spread
  p = self
  p.inner.strike - p.max_gain
end

#breakeven_long_debit_call_spreadObject



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

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

#breakeven_short_credit_call_spreadObject

2026-02-23



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

def breakeven_short_credit_call_spread
  p = self
  p.inner.strike + p.max_gain
end

#breakeven_short_debit_put_spreadObject



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

def breakeven_short_debit_put_spread
  p = self
  p.inner.strike - p.inner.begin_price + p.outer.begin_price
end

#calc_nxtObject



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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
# File 'app/models/iro/position.rb', line 327

def calc_nxt
  pos = self
  # puts! pos, '#calc_nxt...'

  ## 7 days ahead - not configurable
  params = {
    contractType: pos.put_call,
    expirationDate: next_expires_on,
    ticker: ticker,
  }
  # puts! params, 'ze params'
  outs = Tda::Option.get_quotes(params)
  # puts! outs, 'outs'
  outs_bk = outs.dup

  ## cleanup mid-increments
  outs = outs.select do |out|
    ( out[:strikePrice] - pos.inner.strike ) % strategy.stock.options_price_increment == 0
  end

  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
  if strategy.next_inner_strike.present?
    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'
  end

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

  ## next_inner_delta
  outs = outs.select do |out|
    out_delta  = out[:delta].abs rescue 0
    out_delta <= strategy.next_inner_delta
  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_attrs = 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_attrs = 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],
    })
    autonxt_attrs = {
      put_call:     pos.put_call,
      status:      'proposed',
      stock:        strategy.stock,
      inner_strike: inner_attrs[:strike],
      inner_attributes: inner_attrs,
      outer_strike: outer_attrs[:strike],
      outer_attributes: outer_attrs,
      begin_on:     Time.now.to_date,
      expires_on:   next_expires_on,
      purse:        purse,
      strategy:     strategy,
      quantity:     pos.quantity,
      autoprev:     pos,
    }
    pos.autonxt ||= Iro::Position.where({
      inner_strike: inner_attrs[:strike],
      outer_strike: outer_attrs[:strike],
      purse:        purse,
      stock:        strategy.stock,
      strategy:     strategy,
    }).first
    pos.autonxt ||= Iro::Position.new(autonxt_attrs)
    pos.autonxt.update(autonxt_attrs)
    pos.autonxt.inner.update(inner_attrs)
    pos.autonxt.outer.update(outer_attrs)

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

  else
    throw 'zmq - should not happen'
  end
end

#calc_rollpObject

should_roll?



316
317
318
319
320
321
322
323
324
325
# File 'app/models/iro/position.rb', line 316

def calc_rollp
  pos = self
  pos.next_reasons = []

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

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

#close_priceObject

credit spread only



186
187
188
189
190
# File 'app/models/iro/position.rb', line 186

def close_price
  pos = self
  out = pos.outer.end_price - pos.inner.end_price
  return out.round(2)
end

#current_underlying_strikeObject



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

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

#diag_weeksObject



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

def diag_weeks
  pos = self
  ((pos.outer.expires_on - pos.inner.expires_on)/7).to_i
end

#end_deltaObject



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

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

#innerObject

Options



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

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

#inner_strikeObject

2026-02-24 only to make finding easier.



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

validates :inner_strike, presence: true

#innersObject

for history and diagonals



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

has_many :inners, class_name: 'Iro::Option', inverse_of: :poss_of_inner

#max_gainObject

each



233
234
235
# File 'app/models/iro/position.rb', line 233

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

#max_lossObject

each



236
237
238
# File 'app/models/iro/position.rb', line 236

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

#net_amountObject

each



209
210
211
# File 'app/models/iro/position.rb', line 209

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

#net_amount_covered_callObject



212
213
214
# File 'app/models/iro/position.rb', line 212

def net_amount_covered_call
  inner.begin_price - inner.end_price
end

#net_amount_diag_long_call_spreadObject



226
227
228
# File 'app/models/iro/position.rb', line 226

def net_amount_diag_long_call_spread
  inner.begin_price - outer.begin_price + outer.end_price - inner.end_price + realized_gl
end

#net_amount_diag_short_put_spreadObject



229
230
231
# File 'app/models/iro/position.rb', line 229

def net_amount_diag_short_put_spread
  net_amount_diag_long_call_spread
end

#net_amount_long_credit_put_spreadObject

2025-10-14 tested



216
217
218
# File 'app/models/iro/position.rb', line 216

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

#net_amount_short_credit_call_spreadObject

2026-02-19 tested



220
221
222
# File 'app/models/iro/position.rb', line 220

def net_amount_short_credit_call_spread
  return net_amount_long_credit_put_spread
end

#net_amount_short_debit_put_spreadObject



223
224
225
# File 'app/models/iro/position.rb', line 223

def net_amount_short_debit_put_spread
  inner.end_price - inner.begin_price + outer.begin_price - outer.end_price
end

#net_percentObject



206
207
208
# File 'app/models/iro/position.rb', line 206

def net_percent
  net_amount / max_gain
end

#next_expires_onObject

ok



468
469
470
471
472
473
474
# File 'app/models/iro/position.rb', line 468

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.strftime('%Y-%m-%d')
end

#next_reasonsObject

decisions



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

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

#open_priceObject

credit-spread



193
194
195
196
197
# File 'app/models/iro/position.rb', line 193

def open_price
  pos = self
  out = pos.inner.begin_price - pos.outer.begin_price
  return out.round(2)
end

#outer_strikeObject

2026-02-24 only to make finding easier.



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

validates :outer_strike, presence: true

#outersObject

for history and diagonals



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

has_many :outers, class_name: 'Iro::Option', inverse_of: :poss_of_outer

#prevObject

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



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

belongs_to :prev,     class_name: 'Iro::Position', inverse_of: :nxts,    optional: true

#put_callObject



62
63
64
# File 'app/models/iro/position.rb', line 62

def put_call
  self[:put_call] || self.strategy.put_call
end

#qObject



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

def q; quantity; end

#realized_gain_loss_amountObject

for diagonals only



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

field :realized_gain_loss_amount, type: :float, default: 0.0

#realized_glObject



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

def realized_gl; realized_gain_loss_amount; end

#refreshObject



168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'app/models/iro/position.rb', line 168

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

#roll_priceObject



199
200
201
202
203
# File 'app/models/iro/position.rb', line 199

def roll_price
  pos = self
  out = pos.autoprev.outer.end_price - pos.autoprev.inner.end_price + pos.inner.begin_price - pos.outer.begin_price
  return out.round(2)
end

#schwab_queryObject



258
259
260
261
262
263
264
265
266
267
268
269
# File 'app/models/iro/position.rb', line 258

def schwab_query
  pos = self
  case pos.intent
  when Iro::Strategy::INTENT_OPEN
    the_q = Tda::Order.credit_spread_q pos
  when Iro::Strategy::INTENT_ROLL
    the_q = Tda::Order.roll_credit_call_spread_q pos
  else
    throw "prp - #schwab_query undefined for position #{pos.inspect}"
  end
  return the_q
end

#status_label(st) ⇒ Object



24
25
26
27
28
# File 'app/models/iro/position.rb', line 24

def status_label st
  labels = {}
  labels[STATUS_PROPOSED] = 'Selected.'
  return labels[st] || st
end

#syncObject



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'app/models/iro/position.rb', line 241

def sync
  if schwab_order_id
    outs = Tda::Order.check_status schwab_order_id
    update({ schwab_status: outs['status'] })
    if [ Tda::Order::STATUS_FILLED, Tda::Order::STATUS_REPLACED ].include?( outs['status'] )
      ## update amounts.
      purse.update({ available_amount: purse.available_amount + next_gain_loss_amount*quantity*100 })
      ## make this one active
      update({ status: Iro::Position::STATUS_ACTIVE, next_gain_loss_amount: nil })
      ## make previous one closed
      autoprev.update({ status: Iro::Position::STATUS_CLOSED })
    end
  end
  inner.sync
  outer.sync
end

#tickerObject



43
44
45
46
47
48
49
# File 'app/models/iro/position.rb', line 43

def ticker
  if !self[:ticker]
    self[:ticker] = stock.ticker
    self.save
  end
  self[:ticker]
end

#to_sObject



486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
# File 'app/models/iro/position.rb', line 486

def to_s
  out = "#{stock} (#{q}) #{expires_on.to_datetime.strftime('%b %d')} #{strategy.long_or_short} ["
  if Iro::Strategy::KIND_SHORT_DEBIT_PUT_SPREAD == strategy.kind
    out = out + "$#{outer.strike} << $#{inner.strike}"
  elsif 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

#trim_expires_onObject



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

def trim_expires_on
  self.expires_on = expires_on.to_s[0, 10] if expires_on.present?
end