源码
from quantopian.pipeline import Pipeline
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.factors import SimpleMovingAverage
from quantopian.pipeline.classifiers.morningstar import Sector
from quantopian.pipeline.data import morningstar as morningstar
from quantopian.pipeline.filters.morningstar import Q500US, Q1500US
from quantopian.pipeline.factors.morningstar import MarketCap
from quantopian.pipeline.data import Fundamentals
import quantopian.algorithm as algo
def make_pipeline():
q500 = Q500US()
mktcap = MarketCap()
sector = morningstar.asset_classification.morningstar_sector_code.latest
sectors_311 = sector.element_of([102, 311])
mkt_cap = morningstar.valuation.market_cap.latest
growth = Fundamentals.revenue_growth.latest
forward_pe_ratio = Fundamentals.forward_pe_ratio.latest
current_pe = Fundamentals.pe_ratio.latest
pipe = Pipeline()
attach_pipeline(pipe, 'pipeline_tutorial')
pipe.add(Sector(), 'Sector')
pipe.add(mktcap, 'mktcap')
pipe.add(growth, 'growth')
pipe.add(forward_pe_ratio, 'forward_pe_ratio')
pipe.add(current_pe, 'current_pe')
# log.info(current_pe / forward_pe_ratio)
pipe.set_screen(sectors_311 & q500)
# pipe.set_screen(q500)
return pipe
def initialize(context):
set_benchmark(symbol('QQQ'))
algo.schedule_function(
rebalance,
algo.date_rules.month_start(),
algo.time_rules.market_open(hours=1),
)
algo.attach_pipeline(make_pipeline(), 'pipeline')
#购买持仓数量
context.buy_stock_count = 10
#杠杆大小
leverage = 1.0
context.buy_stock_percent = float(leverage / context.buy_stock_count)
cash = context.portfolio.cash
context.purchase_value = cash / context.buy_stock_count
context.stocks_sold = []
def before_trading_start(context, data):
output = pipeline_output('pipeline_tutorial')
context.my_short_universe = output.sort('mktcap', ascending=True).iloc[0:context.buy_stock_count]
context.my_long_universe = output.sort('mktcap', ascending=False).iloc[0:context.buy_stock_count]
def rebalance(context, data):
"""
Execute orders according to our schedule_function() timing.
"""
if True:
# purchase_value = cash / left_buy_count
for stock in context.my_long_universe.index:
if stock != symbol('QQQ'):
order_target_percent(stock, context.buy_stock_percent * 1.3)
for stock in context.my_short_universe.index:
if stock != symbol('QQQ'):
order_target_percent(stock, -context.buy_stock_percent * 0.5)
for stock in context.portfolio.positions:
if stock != symbol('QQQ'):
if stock not in context.my_long_universe.index and stock not in context.my_long_universe.index:
order_target_value(stock, 0)
pass
大市值科技股因子回测
1.持有大市值规则:
在标普500成分股中,选择科技板块(Technology)和消费类周期股(Consumer Cyclical)(主要亚马逊被分到这了)选择市值最大的10只股票,每月调整一次。
可以看出基本跟基准QQQ的走势差不多。在金融危机跌幅也巨大。
最新持仓 AAPL,CSCO,DIS,HD,INTC,MSFT,ORCL,AMZN,GOOG_L,FB
1.1板块暴露风险(Sector Exposures):
最高有96%的科技板块暴露风险,最近的话是63%的科技板块暴露,28%的消费周期板块暴露。
1.2 因子暴露
因为规则是用市值进行筛选,所以毫无意外,在市值(Size)中暴露风险最大
1.3 跟Spy的相关性
可以看出最低也有跟Spy 0.8的相关性,高有1.4的相关性。
2.持有小市值规则
在标普500成分股中,选择科技板块(Technology)和消费类周期股(Consumer Cyclical)(主要亚马逊被分到这了)选择市值最小的10只股票,每月调整一次。
可以看出小市值的科技和消费公司,在2015年后有一波大跌,甚至走势跟QQQ相反。
主要的因子暴露是波动性。小市值有较高的波动性。
3.做多130% 20只大市值科技和消费类周期股,做空50% 20只小市值科技和消费类周期股
在小幅度增加最大回撤的情况下,小幅度跑赢QQQ
因子上暴露比较明显,主要是正的大市值,负的波动性。
跟Spy 的相关性还是维持在0.8以上,最高1.48
4.结论
过去10年是大市值科技股的黄金时间,不保证未来大市值科技股还有这样高的收益。大市值科技股在金融危机这样的情况下,回撤也会惊人。过去4年做空小市值科技和消费类周期股,有较好收益,不保证未来还有这样高的收益,做空要谨慎。
转载:https://blog.csdn.net/fox64194167/article/details/101760319
查看评论