【发布时间】:2019-03-20 02:27:52
【问题描述】:
我有这个 pandas 代码,但是速度很慢。我该如何优化它?这意味着当我运行它时,大约需要 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)
【问题讨论】:
-
你看过pandas' guide for performance tuning吗?我不确定
make_RSI和make_bollinger_bands做了什么,但我怀疑它们也可以进行优化。 -
嗨 jandevies,欢迎来到 SO。请生成mcve,特别是发布您的原始数据样本和预期输出。在我看来,这是一个非常容易矢量化的问题。
-
@JordanSinger 我不确定 OP 使用的是哪个库,但有许多优化良好的金融库。
-
我正在使用熊猫@user32185
-
@jandevries 这很清楚。不过,如果您能够制作一个可重复的示例,您很快就会得到答案。
标签: python pandas dataframe optimization