Skip to content

Commit

Permalink
Add getChartRangeRequest command
Browse files Browse the repository at this point in the history
  • Loading branch information
jacekmaciag committed Oct 20, 2024
1 parent af7122c commit 3834570
Show file tree
Hide file tree
Showing 9 changed files with 225 additions and 59 deletions.
4 changes: 2 additions & 2 deletions lib/xtb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Xtb
h1: 60,
h4: 240,
d1: 1440,
w1: 10080,
mn1: 43200
w1: 10_080,
mn1: 43_200
}.freeze
end
1 change: 1 addition & 0 deletions lib/xtb/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
7 changes: 4 additions & 3 deletions lib/xtb/http/chart_last_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,7 +29,8 @@ def call
def command = :getChartLastRequest

def arguments
{ info:
{
info:
{
period:,
start:,
Expand Down
49 changes: 49 additions & 0 deletions lib/xtb/http/chart_range_request.rb
Original file line number Diff line number Diff line change
@@ -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
37 changes: 20 additions & 17 deletions spec/xtb/http/chart_last_request_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

RSpec.describe Xtb::Http::ChartLastRequest do
subject(:command) { described_class.new(period, start, symbol) }

Expand Down Expand Up @@ -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
}
]
Expand All @@ -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
end
112 changes: 112 additions & 0 deletions spec/xtb/http/chart_range_request_spec.rb
Original file line number Diff line number Diff line change
@@ -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
18 changes: 9 additions & 9 deletions spec/xtb/http/current_user_data_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 13 additions & 13 deletions spec/xtb/http/margin_level_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
)
Expand All @@ -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
30 changes: 15 additions & 15 deletions spec/xtb/http/tick_prices.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
]
}
Expand All @@ -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

0 comments on commit 3834570

Please sign in to comment.