xtb 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3c368d7138b3a90d013565212661d39e0dd339c57c8a4c235eaa30ad786e4601
4
+ data.tar.gz: c1613af477c8de260af049d7fc1ac42df8ead3d327967ecc2b575c8599cdbe19
5
+ SHA512:
6
+ metadata.gz: 98a5f73fb940d41c98f338c14bbb7a11f5b546214d1b9a7249bc3594797f4968216fbc449118a39a7cb5ba5bf8a0837560334521cf758d8eb50cc52ad3678e9a
7
+ data.tar.gz: '046729d8d9a8efad3203bfef09ce870fbb2086b196343460114232f052b8ef194a5063e56d119324595d7ed53db27f3bd9a3217b0ce161c44ec5efe16728ffba'
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,12 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.1
3
+
4
+ Style/StringLiterals:
5
+ EnforcedStyle: single_quotes
6
+
7
+ Style/StringLiteralsInInterpolation:
8
+ EnforcedStyle: double_quotes
9
+
10
+ Metrics/BlockLength:
11
+ Exclude:
12
+ - '**/spec/**/*'
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## 0.1.0
2
+
3
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 jacekmaciag
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # XTB API client for Ruby
2
+
3
+ Xtb is a Ruby implementation of the [XTB broker API (xAPI) version 2.5.0](http://developers.xstore.pro/documentation/2.5.0).
4
+ At the time of writing this it's the latest version of the API.
5
+ This gem allows you to connect to the XTB broker and execute trades, get account information, and more.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
18
+
19
+ ## Usage
20
+
21
+ ### Configuration
22
+
23
+ Before you can connect to the XTB API, you need to configure the connection.
24
+ You can do this by creating a configuration object and passing it to the client.
25
+
26
+ ```ruby
27
+
28
+ ```
29
+
30
+ ### Connect to the XTB API
31
+
32
+ ```ruby
33
+
34
+ ```
35
+
36
+
37
+ ## Development
38
+
39
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
40
+
41
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
42
+
43
+ ## Contributing
44
+
45
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jacekmaciag/xtb.
46
+
47
+ ## License
48
+
49
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/lib/xtb/config.rb ADDED
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Xtb
4
+ # Configuration mapping for XTB API.
5
+ class Config
6
+ class << self
7
+ def https_host
8
+ ENV.fetch('XTB__HTTPS_HOST', 'xapi.xtb.com')
9
+ end
10
+
11
+ def https_port
12
+ ENV.fetch('XTB__HTTPS_PORT', 5124).to_i
13
+ end
14
+
15
+ def wss_host
16
+ ENV.fetch('XTB__WSS_HOST', 'ws.xtb.com')
17
+ end
18
+
19
+ def wss_path
20
+ ENV.fetch('XTB__WSS_PATH', 'demo')
21
+ end
22
+
23
+ def wss_port
24
+ ENV.fetch('XTB__WSS_PORT', 5125).to_i
25
+ end
26
+
27
+ def user_id
28
+ ENV.fetch('XTB__USER_ID') { raise 'XTB__USER_ID is required' }
29
+ end
30
+
31
+ def password
32
+ ENV.fetch('XTB__PASSWORD') { raise 'XTB__PASSWORD is required' }
33
+ end
34
+
35
+ def min_request_interval
36
+ ENV.fetch('XTB__MIN_REQUEST_INTERVAL', 0.2).to_f
37
+ end
38
+ end
39
+ end
40
+ end
data/lib/xtb/errors.rb ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Xtb
4
+ class Error < StandardError; end
5
+
6
+ class NotLoggedInError < Error; end
7
+ class AlreadyLoggedInError < Error; end
8
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Xtb
4
+ module Http
5
+ # http://developers.xstore.pro/documentation/2.5.0#getAllSymbols
6
+ class AllSymbols < Command
7
+ def call
8
+ super.map { |symbol| Xtb::SymbolRecord.new(**symbol) }
9
+ end
10
+
11
+ private
12
+
13
+ def command = :getAllSymbols
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Xtb
4
+ module Http
5
+ # http://developers.xstore.pro/documentation/2.5.0#getCalendar
6
+ class Calendar < Command
7
+ IMPACT = {
8
+ 1 => 'low',
9
+ 2 => 'medium',
10
+ 3 => 'high'
11
+ }.freeze
12
+
13
+ CalendarRecord = Data.define(:country, :current, :forecast, :impact, :period, :previous, :time, :title) do
14
+ def initialize(country:, current:, forecast:, impact:, period:, previous:, time:, title:)
15
+ super(country:, current:, forecast:, impact: IMPACT[impact.to_i], period:, previous:, time:, title:)
16
+ end
17
+ end
18
+
19
+ def call
20
+ super.map { |record| CalendarRecord.new(**record) }
21
+ end
22
+
23
+ private
24
+
25
+ def command = :getCalendar
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Xtb
4
+ module Http
5
+ # http://developers.xstore.pro/documentation/2.5.0#getChartLastRequest
6
+ class ChartLastRequest < Command
7
+ RateInfoRecord = Data.define(:close, :ctm, :ctm_string, :high, :low, :open, :vol)
8
+ ChartLastRequestResponse = Data.define(:digits, :rate_infos)
9
+
10
+ # @param period [Xtb::PERIODS]
11
+ # @param start [Time]
12
+ # @param symbol [String|Symbol]
13
+ def initialize(period, start, symbol)
14
+ @period = period
15
+ @start = start
16
+ @symbol = symbol
17
+ end
18
+
19
+ def call
20
+ digits, rate_infos = super.values_at(:digits, :rate_infos)
21
+ rate_infos = rate_infos.map { |record| RateInfoRecord.new(**record) }
22
+ ChartLastRequestResponse.new(digits:, rate_infos:)
23
+ end
24
+
25
+ private
26
+
27
+ attr_reader :period, :start, :symbol
28
+
29
+ def command = :getChartLastRequest
30
+
31
+ def arguments
32
+ {
33
+ info:
34
+ {
35
+ period:,
36
+ start:,
37
+ symbol:
38
+ }
39
+ }
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Xtb
4
+ module Http
5
+ # http://developers.xstore.pro/documentation/2.5.0#getChartRangeRequest
6
+ class ChartRangeRequest < Command
7
+ RateInfoRecord = Data.define(:close, :ctm, :ctm_string, :high, :low, :open, :vol)
8
+ ChartLastRequestResponse = Data.define(:digits, :rate_infos)
9
+
10
+ # @param end_time [Time] End of chart block
11
+ # @param period [Xtb::PERIODS] Period code
12
+ # @param start_time [Time] Start of chart block
13
+ # @param symbol [String|Symbol] Symbol
14
+ # @param ticks [Integer] (Optional) Number of ticks needed
15
+ def initialize(end_time, period, start_time, symbol, ticks: nil)
16
+ @end_time = end_time
17
+ @period = period
18
+ @start_time = start_time
19
+ @symbol = symbol
20
+ @ticks = ticks
21
+ end
22
+
23
+ def call
24
+ digits, rate_infos = super.values_at(:digits, :rate_infos)
25
+ rate_infos = rate_infos.map { |record| RateInfoRecord.new(**record) }
26
+ ChartLastRequestResponse.new(digits:, rate_infos:)
27
+ end
28
+
29
+ private
30
+
31
+ attr_reader :end_time, :period, :start_time, :symbol, :ticks
32
+
33
+ def command = :getChartRangeRequest
34
+
35
+ def arguments
36
+ {
37
+ info:
38
+ {
39
+ end: end_time,
40
+ period:,
41
+ start: start_time,
42
+ symbol:,
43
+ ticks:
44
+ }.compact
45
+ }
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'ssl_client'
4
+ require_relative '../errors'
5
+ require_relative '../request_queue'
6
+
7
+ module Xtb
8
+ module Http
9
+ class Client
10
+ include RequestQueue
11
+
12
+ class << self
13
+ def post(payload)
14
+ with_request_queue do
15
+ Xtb::Http::SslClient.request(payload)
16
+ end
17
+ # rescue NotLoggedInError => e
18
+ # return if command == :logout
19
+ #
20
+ # Rails.logger.debug(e)
21
+ #
22
+ # BrokerClients::Xtb::Login.call
23
+ # retry
24
+ # rescue AlreadyLoggedInError => _e
25
+ # # noop
26
+ end
27
+
28
+ def stream_session_id
29
+ @stream_session_id ||= BrokerClients::Xtb::Login.call.stream_session_id
30
+ end
31
+
32
+ private
33
+
34
+ def ssl_client
35
+ @ssl_client ||= Xtb::Http::SslClient.new
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,113 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'active_support/inflector'
5
+ require_relative '../errors'
6
+
7
+ module Xtb
8
+ module Http
9
+ # Base class for all commands.
10
+ class Command
11
+ Request = Struct.new(:command, :arguments) do
12
+ def to_json(*_args)
13
+ arguments = transform_keys(self.arguments) if self.arguments
14
+ JSON.dump({ command:, arguments: }.compact)
15
+ end
16
+
17
+ private
18
+
19
+ def transform_keys(hash)
20
+ hash.transform_keys! do |key|
21
+ key.to_s.camelize(:lower)
22
+ end
23
+
24
+ hash.transform_values do |value|
25
+ transform_keys(value) if value.is_a?(Hash)
26
+ value
27
+ end
28
+ end
29
+ end
30
+
31
+ Response = Struct.new(:command, :raw_response) do
32
+ def initialize(**args)
33
+ super
34
+
35
+ @response = parse
36
+ end
37
+
38
+ def success?
39
+ response[:status]
40
+ end
41
+
42
+ def return_data
43
+ response[:return_data]
44
+ end
45
+
46
+ def error_code
47
+ response[:error_code]
48
+ end
49
+
50
+ def error_description
51
+ response[:error_descr]
52
+ end
53
+
54
+ private
55
+
56
+ attr_reader :response
57
+
58
+ def parse
59
+ response = JSON.parse(raw_response)
60
+ transform_keys(response)
61
+ end
62
+
63
+ def transform_keys(hash)
64
+ hash.transform_keys! do |key|
65
+ key.underscore.to_sym
66
+ end
67
+
68
+ hash.transform_values do |value|
69
+ transform_keys(value) if value.is_a?(Hash)
70
+ value.each { |item| transform_keys(item) } if value.is_a?(Array)
71
+ value
72
+ end
73
+ end
74
+ end
75
+
76
+ def initialize(**args); end
77
+
78
+ def self.call(**args)
79
+ new(**args).call
80
+ end
81
+
82
+ def call
83
+ request_data = Request.new(command:, arguments:).to_json
84
+ raw_response = Client.post(request_data)
85
+ response = Response.new(command:, raw_response:)
86
+ raise_error(response.error_code, response.error_description) unless response.success?
87
+
88
+ response.return_data
89
+ end
90
+
91
+ private
92
+
93
+ def command
94
+ raise NotImplementedError
95
+ end
96
+
97
+ def arguments
98
+ nil
99
+ end
100
+
101
+ def raise_error(error_code, error_description)
102
+ case error_code
103
+ when 'BE103'
104
+ raise NotLoggedInError, "(#{error_code}) #{error_description}"
105
+ when 'BE118'
106
+ raise AlreadyLoggedInError, "(#{error_code}) #{error_description}"
107
+ else
108
+ raise "(#{error_code}) #{error_description}"
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Xtb
4
+ module Http
5
+ # http://developers.xstore.pro/documentation/2.5.0#getCommissionDef
6
+ class CommissionDef < Command
7
+ CommissionDefResponse = Data.define(:commission, :rate_of_exchange)
8
+
9
+ # @param symbol [String|Symbol]
10
+ # @param volume [Float]
11
+ def initialize(symbol, volume)
12
+ @symbol = symbol
13
+ @volume = volume
14
+ end
15
+
16
+ def call
17
+ CommissionDefResponse.new(**super)
18
+ end
19
+
20
+ private
21
+
22
+ attr_reader :symbol, :volume
23
+
24
+ def command = :getCommissionDef
25
+
26
+ def arguments
27
+ {
28
+ symbol:,
29
+ volume:
30
+ }
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Xtb
4
+ module Http
5
+ # http://developers.xstore.pro/documentation/2.5.0#getCurrentUserData
6
+ class CurrentUserData < Command
7
+ CurrentUserDataResponse = Data.define(:company_unit, :currency, :group, :ib_account, :leverage,
8
+ :leverage_multiplier, :spread_type, :trailing_stop)
9
+
10
+ def call
11
+ CurrentUserDataResponse.new(**super)
12
+ end
13
+
14
+ private
15
+
16
+ def command = :getCurrentUserData
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Xtb
4
+ module Http
5
+ # http://developers.xstore.pro/documentation/2.5.0#getMarginLevel
6
+ class MarginLevel < Command
7
+ MarginLevelResponse = Data.define(:balance, :credit, :currency, :equity, :margin, :margin_free, :margin_level)
8
+
9
+ def call
10
+ MarginLevelResponse.new(**super)
11
+ end
12
+
13
+ private
14
+
15
+ def command = :getMarginLevel
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Xtb
4
+ module Http
5
+ # http://developers.xstore.pro/documentation/2.5.0#getMarginTrade
6
+ class MarginTrade < Command
7
+ MarginTradeResponse = Data.define(:margin)
8
+
9
+ # @param symbol [String|Symbol]
10
+ # @param volume [Float]
11
+ def initialize(symbol, volume)
12
+ @symbol = symbol
13
+ @volume = volume
14
+ end
15
+
16
+ def call
17
+ MarginTradeResponse.new(**super)
18
+ end
19
+
20
+ private
21
+
22
+ attr_reader :symbol, :volume
23
+
24
+ def command = :getMarginTrade
25
+
26
+ def arguments
27
+ {
28
+ symbol:,
29
+ volume:
30
+ }
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Xtb
4
+ module Http
5
+ # http://developers.xstore.pro/documentation/2.5.0#getNews
6
+ class News < Command
7
+ NewsTopicRecord = Data.define(:body, :bodylen, :key, :time, :time_string, :title)
8
+
9
+ # @param end_time [Time]
10
+ # @param start_time [Time]
11
+ def initialize(end_time, start_time)
12
+ @end_time = end_time
13
+ @start_time = start_time
14
+ end
15
+
16
+ def call
17
+ super.map { |record| NewsTopicRecord.new(**record) }
18
+ end
19
+
20
+ private
21
+
22
+ attr_reader :end_time, :start_time
23
+
24
+ def command = :getNews
25
+
26
+ def arguments
27
+ {
28
+ end_time:,
29
+ start_time:
30
+ }
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Xtb
4
+ module Http
5
+ # http://developers.xstore.pro/documentation/2.5.0#getProfitCalculation
6
+ class ProfitCalculation < Command
7
+ ProfitCalculationResponse = Data.define(:profit)
8
+
9
+ # @param close_price [Float]
10
+ # @param cmd [Xtb::OPERATIONS]
11
+ # @param open_price [Float]
12
+ # @param symbol [String|Symbol]
13
+ # @param volume [Float]
14
+ def initialize(close_price, cmd, open_price, symbol, volume)
15
+ @close_price = close_price
16
+ @cmd = cmd
17
+ @open_price = open_price
18
+ @symbol = symbol
19
+ @volume = volume
20
+ end
21
+
22
+ def call
23
+ ProfitCalculationResponse.new(**super)
24
+ end
25
+
26
+ private
27
+
28
+ attr_reader :close_price, :cmd, :open_price, :symbol, :volume
29
+
30
+ def command = :getProfitCalculation
31
+
32
+ def arguments
33
+ {
34
+ close_price:,
35
+ cmd: Xtb::OPERATIONS[cmd],
36
+ open_price:,
37
+ symbol:,
38
+ volume:
39
+ }
40
+ end
41
+ end
42
+ end
43
+ end