HuobiRDCenter / huobi_Python

Python SDK for Huobi Spot API
https://huobiapi.github.io/docs/spot/v1/en
Apache License 2.0
680 stars 332 forks source link

Callback problem #103

Open Srcanyildiz opened 3 years ago

Srcanyildiz commented 3 years ago

I didn't get how to figure out callback function. I don't want to use print_object() function. I want to hav details of the trades as a dict variable. Can you help me?

def callback(trade_event: 'TradeDetailEvent'): print("---- trade_event: ----") trade_event.print_object() print()

reece762 commented 3 years ago

You need to find it in the Lib\site-packages\huobi\model Using the example: get_candlestick When you run it, you will see something like this:

Id : 1619016300 High : 2427.9 Low : 2414.0 Open : 2421.13 Close : 2421.25 Count : 9742 Amount : 5541.96056214018 Volume : 13414958.630933998

Then you go to"Lib\site-packages\huobi\model\market" You will see there is a file call candlestick Inside this file you can find:

def print_object(self, format_data=""):
    from huobi.utils.print_mix_object import PrintBasic
    PrintBasic.print_basic(self.id, format_data + "Id")
    #PrintBasic.print_basic(self.timestamp, format_data + "Unix Time")
    PrintBasic.print_basic(self.high, format_data + "High")
    PrintBasic.print_basic(self.low, format_data + "Low")
    PrintBasic.print_basic(self.open, format_data + "Open")
    PrintBasic.print_basic(self.close, format_data + "Close")
    PrintBasic.print_basic(self.count, format_data + "Count")
    PrintBasic.print_basic(self.amount, format_data + "Amount")
    PrintBasic.print_basic(self.vol, format_data + "Volume")

Now you can use your own code to get this information, such as: list_obj = market_client.get_candlestick(symbol, interval, 1) count = len(list_obj) for i in range(0,count): list1.append(date.fromtimestamp(list_obj[i].id)) list1.append(list_obj[i].open) list1.append(list_obj[i].close) list1.append(list_obj[i].high) list1.append(list_obj[i].low) list1.append(list_obj[i].vol)

You can see the different between this tow file, hope it can help you. get_candlestick (example).txt get_candlestick (origin).txt

Kalindro commented 3 years ago

Can you elaborate on the method a bit more @reonsee? You showed the example for REST API candlestick request which is history limited and doesn't need a callback argument. Can you show how would you create a function that returns such a list as above with list_obj = market_client.req_candlestick(symbols, interval, callback, from_ts_second, end_ts_second, error_handler) that isn't history limited?

I have code that will print the dataframe I want but how can I get it "out" of the callback to use somewhere else?

   from pandas import DataFrame as df
   from huobi.constant import *
   from datetime import datetime
   import time

   Pair = "btcusdt"
   Since = datetime(2020, 12, 15)
   To = datetime(2020, 12, 30)
   Since_fixed = int(time.mktime(Since.timetuple()))
   To_fixed = int(time.mktime(To.timetuple()))
   History_dataframe_new = df()
   def callback(candlestick_req: 'CandlestickReq'):
        Candles = candlestick_req.data
        History_dataframe_new["Date"] = [float(Candle.id) * 1000 for Candle in Candles]
        History_dataframe_new["Open"] = [Candle.open for Candle in Candles]
        History_dataframe_new["High"] = [Candle.high for Candle in Candles]
        History_dataframe_new["Low"] = [Candle.low for Candle in Candles]
        History_dataframe_new["Close"] = [Candle.close for Candle in Candles]
        History_dataframe_new["Volume"] = [Candle.amount for Candle in Candles]
        print(History_dataframe_new)

   Candles = Market_client.req_candlestick(symbols = Pair, interval = CandlestickInterval.MIN60, from_ts_second = Since_fixed,  end_ts_second = To_fixed, callback = callback)
reece762 commented 3 years ago

Can you elaborate on the method a bit more @reonsee? You showed the example for REST API candlestick request which is history limited and doesn't need a callback argument. Can you show how would you create a function that returns such a list as above with list_obj = market_client.req_candlestick(symbols, interval, callback, from_ts_second, end_ts_second, error_handler) that isn't history limited?

I have code that will print the dataframe I want but how can I get it "out" of the callback to use somewhere else?

   from pandas import DataFrame as df
   from huobi.constant import *
   from datetime import datetime
   import time

   Pair = "btcusdt"
   Since = datetime(2020, 12, 15)
   To = datetime(2020, 12, 30)
   Since_fixed = int(time.mktime(Since.timetuple()))
   To_fixed = int(time.mktime(To.timetuple()))
   History_dataframe_new = df()
   def callback(candlestick_req: 'CandlestickReq'):
        Candles = candlestick_req.data
        History_dataframe_new["Date"] = [float(Candle.id) * 1000 for Candle in Candles]
        History_dataframe_new["Open"] = [Candle.open for Candle in Candles]
        History_dataframe_new["High"] = [Candle.high for Candle in Candles]
        History_dataframe_new["Low"] = [Candle.low for Candle in Candles]
        History_dataframe_new["Close"] = [Candle.close for Candle in Candles]
        History_dataframe_new["Volume"] = [Candle.amount for Candle in Candles]
        print(History_dataframe_new)

   Candles = Market_client.req_candlestick(symbols = Pair, interval = CandlestickInterval.MIN60, from_ts_second = Since_fixed,  end_ts_second = To_fixed, callback = callback)

You want to get the dataframe, you just need to return the dataframe like this:

def account_blance():
    list1=[[],[]]
    list2=[]

    account_client = AccountClient(api_key=g_api_key,
                                   secret_key=g_secret_key)
    #LogInfo.output("====== (SDK encapsulated api) not recommend for low performance and frequence limitation ======")
    account_balance_list = account_client.get_account_balance()

    if account_balance_list and len(account_balance_list):
        for account_obj in account_balance_list:
            #print(account_obj.id)
            balance_list = account_client.get_balance(account_obj.id)
            for row in balance_list:
                if row.balance != '0':
                    list2.append(row.currency)
                    list1[0].append(row.type)
                    list1[1].append(row.balance)
    df = pd.DataFrame(list1,columns = list2)

    return(df)

Then you run this Functions, you will get the 'df', such as print it: print(account_blance())

reece762 commented 3 years ago

Or the second way is create a list out of the function:

list1 = []

def addobj():
    list1.append('1')
    list1.append('a')

addobj()
print(list1)