从python中的API获取的数据中列出类对象

2024-05-14 10:40:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在研究一个交易机器人,我发现很难访问过去时间戳的开盘价、收盘价、高价和低价,以便在烛台图上绘制它们。我使用Bravado包来进行API调用并获取最后一个时间戳上的数据。以下是我目前所掌握的情况:

#!/usr/bin/python

import bitmex
import json

client = bitmex.bitmex(api_key='',api_secret='')

class candle:
    def __init__(self, open, high, low, close, timeStamp):
         self.open = open
         self.high = high
         self.low = low
         self.close = close
         self.timestamp = timeStamp

def get_candles(timeFrame, symbol, count):
tradeHistory = client.Trade.Trade_getBucketed(binSize=timeFrame, partial=True, symbol=symbol, reverse=True, count=count).result()

   openPrice = tradeHistory[0][0]['open']
   highPrice = tradeHistory[0][0]['high']
   lowPrice = tradeHistory[0][0]['low']
   closePrice = tradeHistory[0][0]['close']
   timeStamp = tradeHistory[0][0]['timestamp']

   c = candle(openPrice, highPrice, lowPrice, closePrice, timeStamp)
   print(c.open)
   print(c.high)
   print(c.low)
   print(c.close)
   print(c.timestamp)


#def new_order(symbol, orderQty, ordType, price):

get_candles('5m', 'XBTUSD', count=10)

变量count指定要收集的时间戳的数量。我希望能够使这些时间戳中的每一个都成为一个类蜡烛对象,以便执行统计分析。在

编辑:下面是API调用的结果(print(tradeHistory)):

^{pr2}$

Tags: selfapiclosedefcount时间opensymbol
1条回答
网友
1楼 · 发布于 2024-05-14 10:40:29

让我发布我的解决方案,然后我将讨论以下更改:

class Candle:
    def __init__(self, open, high, low, close, timeStamp):
         self.open = open
         self.high = high
         self.low = low
         self.close = close
         self.timestamp = timeStamp

   def __str__ (self):
        return """
        Open: %s
        High: %s
        Low: %s
        Close: %s
        Timestamp: %s""" % (self.open, self.high, self.low, self.close, self.timestamp)

class GatherCandles :

    trades = []

    def __init__ (self, timeFrame, symbol, count):
       self.client = bitmex.bitmex(api_key='',api_secret='')
       self.tradeHistory = self.getTradeHistory(timeFrame, symbol, count)
       self.constructTrades(self.tradeHistory[0])

    def getTradeHistory (self, timeFrame, symbol, count):
         """Get the trade history from the API"""

         return self.client.Trade.Trade_getBucketed(binSize=timeFrame, partial=True, symbol=symbol, reverse=True, count=count).result()

    def constructTrades (self, th):
        """Iterate through list of trade history items"""

        for trade in th :
             # You can do something here with your `trade` data if you desire...
             self.trades.append( Candle(trade['open'], trade['high'], trade['low'], trade['close'], trade['timestamp']) )

    def printTrades (self):
        """Loop through all trades gathered and instantiated as `Candle` and print properties of each Candle object"""
        [print(t) for t in self.trades]


if __name__ == "__main__":
    g = GatherCandles('5m', 'XBTUSD', count=10)

    for trade in g.trades :
        # do something here on the properties of trade.  Like: trade.open < trade.high
        print( trade.open )
        print( trade.high )

    g.print_trades()

首先,用大写字符命名类(即Candle)。这样,每当您看到对Candle(...)的引用时,您就知道它是新的类实例化语法,而不是看起来像是在调用函数的candle(...)。在

接下来,重写一个类的__str__可以让您在调用print( c )(假设{}是实例化的类)时更改该类的打印方式。在

接下来,我建议创建另一个类来处理所有的get candle函数和操作。与没有结构的程序代码相比,与对象交互更容易。在

相关问题 更多 >

    热门问题