stockcruncher 1.3.1 → 1.4.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +23 -6
- data/lib/stockcruncher/cli.rb +20 -3
- data/lib/stockcruncher/influxdb.rb +19 -8
- data/lib/stockcruncher/stats.rb +1 -1
- data/lib/stockcruncher/version.rb +1 -1
- data/spec/stockcruncher/cli_spec.rb +12 -0
- data/spec/stockcruncher/stubs/servers_stubs.rb +7 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 03dfe0d9f40c0b298cbe4c7d5f3d37683765ccb737b3a4d82ec75f6e04f46e06
|
4
|
+
data.tar.gz: e0da9a269671deb3400042cf87a7bfb39bf7b0842af91483d979d3c672c6c7a6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d858ff01048b66b048ec634519b9e19123e2b74204dc71826118e9c41fec707612e0424260b628fdc3a2e228366c1c1d7b2312bf8191beef0683f88ede20481d
|
7
|
+
data.tar.gz: 70fb3692a8d45ce7c292b89b551a0d78b11076b93a53caeefae4fc642d1f8ae2d50ebd09b43d6ec7cd59edf1117214aa7723a04bd96b958d866015f0479cd35d
|
data/README.md
CHANGED
@@ -5,12 +5,14 @@
|
|
5
5
|
[![Gem Version][gem-img]][gem]
|
6
6
|
|
7
7
|
1. [Overview](#overview)
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
8
|
+
1. [Description](#description)
|
9
|
+
1. [Setup](#setup)
|
10
|
+
1. [Usage](#usage)
|
11
|
+
1. [Environment variables](#environment-variables)
|
12
|
+
1. [Examples](#examples)
|
13
|
+
1. [Limitations](#limitations)
|
14
|
+
1. [Development](#development)
|
15
|
+
1. [Miscellaneous](#miscellaneous)
|
14
16
|
|
15
17
|
## Overview
|
16
18
|
|
@@ -41,6 +43,17 @@ An interactive help is available with:
|
|
41
43
|
|
42
44
|
$ stockcruncher help [subcommand]
|
43
45
|
|
46
|
+
## Environment variables
|
47
|
+
|
48
|
+
Parameters in /etc/stockcruncher/stockcrunucher.yml can be overloaded with
|
49
|
+
environment variables.
|
50
|
+
Environment variables should match SCR_<COMPONENTID>_<ITEM>.
|
51
|
+
Component ID are as follow.
|
52
|
+
- AlphaVantage: AV
|
53
|
+
- InfluxDB: IDB
|
54
|
+
Items are upcase keys. See template /etc/stockcruncher/stockcrunucher.yml for
|
55
|
+
reference.
|
56
|
+
|
44
57
|
## Examples
|
45
58
|
|
46
59
|
To get daily time serie data of a symbol:
|
@@ -51,6 +64,10 @@ To get last day endpoint data of a symbol:
|
|
51
64
|
|
52
65
|
$ stockcruncher quote AAPL
|
53
66
|
|
67
|
+
To override a parameter with environment variable:
|
68
|
+
|
69
|
+
$ SCR_IDB_HOST=192.168.0.80; stockcruncher quote AAPL
|
70
|
+
|
54
71
|
## Limitations
|
55
72
|
|
56
73
|
Data are currently scraped from AlphaVantage API.
|
data/lib/stockcruncher/cli.rb
CHANGED
@@ -45,12 +45,20 @@ module StockCruncher
|
|
45
45
|
default: false,
|
46
46
|
desc: 'Full data size.'
|
47
47
|
)
|
48
|
+
option(
|
49
|
+
:catchup,
|
50
|
+
aliases: ['-u'],
|
51
|
+
type: :boolean,
|
52
|
+
default: false,
|
53
|
+
desc: 'Catch up the missing data only.'
|
54
|
+
)
|
48
55
|
def daily(symbol)
|
49
56
|
opts = options.dup
|
50
57
|
config = YAML.load_file(opts['config'])
|
51
58
|
cruncher = StockCruncher::AlphaVantage.new(config, opts['insecure'])
|
52
59
|
data = cruncher.crunch_daily(symbol, opts['full'])
|
53
|
-
StockCruncher::InfluxDB.new(config)
|
60
|
+
influx = StockCruncher::InfluxDB.new(config)
|
61
|
+
influx.export_history(symbol, data, opts['catchup'])
|
54
62
|
puts JSON.pretty_generate(data) unless opts['quiet']
|
55
63
|
end
|
56
64
|
|
@@ -63,10 +71,18 @@ module StockCruncher
|
|
63
71
|
default: false,
|
64
72
|
desc: 'Recalculate all MA historical values.'
|
65
73
|
)
|
74
|
+
option(
|
75
|
+
:catchup,
|
76
|
+
aliases: ['-u'],
|
77
|
+
type: :boolean,
|
78
|
+
default: false,
|
79
|
+
desc: 'Catch up the missing data only.'
|
80
|
+
)
|
66
81
|
def movingaverages(symbol)
|
67
82
|
opts = options.dup
|
68
83
|
config = YAML.load_file(opts['config'])
|
69
|
-
StockCruncher::InfluxDB.new(config)
|
84
|
+
influx = StockCruncher::InfluxDB.new(config)
|
85
|
+
influx.moving_averages(symbol, opts['all'], opts['catchup'])
|
70
86
|
end
|
71
87
|
|
72
88
|
desc('quote SYMBOL [options]',
|
@@ -76,7 +92,8 @@ module StockCruncher
|
|
76
92
|
config = StockCruncher::Config.load(opts['config'])
|
77
93
|
cruncher = StockCruncher::AlphaVantage.new(config, opts['insecure'])
|
78
94
|
data = cruncher.crunch_quote(symbol)
|
79
|
-
StockCruncher::InfluxDB.new(config)
|
95
|
+
influx = StockCruncher::InfluxDB.new(config)
|
96
|
+
influx.export_last_day(data)
|
80
97
|
puts JSON.pretty_generate(data) unless opts['quiet']
|
81
98
|
end
|
82
99
|
end
|
@@ -20,15 +20,22 @@ module StockCruncher
|
|
20
20
|
data['columns'].zip(data['values'].transpose).to_h
|
21
21
|
end
|
22
22
|
|
23
|
+
def get_ma_values(symbol, fullsize)
|
24
|
+
values = %w[ema200]
|
25
|
+
data = query('ema', symbol, values, fullsize)
|
26
|
+
data['columns'].zip(data['values'].transpose).to_h
|
27
|
+
end
|
28
|
+
|
23
29
|
# Method to calculate moving averages based on last day values
|
24
|
-
def moving_averages(symbol, fullsize)
|
25
|
-
|
30
|
+
def moving_averages(symbol, fullsize, catchup)
|
31
|
+
data = get_daily_values(symbol, fullsize)
|
32
|
+
mas = catchup ? get_ma_values(symbol, fullsize) : { 'time' => [] }
|
26
33
|
tags = create_tags(symbol)
|
27
|
-
series
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
write_moving_averages(tags,
|
34
|
+
dates, series, weights = data.values_at 'time', 'close', 'volume'
|
35
|
+
series.each_index do |i|
|
36
|
+
next if mas['time'].include? dates[i]
|
37
|
+
|
38
|
+
write_moving_averages(tags, series[i, 201], weights[i, 201], dates[i])
|
32
39
|
break unless fullsize
|
33
40
|
end
|
34
41
|
end
|
@@ -39,8 +46,12 @@ module StockCruncher
|
|
39
46
|
end
|
40
47
|
|
41
48
|
# Method to export historical data to database
|
42
|
-
def export_history(symbol, timeseries)
|
49
|
+
def export_history(symbol, timeseries, catchup)
|
43
50
|
tags = create_tags(symbol)
|
51
|
+
if catchup
|
52
|
+
series = get_daily_values(symbol, true)
|
53
|
+
series['time'].each { |date| timeseries.delete(date) }
|
54
|
+
end
|
44
55
|
timeseries.each_pair do |date, values|
|
45
56
|
write('daily', tags, values, date)
|
46
57
|
end
|
data/lib/stockcruncher/stats.rb
CHANGED
@@ -26,12 +26,24 @@ describe StockCruncher::CLI do # rubocop:disable Metrics/BlockLength
|
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
+
context 'daily SYM -u -c spec/files/stockcruncher.yml' do
|
30
|
+
it 'Get the daily time serie for SYM.' do
|
31
|
+
expect { start(self) }.to output(daily).to_stdout
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
29
35
|
context 'movingaverages SYM -c spec/files/stockcruncher.yml' do
|
30
36
|
it 'Get the daily time serie for SYM.' do
|
31
37
|
expect { start(self) }.to output('').to_stdout
|
32
38
|
end
|
33
39
|
end
|
34
40
|
|
41
|
+
context 'movingaverages SYM -u -c spec/files/stockcruncher.yml' do
|
42
|
+
it 'Get the daily time serie for SYM.' do
|
43
|
+
expect { start(self) }.to output('').to_stdout
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
35
47
|
context 'quote NODATA -c spec/files/stockcruncher.yml' do
|
36
48
|
it 'Should not get any data and should fail.' do
|
37
49
|
expect { start(self) }.to raise_error(SystemExit)
|
@@ -12,6 +12,10 @@ daily = "timestamp,open,high,low,close,volume\r\n2020-08-03,23.8500,24.6500," \
|
|
12
12
|
"\n2020-07-29,24.7500,25.0300,24.0400,24.5600,4605804\r\n2020-07-28," \
|
13
13
|
"25.9900,26.0700,24.5100,24.7500,6904261\r\n"
|
14
14
|
d_err = "{\n \"Error Message\": \"Invalid API call.\"\n}"
|
15
|
+
i_ema = "q=SELECT ema200 FROM ema WHERE symbol = 'SYM' ORDER BY time DESC"
|
16
|
+
maval = '{"results":[{"statement_id":0,"series":[{"name":"ema","columns":[' \
|
17
|
+
'"time","ema200"],"values":[["2017-' \
|
18
|
+
'03-01T18:00:00Z",1]]}]}]}'
|
15
19
|
mvavg = '{"results":[{"statement_id":0,"series":[{"name":"daily","columns":[' \
|
16
20
|
'"time","close","change","changePercent","volume"],"values":[["2017-' \
|
17
21
|
'03-01T18:00:00Z",1,0,0,1],["2017-03-02T18:00:00Z",1,0,0,1],["2017-0' \
|
@@ -35,6 +39,9 @@ RSpec.configure do |config|
|
|
35
39
|
'function=TIME_SERIES_DAILY&symbol=SYM&apikey=demo' \
|
36
40
|
'&datatype=csv&outputsize=compact')
|
37
41
|
.to_return('status' => 200, 'body' => daily, 'headers' => {})
|
42
|
+
stub_request(:post, 'http://localhost:8086/query?db=test')
|
43
|
+
.with(body: i_ema)
|
44
|
+
.to_return('status' => 204, 'body' => maval, 'headers' => {})
|
38
45
|
stub_request(:post, 'http://localhost:8086/query?db=test')
|
39
46
|
.to_return('status' => 204, 'body' => mvavg, 'headers' => {})
|
40
47
|
stub_request(:post, 'http://localhost:8086/write?db=test')
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: stockcruncher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.4.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Richard Delaplace
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-08-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|