moex_iss_client 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c9a9044447ebd61d223edc9c7a907a46b4b8cec0a8d41cf741d3b8b236d2a619
4
+ data.tar.gz: 61f281ed4bb063125e16c986d225bdfcf57fefa12dee5b1f14cd2691bf03a262
5
+ SHA512:
6
+ metadata.gz: 34265f0b1fad367a69c3f3bdda907b221f086135451f7d60c748a02041c966ec25d5bcbcf198565d372c449ad2ace55760fe659632c0019a15fcbb77ef75747d
7
+ data.tar.gz: 8c400662e7f6b4d696da5269ed7b3fe527e0b6dd7217263ccf589b0f3231a8e4bab177bf1c4e0f81e14d05d77c0785542f1b58b4f33a25964e43114aa3b4a82e
data/CHANGELOG.md ADDED
@@ -0,0 +1,22 @@
1
+ # Changelog
2
+
3
+ ## [Unreleased]
4
+
5
+ ## [0.2.0]
6
+
7
+ ### Added
8
+
9
+ - `MoexIssClient::Client#history` - support for foreign stocks and DRs, ETFs, MFs, corporate bonds in RUB.
10
+ - `MoexIssClient::Security` - support for parsing any data sample (not only the last one) of sequrity history.
11
+
12
+ ## [0.1.0] - 2023-03-19
13
+
14
+ ### Added
15
+
16
+ - Class `MoexIssClient::Client` with method `#history` gets data from ISS for specified security and date interval. Currently only supports Russian stocks, DRs and government bonds in RUB.
17
+ - Class `MoexIssClient::HistoryValidator` with method `#validate` validates data of security history.
18
+ - Class `MoexIssClient::Security` describes security history and provides methods for parsing. Currently supports parsing only the latest data sample.
19
+ - Method `MoexIssClient::Security#attribute` gets value of specified attribute if it presents.
20
+ - Method `MoexIssClient::Security#last_price` is alias for `#attribute('CLOSE')`.
21
+ - Method `MoexIssClient::Security#shortname` is alias for `#attribute('SHORTNAME').`
22
+ - Method `MoexIssClient::Security#sec_id` is alias for `#attribute('SEC_ID')`.
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'http://rubygems.org'
4
+
5
+ gemspec
6
+
7
+ group :development do
8
+ gem 'rubocop', '~> 1.48', '>= 1.48.1'
9
+ gem 'rubocop-performance', '~> 1.16'
10
+ gem 'rubocop-rspec', '~> 2.19'
11
+ end
12
+
13
+ group :test do
14
+ gem 'rspec', '~> 3.12'
15
+ gem 'simplecov', '~> 0.22.0'
16
+ gem 'webmock', '~> 3.18', '>= 3.18.1'
17
+ end
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 bl1ndy
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,93 @@
1
+ # Moscow Exchange ISS Client
2
+
3
+ This Ruby gem provides a convinient way to get non-real-time securities history from MOEX ISS without authentication.
4
+
5
+ NOT USED for real-time updating of securities quotes and for algorithmic trading.
6
+
7
+ - [Getting started](#getting-started)
8
+ - [Usage](#usage)
9
+ - [Changelog](https://github.com/bl1ndy/moex_iss_client/blob/main/CHANGELOG.md)
10
+
11
+ ## Getting started
12
+
13
+ To install gem, add this line to your application's `Gemfile`:
14
+
15
+ ```
16
+ gem 'moex_iss_client'
17
+ ```
18
+
19
+ Then install as follows:
20
+
21
+ ```
22
+ $ bundle install
23
+ ```
24
+
25
+ Or install it directly using `gem`:
26
+
27
+ ```
28
+ $ gem install moex_iss_client
29
+ ```
30
+
31
+ ## Usage
32
+
33
+ Create an instance of `MoexIssClient::Client`:
34
+
35
+ ```ruby
36
+ client = MoexIssClient::Client.new
37
+ ```
38
+
39
+ To get security history from ISS, call `#history` on client and provide security ID:
40
+
41
+ ```ruby
42
+ history = client.history('MOEX')
43
+ ```
44
+
45
+ Method `#history` provides some options for ISS request:
46
+
47
+ ```ruby
48
+ :market # default value: 'shares'
49
+
50
+ # To search for security history in shares market (could be omitted)
51
+ history = client.history('MOEX', market: 'shares')
52
+
53
+ # To search for security history in bonds market
54
+ history = client.history('SU24021RMFS6', market: 'bonds')
55
+ ```
56
+
57
+ ```ruby
58
+ :from # default value: 6 days ago, format: 'YYYY-MM-DD'
59
+
60
+ history = client.history('MOEX', from: '2023-03-01')
61
+ ```
62
+
63
+ ```ruby
64
+ :till # default value: today, format: 'YYYY-MM-DD'
65
+
66
+ history = client.history('MOEX', till: '2023-03-15')
67
+ ```
68
+
69
+ As we get security history, we should provide it to `MoexIssClient::Security` instance:
70
+
71
+ ```ruby
72
+ security = MoexIssClient::Security.new(history)
73
+ ```
74
+
75
+ Now we can execute some usefull information from history:
76
+
77
+ ```ruby
78
+ # Get last close price
79
+
80
+ security.last_price #> "112.89"
81
+
82
+ # Get shortname
83
+
84
+ security.shortname #> "МосБиржа"
85
+
86
+ # Get security ID
87
+
88
+ security.sec_id #> "MOEX"
89
+
90
+ # Get any existing attribute from data sample
91
+
92
+ security.attribute('tradedate') #> "2023-03-17"
93
+ ```
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'faraday'
4
+
5
+ module MoexIssClient
6
+ class Client
7
+ ROOT_ENDPOINT = 'https://iss.moex.com/iss'
8
+
9
+ attr_reader :client
10
+
11
+ def initialize
12
+ @client = Faraday.new(
13
+ url: ROOT_ENDPOINT,
14
+ headers: { 'Content-Type' => 'application/xml' }
15
+ )
16
+ end
17
+
18
+ def history(sec_id, options = {})
19
+ raise ArgumentError, 'Invalid security ID' if sec_id.to_s.empty?
20
+
21
+ params = history_params(sec_id, options)
22
+
23
+ client.get(params[:path], { from: params[:from], till: params[:till] }).body
24
+ end
25
+
26
+ private
27
+
28
+ def history_params(sec_id, options)
29
+ market = options[:market] == 'bonds' ? 'bonds' : 'shares'
30
+ board = market == 'bonds' ? 'tqob' : 'tqbr'
31
+
32
+ {
33
+ path: "history/engines/stock/markets/#{market}/boards/#{board}/securities/#{sec_id}",
34
+ from: options[:from] || Date.today.prev_day(6).to_s,
35
+ till: options[:till] || Date.today.to_s
36
+ }
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MoexIssClient
4
+ class NoHistoryError < StandardError; end
5
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rexml'
4
+
5
+ module MoexIssClient
6
+ class HistoryValidator
7
+ def validate(history)
8
+ parsed_history = REXML::Document.new(history)
9
+
10
+ raise NoHistoryError, 'Invalid or empty security history' if valid?(parsed_history)
11
+
12
+ parsed_history
13
+ end
14
+
15
+ private
16
+
17
+ def valid?(history)
18
+ history.get_elements('//data/rows/row').empty?
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rexml'
4
+
5
+ module MoexIssClient
6
+ class Security
7
+ attr_reader :history
8
+
9
+ def initialize(history)
10
+ @history = HistoryValidator.new.validate(history)
11
+ end
12
+
13
+ def last_price
14
+ last_data_sample['CLOSE']
15
+ end
16
+
17
+ def shortname
18
+ last_data_sample['SHORTNAME']
19
+ end
20
+
21
+ def sec_id
22
+ last_data_sample['SECID']
23
+ end
24
+
25
+ def attribute(attr)
26
+ last_data_sample[attr.to_s.upcase]
27
+ end
28
+
29
+ private
30
+
31
+ def last_data_sample
32
+ history.get_elements('//data/rows/row[last()]').first
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MoexIssClient
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'moex_iss_client/client'
4
+ require_relative 'moex_iss_client/history_validator'
5
+ require_relative 'moex_iss_client/security'
6
+ require_relative 'moex_iss_client/exceptions'
7
+
8
+ module MoexIssClient
9
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require File.expand_path('lib/moex_iss_client/version', __dir__)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'moex_iss_client'
7
+ spec.version = MoexIssClient::VERSION
8
+ spec.authors = ['bl1ndy']
9
+ spec.email = ['ayoblindstone@gmail.com']
10
+
11
+ spec.summary = 'Ruby client for Moscow Exchange ISS API'
12
+ spec.description = 'This gem allows to interact with Moscow Exchange ISS API'
13
+ spec.homepage = 'https://github.com/bl1ndy/moex_iss_client'
14
+ spec.license = 'MIT'
15
+ spec.platform = Gem::Platform::RUBY
16
+ spec.required_ruby_version = '>= 3.0.0'
17
+
18
+ spec.files = Dir[
19
+ 'lib/**/*.rb',
20
+ 'moex_iss_client.gemspec',
21
+ 'LICENSE',
22
+ 'CHANGELOG.md',
23
+ 'README.md',
24
+ 'Gemfile'
25
+ ]
26
+ spec.extra_rdoc_files = ['README.md']
27
+
28
+ spec.add_dependency 'faraday', '~> 2.7', '>= 2.7.4'
29
+ spec.add_dependency 'rexml', '~> 3.2', '>= 3.2.5'
30
+
31
+ spec.metadata['rubygems_mfa_required'] = 'false'
32
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org/'
33
+ spec.metadata['homepage_uri'] = spec.homepage
34
+ spec.metadata['source_code_uri'] = spec.homepage
35
+ spec.metadata['changelog_uri'] = 'https://github.com/bl1ndy/moex_iss_client/blob/main/CHANGELOG.md'
36
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: moex_iss_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - bl1ndy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-03-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.7'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 2.7.4
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '2.7'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 2.7.4
33
+ - !ruby/object:Gem::Dependency
34
+ name: rexml
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '3.2'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 3.2.5
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '3.2'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 3.2.5
53
+ description: This gem allows to interact with Moscow Exchange ISS API
54
+ email:
55
+ - ayoblindstone@gmail.com
56
+ executables: []
57
+ extensions: []
58
+ extra_rdoc_files:
59
+ - README.md
60
+ files:
61
+ - CHANGELOG.md
62
+ - Gemfile
63
+ - LICENSE
64
+ - README.md
65
+ - lib/moex_iss_client.rb
66
+ - lib/moex_iss_client/client.rb
67
+ - lib/moex_iss_client/exceptions.rb
68
+ - lib/moex_iss_client/history_validator.rb
69
+ - lib/moex_iss_client/security.rb
70
+ - lib/moex_iss_client/version.rb
71
+ - moex_iss_client.gemspec
72
+ homepage: https://github.com/bl1ndy/moex_iss_client
73
+ licenses:
74
+ - MIT
75
+ metadata:
76
+ rubygems_mfa_required: 'false'
77
+ allowed_push_host: https://rubygems.org/
78
+ homepage_uri: https://github.com/bl1ndy/moex_iss_client
79
+ source_code_uri: https://github.com/bl1ndy/moex_iss_client
80
+ changelog_uri: https://github.com/bl1ndy/moex_iss_client/blob/main/CHANGELOG.md
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 3.0.0
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubygems_version: 3.4.8
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Ruby client for Moscow Exchange ISS API
100
+ test_files: []