Class: Tradier::Symbol

Inherits:
Object
  • Object
show all
Defined in:
lib/tradier/symbol.rb

Constant Summary collapse

OPTION_SYMBOL_REGEXP =
/([a-z]+)(\d?)(\d{2})(\d{2})(\d{2})(C|P)(\d{5})(\d{3})/ix
MONTHS =
{
  1 => 'Jan',
  2 => 'Feb',
  3 => 'Mar',
  4 => 'Apr',
  5 => 'May',
  6 => 'Jun',
  7 => 'Jul',
  8 => 'Aug',
  9 => 'Sep',
  10 => 'Oct',
  11 => 'Nov',
  12 => 'Dec'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(symbol) ⇒ Symbol

Returns a new instance of Symbol.



23
24
25
# File 'lib/tradier/symbol.rb', line 23

def initialize(symbol)
  @symbol = symbol
end

Instance Attribute Details

#adjustmentObject

Returns the value of attribute adjustment.



21
22
23
# File 'lib/tradier/symbol.rb', line 21

def adjustment
  @adjustment
end

#centsObject

Returns the value of attribute cents.



21
22
23
# File 'lib/tradier/symbol.rb', line 21

def cents
  @cents
end

#dayObject

Returns the value of attribute day.



21
22
23
# File 'lib/tradier/symbol.rb', line 21

def day
  @day
end

#dollarsObject

Returns the value of attribute dollars.



21
22
23
# File 'lib/tradier/symbol.rb', line 21

def dollars
  @dollars
end

#monthObject

Returns the value of attribute month.



21
22
23
# File 'lib/tradier/symbol.rb', line 21

def month
  @month
end

#option_typeObject

Returns the value of attribute option_type.



21
22
23
# File 'lib/tradier/symbol.rb', line 21

def option_type
  @option_type
end

#symbolObject

Returns the value of attribute symbol.



21
22
23
# File 'lib/tradier/symbol.rb', line 21

def symbol
  @symbol
end

#yearObject

Returns the value of attribute year.



21
22
23
# File 'lib/tradier/symbol.rb', line 21

def year
  @year
end

Class Method Details

.parse(symbol) ⇒ Object

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/tradier/symbol.rb', line 27

def self.parse(symbol)
  symbol.strip!

  sym = if match = symbol.match(OPTION_SYMBOL_REGEXP)
    match = match.to_a
    match.shift

    s = Tradier::Symbol.new(match[0])
    s.adjustment  = match[1]
    s.year        = match[2]
    s.month       = match[3]
    s.day         = match[4]
    s.option_type = match[5]
    s.dollars     = match[6]
    s.cents       = match[7]
    s
  elsif symbol =~ /\d/
    nil
  elsif symbol =~ /[a-zA-Z]+/
    Tradier::Symbol.new(symbol)
  else
    nil
  end

  raise ArgumentError.new("Invalid symbol: #{symbol}") unless sym && sym.valid?
  sym
end

Instance Method Details

#adjustment?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/tradier/symbol.rb', line 55

def adjustment?
  adjustment && adjustment.to_i >= 1
end

#call?Boolean

Returns:

  • (Boolean)


64
65
66
67
# File 'lib/tradier/symbol.rb', line 64

def call?
  return unless option_type
  option_type.upcase == 'C'
end

#descriptionObject



86
87
88
# File 'lib/tradier/symbol.rb', line 86

def description
  equity? ? underlier : "#{underlier} #{expiration_description} $#{strike_description} #{option_type_description}"
end

#equity?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/tradier/symbol.rb', line 73

def equity?
  !option?
end

#expirationObject



132
133
134
# File 'lib/tradier/symbol.rb', line 132

def expiration
  Date.new(year_description.to_i, @month.to_i, @day.to_i)
end

#expiration_descriptionObject



116
117
118
# File 'lib/tradier/symbol.rb', line 116

def expiration_description
  "#{MONTHS[@month.to_i]} #{@day} #{year_description}"
end

#occ_centsObject



102
103
104
105
106
107
108
109
# File 'lib/tradier/symbol.rb', line 102

def occ_cents
  case @cents.size
  when 3 then @cents
  when 2 then "#{@cents}0"
  when 1 then "#{@cents}00"
  when 0 then "000"
  end
end

#occ_dayObject



98
99
100
# File 'lib/tradier/symbol.rb', line 98

def occ_day
  @day.to_i >= 10 ? @day : "0#{@day.to_i}"
end

#occ_dollarsObject



111
112
113
114
# File 'lib/tradier/symbol.rb', line 111

def occ_dollars
  return @dollars if @dollars.size == 5
  ('0' * (5 - @dollars.size)) + @dollars.to_s
end

#occ_monthObject



94
95
96
# File 'lib/tradier/symbol.rb', line 94

def occ_month
  @month.to_i >= 10 ? @month : "0#{@month.to_i}"
end

#option?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/tradier/symbol.rb', line 69

def option?
  put? || call?
end

#option_type_descriptionObject



128
129
130
# File 'lib/tradier/symbol.rb', line 128

def option_type_description
  put? ? 'PUT' : 'CALL'
end

#put?Boolean

Returns:

  • (Boolean)


59
60
61
62
# File 'lib/tradier/symbol.rb', line 59

def put?
  return unless option_type
  option_type.upcase == 'P'
end

#strike_descriptionObject



124
125
126
# File 'lib/tradier/symbol.rb', line 124

def strike_description
  "#{@dollars.to_i}.#{occ_cents[0,2]}"
end

#strike_priceObject



136
137
138
# File 'lib/tradier/symbol.rb', line 136

def strike_price
  "#{@dollars}.#{@cents}".to_f
end

#to_sObject



81
82
83
84
# File 'lib/tradier/symbol.rb', line 81

def to_s
  s = equity? ? underlier : "#{underlier}#{@adjustment if adjustment?}#{@year}#{occ_month}#{occ_day}#{@option_type}#{occ_dollars}#{occ_cents}"
  s.upcase
end

#underlierObject



90
91
92
# File 'lib/tradier/symbol.rb', line 90

def underlier
  @symbol
end

#valid?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/tradier/symbol.rb', line 77

def valid?
  equity? || (option? && valid_month?)
end

#year_descriptionObject



120
121
122
# File 'lib/tradier/symbol.rb', line 120

def year_description
  "20#{@year}"
end