Ejemplo n.º 1
0
 def __init__(self):
     self.api_key = "db8f342a-3540b528-hrf5gdfghe-5e793"
     self.secret_key = "366684d9-94c5fdf7-ad3b02a0-446bf"
     self.account_client = AccountClient(api_key=self.api_key,
                                         secret_key=self.secret_key)
     self.margin_client = MarginClient(api_key=self.api_key,
                                       secret_key=self.secret_key)
     self.accounts = []
     self.asset = {}
     self.balance = pd.DataFrame(
         columns=['account', 'currency', 'type', 'balance'])
     self.margin = {}
     self.th = TradeHandler()
     self.conn = sqlite3.connect('market.db', timeout=10)
     self.open_order = 0
from huobi.client.margin import MarginClient
from huobi.constant import *
from huobi.utils import *

margin_client = MarginClient(api_key=g_api_key, secret_key=g_secret_key)
list_obj = margin_client.get_margin_account_balance(symbol="eosusdt")
LogInfo.output_list(list_obj)

Ejemplo n.º 3
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()
Ejemplo n.º 4
0
from huobi.client.margin import MarginClient
from huobi.constant import *
from huobi.utils import *

margin_client = MarginClient(api_key=g_api_key, secret_key=g_secret_key)
transfer_id = margin_client.post_transfer_in_margin(symbol="eosusdt",
                                                    currency="usdt",
                                                    amount=20)
LogInfo.output("transfer id : {id}".format(id=transfer_id))
from huobi.client.margin import MarginClient
from huobi.constant import *
from huobi.utils import LogInfo

margin_client = MarginClient(api_key=g_api_key, secret_key=g_secret_key)

list_result = margin_client.get_general_repayment_loan_records(limit=10)
LogInfo.output_list(list_result)




Ejemplo n.º 6
0
class AccountHandler:
    def __init__(self):
        self.api_key = "db8f342a-3540b528-hrf5gdfghe-5e793"
        self.secret_key = "366684d9-94c5fdf7-ad3b02a0-446bf"
        self.account_client = AccountClient(api_key=self.api_key,
                                            secret_key=self.secret_key)
        self.margin_client = MarginClient(api_key=self.api_key,
                                          secret_key=self.secret_key)
        self.accounts = []
        self.asset = {}
        self.balance = pd.DataFrame(
            columns=['account', 'currency', 'type', 'balance'])
        self.margin = {}
        self.th = TradeHandler()
        self.conn = sqlite3.connect('market.db', timeout=10)
        self.open_order = 0

    def get_accounts(self):
        self.accounts = self.account_client.get_accounts()
        # LogInfo.output_list(self.accounts)

    def get_account_asset(self, account_type, currency):
        asset_valuation = self.account_client.get_account_asset_valuation(
            account_type=account_type, valuation_currency=currency)
        return float(asset_valuation.balance)

    def update_asset(self):
        for item in self.accounts:
            self.asset[item.type] = self.get_account_asset(item.type, 'usd')

    def get_balance(self, account_id):
        list_obj = self.account_client.get_balance(account_id=account_id)
        for item in list_obj:
            if float(item.balance) != 0:
                self.balance = self.balance.append(pd.Series(
                    [
                        account_id, item.currency, item.type,
                        float(item.balance)
                    ],
                    index=['account', 'currency', 'type', 'balance']),
                                                   ignore_index=True)
        self.balance = self.balance.drop_duplicates()

    def update_balance(self):
        self.balance = pd.DataFrame(
            columns=['account', 'currency', 'type', 'balance'])
        for item in self.accounts:
            self.get_balance(item.id)
        self.balance = self.balance.loc[
            self.balance['balance'] != 0].reset_index(drop=True)

    def get_cross_margin_account(self):
        account_balance = self.margin_client.get_cross_margin_account_balance()
        self.margin['balance'] = float(account_balance.acct_balance_sum)
        self.margin['debt'] = float(account_balance.debt_balance_sum)
        # account_balance.print_object()

    def get_account_ledger(self, account_id):
        list_obj = self.account_client.get_account_ledger(
            account_id=account_id)
        LogInfo.output_list(list_obj)

    def update_history_trades(self, symbol):
        table = symbol + "_trade_log"
        self.th.get_match_result(symbol)
        data = self.th.trade_log.copy()
        c = self.conn.cursor()
        # get the count of tables with the name
        c.execute(
            "SELECT count(name) FROM sqlite_master WHERE type='table' AND name='"
            + table + "'")
        # #if the count is 1, then table exists
        if c.fetchone()[0] == 1:
            last_data = pd.read_sql('select * from ' + table, self.conn)
            last_data = last_data.append(data).drop_duplicates().sort_values(
                by='Time', ascending=True)
            last_data.sort_values(by='Time',
                                  ascending=True).to_sql(table,
                                                         self.conn,
                                                         if_exists='replace',
                                                         index=False)
        else:
            data.sort_values(by='Time',
                             ascending=True).to_sql(table,
                                                    self.conn,
                                                    if_exists='replace',
                                                    index=False)

    def adjust_position(self, account, currency, target, last_price):
        balance = self.balance
        current_balance = balance[(balance['account'] == account)
                                  & (balance['currency'] == currency)]
        if len(current_balance):
            current_position = current_balance['balance'].values[0]
        else:
            current_position = 0.0

        if current_position < target * 0.95:
            od = OrderType.BUY_MARKET
            amt = (target - current_position) * last_price
            if amt < 5:
                return
            else:
                self.place_market_order(currency, account, od, amt)
        elif current_position > target * 1.05 + 0.00005:
            amt = round(current_position - target - 0.00001, 5)
            od = OrderType.SELL_MARKET
            if amt < 0.0001:
                return
            else:
                self.place_market_order(currency, account, od, amt)

    def place_market_order(self, currency, account, od, amt):
        try:
            self.th.batch_cancel(account)
            order_id = self.th.create_order_market(currency + "usdt", account,
                                                   od, round(amt, 6))
            self.open_order = order_id
        except Exception as error:
            print(error)
            print(od)
            print(amt)
            print("CANNOT PLACE ORDER!!!!!")

    def check_latest_order(self):
        if self.open_order > 0:
            return self.th.get_order(self.open_order)
