cotcube-bardata 0.1.3 → 0.1.4

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.
@@ -0,0 +1,117 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Cotcube
4
+ # Missing top level documentation
5
+ module Bardata
6
+ # this is an analysis tool to investigate actual ranges of an underlying symbol
7
+ # it is in particular no true range or average true range, as a 'true range' can only be applied to
8
+ # a steady series, what changing contracts definitely aren't
9
+ #
10
+ # The result printed / returned is a table, containing a matrix of rows:
11
+ # 1. size: the amount of values evaluated
12
+ # 2. avg:
13
+ # 3. lower: like median, but not at 1/2 but 1/4
14
+ # 4. median:
15
+ # 5. upper: like median, bot not at 1/2 but 3/4
16
+ # 6. max:
17
+ # and columns:
18
+ # 1.a) all days os the series
19
+ # 1.b) all days of the series, diminished by 2* :dim*100% extreme values (i.e. at both ends)
20
+ # 1.c) the last 200 days
21
+ # 2.a-c) same with days reduced to weeks (c: 52 weeks)
22
+ # 3.a-c) same with days reduced to months (c: 12 months)
23
+ def range_matrix(symbol: nil, id: nil, print: false, dim: 0.05)
24
+ # rubocop:disable Style/MultilineBlockChain
25
+ sym = get_id_set(symbol: symbol, id: id)
26
+ source = {}
27
+ target = {}
28
+ source[:days] = Cotcube::Bardata.continuous_actual_ml symbol: symbol
29
+ source[:weeks] = Cotcube::Helpers.reduce bars: source[:days], to: :weeks
30
+ source[:months] = Cotcube::Helpers.reduce bars: source[:days], to: :months
31
+
32
+ %i[days weeks months].each do |period|
33
+ source[period].map! do |x|
34
+ x[:range] = ((x[:high] - x[:low]) / sym[:ticksize]).round
35
+ x
36
+ end
37
+ target[period] = {}
38
+ target[period][:all_size] = source[period].size
39
+ target[period][:all_avg] = (source[period].map { |x| x[:range] }.reduce(:+) / source[period].size).round
40
+ target[period][:all_lower] = source[period].sort_by do |x|
41
+ x[:range]
42
+ end.map { |x| x[:range] }[ (source[period].size * 1 / 4).round ]
43
+ target[period][:all_median] = source[period].sort_by do |x|
44
+ x[:range]
45
+ end.map { |x| x[:range] }[ (source[period].size * 2 / 4).round ]
46
+ target[period][:all_upper] = source[period].sort_by do |x|
47
+ x[:range]
48
+ end.map { |x| x[:range] }[ (source[period].size * 3 / 4).round ]
49
+ target[period][:all_max] = source[period].map { |x| x[:range] }.max
50
+ target[period][:all_records] = source[period].sort_by do |x|
51
+ -x[:range]
52
+ end.map { |x| { contract: x[:contract], range: x[:range] } }.take(5)
53
+
54
+ tenth = (source[period].size * dim).round
55
+ custom = source[period].sort_by { |x| x[:range] }[tenth..source[period].size - tenth]
56
+ target[period][:dim_size] = custom.size
57
+ target[period][:dim_avg] = (custom.map { |x| x[:range] }.reduce(:+) / custom.size).round
58
+ target[period][:dim_lower] = custom.sort_by do |x|
59
+ x[:range]
60
+ end.map { |x| x[:range] }[ (custom.size * 1 / 4).round ]
61
+ target[period][:dim_median] = custom.sort_by do |x|
62
+ x[:range]
63
+ end.map { |x| x[:range] }[ (custom.size * 2 / 4).round ]
64
+ target[period][:dim_upper] = custom.sort_by do |x|
65
+ x[:range]
66
+ end.map { |x| x[:range] }[ (custom.size * 3 / 4).round ]
67
+ target[period][:dim_max] = custom.map { |x| x[:range] }.max
68
+ target[period][:dim_records] = custom.sort_by do |x|
69
+ -x[:range]
70
+ end.map { |x| { contract: x[:contract], range: x[:range] } }.take(5)
71
+
72
+ range = case period
73
+ when :months
74
+ -13..-2
75
+ when :weeks
76
+ -53..-2
77
+ when :days
78
+ -200..-1
79
+ else
80
+ raise ArgumentError, "Unsupported period: '#{period}'"
81
+ end
82
+ custom = source[period][range]
83
+ target[period][:rec_size] = custom.size
84
+ target[period][:rec_avg] = (custom.map { |x| x[:range] }.reduce(:+) / custom.size).round
85
+ target[period][:rec_lower] = custom.sort_by do |x|
86
+ x[:range]
87
+ end.map { |x| x[:range] }[ (custom.size * 1 / 4).round ]
88
+ target[period][:rec_median] = custom.sort_by do |x|
89
+ x[:range]
90
+ end.map { |x| x[:range] }[ (custom.size * 2 / 4).round ]
91
+ target[period][:rec_upper] = custom.sort_by do |x|
92
+ x[:range]
93
+ end.map { |x| x[:range] }[ (custom.size * 3 / 4).round ]
94
+ target[period][:rec_max] = custom.map { |x| x[:range] }.max
95
+ target[period][:rec_records] = custom.sort_by do |x|
96
+ -x[:range]
97
+ end.map { |x| { contract: x[:contract], range: x[:range] } }.take(5)
98
+ end
99
+
100
+ if print
101
+ %w[size avg lower median upper max].each do |a|
102
+ print "#{'%10s' % a} | " # rubocop:disable Style/FormatString
103
+ %i[days weeks months].each do |b|
104
+ %w[all dim rec].each do |c|
105
+ print ('%8d' % target[b]["#{c}_#{a}".to_sym]).to_s # rubocop:disable Style/FormatString
106
+ end
107
+ print ' | '
108
+ end
109
+ puts ''
110
+ end
111
+ end
112
+
113
+ target
114
+ # rubocop:enable Style/MultilineBlockChain
115
+ end
116
+ end
117
+ end
@@ -1,14 +1,27 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Cotcube
4
+ # missing top level documentation
4
5
  module Bardata
