Class: Tradier::Symbol
- Inherits:
-
Object
- Object
- Tradier::Symbol
- 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
-
#adjustment ⇒ Object
Returns the value of attribute adjustment.
-
#cents ⇒ Object
Returns the value of attribute cents.
-
#day ⇒ Object
Returns the value of attribute day.
-
#dollars ⇒ Object
Returns the value of attribute dollars.
-
#month ⇒ Object
Returns the value of attribute month.
-
#option_type ⇒ Object
Returns the value of attribute option_type.
-
#symbol ⇒ Object
Returns the value of attribute symbol.
-
#year ⇒ Object
Returns the value of attribute year.
Class Method Summary collapse
Instance Method Summary collapse
- #adjustment? ⇒ Boolean
- #call? ⇒ Boolean
- #description ⇒ Object
- #equity? ⇒ Boolean
- #expiration ⇒ Object
- #expiration_description ⇒ Object
-
#initialize(symbol) ⇒ Symbol
constructor
A new instance of Symbol.
- #occ_cents ⇒ Object
- #occ_day ⇒ Object
- #occ_dollars ⇒ Object
- #occ_month ⇒ Object
- #option? ⇒ Boolean
- #option_type_description ⇒ Object
- #put? ⇒ Boolean
- #strike_description ⇒ Object
- #strike_price ⇒ Object
- #to_s ⇒ Object
- #underlier ⇒ Object
- #valid? ⇒ Boolean
- #year_description ⇒ Object
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
#adjustment ⇒ Object
Returns the value of attribute adjustment.
21 22 23 |
# File 'lib/tradier/symbol.rb', line 21 def adjustment @adjustment end |
#cents ⇒ Object
Returns the value of attribute cents.
21 22 23 |
# File 'lib/tradier/symbol.rb', line 21 def cents @cents end |
#day ⇒ Object
Returns the value of attribute day.
21 22 23 |
# File 'lib/tradier/symbol.rb', line 21 def day @day end |
#dollars ⇒ Object
Returns the value of attribute dollars.
21 22 23 |
# File 'lib/tradier/symbol.rb', line 21 def dollars @dollars end |
#month ⇒ Object
Returns the value of attribute month.
21 22 23 |
# File 'lib/tradier/symbol.rb', line 21 def month @month end |
#option_type ⇒ Object
Returns the value of attribute option_type.
21 22 23 |
# File 'lib/tradier/symbol.rb', line 21 def option_type @option_type end |
#symbol ⇒ Object
Returns the value of attribute symbol.
21 22 23 |
# File 'lib/tradier/symbol.rb', line 21 def symbol @symbol end |
#year ⇒ Object
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
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
55 56 57 |
# File 'lib/tradier/symbol.rb', line 55 def adjustment? adjustment && adjustment.to_i >= 1 end |
#call? ⇒ Boolean
64 65 66 67 |
# File 'lib/tradier/symbol.rb', line 64 def call? return unless option_type option_type.upcase == 'C' end |
#description ⇒ Object
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
73 74 75 |
# File 'lib/tradier/symbol.rb', line 73 def equity? !option? end |
#expiration ⇒ Object
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_description ⇒ Object
116 117 118 |
# File 'lib/tradier/symbol.rb', line 116 def expiration_description "#{MONTHS[@month.to_i]} #{@day} #{year_description}" end |
#occ_cents ⇒ Object
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_day ⇒ Object
98 99 100 |
# File 'lib/tradier/symbol.rb', line 98 def occ_day @day.to_i >= 10 ? @day : "0#{@day.to_i}" end |
#occ_dollars ⇒ Object
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_month ⇒ Object
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
69 70 71 |
# File 'lib/tradier/symbol.rb', line 69 def option? put? || call? end |
#option_type_description ⇒ Object
128 129 130 |
# File 'lib/tradier/symbol.rb', line 128 def option_type_description put? ? 'PUT' : 'CALL' end |
#put? ⇒ Boolean
59 60 61 62 |
# File 'lib/tradier/symbol.rb', line 59 def put? return unless option_type option_type.upcase == 'P' end |
#strike_description ⇒ Object
124 125 126 |
# File 'lib/tradier/symbol.rb', line 124 def strike_description "#{@dollars.to_i}.#{occ_cents[0,2]}" end |
#strike_price ⇒ Object
136 137 138 |
# File 'lib/tradier/symbol.rb', line 136 def strike_price "#{@dollars}.#{@cents}".to_f end |
#to_s ⇒ Object
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 |
#underlier ⇒ Object
90 91 92 |
# File 'lib/tradier/symbol.rb', line 90 def underlier @symbol end |
#valid? ⇒ Boolean
77 78 79 |
# File 'lib/tradier/symbol.rb', line 77 def valid? equity? || (option? && valid_month?) end |
#year_description ⇒ Object
120 121 122 |
# File 'lib/tradier/symbol.rb', line 120 def year_description "20#{@year}" end |