[交易开拓者(TB)]唐奇安通道交易策略源码

0
回复
4624
查看
[复制链接]

53

主题

8

回帖

836

积分

管理员

积分
836
来源: 2020-8-1 08:58:02 显示全部楼层 |阅读模式
交易开拓者(Tradeblazer)唐奇安通道交易策略源码,据说5分钟 15分钟周期都能盈利模型[开拓者公式]
应用周期:5分钟、15分钟、1小时
应用品种:橡胶、股指、塑料、PTA、焦炭等。
// 源码
  1. Params
  2. Numeric LongLength(20);                        // 长周期
  3. Numeric ShortLength(10);                // 短周期
  4. Numeric TrailingScale(0.5);                // 增仓比例
  5. Numeric StopLossSet(2);                        // 止损比例
  6. Numeric Lots(1);                                // 交易数量
  7. Vars
  8. Numeric MinPoint;                                // 最小变动单位
  9. NumericSeries AvgTR;                        // ATR
  10. Numeric N;                          // N 值
  11. NumericSeries DonchianHi;           // 唐奇安通道上轨,延后1个Bar
  12. NumericSeries DonchianLo;           // 唐奇安通道下轨,延后1个Bar
  13. Numeric ExitHighestPrice;           // 离市时判断需要的N周期最高价
  14. Numeric ExitLowestPrice;            // 离市时判断需要的N周期最低价
  15. Numeric myEntryPrice;               // 开仓价格
  16. Numeric myExitPrice;                // 平仓价格
  17. Bool SendOrderThisBar(False);   // 当前Bar有过交易
  18. NumericSeries preEntryPrice(0); // 前一次开仓的价格
  19. Begin
  20. If(BarStatus == 0)
  21. {
  22. preEntryPrice = InvalidNumeric;
  23. } Else
  24. {
  25. preEntryPrice = preEntryPrice[1];
  26. }

  27. AvgTR = XAverage(TrueRange,LongLength);
  28. N = AvgTR[1];
  29. DonchianHi = HighestFC(High[1],LongLength);
  30. DonchianLo = LowestFC(Low[1],LongLength);
  31. ExitLowestPrice = LowestFC(Low[1],ShortLength);
  32. ExitHighestPrice = HighestFC(High[1],ShortLength);
  33. Commentary("N="+Text(N));
  34. Commentary("preEntryPrice="+Text(preEntryPrice));
  35. PlotNumeric("上轨",DonchianHi);
  36. PlotNumeric("下轨",DonchianLo);
  37. PlotNumeric("退出上轨",ExitHighestPrice);
  38. PlotNumeric("退出下轨",ExitLowestPrice);

  39. /*/////////////////////////////////开仓////////////////////////////////////////*/
  40. If(MarketPosition == 0 && High > DonchianHi)
  41. {
  42. myEntryPrice = min(high,DonchianHi);
  43. myEntryPrice = IIF(myEntryPrice < Open, Open,myEntryPrice); // 大跳空的时候用开盘价代替
  44. preEntryPrice = myEntryPrice;
  45. Buy(Lots,myEntryPrice);
  46. SendOrderThisBar = True;
  47. }
  48. If(MarketPosition == 0 && Low < DonchianLo)
  49. {
  50. myEntryPrice = max(low,DonchianLo);
  51. myEntryPrice = IIF(myEntryPrice > Open, Open,myEntryPrice); // 大跳空的时候用开盘价代替
  52. preEntryPrice = myEntryPrice;
  53. SendOrderThisBar = True;
  54. SellShort(Lots,myEntryPrice);
  55. }
  56. /*///////////////////////////////止盈加仓////////////////////////////////////*/
  57. If(MarketPosition == 1)
  58. {
  59. Commentary("ExitLowestPrice="+Text(ExitLowestPrice));
  60. If(Low < ExitLowestPrice)
  61. {
  62. myExitPrice = max(Low,ExitLowestPrice);
  63. myExitPrice = IIF(myExitPrice > Open, Open,myExitPrice); // 大跳空的时候用开盘价代替
  64. Sell(0,myExitPrice);    // 数量用0的情况下将全部平
  65. }Else
  66. {
  67. If(preEntryPrice!=InvalidNumeric)
  68. {
  69. If(Open >= preEntryPrice + TrailingScale*N) // 如果开盘就超过设定的1/2N,则直接用开盘价增仓。
  70. {
  71. myEntryPrice = Open;
  72. preEntryPrice = myEntryPrice;
  73. Buy(Lots,myEntryPrice);
  74. SendOrderThisBar = True;
  75. }
  76. while(High >= preEntryPrice + TrailingScale*N) // 以最高价为标准,判断能进行几次增仓
  77. {
  78. myEntryPrice = preEntryPrice + TrailingScale * N;
  79. preEntryPrice = myEntryPrice;
  80. Buy(Lots,myEntryPrice);
  81. SendOrderThisBar = True;
  82. }
  83. }
  84. /*///////////////////////////////////止损策略///////////////////////////////*/
  85. If(Low <= preEntryPrice - StopLossSet * N && SendOrderThisBar == false) // 加仓Bar不止损
  86. {
  87. myExitPrice = preEntryPrice - StopLossSet * N;
  88. myExitPrice = IIF(myExitPrice > Open, Open,myExitPrice); // 大跳空的时候用开盘价代替
  89. Sell(0,myExitPrice); // 数量用0的情况下将全部平仓
  90. }
  91. }
  92. }Else If(MarketPosition ==-1) // 有空仓的情况
  93. {
  94. Commentary("ExitHighestPrice="+Text(ExitHighestPrice));
  95. If(High > ExitHighestPrice)
  96. {
  97. myExitPrice = Min(High,ExitHighestPrice);
  98. myExitPrice = IIF(myExitPrice < Open, Open,myExitPrice); // 大跳空的时候用开盘价代替
  99. BuyToCover(0,myExitPrice);    // 数量用0的情况下将全部平仓
  100. }Else
  101. {
  102. If(preEntryPrice!=InvalidNumeric)
  103. {
  104. If(Open <= preEntryPrice - TrailingScale*N) // 如果开盘就超过设定的1/2N,则直接用开盘价增仓。
  105. {
  106. myEntryPrice = Open;
  107. preEntryPrice = myEntryPrice;
  108. SellShort(Lots,myEntryPrice);
  109. SendOrderThisBar = True;
  110. }
  111. while(Low <= preEntryPrice - TrailingScale*N) // 以最低价为标准,判断能进行几次增仓
  112. {
  113. myEntryPrice = preEntryPrice - TrailingScale * N;
  114. preEntryPrice = myEntryPrice;
  115. SellShort(Lots,myEntryPrice);
  116. SendOrderThisBar = True;
  117. }
  118. }
  119. /*///////////////////////////////////止损策略///////////////////////////////*/
  120. If(High >= preEntryPrice + StopLossSet * N && SendOrderThisBar==false) // 加仓Bar不止损
  121. {
  122. myExitPrice = preEntryPrice + StopLossSet * N;
  123. myExitPrice = IIF(myExitPrice < Open, Open,myExitPrice); // 大跳空的时候用开盘价代替
  124. BuyToCover(0,myExitPrice); // 数量用0的情况下将全部平仓
  125. }
  126. }
  127. }
  128. End
复制代码
关注公众号,获取更多宽客资讯


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 免费注册
关注微信