Class: Option

Inherits:
Object
  • Object
show all
Includes:
ArgumentProcessor
Defined in:
lib/option.rb

Direct Known Subclasses

Call, Put

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ArgumentProcessor

#process_args

Constructor Details

#initialize(args = {}) ⇒ Option

Returns a new instance of Option.

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
# File 'lib/option.rb', line 6

def initialize(args = {})
  raise ArgumentError, "An option cannot be instantiated" if instance_of? Option

  @current_date = nil

  process_args(args)
end

Instance Attribute Details

#current_dateObject

Returns the value of attribute current_date.



4
5
6
# File 'lib/option.rb', line 4

def current_date
  @current_date
end

#expiresObject

Returns the value of attribute expires.



4
5
6
# File 'lib/option.rb', line 4

def expires
  @expires
end

#priceObject

Returns the value of attribute price.



4
5
6
# File 'lib/option.rb', line 4

def price
  @price
end

#stockObject

Returns the value of attribute stock.



4
5
6
# File 'lib/option.rb', line 4

def stock
  @stock
end

#strikeObject

Returns the value of attribute strike.



4
5
6
# File 'lib/option.rb', line 4

def strike
  @strike
end

#symbolObject

Returns the value of attribute symbol.



4
5
6
# File 'lib/option.rb', line 4

def symbol
  @symbol
end

Instance Method Details

#==(other) ⇒ Object



64
65
66
67
68
# File 'lib/option.rb', line 64

def ==(other)
  other.stock == stock &&
    other.strike == strike &&
    other.expires == expires
end

#at_the_money?Boolean

A specific sub state of being out the money.

Returns:

  • (Boolean)


44
45
46
# File 'lib/option.rb', line 44

def at_the_money?
  stock.price == strike
end

#call?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/option.rb', line 27

def call?
  false
end

#days_to_expiry(opening_date = current_date) ⇒ Object



14
15
16
# File 'lib/option.rb', line 14

def days_to_expiry(opening_date = current_date)
  [0, (expires - opening_date).to_i].max
end

#expired?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/option.rb', line 18

def expired?
  expires < current_date
end

#in_the_money?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


35
36
37
# File 'lib/option.rb', line 35

def in_the_money?
  raise NotImplementedError
end

#out_the_money?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/option.rb', line 39

def out_the_money?
  !in_the_money?
end

#put?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/option.rb', line 31

def put?
  false
end

#to_sObject



48
49
50
51
# File 'lib/option.rb', line 48

def to_s
  [stock.symbol, float_if_needed(strike), 
    expires.strftime("%b %y").upcase, self.class.name.upcase].join(" ")
end

#to_ticker_sObject



53
54
55
56
57
58
59
60
61
62
# File 'lib/option.rb', line 53

def to_ticker_s
  return symbol if symbol

  call_or_put = call? ? "C" : "P"

  dollar  = (strike.to_i / 100).to_s.rjust(5, "0")
  decimal = (strike.to_i % 100).to_s.ljust(3, "0")

  [stock.symbol, expires.strftime("%y%m%d"), call_or_put, dollar, decimal].join.upcase
end