【发布时间】:2022-12-07 12:08:46
【问题描述】:
我将 python pandas 与 freqttade 一起使用。 我有这样的数据框
[
'high', 'low', 'up', 'trend'
123, 112, 0, 0
222, 121, 1, 0
333, 231, 1, 0
555, 444, 0, 0
231, 211, 0, 0
]
基于 UP 数据我想填充趋势
# example
dataframe.loc[
(dataframe["up"] == 1)
, "trend"] = dataframe["high"] / dataframe["low"]
# get prev trend if up == 0
dataframe.loc[
(dataframe["up"] == 0)
, "trend"] = dataframe["trend"].shift(1)
我得到什么作为输出
[
'high', 'low', 'up', 'trend'
123, 112, 0, 0
222, 121, 1, 1.83
333, 231, 1, 1.44
555, 444, 0, 1.44 - this is what i get
231, 211, 0, 1.44 - and this is no longer
]
我理解为接收到上一个时TREND列中的数据还不存在,请问如何实现呢?
如果列 UP == 0 则获取以前的数据
编辑条件:
dataframe["trend"].fillna(0.0)
# up
dataframe.loc[
(dataframe["up"] == 1)
, "trend"] = dataframe["low"] - dataframe['high']
# down
dataframe.loc[
(dataframe["up"] == -1)
, "trend"] = dataframe["high"] + dataframe['low']
# if up == 0, get prev data
dataframe.loc[
(dataframe["up"] == 0)
, "trend"] = dataframe["trend"].shift(1)
如果添加循环,修复它(但这是一个非常糟糕的主意)但它有效,没有这样的拐杖如何解决?
maxRange = len(dataframe.trend)
for x in range(maxRange)
dataframe.loc[
(dataframe["up"] == 0)
, "trend"] = dataframe["trend"].shift(1)
【问题讨论】:
-
你想要的输出是什么?趋势栏 = 0 1.83 1.44 1.44 0?
-
1.83 - 1.44 - 1.44 - 1.44
-
“TrendLine”列在哪里?
-
抱歉,趋势 = 趋势线