我正在尝试构建一个允许我轻松获取加密货币数据的函数。这是我的(来自在线教程)
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from datetime import datetime
import time
from datetime import date
import json
from bs4 import BeautifulSoup
import requests
def timestamp2date(timestamp):
# function converts a Unix timestamp into Gregorian date
return datetime.fromtimestamp(int(timestamp)).strftime('%Y-%m-%d')
def date2timestamp(date):
# function coverts Gregorian date in a given format to timestamp
return datetime.strptime(date_today, '%Y-%m-%d').timestamp()
def fetchCryptoOHLC(fsym, tsym):
# function fetches a crypto price-series for fsym/tsym and stores
# it in pandas DataFrame
cols = ['date', 'timestamp', 'open', 'high', 'low', 'close']
lst = ['time', 'open', 'high', 'low', 'close']
timestamp_today = datetime.today()
curr_timestamp = timestamp_today
for j in range(2):
df = pd.DataFrame(columns=cols)
url = "https://min-api.cryptocompare.com/data/histoday?fsym=" + fsym + "&tsym=" + tsym + "&toTs=" + str(curr_timestamp) + "&limit=2000"
response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")
dic = json.loads(soup.prettify())
for i in range(1, 2001):
tmp = []
for e in enumerate(lst):
x = e[0]
y = dic['Data'][i][e[1]]
if(x == 0):
tmp.append(str(timestamp2date(y)))
tmp.append(y)
if(np.sum(tmp[-4::]) > 0):
df.loc[len(df)] = np.array(tmp)
df.index = pd.to_datetime(df.date)
df.drop('date', axis=1, inplace=True)
curr_timestamp = int(df.ix[0][0])
if(j == 0):
df0 = df.copy()
else:
data = pd.concat([df, df0], axis=0)
return data
我遇到的问题是在运行fetchCryptoOHLC函数时,我遇到了这个错误:" IndexError:列表索引超出范围"