Ejemplo n.º 1
0
    def test_margin(self):
        margin_client = MarginClient(api_key=g_api_key,
                                     secret_key=g_secret_key,
                                     performance_test=True)

        # case post_transfer_in_margin
        tc = TimeCost(
            function_name=margin_client.post_transfer_in_margin.__name__)
        result, tc.server_req_cost, tc.server_api_cost = margin_client.post_transfer_in_margin(
            symbol=loan_symbol, currency=loan_currency, amount=loan_amount)
        tc.run_status = RunStatus.SUCCESS if result else RunStatus.FAILED
        tc.add_record()

        # case get_margin_loan_info
        tc = TimeCost(
            function_name=margin_client.get_margin_loan_info.__name__)
        result, tc.server_req_cost, tc.server_api_cost = margin_client.get_margin_loan_info(
            symbols="btcusdt,ethusdt,eosusdt," + loan_symbol)
        tc.run_status = RunStatus.SUCCESS if result and len(
            result) else RunStatus.FAILED
        tc.add_record()

        # case get_margin_loan_orders
        tc = TimeCost(
            function_name=margin_client.get_margin_loan_orders.__name__)
        result, tc.server_req_cost, tc.server_api_cost = margin_client.get_margin_loan_orders(
            symbol=loan_symbol)
        tc.run_status = RunStatus.SUCCESS if result and len(
            result) else RunStatus.FAILED
        tc.add_record()

        # case post_create_margin_order
        tc = TimeCost(
            function_name=margin_client.post_create_margin_order.__name__)
        result, tc.server_req_cost, tc.server_api_cost = margin_client.post_create_margin_order(
            symbol=loan_symbol, currency=loan_currency, amount=loan_amount)
        tc.run_status = RunStatus.SUCCESS if result and int(
            result) > 0 else RunStatus.FAILED
        tc.add_record()

        time.sleep(3)  # wait for interest amount
        # case get_margin_loan_orders
        tc = TimeCost(
            function_name=margin_client.get_margin_loan_orders.__name__)
        result, tc.server_req_cost, tc.server_api_cost = margin_client.get_margin_loan_orders(
            symbol=loan_symbol, states=LoanOrderState.ACCRUAL)
        cross_loan_accrual_order_list = result
        tc.run_status = RunStatus.SUCCESS if result and len(
            result) else RunStatus.FAILED
        tc.add_record()

        # case post_repay_margin_order
        interest_amount = 0.0
        if cross_loan_accrual_order_list and len(
                cross_loan_accrual_order_list):
            for loan_order in cross_loan_accrual_order_list:
                tc = TimeCost(function_name=margin_client.
                              post_repay_margin_order.__name__)
                repay_amount = float(loan_order.loan_balance) + float(
                    loan_order.interest_balance)
                interest_amount = interest_amount + float(
                    loan_order.interest_balance)
                result, tc.server_req_cost, tc.server_api_cost = margin_client.post_repay_margin_order(
                    loan_id=loan_order.id, amount=repay_amount)
                tc.run_status = RunStatus.SUCCESS if result else RunStatus.FAILED
                tc.add_record()

        time.sleep(2)  # wait for repay result

        # case post_transfer_out_margin
        tc = TimeCost(
            function_name=margin_client.post_transfer_out_margin.__name__)
        result, tc.server_req_cost, tc.server_api_cost = margin_client.post_transfer_out_margin(
            symbol=loan_symbol,
            currency=loan_currency,
            amount=loan_amount - interest_amount)
        tc.run_status = RunStatus.SUCCESS if result else RunStatus.FAILED
        tc.add_record()
from huobi.client.margin import MarginClient
from huobi.constant import *
from huobi.utils import *
import time

loan_amount = 100
interest_amount = 0.004083

margin_client = MarginClient(api_key=g_api_key, secret_key=g_secret_key)
loan_id = margin_client.post_create_margin_order(symbol="eosusdt", currency="usdt", amount=loan_amount)
LogInfo.output("step 1: loan id : {id}".format(id=loan_id))

time.sleep(2)

repay_id = margin_client.post_repay_margin_order(loan_id=loan_id, amount=loan_amount + interest_amount)
LogInfo.output("step 2: repay id : {id}".format(id=repay_id))

list_obj = margin_client.get_margin_loan_orders(symbol="eosusdt", states=LoanOrderState.ACCRUAL)
LogInfo.output("step 3: loaning order information")
LogInfo.output_list(list_obj)


list_obj = margin_client.get_margin_loan_orders(symbol="eosusdt")
LogInfo.output("step 4: loan order history")
LogInfo.output_list(list_obj)