diff --git a/lib/xtb.rb b/lib/xtb.rb index 503d2bb..19e5f06 100644 --- a/lib/xtb.rb +++ b/lib/xtb.rb @@ -14,7 +14,7 @@ module Xtb h1: 60, h4: 240, d1: 1440, - w1: 10080, - mn1: 43200 + w1: 10_080, + mn1: 43_200 }.freeze end diff --git a/lib/xtb/http.rb b/lib/xtb/http.rb index c2dc03d..1316357 100644 --- a/lib/xtb/http.rb +++ b/lib/xtb/http.rb @@ -7,6 +7,7 @@ require_relative 'http/all_symbols' require_relative 'http/calendar' require_relative 'http/chart_last_request' +require_relative 'http/chart_range_request' require_relative 'http/current_user_data' require_relative 'http/margin_level' require_relative 'http/tick_prices' diff --git a/lib/xtb/http/chart_last_request.rb b/lib/xtb/http/chart_last_request.rb index 62db751..37f0e87 100644 --- a/lib/xtb/http/chart_last_request.rb +++ b/lib/xtb/http/chart_last_request.rb @@ -2,14 +2,14 @@ module Xtb module Http - # http://developers.xstore.pro/documentation/current#getCalendar + # http://developers.xstore.pro/documentation/current#getChartLastRequest class ChartLastRequest < Command RateInfoRecord = Data.define(:close, :ctm, :ctm_string, :high, :low, :open, :vol) ChartLastRequestResponse = Data.define(:digits, :rate_infos) # @param period [Xtb::Period] # @param start [Time] - # symbol [String|Symbol] + # @param symbol [String|Symbol] def initialize(period, start, symbol) @period = period @start = start @@ -29,7 +29,8 @@ def call def command = :getChartLastRequest def arguments - { info: + { + info: { period:, start:, diff --git a/lib/xtb/http/chart_range_request.rb b/lib/xtb/http/chart_range_request.rb new file mode 100644 index 0000000..7a6e1da --- /dev/null +++ b/lib/xtb/http/chart_range_request.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +module Xtb + module Http + # http://developers.xstore.pro/documentation/current#getChartRangeRequest + class ChartRangeRequest < Command + RateInfoRecord = Data.define(:close, :ctm, :ctm_string, :high, :low, :open, :vol) + ChartLastRequestResponse = Data.define(:digits, :rate_infos) + + # @param end_time [Time] End of chart block + # @param period [Xtb::Period] Period code + # @param start_time [Time] Start of chart block + # @param symbol [String|Symbol] Symbol + # @param ticks [Integer] (Optional) Number of ticks needed + def initialize(end_time, period, start_time, symbol, ticks: nil) + @end_time = end_time + @period = period + @start_time = start_time + @symbol = symbol + @ticks = ticks + end + + def call + digits, rate_infos = super.values_at(:digits, :rate_infos) + rate_infos = rate_infos.map { |record| RateInfoRecord.new(**record) } + ChartLastRequestResponse.new(digits:, rate_infos:) + end + + private + + attr_reader :end_time, :period, :start_time, :symbol, :ticks + + def command = :getChartRangeRequest + + def arguments + { + info: + { + end: end_time, + period:, + start: start_time, + symbol:, + ticks: + }.compact + } + end + end + end +end diff --git a/spec/xtb/http/chart_last_request_spec.rb b/spec/xtb/http/chart_last_request_spec.rb index b5bc7b9..fc273ee 100644 --- a/spec/xtb/http/chart_last_request_spec.rb +++ b/spec/xtb/http/chart_last_request_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe Xtb::Http::ChartLastRequest do subject(:command) { described_class.new(period, start, symbol) } @@ -26,11 +28,11 @@ 'rateInfos': [ { "close": 1.0, - "ctm": 1389362640000, - "ctmString": "Jan 10, 2014 3:04:00 PM", + "ctm": 1_389_362_640_000, + "ctmString": 'Jan 10, 2014 3:04:00 PM', "high": 6.0, "low": 0.0, - "open": 41848.0, + "open": 41_848.0, "vol": 0.0 } ] @@ -43,22 +45,23 @@ specify do expect(Xtb::Http::SslClient) .to receive(:request) - .with(JSON.dump(request)) - .and_return(response) + .with(JSON.dump(request)) + .and_return(response) expect(command.call) .to have_attributes( - digits: 4, - rate_infos: [ - have_attributes( - close: 1.0, - ctm: 1389362640000, - ctm_string: "Jan 10, 2014 3:04:00 PM", - high: 6.0, - low: 0.0, - open: 41848.0, - vol: 0.0) - ] + digits: 4, + rate_infos: [ + have_attributes( + close: 1.0, + ctm: 1_389_362_640_000, + ctm_string: 'Jan 10, 2014 3:04:00 PM', + high: 6.0, + low: 0.0, + open: 41_848.0, + vol: 0.0 ) + ] + ) end end -end \ No newline at end of file +end diff --git a/spec/xtb/http/chart_range_request_spec.rb b/spec/xtb/http/chart_range_request_spec.rb new file mode 100644 index 0000000..de431b2 --- /dev/null +++ b/spec/xtb/http/chart_range_request_spec.rb @@ -0,0 +1,112 @@ +# frozen_string_literal: true + +RSpec.describe Xtb::Http::ChartRangeRequest do + subject(:command) { described_class.new(end_time, period, start_time, symbol) } + + let(:end_time) { 1_272_529_161_605 } + let(:period) { Xtb::Period[:m5] } + let(:start_time) { 1_272_529_161_605 } + let(:symbol) { 'KOMB.CZ' } + + let(:request) do + { + command: :getChartRangeRequest, + arguments: { + info: { + end: end_time, + period: 5, + start: start_time, + symbol: symbol + } + } + } + end + let(:response) do + JSON.dump( + { + 'status': true, + 'return_data': { + 'digits': 4, + 'rateInfos': [ + { + "close": 1.0, + "ctm": 1_389_362_640_000, + "ctmString": 'Jan 10, 2014 3:04:00 PM', + "high": 6.0, + "low": 0.0, + "open": 41_848.0, + "vol": 0.0 + } + ] + } + } + ) + end + + describe '#call' do + specify do + expect(Xtb::Http::SslClient) + .to receive(:request) + .with(JSON.dump(request)) + .and_return(response) + expect(command.call) + .to have_attributes( + digits: 4, + rate_infos: [ + have_attributes( + close: 1.0, + ctm: 1_389_362_640_000, + ctm_string: 'Jan 10, 2014 3:04:00 PM', + high: 6.0, + low: 0.0, + open: 41_848.0, + vol: 0.0 + ) + ] + ) + end + + context 'with ticks argument' do + subject(:command) { described_class.new(end_time, period, start_time, symbol, ticks:) } + + let(:ticks) { 100 } + + let(:request) do + { + command: :getChartRangeRequest, + arguments: { + info: { + end: end_time, + period: 5, + start: start_time, + symbol: symbol, + ticks: + } + } + } + end + + specify do + expect(Xtb::Http::SslClient) + .to receive(:request) + .with(JSON.dump(request)) + .and_return(response) + expect(command.call) + .to have_attributes( + digits: 4, + rate_infos: [ + have_attributes( + close: 1.0, + ctm: 1_389_362_640_000, + ctm_string: 'Jan 10, 2014 3:04:00 PM', + high: 6.0, + low: 0.0, + open: 41_848.0, + vol: 0.0 + ) + ] + ) + end + end + end +end diff --git a/spec/xtb/http/current_user_data_spec.rb b/spec/xtb/http/current_user_data_spec.rb index d09f078..a61aabb 100644 --- a/spec/xtb/http/current_user_data_spec.rb +++ b/spec/xtb/http/current_user_data_spec.rb @@ -29,15 +29,15 @@ .and_return(response) expect(command.call) .to have_attributes( - company_unit: 8, - currency: 'PLN', - group: 'demoPLeurSTANDARD200', - ib_account: false, - leverage: 1, - leverage_multiplier: 0.25, - spread_type: 'FLOAT', - trailing_stop: false - ) + company_unit: 8, + currency: 'PLN', + group: 'demoPLeurSTANDARD200', + ib_account: false, + leverage: 1, + leverage_multiplier: 0.25, + spread_type: 'FLOAT', + trailing_stop: false + ) end end end diff --git a/spec/xtb/http/margin_level_spec.rb b/spec/xtb/http/margin_level_spec.rb index b9c6516..12a6d48 100644 --- a/spec/xtb/http/margin_level_spec.rb +++ b/spec/xtb/http/margin_level_spec.rb @@ -8,13 +8,13 @@ { 'status': true, 'returnData': { - 'balance': 995800269.43, + 'balance': 995_800_269.43, 'credit': 1000.00, 'currency': 'PLN', - 'equity': 995985397.56, - 'margin': 572634.43, - 'margin_free': 995227635.00, - 'margin_level': 173930.41 + 'equity': 995_985_397.56, + 'margin': 572_634.43, + 'margin_free': 995_227_635.00, + 'margin_level': 173_930.41 } } ) @@ -32,14 +32,14 @@ .and_return(response) expect(command.call) .to have_attributes( - balance: 995800269.43, - credit: 1000.00, - currency: 'PLN', - equity: 995985397.56, - margin: 572634.43, - margin_free: 995227635.00, - margin_level: 173930.41 - ) + balance: 995_800_269.43, + credit: 1000.00, + currency: 'PLN', + equity: 995_985_397.56, + margin: 572_634.43, + margin_free: 995_227_635.00, + margin_level: 173_930.41 + ) end end end diff --git a/spec/xtb/http/tick_prices.rb b/spec/xtb/http/tick_prices.rb index 8b48670..99378e3 100644 --- a/spec/xtb/http/tick_prices.rb +++ b/spec/xtb/http/tick_prices.rb @@ -25,16 +25,16 @@ 'quotations': [ { 'ask': 4000.0, - 'askVolume': 15000, + 'askVolume': 15_000, 'bid': 4000.0, - 'bidVolume': 16000, + 'bidVolume': 16_000, 'high': 4000.0, 'level': 0, 'low': 3500.0, 'spreadRaw': 0.000003, 'spreadTable': 0.00042, 'symbol': 'KOMB.CZ', - 'timestamp': 1272529161605 + 'timestamp': 1_272_529_161_605 } ] } @@ -50,18 +50,18 @@ .and_return(response) expect(command.call.last) .to have_attributes( - ask: 4000.0, - ask_volume: 15000, - bid: 4000.0, - bid_volume: 16000, - high: 4000.0, - level: 0, - low: 3500.0, - spread_raw: 0.000003, - spread_table: 0.00042, - symbol: 'KOMB.CZ', - timestamp: 1_272_529_161_605 - ) + ask: 4000.0, + ask_volume: 15_000, + bid: 4000.0, + bid_volume: 16_000, + high: 4000.0, + level: 0, + low: 3500.0, + spread_raw: 0.000003, + spread_table: 0.00042, + symbol: 'KOMB.CZ', + timestamp: 1_272_529_161_605 + ) end end end