【量化】4天学会python机器学习与量化交易-笔记2(p11~p15)
·
米筐量化交易API:这里
https://www.ricequant.com/doc/rqdata-institutional
https://www.ricequant.com/quant/#?tag=algorithm-ol&id=899857
【量化】4天学会python机器学习与量化交易-笔记1(p6~p10)
p11 案例:介绍
视频链接:案例:介绍
1,需求
- 选股:获取市盈率大于50且小于65,营业总收入前10的股票。
- 调仓:每日调仓,将所有资金平摊到这10个股票的购买策略,卖出时一次性卖出所有不符合条件的股票。
2,自己实现的(不知道是否正确)
def handle_bar(context,bar_dict):
q = query(
fundamentals.eod_derivative_indicator.pe_ratio,
fundamentals.income_statement.operating_revenue
).filter(
# 市盈率大于50且小于65
fundamentals.eod_derivative_indicator.pe_ratio>50,
fundamentals.eod_derivative_indicator.pe_ratio<65
).order_by(
# 营业总收入前10
fundamentals.income_statement.operating_revenue.desc()
).limit(10) # 取10支
# 每天获取符合条件的10支股票
fund = get_fundamentals(q)
# 先用变量存储昨天的
yesterday = context.stocks
# 获取今天的
today = fund.columns.values.tolist()
# context.stocks = today
# 要卖掉的:在yesterday中但不在today中
to_sell_stocks = [x for x in yesterday if x not in today ]
# 要买入的:在today中但不在yesterday中
to_buy_stocks = [y for y in today if y not in yesterday]
# 卖出
print("卖的:",to_sell_stocks)
print("买的:",to_buy_stocks)
print("====")
if len(to_sell_stocks)!=0:
percent1 = 1/len(to_sell_stocks)
for stock_x in to_sell_stocks:
order_percent(stock_x, -percent1)
# 买入
if len(to_buy_stocks)!=0:
percent2 = 1/len(to_buy_stocks)
for stock_y in to_buy_stocks:
order_percent(stock_y, percent2)
# 持仓,明天要以它为基准
context.stocks = context.portfolio.positions.keys()
回测结果:
时间选择2017-01-01~ 2018-05-01.

p12 实现简单的一个选股策略
视频链接:实现简单的一个选股策略
回测常取5~10年周期。
1,老师答案:
def before_trading(context):
q = query(
fundamentals.eod_derivative_indicator.pe_ratio,
fundamentals.income_statement.revenue
).filter(
# 市盈率大于50且小于65
fundamentals.eod_derivative_indicator.pe_ratio>50,
).filter(
fundamentals.eod_derivative_indicator.pe_ratio<65
).order_by(
# 营业总收入前10
fundamentals.income_statement.operating_revenue.desc()
).limit(10) # 取10支
fund = get_fundamentals(q)
# 获取股票代码
context.stock_list = fund.T.index
pass
def handle_bar(context,bar_dict):
# 在这里才能进行交易
# 卖出
if len(context.portfolio.positions.keys()) != 0:
for stock in context.portfolio.positions.keys():
if stock not in context.stock_list:
# 卖出,直到为0
order_target_percent(stock, 0)
# 买入
weight = 1.0 / len(context.stock_list)
for stock in context.stock_list:
order_target_percent(stock, weight)
pass
回测结果:

补充:我觉得老师这个有bug,要是某个股票昨天没卖怎么办,你今天又去买了。
从结果上看,我的回测收益和夏普比例要稍微优于老师的。
2,按月调参
# 按照月去调参数
def init(context):
scheduler.run_monthly(get_data, tradingday=1)
pass
def get_data(context, bar_dict):
# 每个月先调参
q = query(
fundamentals.eod_derivative_indicator.pe_ratio,
fundamentals.income_statement.revenue
).filter(
# 市盈率大于50且小于65
fundamentals.eod_derivative_indicator.pe_ratio>50,
).filter(
fundamentals.eod_derivative_indicator.pe_ratio<65
).order_by(
# 营业总收入前10
fundamentals.income_statement.operating_revenue.desc()
).limit(10) # 取10支
fund = get_fundamentals(q)
# 获取股票代码
context.stock_list = fund.T.index
def before_trading(context):
pass
def handle_bar(context,bar_dict):
# 在这里才能进行交易
# 卖出
if len(context.portfolio.positions.keys()) != 0:
for stock in context.portfolio.positions.keys():
if stock not in context.stock_list:
# 卖出,直到为0
order_target_percent(stock, 0)
# 买入
weight = 1.0 / len(context.stock_list)
for stock in context.stock_list:
order_target_percent(stock, weight)
pass

