lib/cotcube-bardata/daily.rb in cotcube-bardata-0.1.6 vs lib/cotcube-bardata/daily.rb in cotcube-bardata-0.1.7

- old
+ new

@@ -2,19 +2,35 @@ module Cotcube # Missing top level documentation comment module Bardata # just reads bardata/daily/<id>/<contract>.csv - def provide_daily(contract:, + def provide_daily(contract:, # rubocop:disable Metrics/ParameterLists symbol: nil, id: nil, + range: nil, timezone: Time.find_zone('America/Chicago'), config: init) contract = contract.to_s.upcase unless contract.is_a?(String) && [3, 5].include?(contract.size) raise ArgumentError, "Contract '#{contract}' is bogus, should be like 'M21' or 'ESM21'" end + unless range.nil? || + (range.is_a?(Range) && + [Date, DateTime, ActiveSupport::TimeWithZone].map do |cl| + (range.begin.nil? || range.begin.is_a?(cl)) && + (range.end.nil? || range.end.is_a?(cl)) + end.reduce(:|)) + raise ArgumentError, 'Range, if given, must be either (Integer..Integer) or (Timelike..Timelike)' + end + + unless range.nil? + range_begin = range.begin.nil? ? nil : timezone.parse(range.begin.to_s) + range_end = range.end.nil? ? nil : timezone.parse(range.end.to_s) + range = (range_begin..range_end) + end + sym = get_id_set(symbol: symbol, id: id, contract: contract) contract = contract[2..4] if contract.to_s.size == 5 id = sym[:id] id_path = "#{config[:data_path]}/daily/#{id}" data_file = "#{id_path}/#{contract}.csv" @@ -31,11 +47,18 @@ row[:datetime] = timezone.parse(row[:date]) row[:type] = :daily row end data.pop if data.last[:high].zero? - data + if range.nil? + data + else + data.select do |x| + (range.begin.nil? ? true : x[:datetime] >= range.begin) and + (range.end.nil? ? true : x[:datetime] <= range.end) + end + end end # reads all files in bardata/daily/<id> and aggregates by date # (what is a pre-stage of a continuous based on daily bars) def continuous(symbol: nil, id: nil, config: init, date: nil) @@ -117,13 +140,13 @@ puts "#{k }\t#{v.first[:date] }\t#{v.last[:date] }\t#{format('%4d', (Date.parse(v.last[:date]) - Date.parse(v.first[:date]))) }\t#{result[k].map do |x| - x[:volume].select do - x[:contract] == k - end - end.size + x[:volume].select do + x[:contract] == k + end + end.size }" # rubocop:enable Layout/ClosingParenthesisIndentation end end result