Pandas 数据帧上的追踪止损

问题描述 投票:0回答:3

我正在 Pandas 数据框架上对股票市场的一些交易策略进行回溯测试,我想将追踪止损设置为距输入价格 1%。如果股价上涨 5%,那么追踪止损也会上涨 5%。如果股价下跌,追踪止损不会改变。 (https://www.investopedia.com/terms/t/trailingstop.asp)

我有这个表格,其中显示了我的入场信号,如果价格低于追踪止损价格,则退出栏将显示值 1,这意味着交易已退出。

这是我到目前为止的表格:

date           price      entry_signal      
30/06/2018     95              0                
01/07/2018     100             1                
02/07/2018     103             0                
03/07/2018     105             0                
04/07/2018     104.50          0                
05/07/2018     101             0                

我想要一栏显示每个日期的追踪止损是多少。追踪止损首先设置为 2018 年 1 月 7 日输入信号 = 1 时价格的 99%,并在该日期执行交易。

当价格上涨 y% 时,追踪止损也会上涨 y%。然而,如果价格下跌,追踪止损将不会改变其最后的值。

什么时候涨价<= trailing stop loss, the trade is exited and there will be an exit_signal of 1...

我目前陷入困境,如果价格也下跌 y%,则追踪止损不会下跌 y%....

期望的表结果:

date           price      trailing stop loss      entry_signal      exit_signal
30/06/2018     95              NULL                     0                0
01/07/2018     100             99                       1                0
02/07/2018     103             101.97                   0                0
03/07/2018     105             103.95                   0                0
04/07/2018     104.50          103.95                   0                0
05/07/2018     101             103.95                   0                1

我得到的表:

date           price      trailing stop loss      entry_signal      
30/06/2018     95              NULL                     0                
01/07/2018     100             99                       1                
02/07/2018     103             101.97                   0                
03/07/2018     105             103.95                   0                
04/07/2018     104.50          103.455                  0                
05/07/2018     101             99.99                    0                
python pandas quantitative-finance back-testing
3个回答
15
投票

只需取累计最大值的99%并与当前价格进行比较:

df = pd.DataFrame({"price":[95,100,103,105,104.5,101]}) #create price array
df['highest'] = df.cummax() #take the cumulative max
df['trailingstop'] = df['highest']*0.99 #subtract 1% of the max
df['exit_signal'] = df['price'] < df['trailingstop'] #generate exit signal


Out[1]: 
   price  highest  trailingstop  exit_signal
0   95.0     95.0         94.05        False
1  100.0    100.0         99.00        False
2  103.0    103.0        101.97        False
3  105.0    105.0        103.95        False
4  104.5    105.0        103.95        False
5  101.0    105.0        103.95         True

10
投票

涉及难题

cummax
pct_change
+
clip_lower
+
cumprod

s=df.loc[df.entry_signal.cummax().astype(bool),'price'].pct_change().add(1).fillna(1)

df['trailing stop loss']=s.clip_lower(1).cumprod()*99
df['exit_signal']=(df['trailing stop loss']>df['price']).astype(int)
df
Out[114]: 
         date  price  entry_signal  trailing stop loss  exit_signal
0  30/06/2018   95.0             0                 NaN            0
1  01/07/2018  100.0             1               99.00            0
2  02/07/2018  103.0             0              101.97            0
3  03/07/2018  105.0             0              103.95            0
4  04/07/2018  104.5             0              103.95            0
5  05/07/2018  101.0             0              103.95            1

0
投票

如何在同一日期范围内多次执行此操作?假设您有多个入场信号,并且对于每个入场信号,找到追踪止损出场信号?追踪止损在每个入场信号处开始。

© www.soinside.com 2019 - 2024. All rights reserved.