为什么老师的回测年化收益率为-16%(他说这种方式选股有问题),我跑出来的结果为-1%,已经比之前的-18%要好很多了。
3,在沪深300中选股
# 按照月去调参数
def init(context):
# 定义一个选股的范围
context.hs300 = index_components("000300.XSHG")
scheduler.run_monthly(get_data, tradingday=1)
pass
def get_data(context, bar_dict):
# 每个月先调参
q = query(
fundamentals.eod_derivative_indicator.pe_ratio,
fundamentals.income_statement.revenue
).filter(
# 市盈率大于50且小于65
fundamentals.eod_derivative_indicator.pe_ratio>50,
).filter(
fundamentals.eod_derivative_indicator.pe_ratio<65
).order_by(
# 营业总收入前10
fundamentals.income_statement.operating_revenue.desc()
).filter(
fundamentals.stockcode.in_(context.hs300)
).limit(10) # 取10支
fund = get_fundamentals(q)
# 获取股票代码
context.stock_list = fund.T.index
def before_trading(context):
pass
def handle_bar(context,bar_dict):
# 在这里才能进行交易
# 卖出
if len(context.portfolio.positions.keys()) != 0:
for stock in context.portfolio.positions.keys():
if stock not in context.stock_list:
# 卖出,直到为0
order_target_percent(stock, 0)
# 买入
weight = 1.0 / len(context.stock_list)
for stock in context.stock_list:
order_target_percent(stock, weight)
pass
效果:

比刚才要好,比老师的要好太多。。
4,删掉两个条件
# 删掉两个条件
# .filter(
# # 市盈率大于50且小于65
# fundamentals.eod_derivative_indicator.pe_ratio>50,
# ).filter(
# fundamentals.eod_derivative_indicator.pe_ratio<65
# )

p13 总结
视频链接:总结
略过。
p14 复习
视频链接:复习
略过。
p15 alpha与beta多因子策略介绍
视频链接:alpha与beta多因子策略介绍
1,股票量化策略
- 趋势交易(技术分析)、市场中性(基本面分析)
- 趋势追踪:通过交易手段获取超额收益
- 多因子选股策略:通过选股获取超额收益
2,Alpha与Beta
- 与市场相关的:Beta
- 与市场无关的:Alpha
- 回归方程:y总收益 = 市场表现*beta + alpha收益
– 超额汇报alpha较难获取,beta容易。
– alpha为精选个股,跑赢市场。
– beta为市场有行情要跟上,有风险要躲避。 - 获取alpha超额收益:有很多策略,比如多因子选股策略。
3,多因子策略
- 记忆股票的多因子策略的理论基础
- 找到影响股票收益的因子,然后利用这些因子进行选股。
- 特征:因子 目标值:股票收益(计算)
- 多因子:就是基本面数据也包括技术因子的指标
– 大类因子:成长、动量、 价值、 估值等。
– 包含了一些具体的因子。 - 上百种因子
4,多因子策略的优势
- 多元因子,alpha收益 的丰富来源,多因子持续稳定
- 根据市场环境变化选取最优因子和权重,模型可修改
5,多因子理论基础
- 资产定价模型:完全跟市场相关,无风险利率(国债)。
– 单因子模型:市场风险因子。没有超额收益alpha - APT模型:不仅仅跟市场相关,还跟某些特征相关,没有指出因子是哪些。(多因子模型)
- FF三因子模型:肯定存在超额收益,三因子模型指出市场风险溢价因子、规模因子、价值因子。
- FF五因子模型:发现原来的三因子模型不能完全解释所有收益,还存在一些额外alpha收益,加入盈利因子和成长因子这两类因子。
- 这些因子模型:大家公认的这些类别因子是肯定能够获得比市场基础汇报要多的收益(风险收益),自己寻找更多其他没有公认额外因子收益(alpha收益)
- 广义alpha收益 = 公认的风险因子收益 + alpha收益
(一晃又过了一个多星期没有学这个了,应该抽空学习,坚持下去)
更多推荐


所有评论(0)