Class: SchwabRb::DataObjects::OptionChain

Inherits:
Object
  • Object
show all
Defined in:
lib/schwab_rb/data_objects/option_chain.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(symbol:, status:, strategy:, interval:, is_delayed:, is_index:, interest_rate:, underlying_price:, volatility:, days_to_expiration:, asset_main_type:, asset_sub_type:, is_chain_truncated:, call_dates: [], call_opts: [], put_dates: [], put_opts: []) ⇒ OptionChain

Returns a new instance of OptionChain.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/schwab_rb/data_objects/option_chain.rb', line 59

def initialize(
  symbol:, status:, strategy:, interval:, is_delayed:, is_index:, interest_rate:, underlying_price:, volatility:, days_to_expiration:, asset_main_type:, asset_sub_type:, is_chain_truncated:, call_dates: [], call_opts: [], put_dates: [], put_opts: []
)
  @symbol = symbol
  @status = status
  @strategy = strategy
  @interval = interval
  @is_delayed = is_delayed
  @is_index = is_index
  @interest_rate = interest_rate
  @underlying_price = underlying_price
  @volatility = volatility
  @days_to_expiration = days_to_expiration
  @asset_main_type = asset_main_type
  @asset_sub_type = asset_sub_type
  @is_chain_truncated = is_chain_truncated
  @call_dates = call_dates
  @call_opts = call_opts
  @put_dates = put_dates
  @put_opts = put_opts
end

Instance Attribute Details

#asset_main_typeObject (readonly)

Returns the value of attribute asset_main_type.



81
82
83
# File 'lib/schwab_rb/data_objects/option_chain.rb', line 81

def asset_main_type
  @asset_main_type
end

#asset_sub_typeObject (readonly)

Returns the value of attribute asset_sub_type.



81
82
83
# File 'lib/schwab_rb/data_objects/option_chain.rb', line 81

def asset_sub_type
  @asset_sub_type
end

#call_datesObject (readonly)

Returns the value of attribute call_dates.



81
82
83
# File 'lib/schwab_rb/data_objects/option_chain.rb', line 81

def call_dates
  @call_dates
end

#call_optsObject (readonly)

Returns the value of attribute call_opts.



81
82
83
# File 'lib/schwab_rb/data_objects/option_chain.rb', line 81

def call_opts
  @call_opts
end

#days_to_expirationObject (readonly)

Returns the value of attribute days_to_expiration.



81
82
83
# File 'lib/schwab_rb/data_objects/option_chain.rb', line 81

def days_to_expiration
  @days_to_expiration
end

#interest_rateObject (readonly)

Returns the value of attribute interest_rate.



81
82
83
# File 'lib/schwab_rb/data_objects/option_chain.rb', line 81

def interest_rate
  @interest_rate
end

#intervalObject (readonly)

Returns the value of attribute interval.



81
82
83
# File 'lib/schwab_rb/data_objects/option_chain.rb', line 81

def interval
  @interval
end

#is_chain_truncatedObject (readonly)

Returns the value of attribute is_chain_truncated.



81
82
83
# File 'lib/schwab_rb/data_objects/option_chain.rb', line 81

def is_chain_truncated
  @is_chain_truncated
end

#is_delayedObject (readonly)

Returns the value of attribute is_delayed.



81
82
83
# File 'lib/schwab_rb/data_objects/option_chain.rb', line 81

def is_delayed
  @is_delayed
end

#is_indexObject (readonly)

Returns the value of attribute is_index.



81
82
83
# File 'lib/schwab_rb/data_objects/option_chain.rb', line 81

def is_index
  @is_index
end

#put_datesObject (readonly)

Returns the value of attribute put_dates.



81
82
83
# File 'lib/schwab_rb/data_objects/option_chain.rb', line 81

def put_dates
  @put_dates
end

#put_optsObject (readonly)

Returns the value of attribute put_opts.



81
82
83
# File 'lib/schwab_rb/data_objects/option_chain.rb', line 81

def put_opts
  @put_opts
end

#statusObject (readonly)

Returns the value of attribute status.



81
82
83
# File 'lib/schwab_rb/data_objects/option_chain.rb', line 81

def status
  @status
end

#strategyObject (readonly)

Returns the value of attribute strategy.



81
82
83
# File 'lib/schwab_rb/data_objects/option_chain.rb', line 81

def strategy
  @strategy
end

#symbolObject (readonly)

Returns the value of attribute symbol.



81
82
83
# File 'lib/schwab_rb/data_objects/option_chain.rb', line 81

def symbol
  @symbol
end

#underlying_priceObject (readonly)

Returns the value of attribute underlying_price.



81
82
83
# File 'lib/schwab_rb/data_objects/option_chain.rb', line 81

def underlying_price
  @underlying_price
end

#volatilityObject (readonly)

Returns the value of attribute volatility.



81
82
83
# File 'lib/schwab_rb/data_objects/option_chain.rb', line 81

def volatility
  @volatility
end

Class Method Details

.build(data) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
54
55
56
# File 'lib/schwab_rb/data_objects/option_chain.rb', line 11

def build(data)
  underlying_symbol = data.fetch(:symbol)

  call_dates = []
  call_opts = []
  data.fetch(:callExpDateMap).each do |exp_date, options|
    call_dates << Date.strptime(exp_date.to_s.split(":").first, "%Y-%m-%d")
    options.each_value do |opts|
      opts.each do |option_data|
        call_opts << Option.build(underlying_symbol, option_data)
      end
    end
  end

  put_dates = []
  put_opts = []
  data.fetch(:putExpDateMap).each do |exp_date, options|
    put_dates << Date.strptime(exp_date.to_s.split(":").first, "%Y-%m-%d")

    options.each_value do |opts|
      opts.each do |option_data|
        put_opts << Option.build(underlying_symbol, option_data)
      end
    end
  end

  new(
    symbol: data.fetch(:symbol),
    status: data.fetch(:status),
    strategy: data.fetch(:strategy),
    interval: data.fetch(:interval, nil),
    is_delayed: data.fetch(:isDelayed, nil),
    is_index: data.fetch(:isIndex, nil),
    interest_rate: data.fetch(:interestRate, nil),
    underlying_price: data.fetch(:underlyingPrice),
    volatility: data.fetch(:volatility, nil),
    days_to_expiration: data.fetch(:daysToExpiration),
    asset_main_type: data.fetch(:assetMainType, nil),
    asset_sub_type: data.fetch(:assetSubType, nil),
    is_chain_truncated: data.fetch(:isChainTruncated, false),
    call_dates: call_dates,
    call_opts: call_opts,
    put_dates: put_dates,
    put_opts: put_opts
  )
end

Instance Method Details

#to_a(_date = nil) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/schwab_rb/data_objects/option_chain.rb', line 84

def to_a(_date = nil)
  call_opts.map do |copt|
    [copt.expiration_date.strftime("%Y-%m-%d"), copt.put_call, copt.strike, copt.delta, copt.bid, copt.ask,
     copt.mark]
  end + put_opts.map do |popt|
    [popt.expiration_date.strftime("%Y-%m-%d"), popt.put_call, popt.strike, popt.delta, popt.bid, popt.ask,
     popt.mark]
  end
end