Class: Demos::StockDecider::Option
- Inherits:
-
Object
- Object
- Demos::StockDecider::Option
- Defined in:
- ui/examples/stock_decider/option.rb
Constant Summary collapse
- URL =
"https://cdn.cboe.com/api/global/delayed_quotes/options"
Class Method Summary collapse
Instance Method Summary collapse
- #attractive?(weeks) ⇒ Boolean
- #call? ⇒ Boolean
- #code ⇒ Object
- #cycles(weeks) ⇒ Object
- #datespan ⇒ Object
- #expiration ⇒ Object
-
#initialize(payload, current) ⇒ Option
constructor
A new instance of Option.
- #name ⇒ Object
- #option ⇒ Object
- #percent(weeks) ⇒ Object
- #put? ⇒ Boolean
- #strike ⇒ Object
- #type ⇒ Object
Constructor Details
#initialize(payload, current) ⇒ Option
Returns a new instance of Option.
16 17 18 19 |
# File 'ui/examples/stock_decider/option.rb', line 16 def initialize(payload, current) @current_price = current @payload = payload end |
Class Method Details
.from_ticker(sym) ⇒ Object
8 9 10 11 12 13 14 |
# File 'ui/examples/stock_decider/option.rb', line 8 def self.from_ticker(sym) res = RestClient.get("#{URL}/#{sym.upcase}.json").body data = JSON.parse(res, symbolize_names: true)[:data] data[:options].map do |option| new(option, data[:current_price]) end end |
Instance Method Details
#attractive?(weeks) ⇒ Boolean
33 34 35 |
# File 'ui/examples/stock_decider/option.rb', line 33 def attractive?(weeks) datespan > weeks - 1 && datespan < weeks + 1 end |
#call? ⇒ Boolean
49 50 51 |
# File 'ui/examples/stock_decider/option.rb', line 49 def call? option.scan(/([CP])\d+$/)[0][0] == "C" end |
#code ⇒ Object
66 67 68 |
# File 'ui/examples/stock_decider/option.rb', line 66 def code option.scan(/^[A-Za-z]+/)[0] end |
#cycles(weeks) ⇒ Object
25 26 27 |
# File 'ui/examples/stock_decider/option.rb', line 25 def cycles(weeks) 52 / weeks end |
#datespan ⇒ Object
37 38 39 |
# File 'ui/examples/stock_decider/option.rb', line 37 def datespan (expiration - DateTime.now).to_i / 7 end |
#expiration ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'ui/examples/stock_decider/option.rb', line 53 def expiration value = option.scan(/^[A-Za-z]+(\d+)/)[0][0] year = "20#{value[0..1]}".to_i month = value[2..3].to_i day = value[4..5].to_i DateTime.new(year, month, day) end |
#name ⇒ Object
29 30 31 |
# File 'ui/examples/stock_decider/option.rb', line 29 def name option end |
#option ⇒ Object
21 22 23 |
# File 'ui/examples/stock_decider/option.rb', line 21 def option @payload[:option] end |
#percent(weeks) ⇒ Object
41 42 43 |
# File 'ui/examples/stock_decider/option.rb', line 41 def percent(weeks) (@current_price * cycles(weeks)) / strike end |
#put? ⇒ Boolean
70 71 72 |
# File 'ui/examples/stock_decider/option.rb', line 70 def put? !call? end |
#strike ⇒ Object
62 63 64 |
# File 'ui/examples/stock_decider/option.rb', line 62 def strike option.scan(/\d+$/)[0].to_i / 1000 end |
#type ⇒ Object
45 46 47 |
# File 'ui/examples/stock_decider/option.rb', line 45 def type call? ? "Call" : "Put" end |