簡體   English   中英

如何優化Pandas DataFrame的速度?

[英]How to optimize Pandas DataFrame speed?

我有這個熊貓代碼,但它很慢。 我如何優化它? 這意味着當我運行它時,大約需要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)

您的代碼不是真正可重現的。 來點菜吧

# 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM