Class: Iro::Option

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

Constant Summary collapse

CALL =
'CALL'
PUT =
'PUT'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#recomputeObject

Returns the value of attribute recompute.



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

def recompute
  @recompute
end

Class Method Details

.expirations_list(full: false, n: 5) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/models/iro/option.rb', line 27

def self.expirations_list full: false, n: 5
  out = [[nil,nil]]
  day = Date.today - 5.days
  n.times do
    next_exp = day.next_occurring(:thursday).next_occurring(:friday)
    if !next_exp.workday?
      next_exp = Time.previous_business_day( next_exp )
    end

    out.push([ next_exp.strftime('%b %e'), next_exp.strftime('%Y-%m-%d') ])
    day = next_exp
  end
  return out
  # [
  #   [ nil, nil ],
  #   [ 'Mar 22', '2024-03-22'.to_date ],
  #   [ 'Mar 28', '2024-03-28'.to_date ],
  #   [ 'Apr 5',  '2024-04-05'.to_date ],
  #   [ 'Mar 12', '2024-03-12'.to_date ],
  #   [ 'Mar 19', '2024-03-19'.to_date ],
  # ]
end

.symbol_to_h(symbol) ⇒ Object

symbol = “META 260424P00500000”



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/models/iro/option.rb', line 72

def self.symbol_to_h symbol
  ticker = symbol[0,6].strip
  date_str = symbol[6,6]
  type = symbol[12] == 'P' ? 'PUT' : 'CALL'
  strike_str = symbol[13,8]
  expires_on = Date.strptime(date_str, "%y%m%d")
  strike = strike_str.to_i / 1000.0
  return {
    ticker: ticker,
    strike: strike,
    put_call: type,
    expires_on: expires_on,
  }
end

Instance Method Details

#matches_h(h) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'app/models/iro/option.rb', line 87

def matches_h h
  if h[:put_call] == put_call &&
     h[:strike] == strike &&
     h[:expires_on] == expires_on
    return true
  else
    return false
  end
end

#symbolObject

for schwab, eg: “COST 260306C01030000”



63
64
65
66
67
# File 'app/models/iro/option.rb', line 63

def symbol
  p_c_ = put_call == 'PUT' ? 'P' : 'C'
  strike_ = format("%08d", (strike.to_f * 1000).round)
  sym = "#{stock.ticker.ljust(6)}#{expires_on.strftime("%y%m%d")}#{p_c_}#{strike_}"
end

#syncObject

before_save :sync, if: ->() { !Rails.env.test? } ## do not sync in test



98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/models/iro/option.rb', line 98

def sync
  out = Tda::Option.get_quote({
    contractType: put_call,
    strike: strike,
    expirationDate: expires_on.strftime('%Y-%m-%d'),
    ticker: ticker,
  })
  puts! out, "option sync of `#{self.to_s}`"
  self.end_price = ( out.bid + out.ask ) / 2 rescue 0
  self.end_delta = out.delta ? out.delta : 0.0
  self.save! ## 2026-02-19 this must be present.
end

#tickerObject



12
# File 'app/models/iro/option.rb', line 12

def ticker; stock.ticker; end

#to_sObject



111
112
113
# File 'app/models/iro/option.rb', line 111

def to_s
  "#{symbol} :: #{expires_on.strftime('%Y-%m-%d')} #{put_call} #{strike}"
end