本篇是持倉(cāng)1只股票與持倉(cāng)10只股票的小市值策略
一、持倉(cāng)1只股票的小市值策略
策略基本思路:持有市值最小的1只股票,按照調(diào)倉(cāng)頻率定期更新。
收益風(fēng)險(xiǎn):
源碼:
import pandas as pd
import numpy as np
def initialize(context):
g.choicenum = 1 #預(yù)選小市值股票數(shù)
g.days = 0 # 計(jì)時(shí)器
g.runned_years = set([])
g.runned_seasons = set([])
# 現(xiàn)在應(yīng)該持有的倉(cāng)位, 因?yàn)榭赡芡E茖?dǎo)致賣出不成功, 跟實(shí)際持倉(cāng)可能不一樣
g.stocks_to_hold = set()
# 調(diào)倉(cāng)頻率
g.period = 'year' # week, month, season,year,day
# 是否止損
g.should_stop_loss = False #True:開(kāi)啟止損,False:關(guān)閉止損
# 如果是按周調(diào)倉(cāng),
if g.period == 'week':
run_weekly(rebalance, 1)
elif g.period in ('month', 'year', 'season'):
run_monthly(monthly, 1)
else:
run_daily(daily)
# 防止賣出不成功, 每日嘗試賣出
# run_daily(clean_stocks_to_sell)
if g.should_stop_loss:
run_daily(stop_loss)
def daily(context):
if g.days % g.period == 0:
rebalance(context)
g.days += 1
def monthly(context):
if g.period == 'month':
rebalance(context)
elif g.period == 'year':
year = context.current_dt.year
if year not in g.runned_years:
# 這一年沒(méi)運(yùn)行過(guò), 運(yùn)行一次
g.runned_years.add(year)
rebalance(context)
elif g.period == 'season':
year = context.current_dt.year
# 取得月份對(duì)應(yīng)的季度, 1-3月->1季度, 4-6月->2季度, ...
season = (context.current_dt.month - 1) / 3 + 1
year_season = (year, season)
if year_season not in g.runned_seasons:
# 這一年的這一季度沒(méi)運(yùn)行過(guò), 運(yùn)行一次
g.runned_seasons.add(year_season)
rebalance(context)
pass
# 調(diào)整
def rebalance(context):
print 'rebalance at %s' % context.current_dt
# 設(shè)置滬深兩市所有股票為股票池
scu0 = get_index_stocks('000001.XSHG')
scu3 = get_index_stocks('399106.XSHE')
scu = scu0+scu3
# scu = scu[:10]
set_universe(scu)
date=context.current_dt.strftime('%Y-%m-%d')
# 選出低市值的股票,buylist
df = get_fundamentals(query(
valuation.code,valuation.market_cap
).filter(
valuation.code.in_(context.universe)
).order_by(
valuation.market_cap.asc()
), date=date
).dropna()
buylist =unpaused(list(df['code']))
g.stocks_to_hold = buylist=buylist[:g.choicenum]
clean_stocks_to_sell(context)
# 等權(quán)重買入buylist中的股票
position_per_stk = context.portfolio.cash/g.choicenum
closes = history(1, '1d', 'price', df=False)
for stock in buylist:
close = closes[stock][-1]
if not isnan(close):
amount = int(position_per_stk/close)
order(stock, +amount)
set_universe(g.stocks_to_hold)
# 清空應(yīng)該賣出的股票
def clean_stocks_to_sell(context):
for stock in context.portfolio.positions:
if stock not in g.stocks_to_hold:
order_target(stock, 0)
# 止損
def stop_loss(context):
for stock in context.portfolio.positions:
p = context.portfolio.positions[stock]
if p.price/p.avg_cost <>
order_target(stock,0)
if stock in g.stocks_to_hold:
g.stocks_to_hold.remove(stock)
def unpaused(stockspool):
current_data=get_current_data()
return [s for s in stockspool if not current_data[s].paused]
二、持倉(cāng)10只股票的小市值策略
1.初始資金M1分十份,第1年的首個(gè)交易日,分別買入市值最小的十只股票(記做股票池S1);
2.次年的第1個(gè)交易日,查詢得到的市值最小的十只股票(S2_target),賣出S1中不屬于S2_target的股票,在獲得資金M2后等分為十份,分別買入S2_target的十只股票;后續(xù)年份的處理依次類推。
(如果股票因停牌、跌停等原因而未賣出,就繼續(xù)持有這些股票,等到下個(gè)周期第一個(gè)交易日再做處理)
收益風(fēng)險(xiǎn):
源碼:
import pandas as pd
import numpy as np
def initialize(context):
g.choicenum = 10 #預(yù)選小市值股票數(shù)
g.days = 0 # 計(jì)時(shí)器
g.runned_years = set([])
g.runned_seasons = set([])
# 現(xiàn)在應(yīng)該持有的倉(cāng)位, 因?yàn)榭赡芡E茖?dǎo)致賣出不成功, 跟實(shí)際持倉(cāng)可能不一樣
g.stocks_to_hold = set()
# 調(diào)倉(cāng)頻率
g.period = 'year' # week, month, season,year,day
# 是否止損
g.should_stop_loss = False #True:開(kāi)啟止損,False:關(guān)閉止損
# 如果是按周調(diào)倉(cāng),
if g.period == 'week':
run_weekly(rebalance, 1)
elif g.period in ('month', 'year', 'season'):
run_monthly(monthly, 1)
else:
run_daily(daily)
# 防止賣出不成功, 每日嘗試賣出
# run_daily(clean_stocks_to_sell)
if g.should_stop_loss:
run_daily(stop_loss)
def daily(context):
if g.days % g.period == 0:
rebalance(context)
g.days += 1
def monthly(context):
if g.period == 'month':
rebalance(context)
elif g.period == 'year':
year = context.current_dt.year
if year not in g.runned_years:
# 這一年沒(méi)運(yùn)行過(guò), 運(yùn)行一次
g.runned_years.add(year)
rebalance(context)
elif g.period == 'season':
year = context.current_dt.year
# 取得月份對(duì)應(yīng)的季度, 1-3月->1季度, 4-6月->2季度, ...
season = (context.current_dt.month - 1) / 3 + 1
year_season = (year, season)
if year_season not in g.runned_seasons:
# 這一年的這一季度沒(méi)運(yùn)行過(guò), 運(yùn)行一次
g.runned_seasons.add(year_season)
rebalance(context)
pass
# 調(diào)整
def rebalance(context):
print 'rebalance at %s' % context.current_dt
# 設(shè)置滬深兩市所有股票為股票池
scu0 = get_index_stocks('000001.XSHG')
scu3 = get_index_stocks('399106.XSHE')
scu = scu0+scu3
# scu = scu[:10]
set_universe(scu)
date=context.current_dt.strftime('%Y-%m-%d')
# 選出低市值的股票,buylist
df = get_fundamentals(query(
valuation.code,valuation.market_cap
).filter(
valuation.code.in_(context.universe)
).order_by(
valuation.market_cap.asc()
), date=date
).dropna()
buylist =unpaused(list(df['code']))
g.stocks_to_hold = buylist=buylist[:g.choicenum]
clean_stocks_to_sell(context)
# 等權(quán)重買入buylist中的股票
position_per_stk = context.portfolio.cash/g.choicenum
closes = history(1, '1d', 'price', df=False)
for stock in buylist:
close = closes[stock][-1]
if not isnan(close):
amount = int(position_per_stk/close)
order(stock, +amount)
set_universe(g.stocks_to_hold)
# 清空應(yīng)該賣出的股票
def clean_stocks_to_sell(context):
for stock in context.portfolio.positions:
if stock not in g.stocks_to_hold:
order_target(stock, 0)
# 止損
def stop_loss(context):
for stock in context.portfolio.positions:
p = context.portfolio.positions[stock]
if p.price/p.avg_cost <>
order_target(stock,0)
if stock in g.stocks_to_hold:
g.stocks_to_hold.remove(stock)
def unpaused(stockspool):
current_data=get_current_data()
return [s for s in stockspool if not current_data[s].paused]
聯(lián)系客服