python 画线 平仓_使用python和pandas在一个tim只识别一个未平仓头寸,对交易策略进行回溯测试...

这是一个很长的阅读,但我看了很多关于StackOverflow的例子,创建函数来迭代数据帧等等,但是没有找到任何适合我的需求。我也只使用了python和一般的代码大约2个月,所以如果有什么不清楚的地方,我很抱歉。在

我有一个包含每日价格历史的数据框,并尝试基于以下策略创建购买信号的回溯测试:

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

为了启动买入信号,我们等待收盘价回到“基准日”上方的一天。我们现在有一个空头头寸。在

我们一直保持这个位置,直到我们得到一个卖出信号,这个信号与我们所寻找的买入信号相反。(即收盘价低于前一日,且前一日和后一日较高)

我只希望一次有一个买入是活跃的,直到我们得到卖出信号,然后整个过程重新开始。在

下面是一个示例dataframe,其中包含我正在查看的一小部分数据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))

然后,我创建了另一个数据帧,当我收到sell信号时显示结果,这样我可以稍后将结果转换为元组,并迭代以添加事务成本等,以完成图表目的。在

^{pr2}$

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

我想把我的第一个买入信号作为我唯一的头寸,忽略所有其他买入信号,直到我得到卖出信号。在这一点上,我想寻找一个新的买入信号,并持有一个头寸,直到我得到一个新的卖出。在

我可能已经创建了太多的列,但是我很难找到一种方法来获得一个独特的信号来定位,然后将价格与我收到卖出信号时的价格进行比较。如果有人能推荐一种更清洁的方法来做到这一点,我很乐意放弃我的第一次尝试,尝试一下。在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值