Class: Cryptomarket::Websocket::TradingClient
- Inherits:
-
AuthClient
- Object
- ClientBase
- AuthClient
- Cryptomarket::Websocket::TradingClient
- Defined in:
- lib/cryptomarket/websocket/trading_client.rb
Overview
TradingClient connects via websocket to cryptomarket to enable the user to manage orders. uses SHA256 as auth method and authenticates automatically after ceonnection.
Instance Method Summary collapse
- #build_subscription_hash ⇒ Object
-
#cancel_spot_order(client_order_id:, callback: nil) ⇒ Object
cancels a spot order.
-
#cancel_spot_orders(callback: nil) ⇒ Object
cancel all active spot orders and returns the ones that could not be canceled.
-
#create_spot_order(symbol:, side:, quantity:, client_order_id: nil, type: nil, time_in_force: nil, price: nil, stop_price: nil, expire_time: nil, strict_validate: nil, post_only: nil, take_rate: nil, make_rate: nil, callback: nil) ⇒ Object
Creates a new spot order.
-
#create_spot_order_list(orders:, contingency_type:, order_list_id: nil, callback: nil) ⇒ Object
creates a list of spot orders.
-
#get_active_spot_orders(callback:) ⇒ Object
Get the user’s active spot orders.
-
#get_spot_commission(symbol:, callback:) ⇒ Object
(also: #get_spot_commission_of_symbol, #get_spot_commission_by_symbol)
Get the personal trading commission rate of a symbol.
-
#get_spot_commissions(callback:) ⇒ Object
Get the personal trading commission rates for all symbols.
-
#get_spot_trading_balance(currency:, callback:) ⇒ Object
(also: #get_spot_trading_balance_of_currency, #get_spot_trading_balance_by_currency)
Get the user spot trading balance of a currency.
-
#get_spot_trading_balances(callback:) ⇒ Object
Get the user’s spot trading balance for all currencies with balance.
-
#initialize(api_key:, api_secret:, window: nil) ⇒ TradingClient
constructor
- Creates a new client ==== Params
stringapi_key - the user api key
stringapi_secret - the user api secret
Integerwindow -
Maximum difference between the creation of the request and the moment of request processing in milliseconds.
- the user api secret
- the user api key
- Creates a new client ==== Params
-
#replace_spot_order(client_order_id:, new_client_order_id:, quantity:, price:, stop_price: nil, strict_validate: nil, callback: nil) ⇒ Object
changes the parameters of an existing order, quantity or price.
-
#subscribe_to_reports(callback:, result_callback: nil) ⇒ Object
subscribe to a feed of execution reports of the user’s orders.
-
#subscribe_to_spot_balance(callback:, mode: nil, result_callback: nil) ⇒ Object
subscribe to a feed of the user’s spot balances.
-
#unsubscribe_to_reports(callback: nil) ⇒ Object
stop recieveing the report feed subscription.
-
#unsubscribe_to_spot_balance(result_callback: nil) ⇒ Object
stop recieving the feed of balances changes.
Methods inherited from AuthClient
#authenticate, #build_auth_payload, #connect, #connected?, #wait_authed
Methods inherited from ClientBase
#close, #connect, #connected?, #get_callback_for_response, #handle, #handle_good_response, #handle_notification, #handle_response, #on_close, #on_close=, #on_connect, #on_connect=, #on_error, #on_error=, #on_open, #request, #send_subscription, #send_unsubscription, #store_callback_and_send
Constructor Details
#initialize(api_key:, api_secret:, window: nil) ⇒ TradingClient
Creates a new client
Params
stringapi_key-
the user api key
stringapi_secret-
the user api secret
Integerwindow-
Maximum difference between the creation of the request and the moment of request processing in milliseconds. Max is 60_000. Defaul is 10_000
17 18 19 20 21 22 23 24 |
# File 'lib/cryptomarket/websocket/trading_client.rb', line 17 def initialize(api_key:, api_secret:, window: nil) super( url: 'wss://api.exchange.cryptomkt.com/api/3/ws/trading', api_key:, api_secret:, window:, subscription_keys: build_subscription_hash) end |
Instance Method Details
#build_subscription_hash ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/cryptomarket/websocket/trading_client.rb', line 26 def build_subscription_hash reports = 'reports' balances = 'balances' { 'spot_subscribe' => [reports, Args::NotificationType::COMMAND], 'spot_unsubscribe' => [reports, Args::NotificationType::COMMAND], 'spot_orders' => [reports, Args::NotificationType::SNAPSHOT], 'spot_order' => [reports, Args::NotificationType::UPDATE], 'spot_balance_subscribe' => [balances, Args::NotificationType::COMMAND], 'spot_balance_unsubscribe' => [balances, Args::NotificationType::COMMAND], 'spot_balance' => [balances, Args::NotificationType::SNAPSHOT] } end |
#cancel_spot_order(client_order_id:, callback: nil) ⇒ Object
cancels a spot order
api.exchange.cryptomkt.com/#cancel-spot-order-2
Params
Stringclient_order_id-
the client order id of the order to cancel
Proccallback-
Optional. A
Procof two arguments, An exception and a result, called either with the exception or with the result, a list of reports of the canceled orders
189 190 191 |
# File 'lib/cryptomarket/websocket/trading_client.rb', line 189 def cancel_spot_order(client_order_id:, callback: nil) request('spot_cancel_order', callback, { client_order_id: }) end |
#cancel_spot_orders(callback: nil) ⇒ Object
cancel all active spot orders and returns the ones that could not be canceled
api.exchange.cryptomkt.com/#cancel-spot-orders
Params
Proccallback-
A
Procof two arguments, An exception and a result, called either with the exception or with the result, a report of the canceled order
199 200 201 |
# File 'lib/cryptomarket/websocket/trading_client.rb', line 199 def cancel_spot_orders(callback: nil) request('spot_cancel_orders', callback) end |
#create_spot_order(symbol:, side:, quantity:, client_order_id: nil, type: nil, time_in_force: nil, price: nil, stop_price: nil, expire_time: nil, strict_validate: nil, post_only: nil, take_rate: nil, make_rate: nil, callback: nil) ⇒ Object
Creates a new spot order
For fee, for price accuracy and quantity, and for order status information see the api docs at api.exchange.cryptomkt.com/#create-new-spot-order
api.exchange.cryptomkt.com/#place-new-spot-order
Params
Stringsymbol-
Trading symbol
Stringside-
Either ‘buy’ or ‘sell’
Stringquantity-
Order quantity
Stringclient_order_id-
Optional. If given must be unique within the trading day, including all active orders. If not given, is generated by the server
Stringtype-
Optional. ‘limit’, ‘market’, ‘stopLimit’, ‘stopMarket’, ‘takeProfitLimit’ or ‘takeProfitMarket’. Default is ‘limit’
Stringtime_in_force-
Optional. ‘GTC’, ‘IOC’, ‘FOK’, ‘Day’, ‘GTD’. Default to ‘GTC’
Stringprice-
Optional. Required for ‘limit’ and ‘stopLimit’. limit price of the order
Stringstop_price-
Optional. Required for ‘stopLimit’ and ‘stopMarket’ orders. stop price of the order
Stringexpire_time-
Optional. Required for orders with timeInForce = GDT
Boolstrict_validate-
Optional. If False, the server rounds half down for tickerSize and quantityIncrement. Example of ETHBTC: tickSize = ‘0.000001’, then price ‘0.046016’ is valid, ‘0.0460165’ is invalid
boolpost_only-
Optional. If True, your post_only order causes a match with a pre-existing order as a taker, then the order will be cancelled
Stringtake_rate-
Optional. Liquidity taker fee, a fraction of order volume, such as 0.001 (for 0.1% fee). Can only increase the fee. Used for fee markup.
Stringmake_rate-
Optional. Liquidity provider fee, a fraction of order volume, such as 0.001 (for 0.1% fee). Can only increase the fee. Used for fee markup.
Proccallback-
Optional. A
Procof two arguments, An exception and a result, called either with the exception or with the result, the report of the created order
127 128 129 130 131 132 133 134 135 |
# File 'lib/cryptomarket/websocket/trading_client.rb', line 127 def create_spot_order( # rubocop:disable Metrics/ParameterLists symbol:, side:, quantity:, client_order_id: nil, type: nil, time_in_force: nil, price: nil, stop_price: nil, expire_time: nil, strict_validate: nil, post_only: nil, take_rate: nil, make_rate: nil, callback: nil ) request('spot_new_order', callback, { client_order_id:, symbol:, side:, quantity:, type:, time_in_force:, price:, stop_price:, expire_time:, strict_validate:, post_only:, take_rate:, make_rate: }) end |
#create_spot_order_list(orders:, contingency_type:, order_list_id: nil, callback: nil) ⇒ Object
creates a list of spot orders
Types or contingency
-
‘allOrNone’ (AON)
-
‘oneCancelAnother’ (OCO)
-
‘oneTriggerOther’ (OTO)
-
‘oneTriggerOneCancelOther’ (OTOCO)
Restriction in the number of orders:
-
An AON list must have 2 or 3 orders
-
An OCO list must have 2 or 3 orders
-
An OTO list must have 2 or 3 orders
-
An OTOCO must have 3 or 4 orders
Symbol restrictions
-
For an AON order list, the symbol code of orders must be unique for each order in the list.
-
For an OCO order list, there are no symbol code restrictions.
-
For an OTO order list, there are no symbol code restrictions.
-
For an OTOCO order list, the symbol code of orders must be the same for all orders in the list (placing orders in different order books is not supported).
OrderType restrictions
-
For an AON order list, orders must be ‘limit’ or ‘market’
-
For an OCO order list, orders must be ‘limit’, ‘stopLimit’, ‘stopMarket’, takeProfitLimit or takeProfitMarket.
-
An OCO order list cannot include more than one limit order (the same
applies to secondary orders in an OTOCO order list).
-
For OTO order list, there are no order type restrictions.
-
For an OTOCO order list, the first order must be ‘limit’, ‘market’, ‘stopLimit’, ‘stopMarket’, takeProfitLimit or takeProfitMarket.
-
For an OTOCO order list, the secondary orders have the same restrictions as an OCO order
-
Default is ‘limit’
api.exchange.cryptomkt.com/#create-new-spot-order-list
Params
Stringorder_list_id-
order list identifier. If ommited, it will be generated by the system. Must be equal to the client order id of the first order in the request
Stringcontingency_type-
order list type. ‘allOrNone’, ‘oneCancelOther’ or ‘oneTriggerOneCancelOther’
Array[]orders-
the list of orders. aech order in the list has the same parameters of a new spot order
173 174 175 176 177 178 179 180 |
# File 'lib/cryptomarket/websocket/trading_client.rb', line 173 def create_spot_order_list( orders:, contingency_type:, order_list_id: nil, callback: nil ) request('spot_new_order_list', callback, { orders:, contingency_type:, order_list_id: }, orders.count) end |
#get_active_spot_orders(callback:) ⇒ Object
Get the user’s active spot orders
api.exchange.cryptomkt.com/#get-active-spot-orders
Params
Proccallback-
A
Procof two arguments, An exception and a result, called either with the exception or with the result, a list of reports for all active spot orders
102 103 104 |
# File 'lib/cryptomarket/websocket/trading_client.rb', line 102 def get_active_spot_orders(callback:) request('spot_get_orders', callback) end |
#get_spot_commission(symbol:, callback:) ⇒ Object Also known as: get_spot_commission_of_symbol, get_spot_commission_by_symbol
Get the personal trading commission rate of a symbol
Requires the “Place/cancel orders” API key Access Right
api.exchange.cryptomkt.com/#get-trading-commission
Params
Stringsymbol-
The symbol of the commission rate
Proccallback-
A
Procof two arguments, An exception and a result, called either with the exception or with the result, a commission for a symbol for the user
270 271 272 |
# File 'lib/cryptomarket/websocket/trading_client.rb', line 270 def get_spot_commission(symbol:, callback:) request('spot_fee', callback, { symbol: }) end |
#get_spot_commissions(callback:) ⇒ Object
Get the personal trading commission rates for all symbols
Requires the “Place/cancel orders” API key Access Right
api.exchange.cryptomkt.com/#get-all-trading-commission
Params
Proccallback-
A
Procof two arguments, An exception and a result, called either with the exception or with the result, a list of commissions for the user
257 258 259 |
# File 'lib/cryptomarket/websocket/trading_client.rb', line 257 def get_spot_commissions(callback:) request('spot_fees', callback) end |
#get_spot_trading_balance(currency:, callback:) ⇒ Object Also known as: get_spot_trading_balance_of_currency, get_spot_trading_balance_by_currency
Get the user spot trading balance of a currency
Requires the “Orderbook, History, Trading balance” API key Access Right
api.exchange.cryptomkt.com/#get-spot-trading-balance
Params
Stringcurrency-
The currency code to query the balance
Proccallback-
A
Procof two arguments, An exception and a result, called either with the exception or with the result, a trading balance
224 225 226 |
# File 'lib/cryptomarket/websocket/trading_client.rb', line 224 def get_spot_trading_balance(currency:, callback:) request('spot_balance', callback, { currency: }) end |
#get_spot_trading_balances(callback:) ⇒ Object
Get the user’s spot trading balance for all currencies with balance
Requires the “Orderbook, History, Trading balance” API key Access Right
api.exchange.cryptomkt.com/#get-spot-trading-balance
Params
Proccallback-
A
Procof two arguments, An exception and a result, called either with the exception or with the result, a list of the trading balances
211 212 213 |
# File 'lib/cryptomarket/websocket/trading_client.rb', line 211 def get_spot_trading_balances(callback:) request('spot_balances', callback) end |
#replace_spot_order(client_order_id:, new_client_order_id:, quantity:, price:, stop_price: nil, strict_validate: nil, callback: nil) ⇒ Object
changes the parameters of an existing order, quantity or price
api.exchange.cryptomkt.com/#cancel-replace-spot-order
Params
Stringclient_order_id-
the client order id of the order to change
Stringnew_client_order_id-
the new client order id for the modified order. must be unique within the trading day
Stringquantity-
new order quantity
Stringprice-
new order price
Stringstop_price-
Required if order type is ‘stopLimit’, ‘stopMarket’, ‘takeProfitLimit’, or ‘takeProfitMarket’. Order stop price
Boolstrict_validate-
price and quantity will be checked for the incrementation with tick size and quantity step. See symbol’s tick_size and quantity_increment
Proccallback-
Optional. A
Procof two arguments, An exception and a result, called either with the exception or with the result, the new version of the order
240 241 242 243 244 245 246 247 |
# File 'lib/cryptomarket/websocket/trading_client.rb', line 240 def replace_spot_order( # rubocop:disable Metrics/ParameterLists client_order_id:, new_client_order_id:, quantity:, price:, stop_price: nil, strict_validate: nil, callback: nil ) request('spot_replace_order', callback, { client_order_id:, new_client_order_id:, quantity:, price:, stop_price:, strict_validate: }) end |
#subscribe_to_reports(callback:, result_callback: nil) ⇒ Object
subscribe to a feed of execution reports of the user’s orders
api.exchange.cryptomkt.com/#socket-spot-trading
Params
Proccallback-
A
Procthat recieves notifications as a list of reports, and the type of notification (either ‘snapshot’ or ‘update’) Procresult_callback-
Optional. A
Proccalled with a boolean value, indicating the success of the subscription
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/cryptomarket/websocket/trading_client.rb', line 45 def subscribe_to_reports(callback:, result_callback: nil) interceptor = proc { |notification, type| if type == Args::NotificationType::SNAPSHOT callback.call(notification, type) else callback.call([notification], type) end } send_subscription('spot_subscribe', interceptor, {}, result_callback) end |
#subscribe_to_spot_balance(callback:, mode: nil, result_callback: nil) ⇒ Object
subscribe to a feed of the user’s spot balances
only non-zero values are present
api.exchange.cryptomkt.com/#subscribe-to-spot-balances
Proccallback-
A
Procthat recieves notifications as a list of balances, the notification type is always data Procresult_callback-
Optional. A
Proccalled with a boolean value, indicating the success of the subscription ProcStringmode-
Optional. The type of subscription, Either ‘updates’ or ‘batches’. Update messages arrive after an update. Batch messages arrive at equal intervals after a first update
75 76 77 78 79 80 |
# File 'lib/cryptomarket/websocket/trading_client.rb', line 75 def subscribe_to_spot_balance(callback:, mode: nil, result_callback: nil) interceptor = lambda { |notification, _type| callback.call(notification) } send_subscription('spot_balance_subscribe', interceptor, { mode: }, result_callback) end |
#unsubscribe_to_reports(callback: nil) ⇒ Object
stop recieveing the report feed subscription
api.exchange.cryptomkt.com/#socket-spot-trading
Params
Proccallback-
Optional. A
Proccalled with a boolean value, indicating the success of the unsubscription
62 63 64 |
# File 'lib/cryptomarket/websocket/trading_client.rb', line 62 def unsubscribe_to_reports(callback: nil) send_unsubscription('spot_unsubscribe', callback, nil) end |
#unsubscribe_to_spot_balance(result_callback: nil) ⇒ Object
stop recieving the feed of balances changes
api.exchange.cryptomkt.com/#subscribe-to-wallet-balance
Params
Proccallback-
Optional. A
Procof two arguments, An exception and a result, called either with the exception or with the result, a boolean value indicating the success of the unsubscription
88 89 90 91 92 93 94 |
# File 'lib/cryptomarket/websocket/trading_client.rb', line 88 def unsubscribe_to_spot_balance(result_callback: nil) send_unsubscription( 'spot_balance_unsubscribe', result_callback, { mode: Args::SubscriptionMode::UPDATES } ) end |