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

importpandasaspd

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 afterdf['prev_close']=df['close'].shift(1)df['next_close']=df['close'].shift(-1)## BOOLEAN TO RETURN IF PRICE IS LOWER THAN PREVIOUS AND NEXT DAYdf['high_high']=((df['prev_close'])>df['close'])&((df['next_close'])>df['close'])## BOOLEAN TO RETURN TRUE IF PRICE IS GREATER THAN PREVIOUS AND NEXT DAYdf['low_low']=((df['prev_close'])df['comp_price']## DESIGNATE FIRST INSTANCE OF BUY SIGNAL AS DAY TO OPEN POSITIONdf['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 SIGNALdf['sell_sig']=df['close']0)&(df['open_pos_price']>0))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值