Skip to content

Commit

Permalink
feat: get converted candles by symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
Insmael committed Mar 1, 2024
1 parent 5ef2c42 commit 140d0ee
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 28 deletions.
70 changes: 50 additions & 20 deletions lib/cryptomarket/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def get_prices(to:, from: nil)
# +String+ +sort+:: Optional. Sort direction. 'ASC' or 'DESC'. Default is 'DESC'
# +String+ +since+:: Optional. Initial value of the queried interval
# +String+ +until+:: Optional. Last value of the queried interval
# +Integer+ +limit+:: Optional. Prices per currency pair. Defaul is 1. Min is 1. Max is 1000
# +Integer+ +limit+:: Optional. Prices per currency pair. Defaul is 1. Min is 1. Max is 1_000

def get_price_history(to:, from: nil, till: nil, since: nil, limit: nil, period: nil, sort: nil) # rubocop:disable Metrics/ParameterLists
public_get(
Expand Down Expand Up @@ -202,7 +202,7 @@ def get_ticker_price(symbol:)
# +String+ +sort+:: Optional. Sort direction. 'ASC' or 'DESC'. Default is 'DESC'
# +String+ +since+:: Optional. Initial value of the queried interval
# +String+ +until+:: Optional. Last value of the queried interval
# +Integer+ +limit+:: Optional. Prices per currency pair. Defaul is 10. Min is 1. Max is 1000
# +Integer+ +limit+:: Optional. Prices per currency pair. Defaul is 10. Min is 1. Max is 1_000

def get_trades(symbols: nil, by: nil, sort: nil, from: nil, till: nil, limit: nil, offset: nil) # rubocop:disable Metrics/ParameterLists
public_get(
Expand All @@ -224,8 +224,8 @@ def get_trades(symbols: nil, by: nil, sort: nil, from: nil, till: nil, limit: ni
# +String+ +sort+:: Optional. Sort direction. 'ASC' or 'DESC'. Default is 'DESC'
# +String+ +since+:: Optional. Initial value of the queried interval
# +String+ +until+:: Optional. Last value of the queried interval
# +Integer+ +limit+:: Optional. Prices per currency pair. Defaul is 10. Min is 1. Max is 1000
# +Integer+ +offset+:: Optional. Default is 0. Min is 0. Max is 100000
# +Integer+ +limit+:: Optional. Prices per currency pair. Defaul is 10. Min is 1. Max is 1_000
# +Integer+ +offset+:: Optional. Default is 0. Min is 0. Max is 100_000

def get_trades_by_symbol(symbol: nil, by: nil, sort: nil, from: nil, till: nil, limit: nil, offset: nil) # rubocop:disable Metrics/ParameterLists
public_get(
Expand Down Expand Up @@ -280,7 +280,7 @@ def get_orderbook_volume(symbol:, volume: nil)
end

# Get a Hash of candles for all symbols or for specified symbols
# Candels are used for OHLC representation
# Candles are used for OHLC representation
# The result contains candles with non-zero volume only (no trades = no candles)
#
# Requires no API key Access Rights
Expand All @@ -293,7 +293,7 @@ def get_orderbook_volume(symbol:, volume: nil)
# +String+ +sort+:: Optional. Sort direction. 'ASC' or 'DESC'. Default is 'DESC'
# +String+ +from+:: Optional. Initial value of the queried interval. As DateTime
# +String+ +till+:: Optional. Last value of the queried interval. As DateTime
# +Integer+ +limit+:: Optional. Prices per currency pair. Defaul is 10. Min is 1. Max is 1000
# +Integer+ +limit+:: Optional. Prices per currency pair. Defaul is 10. Min is 1. Max is 1_000

def get_candles(symbols: nil, period: nil, sort: nil, from: nil, till: nil, limit: nil, offset: nil) # rubocop:disable Metrics/ParameterLists
public_get(
Expand All @@ -303,7 +303,7 @@ def get_candles(symbols: nil, period: nil, sort: nil, from: nil, till: nil, limi
end

# Get candles of a symbol
# Candels are used for OHLC representation
# Candles are used for OHLC representation
# The result contains candles with non-zero volume only (no trades = no candles)
#
# Requires no API key Access Rights
Expand All @@ -316,8 +316,8 @@ def get_candles(symbols: nil, period: nil, sort: nil, from: nil, till: nil, limi
# +String+ +sort+:: Optional. Sort direction. 'ASC' or 'DESC'. Default is 'DESC'
# +String+ +from+:: Optional. Initial value of the queried interval. As DateTime
# +String+ +till+:: Optional. Last value of the queried interval. As DateTime
# +Integer+ +limit+:: Optional. Prices per currency pair. Defaul is 100. Min is 1. Max is 1000
# +Integer+ +offset+:: Optional. Default is 0. Min is 0. Max is 100000
# +Integer+ +limit+:: Optional. Prices per currency pair. Defaul is 100. Min is 1. Max is 1_000
# +Integer+ +offset+:: Optional. Default is 0. Min is 0. Max is 100_000

def get_candles_by_symbol(symbol:, period: nil, sort: nil, from: nil, till: nil, limit: nil, offset: nil) # rubocop:disable Metrics/ParameterLists
public_get(
Expand All @@ -328,7 +328,9 @@ def get_candles_by_symbol(symbol:, period: nil, sort: nil, from: nil, till: nil,

# Gets OHLCV data regarding the last price converted to the target currency for all symbols or for the specified symbols
#
# Candles are used for the representation of a specific symbol as an OHLC chart
# Candles are used for OHLC representation
#
# The result contains candles with non-zero volume only (no trades = no candles)
#
# 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.
#
Expand All @@ -337,20 +339,48 @@ def get_candles_by_symbol(symbol:, period: nil, sort: nil, from: nil, till: nil,
# https://api.exchange.cryptomkt.com/#candles
#
# +String+ +target_currency+:: Target currency for conversion
# +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'
# +Array[String]+ +symbols+:: Optional. A list of symbols
# +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'
# +String+ +sort+:: Optional. Sort direction. 'ASC' or 'DESC'. Default is 'DESC'
# +String+ +from+:: Optional. Initial value of the queried interval. As DateTime
# +String+ +till+:: Optional. Last value of the queried interval. As DateTime
# +Integer+ +limit+:: Optional. Prices per currency pair. Defaul is 100. Min is 1. Max is 1000
# +Integer+ +limit+:: Optional. Prices per currency pair. Defaul is 100. Min is 1. Max is 1_000

def get_converted_candles(target_currency:, period: nil, symbols: nil, sort: nil, from: nil, till: nil, limit: nil) # rubocop:disable Metrics/ParameterLists
def get_converted_candles(target_currency:, symbols: nil, period: nil, sort: nil, from: nil, till: nil, limit: nil) # rubocop:disable Metrics/ParameterLists
public_get(
'public/converted/candles',
{ target_currency: target_currency, symbols: symbols, period: period, sort: sort, from: from, till: till, limit: limit }
)
end

# Gets OHLCV data regarding the last price converted to the target currency for the specified symbol
#
# Candles are used for OHLC representation
#
# The result contains candles with non-zero volume only (no trades = no candles)
#
# 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.
#
# Requires no API key Access Rights
#
# https://api.exchange.cryptomkt.com/#candles
#
# +String+ +target_currency+:: Target currency for conversion
# +String+ +symbol+:: A symbol id
# +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'
# +String+ +sort+:: Optional. Sort direction. 'ASC' or 'DESC'. Default is 'DESC'
# +String+ +from+:: Optional. Initial value of the queried interval. As DateTime
# +String+ +till+:: Optional. Last value of the queried interval. As DateTime
# +Integer+ +limit+:: Optional. Prices per currency pair. Defaul is 100. Min is 1. Max is 1_000
# +Integer+ +offset+:: Optional. Default is 0. Min is 0. Max is 100_000

def get_converted_candles_by_symbol(target_currency:, symbol:, period: nil, sort: nil, from: nil, till: nil, limit: nil, offset: nil) # rubocop:disable Metrics/ParameterLists
public_get(
"public/converted/candles/#{symbol}",
{ target_currency: target_currency, period: period, sort: sort, from: from, till: till, limit: limit, offset: offset }
)
end

######################
# Spot Trading calls #
######################
Expand Down Expand Up @@ -592,8 +622,8 @@ def get_trading_commission(symbol:)
# +String+ +sort+:: Optional. Sort direction. 'ASC' or 'DESC'. Default is 'DESC'
# +String+ +from+:: Optional. Initial value of the queried interval
# +String+ +till+:: Optional. Last value of the queried interval
# +Integer+ +limit+:: Optional. Prices per currency pair. Defaul is 100. Max is 1000
# +Integer+ +offset+:: Optional. Default is 0. Max is 100000
# +Integer+ +limit+:: Optional. Prices per currency pair. Defaul is 100. Max is 1_000
# +Integer+ +offset+:: Optional. Default is 0. Max is 100_000

def get_spot_orders_history( # rubocop:disable Metrics/ParameterLists
client_order_id: nil, symbol: nil, sort: nil, by: nil, from: nil,
Expand All @@ -619,8 +649,8 @@ def get_spot_orders_history( # rubocop:disable Metrics/ParameterLists
# +String+ +sort+:: Optional. Sort direction. 'ASC' or 'DESC'. Default is 'DESC'
# +String+ +from+:: Optional. Initial value of the queried interval
# +String+ +till+:: Optional. Last value of the queried interval
# +Integer+ +limit+:: Optional. Prices per currency pair. Defaul is 100. Max is 1000
# +Integer+ +offset+:: Optional. Default is 0. Max is 100000
# +Integer+ +limit+:: Optional. Prices per currency pair. Defaul is 100. Max is 1_000
# +Integer+ +offset+:: Optional. Default is 0. Max is 100_000

def get_spot_trades_history( # rubocop:disable Metrics/ParameterLists
order_id: nil, symbol: nil, sort: nil, by: nil, from: nil,
Expand Down Expand Up @@ -931,8 +961,8 @@ def transfer_money_to_another_user(currency:, amount:, by:, identifier:)
# +String+ +id_from+:: Optional. Interval initial value when ordering by id. Min is 0
# +String+ +id_till+:: Optional. Interval end value when ordering by id. Min is 0
# +String+ +sort+:: Optional. Sort direction. 'ASC' or 'DESC'. Default is 'DESC'
# +Integer+ +limit+:: Optional. Transactions per query. Defaul is 100. Max is 1000
# +Integer+ +offset+:: Optional. Default is 0. Max is 100000
# +Integer+ +limit+:: Optional. Transactions per query. Defaul is 100. Max is 1_000
# +Integer+ +offset+:: Optional. Default is 0. Max is 100_000
# +bool+ +group_transactions+:: Optional. Flag indicating whether the returned transactions will be parts of a single operation. Default is false

def get_transaction_history( # rubocop:disable Metrics/ParameterLists
Expand Down Expand Up @@ -996,7 +1026,7 @@ def offchain_available?(
# ==== Params
# +String+ +currency+:: Optional. Currency code
# +bool+ +active+:: Optional. value showing whether the lock is active
# +Integer+ +limit+:: Optional. Dafault is 100. Min is 0. Max is 1000
# +Integer+ +limit+:: Optional. Dafault is 100. Min is 0. Max is 1_000
# +Integer+ +offset+:: Optional. Default is 0. Min is 0
# +String+ +from+:: Optional. Interval initial value. As Datetime
# +String+ +till+:: Optional. Interval end value. As Datetime
Expand Down
2 changes: 1 addition & 1 deletion lib/cryptomarket/websocket/auth_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def wait_authed
# err is None for successful calls, result is None for calls with error: Proc.new {|err, result| ...}

def authenticate(callback = nil)
timestamp = Time.now.to_i * 1000
timestamp = Time.now.to_i * 1_000
digest = OpenSSL::Digest.new 'sha256'
message = timestamp.to_s
message += @window.to_s unless @window.nil?
Expand Down
12 changes: 7 additions & 5 deletions lib/cryptomarket/websocket/market_data_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class MarketDataClient < MarketDataClientCore
# ==== Params
# +Proc+ +callback+:: A +Proc+ that recieves notifications as a hash of trades indexed by symbol, and the type of notification (either 'snapshot' or 'update')
# +Array[String]+ +symbols+:: A list of symbol ids
# +Integer+ +limit+:: Number of historical entries returned in the first feed. Min is 0. Max is 1000. Default is 0
# +Integer+ +limit+:: Number of historical entries returned in the first feed. Min is 0. Max is 1_000. Default is 0
# +Proc+ +result_callback+:: Optional. A +Proc+ of two arguments, An exception and a result, called either with the exception or with the result, a list of subscribed symbols

def subscribe_to_trades(callback:, symbols:, limit: nil, result_callback: nil)
Expand All @@ -51,7 +51,7 @@ def subscribe_to_trades(callback:, symbols:, limit: nil, result_callback: nil)
# +Proc+ +callback+:: A +Proc+ that recieves notifications as a hash of candles indexed by symbol, and the type of notification (either 'snapshot' or 'update')
# +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'
# +Array[String]+ +symbols+:: Optional. A list of symbol ids
# +Integer+ +limit+:: Number of historical entries returned in the first feed. Min is 0. Max is 1000. Default is 0
# +Integer+ +limit+:: Number of historical entries returned in the first feed. Min is 0. Max is 1_000. Default is 0
# +Proc+ +result_callback+:: Optional. A +Proc+ called with a list of subscribed symbols

def subscribe_to_candles(callback:, period:, symbols:, limit: nil, result_callback: nil)
Expand All @@ -70,14 +70,16 @@ def subscribe_to_candles(callback:, period:, symbols:, limit: nil, result_callba
#
# https://api.exchange.cryptomkt.com/#candles
#
# +Proc+ +callback+:: A +Proc+ that recieves notifications as a hash of candles indexed by symbol, and the type of notification (either 'snapshot' or 'update')
# +String+ +target_currency+:: Target currency for conversion
# +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'
# +Array[String]+ +symbols+:: Optional. A list of symbols
# +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'
# +String+ +from+:: Optional. Initial value of the queried interval. As DateTime
# +String+ +till+:: Optional. Last value of the queried interval. As DateTime
# +Integer+ +limit+:: Optional. Prices per currency pair. Defaul is 100. Min is 1. Max is 1000
# +Integer+ +limit+:: Optional. Prices per currency pair. Defaul is 100. Min is 1. Max is 1_000
# +Proc+ +result_callback+:: Optional. A +Proc+ called with a list of subscribed symbols

def subscribe_to_converted_candles(callback:, target_currency:, period:, symbols:, limit: nil, result_callback: nil) # rubocop:disable Metrics/ParameterLists
def subscribe_to_converted_candles(callback:, target_currency:, symbols:, period:, limit: nil, result_callback: nil) # rubocop:disable Metrics/ParameterLists
params = { 'target_currency' => target_currency, 'symbols' => symbols, 'limit' => limit }
send_channel_subscription("converted/candles/#{period}", callback,
intercept_result_callback(result_callback), params)
Expand Down
4 changes: 2 additions & 2 deletions lib/cryptomarket/websocket/wallet_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ def get_wallet_balance(currency:, callback:)
# +String+ +id_till+:: Optional. Interval end value when ordering by id. Min is 0
# +String+ +order_by+:: Optional. sorting parameter.'created_at' or 'id'. Default is 'created_at'
# +String+ +sort+:: Optional. Sort direction. 'ASC' or 'DESC'. Default is 'DESC'
# +Integer+ +limit+:: Optional. Transactions per query. Defaul is 100. Max is 1000
# +Integer+ +offset+:: Optional. Default is 0. Max is 100000
# +Integer+ +limit+:: Optional. Transactions per query. Defaul is 100. Max is 1_000
# +Integer+ +offset+:: Optional. Default is 0. Max is 100_000
# +bool+ +group_transactions+:: Optional. Flag indicating whether the returned transactions will be parts of a single operation. Default is false

def get_transactions( # rubocop:disable Metrics/ParameterLists
Expand Down
5 changes: 5 additions & 0 deletions tests/rest/market_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,9 @@ def test_get_converted_candles
candles.each { |val| assert(Check.good_candle(val)) }
end
end

def test_get_converted_candles_by_symbol
result = @client.get_converted_candles_by_symbol(symbol: 'eoseth', limit: 2, target_currency: 'usdt')
result['data'].each { |val| assert(Check.good_candle(val)) }
end
end

0 comments on commit 140d0ee

Please sign in to comment.