0

I am using the function: polo.returnTicker()['BTC_ETH'] that I got from this python package https://github.com/s4w3d0ff/python-poloniex to get the current ticker from Poloniex. The function returns a json object that looks like this:

{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'}

However, I need a array with all tickers from the past in one-minute intervals , which I don't know how to get. My function can only return the current ticker, and not past tickers, which I need. Does anyone know how to do that?

A. STEFANI
  • 6,707
  • 1
  • 23
  • 48
Hugo
  • 1
  • 1

1 Answers1

0

As Poloniex do not provide ticker history, you will have to request the ticker periodically then store result in a file, json or database. Then read you file, local json or database to return an array of past tickers.

You may use something like that (file version):

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()

Then you may:

  • Change period_in_s=5 to period_in_s=60 to grab ticker in one-minute intervals
  • max_history_size=x to fit your history size requirement.
  • Put your code in the #do your job here section

Which give you :

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

[...]
A. STEFANI
  • 6,707
  • 1
  • 23
  • 48