Skip to content

Commit 5ef2c42

Browse files
committed
feat: get_converted_candles rest and websocket methods
1 parent d35cc8a commit 5ef2c42

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed

lib/cryptomarket/client.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,31 @@ def get_candles_by_symbol(symbol:, period: nil, sort: nil, from: nil, till: nil,
326326
)
327327
end
328328

329+
# Gets OHLCV data regarding the last price converted to the target currency for all symbols or for the specified symbols
330+
#
331+
# Candles are used for the representation of a specific symbol as an OHLC chart
332+
#
333+
# Conversion from the symbol quote currency to the target currency is the mean of "best" bid price and "best" ask price in the order book. If there is no "best" bid of ask price, the last price is returned.
334+
#
335+
# Requires no API key Access Rights
336+
#
337+
# https://api.exchange.cryptomkt.com/#candles
338+
#
339+
# +String+ +target_currency+:: Target currency for conversion
340+
# +String+ +period+:: Optional. A valid tick interval. 'M1' (one minute), 'M3', 'M5', 'M15', 'M30', 'H1' (one hour), 'H4', 'D1' (one day), 'D7', '1M' (one month). Default is 'M30'
341+
# +Array[String]+ +symbols+:: Optional. A list of symbols
342+
# +String+ +sort+:: Optional. Sort direction. 'ASC' or 'DESC'. Default is 'DESC'
343+
# +String+ +from+:: Optional. Initial value of the queried interval. As DateTime
344+
# +String+ +till+:: Optional. Last value of the queried interval. As DateTime
345+
# +Integer+ +limit+:: Optional. Prices per currency pair. Defaul is 100. Min is 1. Max is 1000
346+
347+
def get_converted_candles(target_currency:, period: nil, symbols: nil, sort: nil, from: nil, till: nil, limit: nil) # rubocop:disable Metrics/ParameterLists
348+
public_get(
349+
'public/converted/candles',
350+
{ target_currency: target_currency, symbols: symbols, period: period, sort: sort, from: from, till: till, limit: limit }
351+
)
352+
end
353+
329354
######################
330355
# Spot Trading calls #
331356
######################

lib/cryptomarket/websocket/market_data_client.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,29 @@ def subscribe_to_candles(callback:, period:, symbols:, limit: nil, result_callba
6060
intercept_result_callback(result_callback), params)
6161
end
6262

63+
# Gets OHLCV data regarding the last price converted to the target currency for all symbols or for the specified symbols
64+
#
65+
# Candles are used for the representation of a specific symbol as an OHLC chart
66+
#
67+
# Conversion from the symbol quote currency to the target currency is the mean of "best" bid price and "best" ask price in the order book. If there is no "best" bid of ask price, the last price is returned.
68+
#
69+
# Requires no API key Access Rights
70+
#
71+
# https://api.exchange.cryptomkt.com/#candles
72+
#
73+
# +String+ +target_currency+:: Target currency for conversion
74+
# +String+ +period+:: A valid tick interval. 'M1' (one minute), 'M3', 'M5', 'M15', 'M30', 'H1' (one hour), 'H4', 'D1' (one day), 'D7', '1M' (one month). Default is 'M30'
75+
# +Array[String]+ +symbols+:: Optional. A list of symbols
76+
# +String+ +from+:: Optional. Initial value of the queried interval. As DateTime
77+
# +String+ +till+:: Optional. Last value of the queried interval. As DateTime
78+
# +Integer+ +limit+:: Optional. Prices per currency pair. Defaul is 100. Min is 1. Max is 1000
79+
80+
def subscribe_to_converted_candles(callback:, target_currency:, period:, symbols:, limit: nil, result_callback: nil) # rubocop:disable Metrics/ParameterLists
81+
params = { 'target_currency' => target_currency, 'symbols' => symbols, 'limit' => limit }
82+
send_channel_subscription("converted/candles/#{period}", callback,
83+
intercept_result_callback(result_callback), params)
84+
end
85+
6386
# subscribe to a feed of mini tickers
6487
#
6588
# subscription is for all symbols or for the specified symbols

tests/rest/market_data.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
require_relative '../../lib/cryptomarket/client'
55
require_relative '../checks'
66

7-
class TestMarketDataMethods < Test::Unit::TestCase # rubocop:disable Style/Documentation
7+
class TestMarketDataMethods < Test::Unit::TestCase # rubocop:disable Style/Documentation,Metrics/ClassLength
88
def setup
99
@client = Cryptomarket::Client.new
1010
end
@@ -127,4 +127,13 @@ def test_get_candles_by_symbol
127127
assert(Check.good_candle(candle))
128128
end
129129
end
130+
131+
def test_get_converted_candles
132+
symbols = %w[EOSETH BTCUSDT]
133+
result = @client.get_converted_candles(symbols: symbols, limit: 2, target_currency: 'usdt')
134+
result['data'].each do |symbol, candles|
135+
assert(symbols.include?(symbol))
136+
candles.each { |val| assert(Check.good_candle(val)) }
137+
end
138+
end
130139
end

tests/websocket/market_data_client.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ def test_candles_subscriptions
4545
assert(veredict_checker.good_veredict?)
4646
end
4747

48+
def test_converted_candles_subscription
49+
veredict_checker = VeredictChecker.new
50+
@wsclient.subscribe_to_converted_candles(
51+
period: 'M1', target_currency: 'usdt', symbols: %w[eoseth ethbtc], limit: 2,
52+
callback: gen_check_notification_hash_list_callback(WSCheck.good_ws_public_candle, veredict_checker),
53+
result_callback: gen_result_callback(veredict_checker)
54+
)
55+
sleep(10 * @@SECOND)
56+
assert(veredict_checker.good_veredict?)
57+
end
58+
4859
def test_subscribe_to_mini_ticker
4960
veredict_checker = VeredictChecker.new
5061
@wsclient.subscribe_to_mini_ticker(

0 commit comments

Comments
 (0)