[英]python trading bot( paper trading)
所以我在python寫了一個交易機器人。更多的是為了好玩,我才剛剛開始。 每個方法都單獨工作,所以我在這里排除它們,不給你 300 行代碼。 我還排除了 hole analyze 方法,因為即使我清除了該方法的 rest,也會出現相同的錯誤。 當我只使用一次分析時,它什么都不做,但也沒有錯誤,當我在循環中使用它時,我得到一個錯誤:發生異常:KeyError 'result'(標記 get_crypto_data)
這對我來說沒有意義,因為如果我打印 get_crypto_data 它就可以正常工作。
def get_crypto_data(pair,since):
return api.query_public("OHLC", data = {"pair" : pair, "since" : since})["result"][pair] #array of prices (60sek)
def analyze(pair,since):
data = get_crypto_data(pair[0]+pair[1], since)
if __name__ == "__main__":
api = krakenex.API()
api.load_key("KrakenKey.txt")
pair = ('XETH' , 'ZEUR') # Currency pair
since = str(int(time.time() - 3600))
while True:
analyze(pair,since)
從 API 接收的數據結構如下所示(示例)(無縮進):
{
"error": [ ],
"result": {
"XXBTZUSD": [
[
1616662740,
"52591.9",
"52599.9",
"52591.8",
"52599.9",
"52599.1",
"0.11091626",
5
],
[
1616662800,
"52600.0",
"52674.9",
"52599.9",
"52665.2",
"52643.3",
"2.49035996",
30
],
[
1616662860,
"52677.7",
"52686.4",
"52602.1",
"52609.5",
"52634.5",
"1.25810675",
20
],
[
1616662920,
"52603.9",
"52627.5",
"52601.2",
"52616.4",
"52614.0",
"3.42391799",
23
],
[
1616662980,
"52601.2",
"52601.2",
"52599.9",
"52599.9",
"52599.9",
"0.43748934",
7
]
],
"last": 1616662920
}
}
當您嘗試在KeyError
中搜索不存在的項目時,將引發 Python 中的 KeyError。 例如,如果您發出request
:
response = requests.get(url).json()
response['nonexistent']
# KeyError raised as nonexistent doesn't exist in the object
考慮到這一點,當您撥打KeyError
電話以接收此 object 時出現 KeyError:
api.query_public("OHLC", data = {"pair" : pair, "since" : since})
我們可以推斷,無論出於何種原因, ["result"]
都不是上面 object 中的鍵。 要調試問題,請按照以下步驟操作。
response = api.query_public("OHLC", data = {"pair" : pair, "since" : since})
print(response, type(response))
String
格式(或其他標准可轉換格式),您可以使用內置的json
庫將其轉換為字典 object ,您可以像在示例中那樣調用。import json
response = api.query_public("OHLC", data = {"pair" : pair, "since" : since})
response = json.loads(response)
import json
# Get the response
response = api.query_public("OHLC", data = {"pair" : pair, "since" : since})
# Convert to string
response = str(response)
# Convert to dictionary using JSON
response = json.loads(response)
# Call the data you want
data = response["result"]["XXBTZUSD"]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.