Python - Poloniex推送API
我想通过推送API从Poloniex获取Python 2.7.13中的实时数据。 我看了很多帖子(包括How to connect to poloniex.com websocket api using a python library)和我到达下面的代码:Python - Poloniex推送API
from autobahn.twisted.wamp import ApplicationSession
from autobahn.twisted.wamp import ApplicationRunner
from twisted.internet.defer import inlineCallbacks
import six
class PoloniexComponent(ApplicationSession):
def onConnect(self):
self.join(self.config.realm)
@inlineCallbacks
def onJoin(self, details):
def onTicker(*args):
print("Ticker event received:", args)
try:
yield self.subscribe(onTicker, 'ticker')
except Exception as e:
print("Could not subscribe to topic:", e)
def main():
runner = ApplicationRunner(six.u("wss://api.poloniex.com"), six.u("realm1"))
runner.run(PoloniexComponent)
if __name__ == "__main__":
main()
现在,当我运行的代码,它看起来像它的成功运行,但我不知道我在哪里得到数据。我有两个问题:
我真的很感激,如果有人可以走路我通过订阅和获得股票数据,我会在Python阐述,从步骤0的过程:我上的Spyder上运行的程序视窗。我应该以某种方式激活Crossbar?
如何退出连接?我简单地用
Ctrl+c
杀死了这个进程,现在当我尝试运行它时,出现错误:ReactorNonRestartable
。
我遇到了很多使用Poloniex与Python2.7的问题,但最终找到了一个可以帮助你的解决方案。
我发现Poloniex已经支持原始的WAMP套接字端点,所以我可能完全偏离了这个方法。也许这就是你需要的全部答案,但如果不是这里的另一种获取股票信息的方法。
最终对我最有效的代码实际上是从linked到上面的帖子,但是有一些关于我在别处找到的货币对ID的信息。
import websocket
import thread
import time
import json
def on_message(ws, message):
print(message)
def on_error(ws, error):
print(error)
def on_close(ws):
print("### closed ###")
def on_open(ws):
print("ONOPEN")
def run(*args):
# ws.send(json.dumps({'command':'subscribe','channel':1001}))
ws.send(json.dumps({'command':'subscribe','channel':1002}))
# ws.send(json.dumps({'command':'subscribe','channel':1003}))
# ws.send(json.dumps({'command':'subscribe','channel':'BTC_XMR'}))
while True:
time.sleep(1)
ws.close()
print("thread terminating...")
thread.start_new_thread(run,())
if __name__ == "__main__":
websocket.enableTrace(True)
ws = websocket.WebSocketApp("wss://api2.poloniex.com/",
on_message = on_message,
on_error = on_error,
on_close = on_close)
ws.on_open = on_open
ws.run_forever()
我注释掉提取数据,你似乎并不想台词,但以供参考是从以前的帖子一些更多的信息:
1001 = trollbox (you will get nothing but a heartbeat)
1002 = ticker
1003 = base coin 24h volume stats
1010 = heartbeat
'MARKET_PAIR' = market order books
现在你应该得到一些数据,看起来是这样的:
[121,"2759.99999999","2759.99999999","2758.00000000","0.02184376","12268375.01419869","4495.18724321",0,"2767.80020000","2680.10000000"]]
这也是恼人的,因为“121”开头的是货币对ID,这是未记录也是在其他堆栈溢出问题解答参考红到这里。
但是,如果您访问此网址:https://poloniex.com/public?command=returnTicker它似乎是第一个字段显示的ID,所以您可以创建自己的id->货币对映射或通过您想要的id从数据解析数据。
另外,一些简单的:
import urllib
import urllib2
import json
ret = urllib2.urlopen(urllib2.Request('https://poloniex.com/public?command=returnTicker'))
print json.loads(ret.read())
会回报给你你想要的数据,但你必须把它在一个循环中得到不断更新的信息。一旦收到数据,就不知道你的需求,所以我会把剩下的东西留给你。
希望这会有所帮助!
我在其他文章的帮助下,使用下面的代码来获取使用Python 3.x的最新数据。我希望这可以帮助你:
#TO SAVE THE HISTORICAL DATA (X MINUTES/HOURS) OF EVERY CRYPTOCURRENCY PAIR IN POLONIEX:
from poloniex import Poloniex
import pandas as pd
from time import time
import os
api = Poloniex(jsonNums=float)
#Obtains the pairs of cryptocurrencies traded in poloniex
pairs = [pair for pair in api.returnTicker()]
i = 0
while i < len(pairs):
#Available candle periods: 5min(300), 15min(900), 30min(1800), 2hr(7200), 4hr(14400), and 24hr(86400)
raw = api.returnChartData(pairs[i], period=86400, start=time()-api.YEAR*10)
df = pd.DataFrame(raw)
# adjust dates format and set dates as index
df['date'] = pd.to_datetime(df["date"], unit='s')
df.set_index('date', inplace=True)
# Saves the historical data of every pair in a csv file
path=r'C:\x\y\Desktop\z\folder_name'
df.to_csv(os.path.join(path,r'%s.csv' % pairs[i]))
i += 1
你从哪里知道他们正在拉动WAMP支持?他们的API文档仍然声称这是使用它的方式。 – Nate
感谢解决方案@scott_det!我有和@Nate一样的问题,在WAMP上浪费了几个小时。 – FujiApple