小编ark*_*har的帖子

使用 pandas python 的 SuperTrend 代码

我正在尝试使用 pandas 在 python 中为 SuperTrend 指标编写以下算法。

BASIC UPPERBAND = (HIGH + LOW) / 2 + Multiplier * ATR
BASIC LOWERBAND = (HIGH + LOW) / 2 - Multiplier * ATR

FINAL UPPERBAND = IF( (Current BASICUPPERBAND < Previous FINAL UPPERBAND) or (Previous Close > Previous FINAL UPPERBAND))
                    THEN (Current BASIC UPPERBAND) ELSE Previous FINALUPPERBAND)
FINAL LOWERBAND = IF( (Current BASIC LOWERBAND > Previous FINAL LOWERBAND) or (Previous Close < Previous FINAL LOWERBAND)) 
                    THEN (Current BASIC LOWERBAND) ELSE Previous FINAL …
Run Code Online (Sandbox Code Playgroud)

python dataframe python-3.x pandas technical-indicator

4
推荐指数
1
解决办法
3万
查看次数

确定列的累积最大值

我正在尝试以下代码

df = pd.DataFrame([[23, 52], [36, 49], [52, 61], [75, 82], [97, 12]], columns=['A', 'B'])
df['C'] = np.where(df['A'] > df['C'].shift(), df['A'], df['C'].shift())
print(df)
Run Code Online (Sandbox Code Playgroud)

假设第一个df['C].shift()操作应假设为0(因为df['C']不存在)

预期产出

    A   B   C
0  23  52  23
1  36  49  36
2  12  61  36
3  75  82  75
4  70  12  75
Run Code Online (Sandbox Code Playgroud)

但我得到一个KeyError异常.

Traceback (most recent call last):
  File "C:\Program Files\Python36\lib\site-packages\pandas\core\indexes\base.py", line 2442, in get_loc
    return self._engine.get_loc(key)
  File "pandas\_libs\index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5280)
  File "pandas\_libs\index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc …
Run Code Online (Sandbox Code Playgroud)

python dataframe pandas

0
推荐指数
1
解决办法
266
查看次数