Class: Option
- Inherits:
-
Object
- Object
- Option
- Defined in:
- lib/options-lib/option.rb
Constant Summary collapse
- CALL =
'CALL'- PUT =
'PUT'
Instance Attribute Summary collapse
-
#exp ⇒ Object
readonly
Returns the value of attribute exp.
-
#internal_symbol ⇒ Object
readonly
Returns the value of attribute internal_symbol.
-
#stock ⇒ Object
readonly
Returns the value of attribute stock.
-
#strike ⇒ Object
readonly
Returns the value of attribute strike.
-
#symbol ⇒ Object
readonly
Returns the value of attribute symbol.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#days_to_exp ⇒ Object
Business days (= minus Sat and Sun) until expiration.
-
#initialize(type, stock, strike, exp, symbol = nil) ⇒ Option
constructor
A new instance of Option.
- #inspect ⇒ Object
- #is_call? ⇒ Boolean
- #is_put? ⇒ Boolean
- #to_s ⇒ Object
Constructor Details
#initialize(type, stock, strike, exp, symbol = nil) ⇒ Option
Returns a new instance of Option.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/options-lib/option.rb', line 11 def initialize(type, stock, strike, exp, symbol = nil) raise "Invalid type: #{type}" if type != CALL and type != PUT @type, @stock, @strike = type, stock, strike.to_f if exp =~ /(\d{4})[\-\/](\d{1,2})[\-\/](\d{1,2})/ @exp = Date.new($1.to_i, $2.to_i, $3.to_i) else raise "Cannot parse expiration date: #{exp}" end s = "#{stock}_#{type == CALL ? 'C' : 'P'}#{@strike.prettify}_#{@exp.day}" s << Date::MONTHNAMES[@exp.month][0,3].upcase s << @exp.year.to_s @internal_symbol = s if not symbol @symbol = @internal_symbol else @symbol = symbol end end |
Instance Attribute Details
#exp ⇒ Object (readonly)
Returns the value of attribute exp.
9 10 11 |
# File 'lib/options-lib/option.rb', line 9 def exp @exp end |
#internal_symbol ⇒ Object (readonly)
Returns the value of attribute internal_symbol.
9 10 11 |
# File 'lib/options-lib/option.rb', line 9 def internal_symbol @internal_symbol end |
#stock ⇒ Object (readonly)
Returns the value of attribute stock.
9 10 11 |
# File 'lib/options-lib/option.rb', line 9 def stock @stock end |
#strike ⇒ Object (readonly)
Returns the value of attribute strike.
9 10 11 |
# File 'lib/options-lib/option.rb', line 9 def strike @strike end |
#symbol ⇒ Object (readonly)
Returns the value of attribute symbol.
9 10 11 |
# File 'lib/options-lib/option.rb', line 9 def symbol @symbol end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
9 10 11 |
# File 'lib/options-lib/option.rb', line 9 def type @type end |
Instance Method Details
#days_to_exp ⇒ Object
Business days (= minus Sat and Sun) until expiration
43 44 45 46 47 48 49 50 51 |
# File 'lib/options-lib/option.rb', line 43 def days_to_exp total = 0 curr_day = Date.today while curr_day <= exp total += 1 if curr_day.wday != 0 and curr_day.wday != 6 curr_day = curr_day.next end total end |
#inspect ⇒ Object
57 58 59 |
# File 'lib/options-lib/option.rb', line 57 def inspect @internal_symbol end |
#is_call? ⇒ Boolean
34 35 36 |
# File 'lib/options-lib/option.rb', line 34 def is_call? @type == CALL end |
#is_put? ⇒ Boolean
38 39 40 |
# File 'lib/options-lib/option.rb', line 38 def is_put? @type == PUT end |
#to_s ⇒ Object
53 54 55 |
# File 'lib/options-lib/option.rb', line 53 def to_s @internal_symbol end |