此文章是接着【原创】【量化策略】海龟交易体系的小白构建(一)之交易法则所用策略平台:ricequant,米筐科技的策略平台(www.ricequant.com)
不考虑N仅根据突破信号构建的策略TurtleBreakthroughStrategy Code:
http://commons.apache.org/proper/commons-math/userguide/stat.html 中1.2节示范代码 DescriptiveStatistics forMax = new DescriptiveStatistics(); for (int i = 0; i < highPrice.length-1; i++) { forMax.addValue(highPrice); } double max = forMax.getMax(); //获得过去10天最低价的最小值 DescriptiveStatistics forMin = new DescriptiveStatistics(); for (int i = 0; i < lowPrice.length-1; i++) { forMin.addValue(lowPrice); } double min = forMin.getMin(); //获取初始资金 double initialCash = info.portfolio().getInitialCash(); //每次使用初始资金的1/10进行交易 double quantity = Math.floor(initialCash * 0.1 / stats.get(stockId).getLastPrice()); double curPosition = info.position(stockId).getNonClosedTradeQuantity(); //如当前价格向下突破最近20日最低价,则卖出上一次买入的持仓 if (stats.get(stockId).getLastPrice() < min) { if (curPosition > 0) { trans.sell(stockId).shares(quantity).commit(); } } //如当前价格突破最近55日最高价,则买入1/10初始资金所对应数量的持仓 if (stats.get(stockId).getLastPrice() > max) { trans.buy(stockId).shares(quantity).commit(); } }); }); }}回测结果:
http://commons.apache.org/proper/commons-math/userguide/stat.html 中1.2节示范代码 DescriptiveStatistics forMax = new DescriptiveStatistics(); for (int i = 0; i < highPrice.length-1; i++) { forMax.addValue(highPrice); } double max = forMax.getMax(); //获得过去10天最低价的最小值 DescriptiveStatistics forMin = new DescriptiveStatistics(); for (int i = 0; i < lowPriceForExtremem.length-1; i++) { forMin.addValue(lowPriceForExtremem); } double min = forMin.getMin(); double portfolioValue = info.portfolio().getPortfolioValue(); double atr = atrArray[atrLength.value-1]; informer.info(atr); double unit = Math.floor(portfolioValue * .01 / atr); //informer.info(unit); //获取初始资金 double initialCash = info.portfolio().getInitialCash(); //每次交易一个单位 double quantity = unit; double curPosition = info.position(stockId).getNonClosedTradeQuantity(); //informer.info("lastPrice is " + stats.get(stockId).getLastPrice()); //informer.info("max is" + max); //如当前价格向下突破最近20日最低价,则卖出上一次买入的持仓 if (stats.get(stockId).getLastPrice() < min) { if (curPosition > 0) { trans.sell(stockId).shares(quantity).commit(); } } //如当前价格突破最近55日最高价,则买入一个单位 if (stats.get(stockId).getLastPrice() > max) { trans.buy(stockId).shares(quantity).commit(); } }); }}回测结果:

额,夏普比率终于过1了,与之前没有引入N的策略相比,表现有了很大提高,最大回撤也由47降低到28,这说明,海龟体系中引入的N,即根据过去价格波动幅度而调节开平仓的单位,是经受得住数据考验的。