【问题标题】:Python pandas with freqtrade带有 freqtrade 的 Python pandas
【发布时间】: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”列在哪里?
  • 抱歉,趋势 = 趋势线

标签: python pandas dataframe


【解决方案1】:

例子

data = {'high': {0: 123, 1: 222, 2: 333, 3: 555, 4: 231},
        'low': {0: 112, 1: 121, 2: 231, 3: 444, 4: 211},
        'up': {0: 0, 1: 1, 2: 1, 3: 0, 4: 0}}
df = pd.DataFrame(data)

df

    high    low up
0   123     112 0
1   222     121 1
2   333     231 1
3   555     444 0
4   231     211 0

代码

df['high'].div(df['low']).where(df['up'].eq(1)).ffill()

结果:

0   NaN
1   1.83
2   1.44
3   1.44
4   1.44
dtype: float64

将结果添加到趋势列

df.assign(trend=df['high'].div(df['low']).where(df['up'].eq(1)).ffill())

输出:

    high    low up  trend
0   123     112 0   NaN
1   222     121 1   1.83
2   333     231 1   1.44
3   555     444 0   1.44
4   231     211 0   1.44

【讨论】:

  • 谢谢,也许它适用于一个条件,但如果 3 个条件 up == 1 和 up == -1,rest 0 take previous?编辑(添加代码)
猜你喜欢
  • 1970-01-01
  • 2019-01-08
  • 2018-02-24
  • 2018-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多