-11-28 程序化交易者
程序化交易者
程序化交易者网(),国内最专业的程序化交易门户,提供专业的程序化交易,高频交易,量化投资,算法交易,交易策略等资讯、培训、技术支持等服务。
近几年来,A股中最牛的策略非小市值组合莫属。如何探索小市值的高收益呢
一、小市值&低股价
此策略首先选出股市中市值最低的300支股票,之后选出其中stocknum支最低股价的股票用于投资,加入了止损条件,每15个天进行一次调仓,回测主要参数如下:
Alpha Beta Sharpe Volatility Max Drawdown
2.020 0.390 8.974 0.242 11.534%
收益风险:
源码:
from heapq import nsmallest
# 选出所有的总市值最小的N只股票
df = get_fundamentals(query(
valuation.code, valuation.market_cap
), date='-01-01') #日期改为回测开始日期
df = df.dropna().sort(columns='market_cap',ascending=True)
df = df.head(300)
# 选取上面的结果作为universe
g.security = list(df['code'])
set_universe(g.security)
# 策略参考标准
set_benchmark('000300.XSHG')
# 设置手续费,买入时万分之三,卖出时万分之三加千分之一印花税, 每笔交易最低扣5块钱
set_commission(PerTrade(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
stocknum = 15 #购买股票数
g.count = 0
g.refresh_rate = 15 #调仓频率
def handle_data(context, data):
# 止损
for stock in list(context.portfolio.positions.keys()):
his = history(2, '1d', 'close', [stock])
if ((1-(his[stock][-1]/his[stock][0]))>=0.05):
order_target(stock, 0)
# 调仓日交易
if g.count % g.refresh_rate == 0:
# security = g.security
# 去除流动性差的股票
hist = history(20, '1d', 'volume', g.security)
x = hist.sum()
x = x[x > 10**8]
vw_list = list(x.keys())
# 选出低股价的股票
bucket = {}
for stock in vw_list:
hi = history(1, '1d', 'price', stock)
bucket[stock] = hi[stock][-1]
buylist = nsmallest(stocknum, bucket, key=bucket.get)
# 目前持仓中不在buylist中的股票,清仓
for stock in list(context.portfolio.positions.keys()):
his = history(2, '1d', 'close', [stock])
if stock not in buylist:
order_target(stock, 0)
# 等权重买入buylist中的股票
position_per_stk = context.portfolio.cash/stocknum
for stock in buylist:
if not data[stock].isnan():
amount = int(position_per_stk/data[stock].pre_close/100.0) * 100
order(stock, +amount)
# 天数加一
g.count += 1
二、小市值改进-超跌
此策略加了两个条件:1.超跌;2.简单的止损,大盘跌了3个点立即空仓。
收益风险
源码:
def initialize(context):
# 定义一个全局变量, 保存要操作的股票
# 000001(股票:平安银行)
g.security = '000001.XSHE'
# 初始化此策略
g.index = '000001.XSHG'
g.count = 50
g.top_count = 10
g.stop_threshold = -0.03
g.period = 150
g.cash = 1000000
# schedule rebalance function
run_daily(rebalance, 'open')
def before_trading_start(context):
df = get_fundamentals(query(
valuation.code, valuation.market_cap
).order_by(
# 按市值降序排列
valuation.market_cap.asc()
).limit(
g.count
))
print(list(df['code']))
set_universe(list(df['code']))
g.stocks = list(df['code'])
# open/close orders
def rebalance(context):
# close stock positions not in the current universe
for stock in context.portfolio.positions.keys():
if stock not in g.stocks:
order_target_value(stock, 0)
# close all positions when index drops by stop_threshold
index_hist = history(2, '1d', 'close', security_list=[g.index])
print(index_hist)
index_return_1d = index_hist[g.index].iloc[-1]/index_hist[g.index].iloc[-2] - 1
if index_return_1d <>
for stock in g.stocks:
order_target_value(stock, 0)
return
stock_hist = history(g.period, '1d', 'close')
stock_return = (stock_hist.ix[g.period-1]-stock_hist.ix[0])/stock_hist.ix[0]
stock_return = stock_return[g.stocks].order()
print(stock_return)
g.stocks = [stock for stock in stock_return.index]
g.stocks = g.stocks[:g.top_count]
# place equally weighted orders
if len(g.stocks) == 0:
return
weight = 1.0/len(g.stocks)
for stock in g.stocks:
order_target_value(stock, g.cash*weight)
# 每个单位时间(如果按天回测,则每天调用一次,如果按分钟,则每分钟调用一次)调用一次
def handle_data(context, data):
pass
如果觉得《量化交易策略——小市值策略(一)》对你有帮助,请点赞、收藏,并留下你的观点哦!