Sha256: c4ab5485918ff803c0eb9f31dd7c7a7c31fc6212a2afb4a524da370f07d716d6

Contents?: true

Size: 1.17 KB

Versions: 20

Compression:

Stored size: 1.17 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

        underlying + expiration(expiry) + type + padded_strike(strike)
      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

20 entries across 20 versions & 1 rubygems

Version Path
sensei_eod_utils-0.0.30 lib/eod_services/contract.rb
sensei_eod_utils-0.0.29 lib/eod_services/contract.rb
sensei_eod_utils-0.0.28 lib/eod_services/contract.rb
sensei_eod_utils-0.0.27 lib/eod_services/contract.rb
sensei_eod_utils-0.0.26 lib/eod_services/contract.rb
sensei_eod_utils-0.0.25 lib/eod_services/contract.rb
sensei_eod_utils-0.0.24 lib/eod_services/contract.rb
sensei_eod_utils-0.0.23 lib/eod_services/contract.rb
sensei_eod_utils-0.0.22 lib/eod_services/contract.rb
sensei_eod_utils-0.0.21 lib/eod_services/contract.rb
sensei_eod_utils-0.0.20 lib/eod_services/contract.rb
sensei_eod_utils-0.0.19 lib/eod_services/contract.rb
sensei_eod_utils-0.0.17 lib/eod_services/contract.rb
sensei_eod_utils-0.0.16 lib/eod_services/contract.rb
sensei_eod_utils-0.0.15 lib/eod_services/contract.rb
sensei_eod_utils-0.0.14 lib/eod_services/contract.rb
sensei_eod_utils-0.0.13 lib/eod_services/contract.rb
sensei_eod_utils-0.0.12 lib/eod_services/contract.rb
sensei_eod_utils-0.0.11 lib/eod_services/contract.rb
sensei_eod_utils-0.0.10 lib/eod_services/contract.rb