Sha256: 441eb82fb760d56315b49834b8b26eb77eb1da491779ec6ead11cb9a11566353

Contents?: true

Size: 1.35 KB

Versions: 23

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

module EodServices
  class Contract
    class <<self
      # rubocop:disable Metrics/LineLength
      CONTRACT_REGEX = /^(?<symbol>[A-Z]+)(?<expiry>[0-9]{6})(?<option_type>[CP]{1})[0-9]{8}$/.freeze
      # rubocop:enable Metrics/LineLength

      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

      def underlying_symbol(contract_symbol)
        match = contract_symbol.match(CONTRACT_REGEX)
        return match[:symbol] if match

        contract_symbol
      end

      def contract_expiry(contract_symbol)
        match = contract_symbol.match(CONTRACT_REGEX)
        Date.parse(match[:expiry]) if match
      end

      def contract_type(contract_symbol)
        match = contract_symbol.match(CONTRACT_REGEX)
        return match[:option_type] if match

        EodModels::OptionType::STOCK
      end

      private

      def padded_strike(strike)
        (strike * 1000).to_i.to_s.rjust(8, '0')
      end

      def expiration(expiry)
        expiry.strftime('%y%m%d')
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
sensei_eod_utils-0.0.53 lib/eod_services/contract.rb
sensei_eod_utils-0.0.52 lib/eod_services/contract.rb
sensei_eod_utils-0.0.51 lib/eod_services/contract.rb
sensei_eod_utils-0.0.50 lib/eod_services/contract.rb
sensei_eod_utils-0.0.49 lib/eod_services/contract.rb
sensei_eod_utils-0.0.48 lib/eod_services/contract.rb
sensei_eod_utils-0.0.47 lib/eod_services/contract.rb
sensei_eod_utils-0.0.46 lib/eod_services/contract.rb
sensei_eod_utils-0.0.45 lib/eod_services/contract.rb
sensei_eod_utils-0.0.44 lib/eod_services/contract.rb
sensei_eod_utils-0.0.43 lib/eod_services/contract.rb
sensei_eod_utils-0.0.42 lib/eod_services/contract.rb
sensei_eod_utils-0.0.41 lib/eod_services/contract.rb
sensei_eod_utils-0.0.40 lib/eod_services/contract.rb
sensei_eod_utils-0.0.39 lib/eod_services/contract.rb
sensei_eod_utils-0.0.38 lib/eod_services/contract.rb
sensei_eod_utils-0.0.37 lib/eod_services/contract.rb
sensei_eod_utils-0.0.36 lib/eod_services/contract.rb
sensei_eod_utils-0.0.35 lib/eod_services/contract.rb
sensei_eod_utils-0.0.34 lib/eod_services/contract.rb