class EodServices::Contract

Constants

CONTRACT_REGEX

rubocop:disable Metrics/LineLength

Public Class Methods

contract_expiry(contract_symbol) click to toggle source
# File lib/eod_services/contract.rb, line 29
def contract_expiry(contract_symbol)
  match = contract_symbol.match(CONTRACT_REGEX)
  Date.parse(match[:expiry]) if match
end
contract_symbol(underlying:, expiry:, type:, strike:) click to toggle source

rubocop:enable Metrics/LineLength

# File lib/eod_services/contract.rb, line 10
def contract_symbol(underlying:, expiry:, type:, strike:)
  return underlying if type == EodModels::OptionType::STOCK

  contract = underlying + expiration(expiry) + type + padded_strike(strike)

  unless EodServices::InstrumentType.option?(contract)
    raise ArgumentError, "Contract #{contract} is not a valid options contract"
  end

  contract
end
contract_type(contract_symbol) click to toggle source
# File lib/eod_services/contract.rb, line 34
def contract_type(contract_symbol)
  match = contract_symbol.match(CONTRACT_REGEX)
  return match[:option_type] if match

  EodModels::OptionType::STOCK
end
underlying_symbol(contract_symbol) click to toggle source
# File lib/eod_services/contract.rb, line 22
def underlying_symbol(contract_symbol)
  match = contract_symbol.match(CONTRACT_REGEX)
  return match[:symbol] if match

  contract_symbol
end

Private Class Methods

expiration(expiry) click to toggle source
# File lib/eod_services/contract.rb, line 47
def expiration(expiry)
  expiry.strftime('%y%m%d')
end
padded_strike(strike) click to toggle source
# File lib/eod_services/contract.rb, line 43
def padded_strike(strike)
  (strike * 1000).to_i.to_s.rjust(8, '0')
end