动量策略是最风行的量化策略之一。商品期货的CTA策略,绝大多数都是基于动量策略。在股票市场,动量策略也是罕用的量化因子之一。艰深地讲,动量策略就是“追涨杀跌”。上面咱们将介绍如何在DolphinDB中测试动量交易策略,并计算动量交易策略的累积回报。

DolphinDB database 是一款高性能分布式时序数据库。与其它通常的数据库不同,DolphinDB不仅能够存储和检索数据,而且具备弱小的编程和剖析性能,能够间接在数据库内实现策略回测等简单的工作,便捷且高效。

最罕用的股票动量因素是基于过来一年中扣除最近一个月的收益率。动量策略通常是一个月调整一次并且持有期也是一个月。本文的例子中,每天调整1/21的投资组合,并持有新的投资组合21天。为了简化起见,本文的回测没有思考交易成本。

假如原始数据是一个CSV文件。它蕴含以下列:

PERMNO:股票代码

date:日期

PRC:每股价格

SHROUT:流通股数

RET:股票日收益

VOL:每日交易量

步骤1. 加载股票交易数据,对数据进行荡涤和过滤,而后为每只股票构建过来一年扣除最近一个月收益率的动量信号。

US = loadText("C:/DolphinDB/Data/US.csv")def loadPriceData(inData){    USstocks = select PERMNO, date, abs(PRC) as PRC, VOL, RET, SHROUT*abs(PRC) as MV from inData where weekday(date) between 1:5, isValid(PRC), isValid(VOL) order by PERMNO, date    USstocks = select PERMNO, date, PRC, VOL, RET, MV, cumprod(1+RET) as cumretIndex from USstocks context by PERMNO    return select PERMNO, date, PRC, VOL, RET, MV, move(cumretIndex,21)move(cumretIndex,252)-1 as signal from USstocks context by PERMNO }priceData = loadPriceData(US)

步骤2. 为动量策略生成投资组合

首先,抉择满足以下条件的流通股:动量信号值无缺失、当天的交易量为正、市值超过1亿美元以及每股价格超过5美元。

def genTradables(indata){    return select date, PERMNO, MV, signal from indata where PRC>5, MV>100000, VOL>0, isValid(signal) order by date}tradables = genTradables(priceData)

而后依据每天的动量信号,产生10组流通股票。只保留2个最极其的群体(赢家和输家)。假如在21天内,每天总是多头1美元和空头1美元,所以咱们每天在赢家组多头$1/21,在输家组每天空头$1/21。在每组中,咱们能够应用等权重或市值权重, 来计算投资组合造成日期上每个股票的权重。

//WtScheme=1示意等权重;WtScheme=2示意值权重def formPortfolio(startDate, endDate, tradables, holdingDays, groups, WtScheme){    ports = select date, PERMNO, MV, rank(signal,,groups) as rank, count(PERMNO) as symCount, 0.0 as wt from tradables where date between startDate:endDate context by date having count(PERMNO)>=100    if (WtScheme==1){        update ports set wt = -1.0count(PERMNO)holdingDays where rank=0 context by date        update ports set wt = 1.0count(PERMNO)holdingDays where rank=groups-1 context by date    }    else if (WtScheme==2){        update ports set wt = -MVsum(MV)holdingDays where rank=0 context by date        update ports set wt = MVsum(MV)holdingDays where rank=groups-1 context by date    }    return select PERMNO, date as tranche, wt from ports where wt != 0 order by PERMNO, date}startDate=1996.01.01endDate=2017.01.01 holdingDays=21groups=10ports = formPortfolio(startDate, endDate, tradables, holdingDays, groups, 2)dailyRtn = select date, PERMNO, RET as dailyRet from priceData where date between startDate:endDate

步骤3. 计算投资组合中每只股票接下来21天的利润或损失。在投资组合造成后的21天关停投资组合。

def calcStockPnL(ports, dailyRtn, holdingDays, endDate, lastDays){    ages = table(1..holdingDays as age)    dates = sort distinct ports.tranche    dictDateIndex = dict(dates, 1..dates.size())    dictIndexDate = dict(1..dates.size(), dates)    pos = select dictIndexDate[dictDateIndex[tranche]+age] as date, PERMNO, tranche, age, take(0.0,size age) as ret, wt as expr, take(0.0,size age) as pnl from cj(ports,ages) where isValid(dictIndexDate[dictDateIndex[tranche]+age]), dictIndexDate[dictDateIndex[tranche]+age]<=min(lastDays[PERMNO], endDate)    update pos set ret = dailyRet from ej(pos, dailyRtn,`date`PERMNO)    update pos set expr = expr*cumprod(1+ret) from pos context by PERMNO, tranche    update pos set pnl = expr*ret/(1+ret)    return pos}lastDaysTable = select max(date) as date from priceData group by PERMNOlastDays = dict(lastDaysTable.PERMNO, lastDaysTable.date)undef(`priceData, VAR)stockPnL = calcStockPnL(ports, dailyRtn, holdingDays, endDate, lastDays)

步骤4. 计算投资组合的利润或损失,并绘制随时间推移的动量策略累积回报。

portPnL = select sum(pnl) as pnl from stockPnL group by dateportPnL = select * from portPnL order by date;plot(cumsum(portPnL.pnl) as cumulativeReturn,portPnL.date, "Cumulative Returns of the Momentum Strategy")

以下是美国股票市场1996年到2016年,20年回测的后果。回测时,每天产生一个新的tranche,蕴含大概1500只股票(均匀每天约7500只股票,取20%),持有21天。如此宏大的数据量和计算量,应用单线程计算,DolphinDB耗时仅3分钟。

动量交易策略施行起来须要了解取得超额回报的原理和肯定的交易技能,以及可能带来的投资危险。感兴趣的敌人能够到官网下载 DolphinDB database,设计本人的动量交易策略。