Class: SchwabMCP::Tools::OptionChainTool

Inherits:
MCP::Tool
  • Object
show all
Extended by:
Loggable
Defined in:
lib/schwab_mcp/tools/option_chain_tool.rb

Defined Under Namespace

Classes: FilteredOptionChain

Class Method Summary collapse

Methods included from Loggable

log_debug, log_error, log_fatal, log_info, log_warn, logger

Class Method Details

.call(symbol:, server_context:, contract_type: nil, strike_count: nil, include_underlying_quote: nil, strategy: nil, strike_range: nil, option_type: nil, exp_month: nil, interval: nil, strike: nil, from_date: nil, to_date: nil, volatility: nil, underlying_price: nil, interest_rate: nil, days_to_expiration: nil, entitlement: nil, max_delta: nil, min_delta: nil, max_strike: nil, min_strike: nil, expiration_date: nil) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/schwab_mcp/tools/option_chain_tool.rb', line 129

def self.call(
  symbol:, server_context:, contract_type: nil, strike_count: nil,
  include_underlying_quote: nil,
  strategy: nil, strike_range: nil,
  option_type: nil, exp_month: nil,
  interval: nil, strike: nil, from_date: nil, to_date: nil, volatility: nil,
  underlying_price: nil, interest_rate: nil, days_to_expiration: nil,
  entitlement: nil, max_delta: nil, min_delta: nil, max_strike: nil,
  min_strike: nil, expiration_date: nil
)
  log_info("Getting option chain for symbol: #{symbol}")

  begin
    client = SchwabClientFactory.create_client
    return SchwabClientFactory.client_error_response unless client

    params = {}
    params[:contract_type] = contract_type if contract_type
    params[:strike_count] = strike_count if strike_count
    params[:include_underlying_quote] = include_underlying_quote unless include_underlying_quote.nil?
    params[:strategy] = strategy if strategy
    params[:interval] = interval if interval
    params[:strike] = strike if strike
    params[:strike_range] = strike_range if strike_range

    if expiration_date
      exp_date = Date.parse(expiration_date)
      params[:from_date] = exp_date
      params[:to_date] = exp_date
    else
      params[:from_date] = Date.parse(from_date) if from_date
      params[:to_date] = Date.parse(to_date) if to_date
    end

    params[:volatility] = volatility if volatility
    params[:underlying_price] = underlying_price if underlying_price
    params[:interest_rate] = interest_rate if interest_rate
    params[:days_to_expiration] = days_to_expiration if days_to_expiration
    params[:exp_month] = exp_month if exp_month
    params[:option_type] = option_type if option_type
    params[:entitlement] = entitlement if entitlement

    log_debug("Making API request for option chain with params: #{params}")
    option_chain = client.get_option_chain(symbol.upcase, **params)

    if option_chain
      log_info("Successfully retrieved option chain for #{symbol}")

      if max_delta || min_delta || max_strike || min_strike
        begin
          log_debug("Applying option chain filtering")

          filter = SchwabMCP::OptionChainFilter.new(
            expiration_date: Date.parse(expiration_date),
            max_delta: max_delta || 1.0,
            min_delta: min_delta || 0.0,
            max_strike: max_strike,
            min_strike: min_strike
          )

          filtered_calls = filter.select(option_chain.call_opts)
          filtered_puts = filter.select(option_chain.put_opts)

          log_debug("Filtered #{filtered_calls.size} call options")
          log_debug("Filtered #{filtered_puts.size} put options")

          filtered_option_chain = create_filtered_option_chain(option_chain, filtered_calls, filtered_puts)

          MCP::Tool::Response.new([{
                                    type: "text",
                                    text: format_option_chain_response(filtered_option_chain)
                                  }])
        rescue StandardError => e
          log_error("Error applying option chain filter: #{e.message}")
        end
      else
        log_debug("No filtering applied, returning full response")
        MCP::Tool::Response.new([{
                                  type: "text",
                                  text: format_option_chain_response(option_chain)
                                }])
      end
    else
      log_warn("Empty response from Schwab API for option chain: #{symbol}")
      MCP::Tool::Response.new([{
                                type: "text",
                                text: "**No Data**: Empty response from Schwab API for option chain: #{symbol}"
                              }])
    end
  rescue StandardError => e
    log_error("Error retrieving option chain for #{symbol}: #{e.message}")
    log_debug("Backtrace: #{e.backtrace.first(3).join('\n')}")
    error_text = "**Error** retrieving option chain for #{symbol}: #{e.message}\n\n"
    error_text += e.backtrace.first(3).join('\n')
    MCP::Tool::Response.new([{
                              type: "text",
                              text: error_text
                            }])
  end
end