我怎样才能从波罗尼克斯买到所有的票

2024-04-28 19:30:45 发布

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

我正在使用从这个python包https://github.com/s4w3d0ff/python-poloniex获得的函数:polo.returnTicker()['BTC_ETH']从Poloniex获取当前的ticker。函数返回如下所示的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'}

但是,我需要一个数组,在一分钟的时间间隔内包含所有过去的代码,我不知道如何获取这些代码。我的函数只能返回当前的ticker,而不能返回我需要的过去的ticker。有人知道怎么做吗?你知道吗


Tags: 对象函数代码httpsgithubcomjsonpoloniex
1条回答
网友
1楼 · 发布于 2024-04-28 19:30:45

由于Poloniex不提供ticker历史,您必须定期请求ticker,然后将结果存储在文件、json或数据库中。然后读取您的文件、本地json或数据库以返回过去的ticker数组。你知道吗

您可以使用类似的内容(文件版本):

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以在一分钟的时间间隔内获取ticker
  • 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

[...]

相关问题 更多 >