首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当多个条件等于True时,设置'Buy‘信号/backtest的最佳方法(股票数据)

当多个条件等于True时,设置'Buy‘信号/backtest的最佳方法(股票数据)
EN

Stack Overflow用户
提问于 2020-01-01 00:04:40
回答 1查看 218关注 0票数 0

使用每日Date、Open、High、Low、Close股票数据,我试图更好地理解在回测多个条件时要使用的语句类型的好方法。例如:

代码语言:javascript
复制
#Signal:
Todays Close > Todays Open  AND
Yesterdays Close > Yesterdays Open  AND
Todays Close >= Todays High - 10%

#Position:
If ALL of the signal conditions above are true, then "Buy" tomorrow at (todays High + 5%) and "Sell" at the Close of the day. 
**To take the position I would have to test that the "Buy" condition was satisfied on the 'tomorrow' bar

#Calculate Return:
If Position taken, calculate profit or loss for the day

我见过示例算法,但许多示例只是基本的移动平均交叉系统(一个条件),使用矢量化方法非常简单。

当你有像上面这样的多个条件时,有人能告诉我一个很好的编码方法吗?

EN

回答 1

Stack Overflow用户

发布于 2020-01-01 00:27:09

假设您的数据已按日期排序并按顺序编制索引,请尝试执行以下操作:

代码语言:javascript
复制
cond1 = df['Close'] > df['Open']
cond2 = df['Close'].shift() > df['Open'].shift()
cond3 = df['Close'] >= (df['High'] * 0.9)
signal = df[cond1 & cond2 & cond3]

df.loc[signal.index + 1, 'BuyAt'] = (signal['High'] * 1.05).values
df.loc[signal.index + 1, 'SellAt'] = df.loc[signal.index + 1, 'Close']
df['PnL'] = df['SellAt'] - df['BuyAt']

结果(来自雅虎财经的MSFT股票价格):

代码语言:javascript
复制
        Date        Open        High         Low       Close       BuyAt      SellAt       PnL
0  2019-01-02   99.550003  101.750000   98.940002  101.120003         NaN         NaN       NaN
1  2019-01-03  100.099998  100.190002   97.199997   97.400002         NaN         NaN       NaN
2  2019-01-04   99.720001  102.510002   98.930000  101.930000         NaN         NaN       NaN
3  2019-01-07  101.639999  103.269997  100.980003  102.059998         NaN         NaN       NaN
4  2019-01-08  103.040001  103.970001  101.709999  102.800003  108.433497  102.800003 -5.633494
5  2019-01-09  103.860001  104.879997  103.239998  104.269997         NaN         NaN       NaN
6  2019-01-10  103.220001  103.750000  102.379997  103.599998         NaN         NaN       NaN
7  2019-01-11  103.190002  103.440002  101.639999  102.800003  108.937500  102.800003 -6.137497
8  2019-01-14  101.900002  102.870003  101.260002  102.050003         NaN         NaN       NaN
9  2019-01-15  102.510002  105.050003  101.879997  105.010002         NaN         NaN       NaN
10 2019-01-16  105.260002  106.260002  104.959999  105.379997  110.302503  105.379997 -4.922506
11 2019-01-17  105.000000  106.629997  104.760002  106.120003  111.573002  106.120003 -5.452999
12 2019-01-18  107.459999  107.900002  105.910004  107.709999  111.961497  107.709999 -4.251498
13 2019-01-22  106.750000  107.099998  104.860001  105.680000  113.295002  105.680000 -7.615002
14 2019-01-23  106.120003  107.040001  105.339996  106.709999         NaN         NaN       NaN
15 2019-01-24  106.860001  107.000000  105.339996  106.199997         NaN         NaN       NaN
16 2019-01-25  107.239998  107.879997  106.199997  107.169998         NaN         NaN       NaN
17 2019-01-28  106.260002  106.480003  104.660004  105.080002         NaN         NaN       NaN
18 2019-01-29  104.879997  104.970001  102.169998  102.940002         NaN         NaN       NaN
19 2019-01-30  104.620003  106.379997  104.330002  106.379997         NaN         NaN       NaN

在我看来,这是一个失败的策略!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59546279

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档