5
-
6
- # fetching official tradedates from CME
6
+ # fetching official trade dates from CME
7
+ # it returns the current trade date or, if today isn't a trading day, the last trade date.
7
8
  def last_trade_date
8
- uri = "https://www.cmegroup.com/CmeWS/mvc/Volume/TradeDates?exchange=CME"
9
- res = nil
10
- res = HTTParty.get(uri).parsed_response
11
- res.map{|x| a = x["tradeDate"].chars.each_slice(2).map(&:join); "#{a[0]}#{a[1]}-#{a[2]}-#{a[3]}"}.first
9
+ uri = 'https://www.cmegroup.com/CmeWS/mvc/Volume/TradeDates?exchange=CME'
10
+ begin
11
+ HTTParty.get(uri)
12
+ .parsed_response
13
+ .map do |x|
14
+ a = x['tradeDate'].chars.each_slice(2).map(&:join)
15
+ "#{a[0]}#{a[1]}-#{a[2]}-#{a[3]}"
16
+ end
17
+ .first
18
+ rescue StandardError
19
+ nil
20
+ end
21
+ end
22
+
23
+ def holidays(config: init)
24
+ CSV.read("#{config[:data_path]}/holidays.csv").map{|x| DateTime.parse(x[0])}
12
25
  end
13
26
 
14
27
  end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Cotcube
