Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

代码示例教学贴:(选股篇) #674

Open
yutiansut opened this issue Jul 4, 2018 · 15 comments
Open

代码示例教学贴:(选股篇) #674

yutiansut opened this issue Jul 4, 2018 · 15 comments

Comments

@yutiansut
Copy link
Owner

yutiansut commented Jul 4, 2018

选出全市场近100天威廉指标WR1连续两天为0的股票?

import QUANTAXIS as QA
code=QA.QA_fetch_stock_list_adv().code.tolist()
day_data=QA.QA_fetch_stock_day_adv(code,'2018-01-03','2018-07-04')
ind=day_data.add_func(QA.QA_indicator_WR,10,6)
ind_ds=QA.QA_DataStruct_Indicators(ind)
res=ind_ds.add_func(lambda x:(x.tail(100))['WR1'].rolling(2).sum()==0)
res[res]
@yutiansut
Copy link
Owner Author

yutiansut commented Jul 4, 2018

##[升级] 在一年的周期内, 选出前100天威廉指标WR1连续两天为0的股票?

import QUANTAXIS as QA
code=QA.QA_fetch_stock_list_adv().code.tolist()
day_data=QA.QA_fetch_stock_day_adv(code,'2017-01-03','2018-07-04')
ind=day_data.add_func(QA.QA_indicator_WR,10,6)
res=ind.groupby(level=1,as_index=False,group_keys=False).apply(lambda x: x.WR1.rolling(2).sum()).groupby(level=1).apply(lambda x:x.rolling(100).min()<=0).sort_index()
res[res]

@pchaos
Copy link
Contributor

pchaos commented Jul 5, 2018

x.tail(100))['WR1'],中间停牌了的就不是100天以内的数据了?

@yutiansut
Copy link
Owner Author

@pchaos 有道理

@yutiansut
Copy link
Owner Author

应该是select_time_with_gap

@pchaos
Copy link
Contributor

pchaos commented Jul 5, 2018

我也是测试这个公式的时候发现这个时间问题的:
30天内创半年新高
tdate = '2018-1-2'
end = datetime.datetime.now().date()
code = qa.QA_fetch_stock_list_adv().code.tolist()
data = qa.QA_fetch_stock_day_adv(code, start=tdate, end=end)
ind = data.add_func(lambda x: x.tail(30).high.max() == x.high.max())
code = list(ind[ind].reset_index().code)

@yutiansut
Copy link
Owner Author

不对 你这个需求当时不是无视停牌我才这么说的吗/...

@pchaos
Copy link
Contributor

pchaos commented Jul 5, 2018

创新高以后的最低点能一条语句找出来吗?

@pchaos
Copy link
Contributor

pchaos commented Jul 5, 2018

30天内创半年新高
data = QA.QA_fetch_stock_day_adv(code, start=tdate, end=end).to_qfq()
ind = data.add_func(lambda x: x.tail(30).high.max() == x.high.max())
inc=QA.QA_DataStruct_Indicators(ind)
调研inc的时候会报错: return '< QA_DATASTRUCT_INDICATOR FROM {} TO {} WITH {} CODES >'.format(self.data.index.levels[0][0],self.data.index.levels[0][-1],len(self.data.index.levels[1]))
AttributeError: 'Index' object has no attribute 'levels'

上面还是有逻辑问题,这个逻辑是在最近半年,创新高在30交易日以内。
不是完整的创半年新高,创半年新高应该是:半年时间内,最后一天创新高;

@pchaos
Copy link
Contributor

pchaos commented Jul 6, 2018

day_data=QA.QA_fetch_stock_day_adv(code,'2017-01-03','2018-07-04')
是否要加上 .to_qfq()?

@yutiansut
Copy link
Owner Author

创新高以后的最低点 算法和 最大回撤的算法一致 @pchaos

@yutiansut
Copy link
Owner Author

@pchaos 要加的

@pchaos
Copy link
Contributor

pchaos commented Jul 6, 2018

ind = data.add_func(lambda x: x.tail(30).high.max() == x.high.max())
这个是标示某个股创新高了,能不能标示出具体是哪一天新高?

@pchaos
Copy link
Contributor

pchaos commented Jul 7, 2018

标示出具体是哪一天新高:
ind = data.add_func(lambda x: x.high == x.high.max())
会报错:ValueError: Duplicated level name: "code", assigned to level 2, is already used for level 0.

@pchaos
Copy link
Contributor

pchaos commented Jul 7, 2018

def cmpfunc(df):
return pd.DataFrame(df.high == df.high.max())

ind = data.add_func(cmpfunc)
这样就可以了

改进一下,半年时间内,最后一天创新高;
def cmpfunc120(df):
# 标记创新高
return pd.DataFrame(df.high == df.high.rolling(120).max()) # 重点是这里
tdate = '2018-1-2'
end = datetime.datetime.now().date()
code = qa.QA_fetch_stock_list_adv().code.tolist()
data = qa.QA_fetch_stock_day_adv(code, start=tdate, end=end).to_qfq()
ind = data.add_func(lambda x: x.tail(20).high.max() == x.high.max())
ind = data.add_func(cmpfunc120)

@ccliuyang
Copy link

选出全市场近100天威廉指标WR1连续两天为0的股票?

import QUANTAXIS as QA
code=QA.QA_fetch_stock_list_adv().code.tolist()
day_data=QA.QA_fetch_stock_day_adv(code,'2018-01-03','2018-07-04')
ind=day_data.add_func(QA.QA_indicator_WR,10,6)
ind_ds=QA.QA_DataStruct_Indicators(ind)
res=ind_ds.add_func(lambda x:(x.tail(100))['WR1'].rolling(2).sum()==0)
res[res]

试了一下这个代码,发现在day_data获取不到数据的时候,会出现错误。刚开始测试自己写的指标的时候,跑着跑着就会出现问题,以为是自己的指标的问题,百思不得其解。后来单个add_func才发现是没有获取到数据(由于股票未上市、或者退市)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants