gpt4 book ai didi

python - 使用 python 和 pandas 回测交易策略 - 一次仅识别一个未平仓头寸

转载 作者:行者123 更新时间:2023-12-01 09:27:16 38 4
gpt4 key购买 nike

这是一篇很长的文章,但我在 StackOverflow 上浏览了很多创建函数来迭代 DataFrame 等的示例,但找不到任何可以满足我需求的内容。我也只使用 python 和编码大约 2 个月,所以如果有不清楚的地方,我深表歉意。

我有一个包含每日价格历史记录的数据框,并尝试根据此策略创建购买信号的回测:

我们首先寻找收盘价大于前一天和后一天收盘价的一天。我们称之为“基准日”。

为了发出买入信号,我们等待收盘价回到“基准日”上方的那一天。我们现在有一个空缺职位。

我们持有该仓位,直到收到与我们所寻找的买入信号相反的卖出信号。 (即收盘价低于前一天,而前一天和后一天的收盘价较高)

我只想一次激活一个买入,直到我们收到卖出信号,然后该过程重新开始。

下面是一个示例数据框,其中包含我正在查看的一小部分数据

import pandas as pd

data = {
'date': [1/3/2000,1/4/2000,1/5/2000,1/6/2000,1/7/2000,1/10/2000,1/11/2000,1/12/2000,1/13/2000,1/14/2000,1/18/2000,1/19/2000,1/20/2000,1/21/2000,1/24/2000,1/25/2000,1/26/2000,1/27/2000,1/28/2000,1/31/2000,2/1/2000,2/2/2000,2/3/2000,2/4/2000,2/7/2000,2/8/2000,2/9/2000,2/10/2000,2/11/2000,2/14/2000,2/15/2000,2/16/2000,2/17/2000,2/18/2000,2/22/2000,2/23/2000,2/24/2000,2/25/2000,2/28/2000,2/29/2000],

'close': [308.3,315.3,314.4,307.5,309.8,313.4,310.7,324.2,332.5,348.8,351.1,348.2,348.7,343.5,343,343.3,342.4,343,334.4,334.6,336,333.8,331.6,332.8,335.9,341.2,338.4,342.1,343.2,339.5,346.9,342,339.6,337.4,335,330.8,331.3,331.1,332.6,335.1]}

df = pd.DataFrame(data)
## Create columns to compare price to day before and day after
df['prev_close'] = df['close'].shift(1)
df['next_close'] = df['close'].shift(-1)


## BOOLEAN TO RETURN IF PRICE IS LOWER THAN PREVIOUS AND NEXT DAY
df['high_high'] = ((df['prev_close']) > df['close']) & ((df['next_close']) > df['close'])

## BOOLEAN TO RETURN TRUE IF PRICE IS GREATER THAN PREVIOUS AND NEXT DAY
df['low_low'] = ((df['prev_close']) < df['close']) & ((df['next_close']) < df['close'])


## RETURN PRICE OF MOST RECENT true IN low_low
df['comp_price'] = df['close'].where(df['low_low'] == True)
## FILL IN BLANKS WITH PREVIOUS VALUE TO KEEP COMPARISON PRICE ACTIVE
df['comp_price'].fillna(method='pad',inplace=True)

## CREATE SELL COMPARISON DATE TO REFERENCE WHEN CLOSING POSITION
df['sell_comp'] = df['close'].where(df['high_high'] == True)
df['sell_comp'].fillna(method='pad',inplace=True)

## CREATE BUY SIGNAL
df['buy_sig'] = df['close'] > df['comp_price']

## DESIGNATE FIRST INSTANCE OF BUY SIGNAL AS DAY TO OPEN POSITION
df['open_pos'] = (df['buy_sig'] == 1) & (df['buy_sig'].shift(1) != 1)
df['take_signal'] = (df['buy_sig'] == 1) & (df['open_pos'] == True)
df['open_pos_price'] = df['close'].where(df['take_signal'] == True)
df['open_pos_price'].fillna(method='pad',inplace=True)


## CREATE SELL SIGNAL
df['sell_sig'] = df['close'] < df['sell_comp']
## DESIGNATE FIRST INSTANCE OF SELL AS DAY TO CLOSE POSITION
df['close_pos'] = (df['sell_sig'] == True) & (df['sell_sig'].shift(1) == False)

## CREATE COLUMNS THAT ORGANIZE WHEN POSITION WAS OPENED
df['open_pos_date'] = df['date'].where((df['open_pos'] == True)&(df['take_signal'] == True))
df['open_pos_date'].fillna(method='pad',inplace=True)

## CREATE COLUMNS SHOW DATE AND PRICE OF CLOSING POSITION
df['close_pos_price'] = df['close'].where(df['close_pos'] == True)
df['close_pos_date'] = df['date'].where((df['close_pos'] == True))

## CALCULATE GAIN FOR TRADE
df['gain'] = (df['close_pos_price'] - df['open_pos_price']).where((df['close_pos_price'] > 0)& (df['open_pos_price'] > 0))

然后,我创建了另一个数据框,在收到卖出信号时显示结果,以便稍后将结果转换为元组并迭代以添加交易成本等,以完成图表目的。

strat_df = df.loc[(df['close_pos'] == True)&(df['sell_sig'] == True), ['open_pos_date','open_pos_price', 'close_pos_date','close_pos_price','gain']]

我看到同一 open_pos_date 的多个实例具有不同的 close_pos_date 值。在此过程中,我允许多个空缺职位发挥作用。

我想将我的第一个买入信号作为我唯一的头寸,忽略所有其他买入信号,直到收到卖出信号。那时,我想寻找新的买入信号,并仅持有该仓位,直到获得新的卖出信号。

我可能创建了比必要的更多的列,但我很难找到一种方法来获得独特的持仓信号,然后将价格与收到卖出信号时的价格进行比较。如果有人可以推荐一种更干净的方法来做到这一点,我很乐意放弃我的第一次尝试并尝试一下。

最佳答案

虽然您通常希望避免迭代数据帧的行,因为它非常慢且效率低下,但我发现它通常是回溯测试时的最佳方法。由于您的头寸和投资组合值(value)取决于 T-1 值才能计算 T 时的值,因此通常需要逐行计算,而且要简单得多。

import pandas as pd
data = {'date': ['1/3/2000','1/4/2000','1/5/2000','1/6/2000','1/7/2000','1/10/2000',
'1/11/2000','1/12/2000','1/13/2000','1/14/2000','1/18/2000','1/19/2000','1/20/2000','1/21/2000',
'1/24/2000','1/25/2000','1/26/2000','1/27/2000','1/28/2000','1/31/2000','2/1/2000','2/2/2000',
'2/3/2000','2/4/2000','2/7/2000','2/8/2000','2/9/2000','2/10/2000','2/11/2000','2/14/2000',
'2/15/2000','2/16/2000','2/17/2000','2/18/2000','2/22/2000','2/23/2000','2/24/2000','2/25/2000',
'2/28/2000','2/29/2000'],
'close': [308.3,315.3,314.4,307.5,309.8,313.4,310.7,324.2,332.5,348.8,351.1,348.2,348.7,343.5,343,343.3,342.4,343,
334.4,334.6,336,333.8,331.6,332.8,335.9,341.2,338.4,342.1,343.2,339.5,346.9,342,339.6,337.4,335,330.8,331.3,
331.1,332.6,335.1]}

df = pd.DataFrame(data)
df = df.set_index(['date'])
df['pos'] = 0

base_buy = 999999.0
base_sell = 0.0
for i in range(2, df.shape[0] - 1):

px_m1 = df.iloc[i - 1].loc['close']
px = df.iloc[i].loc['close']
px_p1 = df.iloc[i + 1].loc['close']
pos = df.iloc[i - 1].loc['pos']

#base_buy
if px > px_m1 and px > px_p1 and pos == 0:
base_buy = px

#entry signal
if px > base_buy and pos == 0:
pos = 1.0
base_sell = 0.0

#base_sell
if px < px_m1 and px < px_p1 and pos == 1:
base_sell = px

#exit signal
if px < base_sell and pos == 1.0:
pos = 0.0
base_buy = 999999.0

df.iloc[i, 1] = pos

print(df)

输出:

           close  pos
date
1/3/2000 308.3 0.0
1/4/2000 315.3 0.0
1/5/2000 314.4 0.0
1/6/2000 307.5 0.0
1/7/2000 309.8 0.0
1/10/2000 313.4 0.0
1/11/2000 310.7 0.0
1/12/2000 324.2 1.0
1/13/2000 332.5 1.0
1/14/2000 348.8 1.0
1/18/2000 351.1 1.0
1/19/2000 348.2 1.0
1/20/2000 348.7 1.0
1/21/2000 343.5 0.0
1/24/2000 343.0 0.0
1/25/2000 343.3 0.0

关于python - 使用 python 和 pandas 回测交易策略 - 一次仅识别一个未平仓头寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50281005/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com