4
+ # Missing top level comment
5
+ module Bardata
6
+ # returns an Array of ranges containing a week of trading hours, specified by seconds since monday morning
7
+ # (as sunday is wday:0)
8
+ # according files are located in config[:data_path]/trading_hours and picked either
9
+ # by the symbol itself or by the assigned type
10
+ # commonly there are two sets for each symbol: :full and :rth, exceptions are e.g. meats
11
+ def trading_hours(symbol: nil, id: nil, # rubocop:disable Metrics/ParameterLists
12
+ set: :full, force_set: false,
13
+ config: init, debug: false)
14
+ return (0...24 * 7 * 3600) if set.to_s =~ /24x7/
15
+
16
+ prepare = lambda do |f|
17
+ CSV.read(f, converters: :numeric)
18
+ .map(&:to_a)
19
+ .tap { |x| x.shift unless x.first.first.is_a?(Numeric) }
20
+ .map { |x| (x.first...x.last) }
21
+ end
22
+
23
+ sym = get_id_set(symbol: symbol, id: id)
24
+
25
+ file = "#{config[:data_path]}/trading_hours/#{sym[:symbol]}_#{set}.csv"
26
+ puts "Trying to use #{file} for #{symbol} + #{set}" if debug
27
+ return prepare.call(file) if File.exist? file
28
+
29
+ file = "#{config[:data_path]}/trading_hours/#{sym[:symbol]}_full.csv"
30
+ puts "Failed. Trying to use #{file} now" if debug
31
+ return prepare.call(file) if File.exist?(file) && (not force_set)
32
+
33
+ file = "#{config[:data_path]}/trading_hours/#{sym[:type]}_#{set}.csv"
34
+ puts "Failed. Trying to use #{file} now." if debug
35
+ return prepare.call(file) if File.exist? file
36
+
37
+ file = "#{config[:data_path]}/trading_hours/#{sym[:type]}_full.csv"
38
+ puts "Failed. Trying to use #{file} now." if debug
39
+ return prepare.call(file) if File.exist?(file) && (not force_set)
40
+
41
+ puts "Finally failed to find range set for #{symbol} + #{set}, returning 24x7"..colorize(:light_yellow)
42
+ (0...24 * 7 * 3600)
43
+ end
44
+ end
45
+ end
metadata CHANGED
@@ -1,127 +1,127 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cotcube-bardata
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benjamin L. Tischendorf
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-12-23 00:00:00.000000000 Z
11
+ date: 2021-01-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: cotcube-indicators
14
+ name: activesupport
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '6'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: '6'
27
27
  - !ruby/object:Gem::Dependency
28
- name: cotcube-helpers
28
+ name: colorize
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: '0.8'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: '0.8'
41
41
  - !ruby/object:Gem::Dependency
42
- name: yaml
42
+ name: cotcube-helpers
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: '0.1'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '0'
54
+ version: '0.1'
55
55
  - !ruby/object:Gem::Dependency
56
- name: activesupport
56
+ name: cotcube-indicators
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ">="
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '0'
61
+ version: '0.1'
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ">="
66
+ - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '0'
68
+ version: '0.1'
69
69
  - !ruby/object:Gem::Dependency
70
- name: colorize
70
+ name: httparty
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ">="
73
+ - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '0'
75
+ version: '0.18'
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ">="
80
+ - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '0'
82
+ version: '0.18'
83
83
  - !ruby/object:Gem::Dependency
84
- name: httparty
84
+ name: parallel
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - ">="
87
+ - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: '0'
89
+ version: '1'
90
90
  type: :runtime
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - ">="
94
+ - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: '0'
96
+ version: '1'
97
97
  - !ruby/object:Gem::Dependency
98
- name: rubyzip
98
+ name: yaml
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - ">="
101
+ - - "~>"
102
102
  - !ruby/object:Gem::Version
103
- version: '0'
103
+ version: '0.1'
104
104
  type: :runtime
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
- - - ">="
108
+ - - "~>"
109
109
  - !ruby/object:Gem::Version
110
- version: '0'
110
+ version: '0.1'
111
111
  - !ruby/object:Gem::Dependency
112
112
  name: rake
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
115
  - - ">="
116
116
  - !ruby/object:Gem::Version
117
- version: '0'
117
+ version: '12'
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
122
  - - ">="
123
123
  - !ruby/object:Gem::Version
124
- version: '0'
124
+ version: '12'
125
125
  - !ruby/object:Gem::Dependency
126
126
  name: rspec
127
127
  requirement: !ruby/object:Gem::Requirement
@@ -166,13 +166,17 @@ files:
166
166
  - VERSION
167
167
  - cotcube-bardata.gemspec
168
168
  - lib/cotcube-bardata.rb
169
+ - lib/cotcube-bardata/cached.rb
169
170
  - lib/cotcube-bardata/constants.rb
170
171
  - lib/cotcube-bardata/daily.rb
171
172
  - lib/cotcube-bardata/eods.rb
173
+ - lib/cotcube-bardata/helpers.rb
172
174
  - lib/cotcube-bardata/init.rb
173
175
  - lib/cotcube-bardata/provide.rb
174
176
  - lib/cotcube-bardata/quarters.rb
177
+ - lib/cotcube-bardata/range_matrix.rb
175
178
  - lib/cotcube-bardata/trade_dates.rb
179
+ - lib/cotcube-bardata/trading_hours.rb
176
180
  homepage: https://github.com/donkeybridge/cotcube-bardata
177
181
  licenses:
178
182
  - BSD-4-Clause