cotcube-bardata 0.1.4 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +3 -0
- data/VERSION +1 -1
- data/lib/cotcube-bardata/cached.rb +27 -28
- data/lib/cotcube-bardata/daily.rb +5 -1
- data/lib/cotcube-bardata/eods.rb +2 -1
- data/lib/cotcube-bardata/helpers.rb +18 -15
- data/lib/cotcube-bardata/provide.rb +21 -19
- data/lib/cotcube-bardata/quarters.rb +0 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c6685b99ba2fcbd933356ebefad21f83c3ec641101b8772419c030b3d197f0de
|
4
|
+
data.tar.gz: 825d415ff9c9f3a5ffe072ff1cec47e8238ce63399c9c7519e7a496392aef4f8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dff1e21c1076d0d566a088a0e8af2955abaf4d9d3c26ddfa33685f4b8fa660744dfbd3210ede01badf71bd85398cb064d292f0ec6998f7784d3db4f06bac2b88
|
7
|
+
data.tar.gz: ed811a9909dc7c08b08030c07b8c4ee811c13e1313a7481d8747f4ca4107b0597f5abde1e2eb8f78ac907e15f37aa494398144df565e354b00042b41bc00265e
|
data/CHANGELOG.md
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.5
|
@@ -3,35 +3,34 @@
|
|
3
3
|
module Cotcube
|
4
4
|
# missing top level documentation
|
5
5
|
module Bardata
|
6
|
-
|
7
6
|
# send pre-created days based on quarters
|
8
|
-
def provide_cached(contract:,
|
7
|
+
def provide_cached(contract:, # rubocop:disable Metrics/ParameterLists
|
9
8
|
symbol: nil, id: nil,
|
10
9
|
config: init,
|
11
10
|
timezone: Time.find_zone('America/Chicago'),
|
12
|
-
|
13
|
-
force_update: false,
|
14
|
-
force_recent: false)
|
11
|
+
filter: :full, # most probably either :full or :rth
|
12
|
+
force_update: false, # force reloading via provide_quarters
|
13
|
+
force_recent: false) #
|
15
14
|
|
16
15
|
headers = %i[contract datetime open high low close volume]
|
17
16
|
sym = get_id_set(symbol: symbol, id: id, contract: contract)
|
18
|
-
contract = contract[-3
|
19
|
-
dir = "#{config[:data_path]}/cached/#{sym[:id]}_#{
|
20
|
-
symlink = "#{config[:data_path]}/cached/#{sym[:symbol]}_#{
|
17
|
+
contract = contract[-3..]
|
18
|
+
dir = "#{config[:data_path]}/cached/#{sym[:id]}_#{filter.to_s.downcase}"
|
19
|
+
symlink = "#{config[:data_path]}/cached/#{sym[:symbol]}_#{filter.to_s.downcase}"
|
21
20
|
`mkdir -p #{dir}` unless Dir.exist? dir
|
22
21
|
`ln -s #{dir} #{symlink}` unless File.exist? symlink
|
23
22
|
file = "#{dir}/#{contract}.csv"
|
24
|
-
quarters_file = "#{config[:data_path]}/quarters/#{sym[:id]}/#{contract[-3
|
25
|
-
if File.exist?(file)
|
26
|
-
base = CSV.read(file, headers: headers).map
|
23
|
+
quarters_file = "#{config[:data_path]}/quarters/#{sym[:id]}/#{contract[-3..]}.csv"
|
24
|
+
if File.exist?(file) && (not force_update)
|
25
|
+
base = CSV.read(file, headers: headers).map do |x|
|
27
26
|
x = x.to_h
|
28
27
|
x[:datetime] = timezone.parse(x[:datetime])
|
29
|
-
%i[open high low close].each{|z| x[z] = x[z].to_f.round(9)}
|
28
|
+
%i[open high low close].each { |z| x[z] = x[z].to_f.round(9) }
|
30
29
|
x[:volume] = x[:volume].to_i
|
31
|
-
x[:type] = "#{
|
30
|
+
x[:type] = "#{filter.to_s.downcase}_day".to_sym
|
32
31
|
x
|
33
|
-
|
34
|
-
if base.last[:high].zero?
|
32
|
+
end
|
33
|
+
if base.last[:high].zero?
|
35
34
|
# contract exists but is closed (has the CLOSED marker)
|
36
35
|
base.pop
|
37
36
|
return base
|
@@ -41,40 +40,40 @@ module Cotcube
|
|
41
40
|
puts "File #{file} exists, but is neither closed nor current. Running update.".colorize(:light_green)
|
42
41
|
end
|
43
42
|
end
|
44
|
-
begin
|
43
|
+
begin
|
45
44
|
data = provide_quarters(contract: contract, id: sym[:id], keep_marker: true)
|
46
|
-
rescue
|
47
|
-
puts "Cannot provide quarters for requested contract #{sym[:symbol]}:#{contract},
|
48
|
-
|
45
|
+
rescue StandardError
|
46
|
+
puts "Cannot provide quarters for requested contract #{sym[:symbol]}:#{contract},"\
|
47
|
+
"returning '[ ]'".colorize(:light_red)
|
48
|
+
return []
|
49
49
|
end
|
50
50
|
|
51
51
|
# removing marker if existing
|
52
52
|
contract_is_marked = data.last[:high].zero?
|
53
53
|
data.pop if contract_is_marked
|
54
|
-
unless
|
55
|
-
requested_set = trading_hours(symbol: sym[:symbol], set:
|
56
|
-
data = data.select_within(ranges: requested_set, attr: :datetime) {|x| x.to_datetime.to_sssm }
|
54
|
+
unless (filter == :full) || (data.size < 3)
|
55
|
+
requested_set = trading_hours(symbol: sym[:symbol], set: filter)
|
56
|
+
data = data.select_within(ranges: requested_set, attr: :datetime) { |x| x.to_datetime.to_sssm }
|
57
57
|
end
|
58
58
|
|
59
59
|
base = Cotcube::Helpers.reduce(bars: data, to: :days)
|
60
60
|
|
61
61
|
# remove last day of result if not marked
|
62
|
-
base.pop unless contract_is_marked
|
62
|
+
base.pop unless contract_is_marked || force_recent
|
63
63
|
|
64
|
-
base.map do |x|
|
64
|
+
base.map do |x|
|
65
65
|
x[:datetime] = x[:datetime].to_date
|
66
|
-
x[:type]= "#{
|
66
|
+
x[:type] = "#{filter}_day".to_sym
|
67
67
|
x.delete(:day)
|
68
68
|
end
|
69
69
|
CSV.open(file, 'w') do |csv|
|
70
|
-
base.each{|b| csv << b.values_at(*headers) }
|
70
|
+
base.each { |b| csv << b.values_at(*headers) }
|
71
71
|
if contract_is_marked
|
72
|
-
marker = [
|
72
|
+
marker = ["#{sym[:symbol]}#{contract}", base.last[:datetime] + 1.day, 0, 0, 0, 0, 0]
|
73
73
|
csv << marker
|
74
74
|
end
|
75
75
|
end
|
76
76
|
base
|
77
77
|
end
|
78
|
-
|
79
78
|
end
|
80
79
|
end
|
@@ -4,7 +4,10 @@ module Cotcube
|
|
4
4
|
# Missing top level documentation comment
|
5
5
|
module Bardata
|
6
6
|
# just reads bardata/daily/<id>/<contract>.csv
|
7
|
-
def provide_daily(contract:,
|
7
|
+
def provide_daily(contract:,
|
8
|
+
symbol: nil, id: nil,
|
9
|
+
timezone: Time.find_zone('America/Chicago'),
|
10
|
+
config: init)
|
8
11
|
contract = contract.to_s.upcase
|
9
12
|
unless contract.is_a?(String) && [3, 5].include?(contract.size)
|
10
13
|
raise ArgumentError, "Contract '#{contract}' is bogus, should be like 'M21' or 'ESM21'"
|
@@ -96,6 +99,7 @@ module Cotcube
|
|
96
99
|
|
97
100
|
sym = get_id_set(symbol: symbol, id: id)
|
98
101
|
id = sym[:id]
|
102
|
+
# noinspection RubyNilAnalysis
|
99
103
|
data = continuous(id: id, config: config).map do |x|
|
100
104
|
{
|
101
105
|
date: x[:date],
|
data/lib/cotcube-bardata/eods.rb
CHANGED
@@ -15,7 +15,8 @@ module Cotcube
|
|
15
15
|
date: last_trade_date,
|
16
16
|
filter: :volume_part,
|
17
17
|
age: 1.hour)
|
18
|
-
sym = get_id_set(symbol: symbol, id: id) if symbol
|
18
|
+
sym = get_id_set(symbol: symbol, id: id) if symbol || id
|
19
|
+
# noinspection RubyScope
|
19
20
|
eods = provide_eods(id: sym.nil? ? nil : sym[:id], config: config, dates: date, filter: filter)
|
20
21
|
result = []
|
21
22
|
eods.map do |eod|
|
@@ -79,26 +79,29 @@ module Cotcube
|
|
79
79
|
|
80
80
|
def compare(contract:, format: '%5.2f')
|
81
81
|
format = "%#{format}" unless format[0] == '%'
|
82
|
-
daily = provide(contract: contract,
|
83
|
-
full = provide(contract: contract,
|
84
|
-
rth = provide(contract: contract,
|
85
|
-
rth_dates = rth.map{|x| x[:datetime] }
|
86
|
-
daily.select!{ |x| rth_dates.include? x[:datetime] }
|
87
|
-
full.select!{ |x| rth_dates.include? x[:datetime] }
|
82
|
+
daily = provide(contract: contract, interval: :daily)
|
83
|
+
full = provide(contract: contract, interval: :days, filter: :full)
|
84
|
+
rth = provide(contract: contract, interval: :days, filter: :rth)
|
85
|
+
rth_dates = rth.map { |x| x[:datetime] }
|
86
|
+
daily.select! { |x| rth_dates.include? x[:datetime] }
|
87
|
+
full.select! { |x| rth_dates.include? x[:datetime] }
|
88
88
|
|
89
|
-
printer = lambda {|z|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
89
|
+
printer = lambda { |z|
|
90
|
+
# rubocop:disable Layout/ClosingParenthesisIndentation
|
91
|
+
"#{z[:datetime].strftime('%m-%d') # rubocop:disable Layout/IndentationWidth
|
92
|
+
}\t#{format format, z[:open]
|
93
|
+
}\t#{format format, z[:high]
|
94
|
+
}\t#{format format, z[:low]
|
95
|
+
}\t#{format format, z[:close]
|
96
|
+
}\t#{format '%7d', z[:volume]}"
|
97
|
+
# rubocop:enable Layout/ClosingParenthesisIndentation
|
98
|
+
}
|
99
|
+
daily.each_with_index do |_x, i|
|
96
100
|
puts "DAILY #{printer.call daily[i]}"
|
97
101
|
puts "FULL #{printer.call full[i]}"
|
98
102
|
puts "RTH #{printer.call rth[i]}"
|
99
|
-
puts
|
103
|
+
puts ' '
|
100
104
|
end
|
101
|
-
|
102
105
|
end
|
103
106
|
end
|
104
107
|
end
|
@@ -1,48 +1,50 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Cotcube
|
4
|
+
# Missing top level documentation comment
|
4
5
|
module Bardata
|
5
|
-
|
6
|
-
def provide(contract:,
|
6
|
+
def provide(contract:, # rubocop:disable Metrics/ParameterLists
|
7
7
|
# Can be like ("2020-12-01 12:00"..."2020-12-14 11:00")
|
8
|
-
range: nil,
|
8
|
+
range: nil,
|
9
9
|
symbol: nil, id: nil,
|
10
10
|
config: init,
|
11
11
|
# supported types are :quarters, :hours, :days, :rth, :dailies, :weeks, :months
|
12
|
-
|
13
|
-
# supported
|
14
|
-
|
12
|
+
interval: :days,
|
13
|
+
# supported filters are :raw, :_24x7_, :full and :rth (and custom named, if provided as file)
|
14
|
+
filter: :full,
|
15
15
|
# TODO: for future compatibility and suggestion: planning to include a function to update
|
16
16
|
# with live data from broker
|
17
17
|
force_recent: false)
|
18
18
|
|
19
19
|
sym = get_id_set(symbol: symbol, id: id, contract: contract, config: config)
|
20
20
|
|
21
|
-
case
|
21
|
+
case interval
|
22
22
|
when :quarters, :hours, :quarter, :hour
|
23
23
|
base = provide_quarters(contract: contract, symbol: symbol, id: id, config: config)
|
24
24
|
base = extended_select_for_range(range: range, base: base) if range
|
25
|
-
requested_set = trading_hours(symbol: sym[:symbol], set:
|
25
|
+
requested_set = trading_hours(symbol: sym[:symbol], set: filter)
|
26
26
|
|
27
|
-
base = base.select_within(ranges: requested_set, attr: :datetime) {|x| x.to_datetime.to_sssm }
|
28
|
-
return base if [
|
27
|
+
base = base.select_within(ranges: requested_set, attr: :datetime) { |x| x.to_datetime.to_sssm }
|
28
|
+
return base if %i[quarters quarter].include? interval
|
29
29
|
|
30
|
-
|
31
|
-
c[:day] == b[:day] and c[:datetime].hour == b[:datetime].hour
|
32
|
-
|
30
|
+
Cotcube::Helpers.reduce(bars: base, to: :hours) do |c, b|
|
31
|
+
c[:day] == b[:day] and c[:datetime].hour == b[:datetime].hour
|
32
|
+
end
|
33
33
|
|
34
34
|
when :days, :weeks, :months
|
35
|
-
base = provide_cached contract: contract, symbol: symbol, id: id, config: config,
|
35
|
+
base = provide_cached contract: contract, symbol: symbol, id: id, config: config, filter: filter,
|
36
|
+
force_recent: force_recent
|
36
37
|
base = extended_select_for_range(range: range, base: base) if range
|
37
|
-
return base if [
|
38
|
-
|
39
|
-
|
38
|
+
return base if %i[day days].include? interval
|
39
|
+
|
40
|
+
# TODO: Missing implementation to reduce cached days to weeks or months
|
41
|
+
raise 'Missing implementation to reduce cached days to weeks or months'
|
40
42
|
when :dailies, :daily
|
41
43
|
base = provide_daily contract: contract, symbol: symbol, id: id, config: config
|
42
44
|
base = extended_select_for_range(range: range, base: base) if range
|
43
|
-
|
45
|
+
base
|
44
46
|
else
|
45
|
-
raise ArgumentError, "Unsupported or unknown
|
47
|
+
raise ArgumentError, "Unsupported or unknown interval '#{interval}' in Bardata.provide"
|
46
48
|
end
|
47
49
|
end
|
48
50
|
end
|