我正在研究一个交易机器人,我发现很难访问过去时间戳的开盘价、收盘价、高价和低价,以便在烛台图上绘制它们。我使用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}$
让我发布我的解决方案,然后我将讨论以下更改:
首先,用大写字符命名类(即
Candle
)。这样,每当您看到对Candle(...)
的引用时,您就知道它是新的类实例化语法,而不是看起来像是在调用函数的candle(...)
。在接下来,重写一个类的}是实例化的类)时更改该类的打印方式。在
__str__
可以让您在调用print( c )
(假设{接下来,我建议创建另一个类来处理所有的
get candle
函数和操作。与没有结构的程序代码相比,与对象交互更容易。在相关问题 更多 >
编程相关推荐