我有一个开放/高/低/收盘价的熊猫数据框,我正在写一个函数,它将抛物线形SAR添加到我的数据框中。目前,PSAR的数量正疯狂地增长,而且我似乎从未在牛市和熊市之间转换时获得太多收益。任何有助于理解我的PSAR为何如此疯狂的帮助都将是巨大的。我已经尝试过对此代码进行几种变体,但无济于事。
对于不熟悉PSAR的人:
加速度因子乘以极点和上一时期的SAR之差。然后将其添加到上一时期的SAR。但是请注意,SAR永远不能高于前两个周期的低点。如果SAR高于最低点之一,请使用两者中的最低点。
Current SAR = Prior SAR + Prior AF(Prior EP - Prior SAR)
例如:10年4月13日: SAR = 48.28 = 48.13 + .14(49.20-48.13)
加速度因子乘以上一时期的SAR与极点之间的差。然后从上一时期的SAR中减去此费用。但是请注意,SAR永远不能低于前两个周期的高点。如果SAR低于上述最高点之一,请使用SAR中的最高点。
Current SAR = Prior SAR - Prior AF(Prior EP - Prior SAR)
例如:10年2月9日: SAR = 43.56 = 43.84-.16(43.84-42.07)
在反转期间,取决于翻转的方向,PSAR成为先前的极限点EP,而新的EP为先前的最高点或最低点。 AF重置为0.02。
我的功能:
def addSAR(df):
df.loc[0, 'AF'] =0.02
df.loc[0, 'PSAR'] = df.loc[0, 'low']
df.loc[0, 'EP'] = df.loc[0, 'high']
df.loc[0, 'PSARdir'] = "bull"
for a in range(1, len(df)):
if df.loc[a-1, 'PSARdir'] == 'bull':
df.loc[a, 'PSAR'] = df.loc[a-1, 'PSAR'] + (df.loc[a-1, 'AF']*(df.loc[a-1, 'EP']-df.loc[a-1, 'PSAR']))
df.loc[a, 'PSARdir'] = "bull"
if df.loc[a, 'low'] < df.loc[a-1, 'PSAR']:
df.loc[a, 'PSARdir'] = "bear"
df.loc[a, 'PSAR'] = df.loc[a-1, 'EP']
df.loc[a, 'EP'] = df.loc[a-1, 'low']
df.loc[a, 'AF'] = .02
else:
if df.loc[a, 'high'] > df.loc[a-1, 'EP']:
df.loc[a, 'EP'] = df.loc[a, 'high']
if df.loc[a-1, 'AF'] <= 0.18:
df.loc[a, 'AF'] =df.loc[a-1, 'AF'] + 0.02
else:
df.loc[a, 'AF'] = df.loc[a-1, 'AF']
elif df.loc[a, 'high'] <= df.loc[a-1, 'EP']:
df.loc[a, 'AF'] = df.loc[a-1, 'AF']
df.loc[a, 'EP'] = df.loc[a-1, 'EP']
elif df.loc[a-1, 'PSARdir'] == 'bear':
df.loc[a, 'PSAR'] = df.loc[a-1, 'PSAR'] - (df.loc[a-1, 'AF']*(df.loc[a-1, 'EP']-df.loc[a-1, 'PSAR']))
df.loc[a, 'PSARdir'] = "bear"
if df.loc[a, 'high'] > df.loc[a-1, 'PSAR']:
df.loc[a, 'PSARdir'] = "bull"
df.loc[a, 'PSAR'] = df.loc[a-1, 'EP']
df.loc[a, 'EP'] = df.loc[a-1, 'high']
df.loc[a, 'AF'] = .02
else:
if df.loc[a, 'low'] < df.loc[a-1, 'EP']:
df.loc[a, 'EP'] = df.loc[a, 'low']
if df.loc[a-1, 'AF'] <= 0.18:
df.loc[a, 'AF'] = df.loc[a-1, 'AF'] + 0.02
else:
df.loc[a, 'AF'] = df.loc[a-1, 'AF']
elif df.loc[a, 'low'] >= df.loc[a-1, 'EP']:
df.loc[a, 'AF'] = df.loc[a-1, 'AF']
df.loc[a, 'EP'] = df.loc[a-1, 'EP']
return df
答案 0 :(得分:1)
弄清楚了 Facepalm
df.loc[a, 'PSAR'] = df.loc[a-1, 'PSAR'] + (df.loc[a-1, 'AF']*(df.loc[a-1, 'EP']-df.loc[a-1, 'PSAR']))
应该为df.loc[a, 'PSAR'] = df.loc[a-1, 'PSAR'] + (df.loc[a-1, 'AF']*(df.loc[a-1, 'PSAR']-df.loc[a-1, 'EP']))
最后两个变量转置!多么痛苦...
现在我可以清理该功能并将其改进。.
希望这可以帮助其他人做一些愚蠢的事情,并使其停留2天
答案 1 :(得分:0)
我对您的功能进行了如下编辑:
对于我来说,我的数据框的索引是日期时间,因此我将所有a
都转换为df.index[a]
,并且将a-1
都转换为df.index[a-1]
。我也更改了AF
,PSAR
,EP
,PSARdir
列,因为所有行值都将初始化为相同的值,例如df['AF'] =0.02
def addSAR(df):
df['AF'] =0.02
df['PSAR'] = df['Low']
df['EP'] = df['High']
df['PSARdir'] = "bull"
for a in range(1, len(df)):
if df.loc[df.index[a-1], 'PSARdir'] == 'bull':
df.loc[df.index[a], 'PSAR'] = df.loc[df.index[a-1], 'PSAR'] + (df.loc[df.index[a-1], 'AF']*(df.loc[df.index[a-1], 'PSAR']-df.loc[df.index[a-1], 'EP']))
df.loc[df.index[a], 'PSARdir'] = "bull"
if df.loc[df.index[a], 'Low'] < df.loc[df.index[a-1], 'PSAR']:
df.loc[df.index[a], 'PSARdir'] = "bear"
df.loc[df.index[a], 'PSAR'] = df.loc[df.index[a-1], 'EP']
df.loc[df.index[a], 'EP'] = df.loc[df.index[a-1], 'Low']
df.loc[df.index[a], 'AF'] = .02
else:
if df.loc[df.index[a], 'High'] > df.loc[df.index[a-1], 'EP']:
df.loc[df.index[a], 'EP'] = df.loc[df.index[a], 'High']
if df.loc[df.index[a-1], 'AF'] <= 0.18:
df.loc[df.index[a], 'AF'] =df.loc[df.index[a-1], 'AF'] + 0.02
else:
df.loc[df.index[a], 'AF'] = df.loc[df.index[a-1], 'AF']
elif df.loc[df.index[a], 'High'] <= df.loc[df.index[a-1], 'EP']:
df.loc[df.index[a], 'AF'] = df.loc[df.index[a-1], 'AF']
df.loc[df.index[a], 'EP'] = df.loc[df.index[a-1], 'EP']
elif df.loc[df.index[a-1], 'PSARdir'] == 'bear':
df.loc[df.index[a], 'PSAR'] = df.loc[df.index[a-1], 'PSAR'] - (df.loc[df.index[a-1], 'AF']*(df.loc[df.index[a-1], 'EP']-df.loc[df.index[a-1], 'PSAR']))
df.loc[df.index[a], 'PSARdir'] = "bear"
if df.loc[df.index[a], 'High'] > df.loc[df.index[a-1], 'PSAR']:
df.loc[df.index[a], 'PSARdir'] = "bull"
df.loc[df.index[a], 'PSAR'] = df.loc[df.index[a-1], 'EP']
df.loc[df.index[a], 'EP'] = df.loc[df.index[a-1], 'High']
df.loc[df.index[a], 'AF'] = .02
else:
if df.loc[df.index[a], 'Low'] < df.loc[df.index[a-1], 'EP']:
df.loc[df.index[a], 'EP'] = df.loc[df.index[a], 'Low']
if df.loc[df.index[a-1], 'AF'] <= 0.18:
df.loc[df.index[a], 'AF'] = df.loc[df.index[a-1], 'AF'] + 0.02
else:
df.loc[df.index[a], 'AF'] = df.loc[df.index[a-1], 'AF']
elif df.loc[df.index[a], 'Low'] >= df.loc[df.index[a-1], 'EP']:
df.loc[df.index[a], 'AF'] = df.loc[df.index[a-1], 'AF']
df.loc[df.index[a], 'EP'] = df.loc[df.index[a-1], 'EP']
return df