Ejemplo n.º 7
0
from huobi.client.margin import MarginClient
from huobi.constant import *
from huobi.utils import *

margin_client = MarginClient(api_key=g_api_key, secret_key=g_secret_key)
list_obj = margin_client.get_margin_loan_info(
    symbols="btcusdt,ethusdt,eosusdt")
LogInfo.output_list(list_obj)
from huobi.client.margin import MarginClient
from huobi.constant import *
from huobi.utils import *

margin_client = MarginClient(api_key=g_api_key, secret_key=g_secret_key)
transfer_id = margin_client.post_repay_margin_order(loan_id=7440184,
                                                    amount=100.004083)
LogInfo.output("transfer id : {id}".format(id=transfer_id))
Ejemplo n.º 9
0
from huobi.client.margin import MarginClient
from huobi.constant import *

margin_client = MarginClient(api_key=g_api_key, secret_key=g_secret_key)
account_balance = margin_client.get_cross_margin_account_balance()
account_balance.print_object()
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)
from huobi.client.margin import MarginClient
from huobi.constant import *
from huobi.utils import *

margin_client = MarginClient(api_key=g_api_key, secret_key=g_secret_key)
list_obj = margin_client.get_margin_loan_orders(symbol="eosusdt")
LogInfo.output_list(list_obj)

from huobi.client.margin import MarginClient
from huobi.constant import *
from huobi.utils import *

margin_client = MarginClient(api_key=g_api_key, secret_key=g_secret_key)
transfer_id = margin_client.post_cross_margin_transfer_in(currency="eos", amount=5)
LogInfo.output(transfer_id)

transfer_id = margin_client.post_cross_margin_transfer_out(currency="eos", amount=5)
LogInfo.output(transfer_id)


Ejemplo n.º 13
0
from huobi.client.margin import MarginClient
from huobi.constant import *
from huobi.utils import *


amount = 100
currency = "usdt"


margin_client = MarginClient(api_key=g_api_key, secret_key=g_secret_key)
# order_id = margin_client.post_cross_margin_create_loan_orders(currency=currency, amount=amount)
# LogInfo.output("step 1: create loan order {id}".format(id=order_id))

# to check not clearing loan orders
list_obj = margin_client.get_cross_margin_loan_orders(currency=currency, state=LoanOrderState.ACCRUAL)
LogInfo.output("step 2: loaning order information ")
LogInfo.output_list(list_obj)

if list_obj and len(list_obj):
    for loan_order in list_obj:
        # pay attention to Scientific Notation
        repay_amount = float(loan_order.loan_balance) + float(loan_order.interest_balance)
        LogInfo.output("repay loan order {id}, repay amount : {amount}".format(id=loan_order.id, amount=repay_amount))
        result = margin_client.post_cross_margin_loan_order_repay(order_id=loan_order.id, amount=repay_amount)
        LogInfo.output("step 3: repay loan order {id}, status : {status}, repay amount : {amount}".format(id=loan_order.id, status=result, amount=repay_amount))

list_obj = margin_client.get_cross_margin_loan_orders(currency=currency, state=LoanOrderState.ACCRUAL)
LogInfo.output("step 4: loaning order history ")
LogInfo.output_list(list_obj)
Ejemplo n.º 14
0
from huobi.client.margin import MarginClient
from huobi.constant import *
from huobi.utils import LogInfo

margin_client = MarginClient(api_key=g_api_key, secret_key=g_secret_key)

list_result = margin_client.post_general_repay_loan(account_id=g_account_id,
                                                    currency="usdt",
                                                    amount=1)
LogInfo.output_list(list_result)