如何优化Pandas DataFrame的速度?

时间:2019-03-19 13:19:09

标签: python pandas dataframe optimization

我有这个熊猫码,但是非常慢。我如何优化它?这意味着当我运行它时,大约需要4秒钟。我在这里反复调用的这段代码就是我一遍又一遍地调用的代码,应该尽可能快,因为它目前不是...没有人知道吗?

    self.dataframe = pd.DataFrame(columns=list(['O' ,'H' ,'L' ,'C' ,'RSI', 'Upper Band', 'Lower Band'])) 

    BinanceHistoricalUrl = "https://api.binance.com/api/v1/klines?"
    BinanceHistoricalPayload = {'symbol' : 'BTCUSDT','interval': '1m','limit': 100}
    HistoricalRequestData = requests.get(url=BinanceHistoricalUrl, params=BinanceHistoricalPayload).json()

    Lenght = len(HistoricalRequestData)

    for i in range(Lenght):

        O = HistoricalRequestData[i][1]
        O = "{:.4f}".format(O)
        O = float(O)

        H = HistoricalRequestData[i][2]
        H = "{:.4f}".format(H)
        H = float(H)

        L = HistoricalRequestData[i][3]
        L = "{:.4f}".format(H)
        L = float(L)

        C = HistoricalRequestData[i][4]
        C = "{:.4f}".format(C)
        C = float(C)

#        Volume = HistoricalRequestData[0]["priceData"][i]['volume']
#        Volume = "{:.4f}".format(Volume)
#        Volume = float(Volume)

        self.dataframe = self.dataframe.append({'O': O, 'H' : H, 'L' : L, 'C' : C}, ignore_index=True)         

    make_RSI(self.dataframe)
    make_bollinger_bands(self.dataframe)
    RSI = self.dataframe['RSI'][99]
    RSI = float(RSI)
    UppBoll = self.dataframe['Upper Band'][99]
    UndBoll = self.dataframe['Lower Band'][99]
    previouscloseprice = self.dataframe['C'][99]
    MA = self.dataframe['20 Day MA'][99]
    DistanceUppBoll = UppBoll - MA
    DistanceUppBoll = float(DistanceUppBoll)
    DistanceUndBoll = UndBoll - MA
    DistanceUndBoll = float(DistanceUndBoll)

    self.dataframe = self.dataframe.iloc[0:0]




def make_RSI(dataframe):
    delta = dataframe['C'].diff()
    dUp, dDown = delta.copy(), delta.copy()
    dUp[dUp < 0] = 0
    dDown[dDown > 0] = 0
    RolUp = dUp.rolling(14).mean()
    RolDown = dDown.rolling(14).mean().abs()

    RS = RolUp / RolDown
    dataframe['RSI'] = 100 - (100/(1+RS))

def make_bollinger_bands(dataframe):
    dataframe['20 Day MA'] = dataframe['C'].rolling(window=20).mean()
    dataframe['20 Day STD'] = dataframe['C'].rolling(window=20).std()
    dataframe['Upper Band'] = dataframe['20 Day MA'] + (dataframe['20 Day STD'] * 2)
    dataframe['Lower Band'] = dataframe['20 Day MA'] - (dataframe['20 Day STD'] * 2)

1 个答案:

答案 0 :(得分:0)

您的代码并非真正可复制。让我们点菜吧

# first import libraries
import pandas as pd
import requests

#define functions
def make_RSI(dataframe):
    delta = dataframe['C'].diff()
    dUp, dDown = delta.copy(), delta.copy()
    dUp[dUp < 0] = 0
    dDown[dDown > 0] = 0
    RolUp = dUp.rolling(14).mean()
    RolDown = dDown.rolling(14).mean().abs()

    RS = RolUp / RolDown
    dataframe['RSI'] = 100 - (100/(1+RS))

def make_bollinger_bands(dataframe):
    dataframe['20 Day MA'] = dataframe['C'].rolling(window=20).mean()
    dataframe['20 Day STD'] = dataframe['C'].rolling(window=20).std()
    dataframe['Upper Band'] = dataframe['20 Day MA'] + (dataframe['20 Day STD'] * 2)
    dataframe['Lower Band'] = dataframe['20 Day MA'] - (dataframe['20 Day STD'] * 2)

#############
# your code #
############
BinanceHistoricalUrl = "https://api.binance.com/api/v1/klines?"
BinanceHistoricalPayload = {'symbol' : 'BTCUSDT','interval': '1m','limit': 100}
#get data
HistoricalRequestData = requests.get(url=BinanceHistoricalUrl, 
                                     params=BinanceHistoricalPayload)\
                                .json()

# put on a dataframe
dataframe = pd.DataFrame(HistoricalRequestData)
# consider only columns from 1 to 4(included)
dataframe = dataframe[dataframe.columns[1:5]]
# assign column names
dataframe.columns = ["O", "H", "L", "C"]
# set type float
dataframe = dataframe.astype("float64")
# call functions
make_RSI(dataframe)
make_bollinger_bands(dataframe)

尚不清楚您最终想要实现什么,但是您只是使用dataframe的最后一行,因此您可能会考虑

last = dataframe.iloc[-1]
DistanceUppBoll = last["Upper Band"] - last["20 Day MA"]
DistanceUndBoll = last["Lower Band"] - last["20 Day MA"]

这花了717 ms在我的笔记本电脑上。我想这主要取决于您的连接速度。

注意:这里的要点是,如果可能,应避免循环。

更新:如果您尝试基于基本技术分析实施交易策略,则应了解如何计算流式传输MA