def test_result(self): impl = WebsocketRequestImpl("12345") symbols = list() symbols.append("btcusdt") def callback(event): pass request = impl.subscribe_order_update(symbols, callback) event = request.json_parser(parse_json_from_string(data)) self.assertEqual("btcusdt", event.symbol) self.assertEqual(1.6621, event.data.price) self.assertEqual(5001, event.data.amount) self.assertEqual(5000, event.data.filled_amount) self.assertEqual("btcusdt", event.data.symbol) self.assertEqual(convert_cst_in_millisecond_to_utc(1522856623232), event.timestamp) self.assertEqual(AccountType.SPOT, event.data.account_type) self.assertEqual(convert_cst_in_millisecond_to_utc(1522858623622), event.data.created_timestamp) self.assertEqual(2039498445, event.data.order_id) self.assertEqual(OrderType.BUY_LIMIT, event.data.order_type) self.assertEqual(OrderSource.API, event.data.source) self.assertEqual(8301.35728, event.data.filled_cash_amount) self.assertEqual(8, event.data.filled_fees)
def test_result(self): impl = WebsocketRequestImpl("") symbols = list() symbols.append("btcusdt") def callback(event): pass request = impl.subscribe_trade_event(symbols, callback) event = request.json_parser(parse_json_from_string(data)) self.assertEqual("btcusdt", event.symbol) self.assertEqual(2, len(event.trade_list)) self.assertEqual(convert_cst_in_millisecond_to_utc(1550558574702), event.timestamp) self.assertEqual(0.001, event.trade_list[0].amount) self.assertEqual(convert_cst_in_millisecond_to_utc(1550558574684), event.trade_list[0].timestamp) self.assertEqual("10033544262424890651900", event.trade_list[0].trade_id) self.assertEqual(3892.36, event.trade_list[0].price) self.assertEqual(TradeDirection.SELL, event.trade_list[0].direction) self.assertEqual(0.0512, event.trade_list[1].amount) self.assertEqual(convert_cst_in_millisecond_to_utc(1550558574684), event.trade_list[1].timestamp) self.assertEqual("10033544262424890651183", event.trade_list[1].trade_id) self.assertEqual(3892.35, event.trade_list[1].price) self.assertEqual(TradeDirection.BUY, event.trade_list[1].direction)
def json_parse(json_data, account_type): order = Order() order.order_id = json_data.get_int("id") order.symbol = json_data.get_string("symbol") order.price = json_data.get_float("price") order.amount = json_data.get_float("amount") order.created_timestamp = convert_cst_in_millisecond_to_utc( json_data.get_int("created-at")) order.canceled_timestamp = convert_cst_in_millisecond_to_utc( json_data.get_int("canceled-at")) order.finished_timestamp = convert_cst_in_millisecond_to_utc( json_data.get_int("finished-at")) order.order_type = json_data.get_string("type") order.filled_amount = json_data.get_float_or_default( "field-amount", json_data.get_float_or_default("filled-amount", 0)) order.filled_cash_amount = json_data.get_float_or_default( "field-cash-amount", json_data.get_float_or_default("filled-cash-amount", 0)) order.filled_fees = json_data.get_float_or_default( "field-fees", json_data.get_float_or_default("filled-fees", 0)) order.account_type = account_type order.source = json_data.get_string("source") order.state = json_data.get_string("state") order.stop_price = json_data.get_float_or_default("stop-price", 0.0) order.operator = json_data.get_string_or_default("operator", "") order.next_time = json_data.get_string_or_default("next-time", "") return order
def test_result(self): impl = RestApiRequestImpl("12345", "67890") request = impl.get_loan("btcusdt", "2019-01-02", "2019-02-03", LoanOrderState.CREATED, 23456, 123) loan_list = request.json_parser(parse_json_from_string(data)) self.assertEqual(2, len(loan_list)) self.assertEqual(390, loan_list[0].id) self.assertEqual(0.1, loan_list[0].loan_balance) self.assertEqual(0.0002, loan_list[0].interest_balance) self.assertEqual(0.002, loan_list[0].interest_rate) self.assertEqual(0.11, loan_list[0].loan_amount) self.assertEqual(0.0012, loan_list[0].interest_amount) self.assertEqual("ethbtc", loan_list[0].symbol) self.assertEqual(LoanOrderState.ACCRUAL, loan_list[0].state) self.assertEqual(AccountType.SPOT, loan_list[0].account_type) self.assertEqual(119910, loan_list[0].user_id) self.assertEqual(convert_cst_in_millisecond_to_utc(1511169724000), loan_list[0].accrued_timestamp) self.assertEqual(convert_cst_in_millisecond_to_utc(1511169724530), loan_list[0].created_timestamp)
def test_result(self): impl = RestApiRequestImpl("", "") request = impl.get_historical_trade("btcusdt", "12345", 100) trade_list = request.json_parser(parse_json_from_string(data)) self.assertEqual(3, len(trade_list)) self.assertEqual(122.18, trade_list[0].price) self.assertEqual(0.0071, trade_list[0].amount) self.assertEqual(convert_cst_in_millisecond_to_utc(1550222502562), trade_list[0].timestamp) self.assertEqual(TradeDirection.SELL, trade_list[0].direction)
def json_parse(json_data): trade = Trade() trade.amount = json_data.get_float("amount") trade.price = json_data.get_float("price") trade.trade_id = json_data.get_string("id") trade.direction = json_data.get_string("direction") trade.timestamp = convert_cst_in_millisecond_to_utc( json_data.get_int("ts")) return trade
def test_result(self): impl = RestApiRequestImpl("12345", "67890") request = impl.get_withdraw_history("btc", 24966984923, 1) withdraws = request.json_parser(parse_json_from_string(data)) self.assertEqual(1, len(withdraws)) self.assertEqual(345, withdraws[0].fee) self.assertEqual(1171, withdraws[0].id) self.assertEqual(convert_cst_in_millisecond_to_utc(1510912472199), withdraws[0].created_timestamp) self.assertEqual(convert_cst_in_millisecond_to_utc(1511145876575), withdraws[0].updated_timestamp) self.assertTrue("rae93V8d2mdoUQHwBDBdM4NHCMehRJAsbm", withdraws[0].address) self.assertEqual("100040", withdraws[0].address_tag) self.assertEqual("ht", withdraws[0].currency) self.assertEqual( "ed03094b84eafbe4bc16e7ef766ee959885ee5bcb265872baaa9c64e1cf86c2b", withdraws[0].tx_hash) self.assertEqual(WithdrawState.CONFIRMED, withdraws[0].withdraw_state)
def test_result(self): impl = RestApiRequestImpl("", "") request = impl.get_best_quote("btcusdt") best_quote = request.json_parser(parse_json_from_string(data)) self.assertEqual(convert_cst_in_millisecond_to_utc(1550223581490), best_quote.timestamp) self.assertEqual(122.26, best_quote.ask_price) self.assertEqual(0.8271, best_quote.ask_amount) self.assertEqual(122.24, best_quote.bid_price) self.assertEqual(2.6672, best_quote.bid_amount)
def test_result(self): impl = RestApiRequestImpl("", "") request = impl.get_price_depth("btcustd", 1) price_depth = request.json_parser(parse_json_from_string(data)) self.assertEqual(1, len(price_depth.bids)) self.assertEqual(convert_cst_in_millisecond_to_utc(1550218546020), price_depth.timestamp) self.assertEqual(122.92, price_depth.bids[0].price) self.assertEqual(2.7468, price_depth.bids[0].amount) self.assertEqual(122.94, price_depth.asks[0].price) self.assertEqual(67.5549, price_depth.asks[0].amount)
def json_parse(json_wrapper): ch = json_wrapper.get_string("ch") parse = ChannelParser(ch) trade_statistics_event = TradeStatisticsEvent() trade_statistics_event.symbol = parse.symbol ts = convert_cst_in_millisecond_to_utc(json_wrapper.get_int("ts")) trade_statistics_event.timestamp = ts tick = json_wrapper.get_object("tick") statistics = TradeStatistics.json_parse(tick, ts) trade_statistics_event.trade_statistics = statistics return trade_statistics_event
def json_parse(json_wrapper): ch = json_wrapper.get_string(OutputKey.KeyChannelCh) parse = ChannelParser(ch) price_depth_event = PriceDepthEvent() price_depth_event.symbol = parse.symbol price_depth_event.timestamp = convert_cst_in_millisecond_to_utc( json_wrapper.get_int("ts")) price_depth_event.ch = ch data = json_wrapper.get_object(OutputKey.KeyTick) price_depth = PriceDepth.json_parse(data) price_depth_event.data = price_depth return price_depth_event
def test_result(self): impl = RestApiRequestImpl("12345", "67890") request = impl.get_historical_orders("btcusdt", OrderState.CANCELED, OrderType.SELL_LIMIT, "2019-01-03", "2019-02-03", 123, 456) order_list = request.json_parser(parse_json_from_string(data)) self.assertEqual(2, len(order_list)) self.assertEqual(24965104183, order_list[0].order_id) self.assertEqual(AccountType.SPOT, order_list[0].account_type) self.assertEqual(convert_cst_in_millisecond_to_utc(1550630155568), order_list[0].canceled_timestamp) self.assertEqual(convert_cst_in_millisecond_to_utc(1550630155647), order_list[0].finished_timestamp) self.assertEqual(convert_cst_in_millisecond_to_utc(1550630155350), order_list[0].created_timestamp) self.assertEqual(0.0888, order_list[0].filled_amount) self.assertEqual(0.011, order_list[0].filled_cash_amount) self.assertEqual(0.03445, order_list[0].filled_fees) self.assertEqual(1.000000000000000001, order_list[0].price) self.assertEqual("htbtc", order_list[0].symbol) self.assertEqual(1, order_list[0].amount) self.assertEqual(OrderSource.API, order_list[0].source) self.assertEqual(OrderState.CANCELED, order_list[0].state) self.assertEqual(OrderType.SELL_LIMIT, order_list[0].order_type) self.assertEqual(0, order_list[1].finished_timestamp) self.assertEqual(0, order_list[1].canceled_timestamp)
def json_parse(json_wrapper): ch = json_wrapper.get_string(OutputKey.KeyChannelCh) parse = ChannelParser(ch) candlestick_event = CandlestickEvent() candlestick_event.symbol = parse.symbol candlestick_event.interval = "" candlestick_event.timestamp = convert_cst_in_millisecond_to_utc( json_wrapper.get_int("ts")) tick = json_wrapper.get_object(OutputKey.KeyTick) data = Candlestick.json_parse(tick) candlestick_event.data = data return candlestick_event
def test_result(self): impl = RestApiRequestImpl("12345", "67890") request = impl.get_order("htbtc", 24962048654) order = request.json_parser(parse_json_from_string(data)) self.assertEqual(convert_cst_in_millisecond_to_utc(1550626936504), order.created_timestamp) self.assertEqual(AccountType.SPOT, order.account_type) self.assertEqual(convert_cst_in_millisecond_to_utc(1550626936722), order.canceled_timestamp) self.assertEqual(convert_cst_in_millisecond_to_utc(1550626936798), order.finished_timestamp) self.assertEqual(24962048654, order.order_id) self.assertEqual(0.08888, order.filled_amount) self.assertEqual(0.204, order.filled_cash_amount) self.assertEqual(0.0345, order.filled_fees) self.assertEqual(1.00000123, order.price) self.assertEqual("htbtc", order.symbol) self.assertEqual(1, order.amount) self.assertEqual(OrderSource.API, order.source) self.assertEqual(OrderState.CANCELED, order.state) self.assertEqual(OrderType.SELL_LIMIT, order.order_type)
def json_parse(json_data): account_balance = AccountBalanceRequest() account_balance.timestamp = convert_cst_in_millisecond_to_utc( json_data.get_int("ts")) account_balance.client_req_id = json_data.get_string("cid") account_balance.topic = json_data.get_string("topic") subaccount_list = json_data.get_array("data") account_list = list() for subaccount in subaccount_list.get_items(): account = Account.json_parse(subaccount) account_list.append(account) account_balance.account_list = account_list return account_balance
def test_result(self): impl = RestApiRequestImpl("", "") request = impl.get_etf_candlestick("hb10", CandlestickInterval.YEAR1, 100) candlestick_list = request.json_parser(parse_json_from_string(data)) self.assertEqual(1, len(candlestick_list)) self.assertEqual(convert_cst_in_millisecond_to_utc(1499184000), candlestick_list[0].timestamp) self.assertEqual(0.7694, candlestick_list[0].high) self.assertEqual(0.769, candlestick_list[0].low) self.assertEqual(0.7794, candlestick_list[0].open) self.assertEqual(0.779, candlestick_list[0].close) self.assertEqual(123.123, candlestick_list[0].amount) self.assertEqual(456.456, candlestick_list[0].volume)
def test_request(self): impl = RestApiRequestImpl("12345", "67890") request = impl.get_match_results("htbtc", 24966984923) match_result_list = request.json_parser(parse_json_from_string(data)) self.assertEqual(2, len(match_result_list)) self.assertEqual(convert_cst_in_millisecond_to_utc(1550632074577), match_result_list[0].created_timestamp) self.assertEqual(4191225853, match_result_list[0].id) self.assertEqual(100047251154, match_result_list[0].match_id) self.assertEqual(24966984923, match_result_list[0].order_id) self.assertEqual(1, match_result_list[0].filled_amount) self.assertEqual(0.00000061508, match_result_list[0].filled_fees) self.assertEqual(0.00030754, match_result_list[0].price) self.assertEqual("spot-api", match_result_list[0].source) self.assertEqual("sell-market", match_result_list[0].order_type)
def json_parse(json_wrapper): ch = json_wrapper.get_string("ch") parse = ChannelParser(ch) trade_event = TradeEvent() trade_event.symbol = parse.symbol trade_event.timestamp = convert_cst_in_millisecond_to_utc(json_wrapper.get_int("ts")) tick = json_wrapper.get_object("tick") data_array = tick.get_array("data") trade_list = list() for item in data_array.get_items(): trade = Trade.json_parse(item) trade_list.append(trade) trade_event.trade_list = trade_list return trade_event
def json_parse(json_data, account_id_type_map): req_obj = OrderDetailRequest() req_obj.timestamp = convert_cst_in_millisecond_to_utc( json_data.get_int("ts")) req_obj.client_req_id = json_data.get_string("cid") req_obj.topic = json_data.get_string("topic") order_json = json_data.get_object("data") account_id = order_json.get_int("account-id") account_type = AccountType.INVALID if account_id in account_id_type_map: account_type = account_id_type_map[account_id] order_obj = Order.json_parse(order_json, account_type) req_obj.symbol = order_obj.symbol req_obj.data = order_obj return req_obj
def test_result(self): impl = RestApiRequestImpl("12345", "67890") request = impl.get_open_orders("btcusdt", AccountType.SPOT, 10, OrderSide.BUY) order_list = request.json_parser(parse_json_from_string(data)) self.assertEqual(2, len(order_list)) self.assertEqual("htbtc", order_list[0].symbol) self.assertEqual(1, order_list[0].amount) self.assertEqual(1.1, order_list[0].price) self.assertEqual(0.1, order_list[0].filled_amount) self.assertEqual(0.2, order_list[0].filled_cash_amount) self.assertEqual(0.3, order_list[0].filled_fees) self.assertEqual(24963751000, order_list[0].order_id) self.assertEqual(AccountType.SPOT, order_list[0].account_type) self.assertEqual(OrderState.SUBMITTED, order_list[0].state) self.assertEqual(OrderType.SELL_LIMIT, order_list[0].order_type) self.assertEqual(convert_cst_in_millisecond_to_utc(1550628731111), order_list[0].created_timestamp)
def test_result(self): impl = WebsocketRequestImpl("") symbols = list() symbols.append("btcusdt") def callback(event): pass request = impl.subscribe_price_depth_event(symbols, callback) event = request.json_parser(parse_json_from_string(data)) self.assertEqual("btcusdt", event.symbol) self.assertEqual(convert_cst_in_millisecond_to_utc(1550558788054), event.timestamp) self.assertEqual(3, len(event.data.bids)) self.assertEqual(2, len(event.data.asks)) self.assertEqual(3891.94, event.data.bids[0].price) self.assertEqual(0.0257, event.data.bids[0].amount) self.assertEqual(3891.95, event.data.asks[0].price) self.assertEqual(0.0283, event.data.asks[0].amount)
def test_result(self): impl = WebsocketRequestImpl("") symbols = list() symbols.append("btcusdt") def callback(event): pass request = impl.subscribe_candlestick_event(symbols, CandlestickInterval.MIN1, callback) candlestick_event = request.json_parser(parse_json_from_string(data)) self.assertEqual("btcusdt", candlestick_event.symbol) self.assertEqual( convert_cst_in_millisecond_to_utc(1550469651403), candlestick_event.timestamp) self.assertEqual(CandlestickInterval.MIN1, candlestick_event.interval) self.assertEqual(3719.88, candlestick_event.data.open) self.assertEqual(3719.91, candlestick_event.data.close) self.assertEqual(3719.45, candlestick_event.data.low) self.assertEqual(3719.99, candlestick_event.data.high) self.assertEqual(5.470327974838371482, candlestick_event.data.amount) self.assertEqual(20349.2034519999999944659, candlestick_event.data.volume) self.assertEqual(73, candlestick_event.data.count)
def test_result(self): impl = WebsocketRequestImpl("") symbols = list() symbols.append("btcusdt") def callback(event): pass request = impl.subscribe_24h_trade_statistics_event(symbols, callback) event = request.json_parser(parse_json_from_string(data)) self.assertEqual("btcusdt", event.symbol) self.assertEqual(convert_cst_in_millisecond_to_utc(1550740513421), event.timestamp) self.assertEqual(204966, event.trade_statistics.count) self.assertEqual(115320213.26007387, event.trade_statistics.volume) self.assertEqual(0.00030342, event.trade_statistics.open) self.assertEqual(3903.5, event.trade_statistics.low) self.assertEqual(4015, event.trade_statistics.high) self.assertEqual(3947.03, event.trade_statistics.close) self.assertEqual(29147.328607142535, event.trade_statistics.amount)
def test_result(self): impl = WebsocketRequestImpl("12345") symbols = list() symbols.append("btcusdt") def callback(event): pass request = impl.subscribe_account_event(BalanceMode.AVAILABLE, callback) event = request.json_parser(parse_json_from_string(data)) self.assertEqual(convert_cst_in_millisecond_to_utc(1550556381242), event.timestamp) self.assertEqual(1, len(event.account_change_list)) self.assertEqual(AccountChangeType.NEWORDER, event.change_type) self.assertEqual(AccountType.SPOT, event.account_change_list[0].account_type) self.assertEqual("ht", event.account_change_list[0].currency) self.assertEqual(10.8208984536412, event.account_change_list[0].balance) self.assertEqual(BalanceType.TRADE, event.account_change_list[0].balance_type)