我正在使用从该python软件包https://github.com/s4w3d0ff/python-poloniex中获得的函数polo.returnTicker()['BTC_ETH']
,以从Poloniex获取当前行情记录。该函数返回一个如下所示的json对象:
{u'last': u'0.07120000', u'quoteVolume': u'3523.11980704', u'high24hr': u'0.07170000', u'isFrozen': u'0', u'highestBid': u'0.07120026', u'percentChange': u'-0.00154256', u'low24hr': u'0.07078026', u'lowestAsk': u'0.07120370', u'id': 148, u'baseVolume': u'251.02174618'}
但是,我需要一个数组,其中包含过去每隔一分钟的所有行情自动收录器,我不知道该如何获得。我的函数只能返回我需要的当前股票行情,而不能返回过去股票行情。有谁知道这是怎么做到的吗?
答案 0 :(得分:0)
由于Poloniex不提供股票行情记录,因此您将不得不定期请求股票行情记录,然后将结果存储在文件,json或数据库中。然后读取文件,本地json或数据库,以返回过去的行情清单的数组。
您可以使用类似的东西(文件版本):
import requests
import time
import shutil
import os
import json
#this func is use to give you the ticker history
def do_job_with_ticker_history(ticker_history_directory="tickers_hist"):
sorted_ticker_history = sorted(os.listdir(ticker_history_directory))
ticker_array=[]
#iterate over all ticker file
for ticker_filename in sorted_ticker_history:
#get the filepath
ticker_path = ticker_history_directory+os.sep+ ticker_filename
#read the ticker
with open(ticker_path,'r') as f:
#add the ticker data to the array
ticker_array.append(json.loads(f.read()))
#############
#do your job here (first element is the older ticker, and last element is the last ticker)
print "\ndo the job with history (here we show the ",len(sorted_ticker_history),"last price)"
for ticker in ticker_array:
print ticker['BTC_ETH']['last']
#############
#this func is use to grab the ticker
def grab_ticker_periodically(period_in_s=5,ticker_history_directory="tickers_hist",max_history_size=3):
#delete directory
try:
shutil.rmtree(ticker_history_directory)
except:
pass
#create directory
try:
os.mkdir(ticker_history_directory)
except:
pass
ticker_path_name_list=[]
while 1:
print "\nWe get new ticker"
#get timestamp
timestamp = str(time.time())
#generate a filename using timestamp
ticker_path = ticker_history_directory+os.sep+timestamp+".json"
#write ticker data to filename
with open(ticker_path,"w") as f:
f.write(requests.get('https://poloniex.com/public?command=returnTicker').text)
#add new file to list
ticker_path_name_list.append( ticker_path )
#check we not reach the max size
if len(ticker_path_name_list)>max_history_size:
os.remove(ticker_path_name_list[0])
ticker_path_name_list.pop(0)
do_job_with_ticker_history()
#sleep until next grab
time.sleep(period_in_s)
grab_ticker_periodically()
那么您可以:
period_in_s=5
更改为period_in_s=60
可以在一分钟的间隔内抓取股票行情max_history_size=x
以符合您的历史记录大小要求。#do your job here section
哪个给你:
We get new ticker
do the job with history (here we show the 1 last price)
0.03102500
We get new ticker
do the job with history (here we show the 2 last price)
0.03102500
0.03102500
We get new ticker
do the job with history (here we show the 3 last price)
0.03102500
0.03102500
0.03102500
We get new ticker
do the job with history (here we show the 3 last price)
0.03102500
0.03102500
0.03102500
[...]