楼主: dana.quant
2843 3

[程序化交易] 【原创】【量化策略】海龟交易体系的小白构建(三)之完全体系构建 [推广有奖]

  • 0关注
  • 7粉丝

已卖:3份资源

硕士生

52%

还不是VIP/贵宾

-

威望
0
论坛币
1030 个
通用积分
0
学术水平
8 点
热心指数
14 点
信用等级
8 点
经验
3609 点
帖子
103
精华
1
在线时间
55 小时
注册时间
2015-8-30
最后登录
2016-5-17

楼主
dana.quant 企业认证  发表于 2015-9-15 12:38:22 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
这个文章是接着【原创】【量化策略】海龟交易体系的小白构建(二)之交易实现所用的平台依然是我钟爱的ricequant  米筐科技的
海龟策略完全体系构建

最后的最后,放上海龟体系的完全版,Code里面包含了上一节中提到的所有环节,注意本海龟策略是基于体系二的,感兴趣的朋友可以尝试构建体系一。

TurtleOriginalStrategy Code:

[url=]Clone[/url]public class TurtleOriginalStrategy implements IHStrategy {  Core talibCore;//定义全局变量  static int tradedayNum = 0;  static double unit = 0;  static double atr = 0;  static String tradingSignal = "start";  static String preTradingSignal = "";  static int units_hold_max = 4;  static int units_hold = 0;  static double quantity = 0;  static double max_add = 0;  static double firstOpenPrice = 0;    //计算最大最小值  public double[] getExtremem(double[] arrayHighPriceResult, double[] arrayLowPriceResult) {      DescriptiveStatistics forMax = new DescriptiveStatistics();      for (int i = 0; i < arrayHighPriceResult.length-1; i++) {          forMax.addValue(arrayHighPriceResult);      }      double maxResult = forMax.getMax();            DescriptiveStatistics forMin = new DescriptiveStatistics();      for (int i = 0; i < arrayLowPriceResult.length-1; i++) {          forMin.addValue(arrayLowPriceResult);      }      double minResult = forMin.getMin();            double[] forExtremum = new double[2];      forExtremum[0] = maxResult;      forExtremum[1] = minResult;      return forExtremum;  }  //计算Atr以及单位  public double[] getAtrAndUnit(double[] atrArrayResult, MInteger atrLengthResult, double portfolioValueResult) {      double atr = atrArrayResult[atrLengthResult.value-1];      double unit = Math.floor(portfolioValueResult * .01 / atr);      double[] atrAndUnit = new double[2];      atrAndUnit[0] = atr;      atrAndUnit[1] = unit;      return atrAndUnit;  }  //计算止损线价位  public double getStopPrice(double firstOpenPriceResult, int units_hold_result, double atrResult) {      double stopPrice =  firstOpenPriceResult - 2*atrResult + (units_hold_result-1)*0.5*atrResult;      return stopPrice;  }       @Override  public void init(IHInformer informer, IHInitializers initializers) {        talibCore = new Core();            int openObserveTime = 55;    int closeObserveTime = 20;    int atrTime = 20;    MInteger atrBegin = new MInteger();    MInteger atrLength = new MInteger();        String stockId = "CSI300.INDX";    initializers.instruments((universe) -> universe.add(stockId));            initializers.events().statistics((stats, info, trans) -> {        //获取组合总价值,包含市场价值与剩余资金        double portfolioValue = info.portfolio().getPortfolioValue();                        double[] highPrice = stats.get(stockId).history(openObserveTime+1, HPeriod.Day).getHighPrice();        double[] lowPriceForAtr = stats.get(stockId).history(openObserveTime+1, HPeriod.Day).getLowPrice();        double[] lowPriceForExtremem = stats.get(stockId).history(closeObserveTime+1, HPeriod.Day).getLowPrice();        double[] closePrice = stats.get(stockId).history(openObserveTime+2, HPeriod.Day).getClosingPrice();                double closePriceForAtr[] = new double[closePrice.length-1];        for (int i = 0; i < closePrice.length-1; i++) {            closePriceForAtr = closePrice;        }                       double[] atrArray = new double[openObserveTime];        //Talib计算N即ATR        RetCode retCode = talibCore.atr(0, openObserveTime-1, highPrice, lowPriceForAtr, closePriceForAtr, atrTime, atrBegin, atrLength, atrArray);                        double max = getExtremem(highPrice, lowPriceForExtremem)[0];        double min = getExtremem(highPrice, lowPriceForExtremem)[1];                        double atr = atrArray[atrLength.value-1];                if (tradingSignal != "start") {            if (units_hold != 0) {            max_add += 0.5 * getAtrAndUnit(atrArray, atrLength, portfolioValue)[0];            }        } else {            max_add = stats.get(stockId).getLastPrice();        }                informer.info(units_hold);                double curPosition = info.position(stockId).getNonClosedTradeQuantity();        double availableCash = info.portfolio().getAvailableCash();        double marketValue = info.portfolio().getMarketValue();                        if (curPosition > 0 & stats.get(stockId).getLastPrice() < getStopPrice(firstOpenPrice, units_hold, atr)) {            tradingSignal = "stop";        } else {            if (curPosition > 0 & stats.get(stockId).getLastPrice() < min) {                tradingSignal = "exit";            } else {                if (stats.get(stockId).getLastPrice() > max_add & units_hold != 0 & units_hold < units_hold_max & availableCash > stats.get(stockId).getLastPrice()*unit) {                    tradingSignal = "entry_add";                } else {                    if (stats.get(stockId).getLastPrice() > max & units_hold == 0) {                        max_add = stats.get(stockId).getLastPrice();                        tradingSignal = "entry";                    }                }            }        }                //informer.info(tradingSignal);                atr = getAtrAndUnit(atrArray, atrLength, portfolioValue)[0];        if (tradedayNum % 5 == 0) {            unit = getAtrAndUnit(atrArray, atrLength, portfolioValue)[1];        }        tradedayNum += 1;                double quantity = unit;                        if (tradingSignal != preTradingSignal | (units_hold < units_hold_max & units_hold > 1) | tradingSignal == "stop") {                                    if (tradingSignal == "entry") {                quantity = unit;                if (availableCash > stats.get(stockId).getLastPrice()*quantity) {                    trans.buy(stockId).shares(quantity).commit();                    firstOpenPrice = stats.get(stockId).getLastPrice();                    units_hold = 1;                    informer.info("entrybuy" + quantity);                }            }            if (tradingSignal == "entry_add") {                quantity = unit;                trans.buy(stockId).shares(quantity).commit();                units_hold += 1;                informer.info("entry_addbuy" + quantity);            }                                    if (tradingSignal == "stop") {                if (/*curPosition marketValue*/ units_hold > 0) {                    trans.sell(stockId).shares(quantity).commit();                    units_hold -= 1;                    informer.info("stop" + quantity);                }            }            if (tradingSignal == "exit") {                if (curPosition > 0) {                    trans.sell(stockId).shares(curPosition).commit();                    units_hold = 0;                    informer.info("exitsell" + curPosition);                }            }                    }                preTradingSignal = tradingSignal;        });        }}

回测结果:


opps吐血,虽然代码蛮长运行还是挺快哒,终于搞定,完整版的策略包括了按照海龟中的进场、追踪、止损、离场所有细节,然而并没有什么卵用,结果没有想象那么漂亮,不过继续在之前的基础上把最大回撤降低到22,我想这还是因为止损大法好的原因,宽慰一点点。

多少自己还是体会到一个交易系统雏形构建的方方面面,我这么对自己说,成功完成了一次心理按摩。

最后,欢迎小伙伴就代码进行深度交流。——> ________ ——>
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:海龟交易 量化策略 交易体系 descriptive Instruments 量化策略 量化交易 python基础教程 java教程

已有 1 人评分经验 论坛币 收起 理由
fantuanxiaot + 80 + 80 精彩帖子

总评分: 经验 + 80  论坛币 + 80   查看全部评分

沙发
dana.quant 企业认证  发表于 2015-10-15 18:13:05
QQ截图20151015150346.png

QQ截图20151015150346.png (292.39 KB)

QQ截图20151015150346.png

藤椅
leichuanmei 发表于 2016-3-11 10:07:03
辛苦了。

板凳
sukiyou2000 发表于 2016-3-11 11:45:41
多谢分享,学习中

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jr
拉您进交流群
GMT+8, 2025-12-5 18:01