[TB源码] TB唐奇安通道(TB源码)

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

294

主题

20

回帖

1380

积分

专栏作家

积分
1380
来源: 2020-5-30 13:44:45 显示全部楼层 |阅读模式
  1. //唐奇安通道
  2. Params
  3.                         Numeric LongLength(20);                        // 长周期
  4.                         Numeric ShortLength(10);                // 短周期
  5.                         Numeric TrailingScale(0.5);                // 增仓比例
  6.                         Numeric StopLossSet(2);                        // 止损比例
  7.                         Numeric Lots(1);                                // 交易数量                              
  8. Vars
  9.                         Numeric MinPoint;                                // 最小变动单位
  10.                         NumericSeries AvgTR;                        // ATR
  11.                         Numeric N;                          // N 值
  12.                         NumericSeries DonchianHi;           // 唐奇安通道上轨,延后1个Bar
  13.                         NumericSeries DonchianLo;           // 唐奇安通道下轨,延后1个Bar
  14.                         Numeric ExitHighestPrice;           // 离市时判断需要的N周期最高价
  15.                         Numeric ExitLowestPrice;            // 离市时判断需要的N周期最低价
  16.                         Numeric myEntryPrice;               // 开仓价格
  17.                         Numeric myExitPrice;                // 平仓价格
  18.                         Bool SendOrderThisBar(False);   // 当前Bar有过交易
  19.                         NumericSeries preEntryPrice(0); // 前一次开仓的价格
  20. Begin
  21.                         If(BarStatus == 0)
  22.                         {
  23.                                 preEntryPrice = InvalidNumeric;
  24.                         } Else
  25.                         {
  26.                                 preEntryPrice = preEntryPrice[1];
  27.                         } //程序化交易 www.cxh99.com
  28.       
  29.                         AvgTR = XAverage(TrueRange,LongLength);
  30.                         N = AvgTR[1];      
  31.                         DonchianHi = HighestFC(High[1],LongLength);
  32.                         DonchianLo = LowestFC(Low[1],LongLength);      
  33.                         ExitLowestPrice = LowestFC(Low[1],ShortLength);
  34.                         ExitHighestPrice = HighestFC(High[1],ShortLength);
  35.                         Commentary("N="+Text(N));
  36.                         Commentary("preEntryPrice="+Text(preEntryPrice));
  37.                         PlotNumeric("上轨",DonchianHi);
  38.                         PlotNumeric("下轨",DonchianLo);
  39.                         PlotNumeric("退出上轨",ExitHighestPrice);
  40.                         PlotNumeric("退出下轨",ExitLowestPrice);
  41.                        
  42.                         /*/////////////////////////////////开仓////////////////////////////////////////*/
  43.                         If(MarketPosition == 0 && High > DonchianHi)
  44.                         {
  45.                                 myEntryPrice = min(high,DonchianHi);
  46.                                 myEntryPrice = IIF(myEntryPrice < Open, Open,myEntryPrice); // 大跳空的时候用开盘价代替
  47.                                 preEntryPrice = myEntryPrice;
  48.                                 Buy(Lots,myEntryPrice);
  49.                                 SendOrderThisBar = True;
  50.                         }
  51.                         If(MarketPosition == 0 && Low < DonchianLo)
  52.                         {
  53.                                 myEntryPrice = max(low,DonchianLo);
  54.                                 myEntryPrice = IIF(myEntryPrice > Open, Open,myEntryPrice); // 大跳空的时候用开盘价代替
  55.                                 preEntryPrice = myEntryPrice;
  56.                                 SendOrderThisBar = True;
  57.                                 SellShort(Lots,myEntryPrice);
  58.                         }
  59.                         /*///////////////////////////////止盈加仓////////////////////////////////////*/
  60.                         If(MarketPosition == 1)
  61.                         {      
  62.                                 Commentary("ExitLowestPrice="+Text(ExitLowestPrice));
  63.                                 If(Low < ExitLowestPrice)
  64.                                 {
  65.                                         myExitPrice = max(Low,ExitLowestPrice);
  66.                                         myExitPrice = IIF(myExitPrice > Open, Open,myExitPrice); // 大跳空的时候用开盘价代替
  67.                                         Sell(0,myExitPrice);    // 数量用0的情况下将全部平
  68.                                 }Else
  69.                                 {
  70.                                         If(preEntryPrice!=InvalidNumeric)
  71.                                         {
  72.                                                 If(Open >= preEntryPrice + TrailingScale*N) // 如果开盘就超过设定的1/2N,则直接用开盘价增仓。
  73.                                                 {
  74.                                                         myEntryPrice = Open;
  75.                                                         preEntryPrice = myEntryPrice;
  76.                                                         Buy(Lots,myEntryPrice);
  77.                                                         SendOrderThisBar = True;
  78.                                                 }      
  79.                                                 while(High >= preEntryPrice + TrailingScale*N) // 以最高价为标准,判断能进行几次增仓
  80.                                                 {
  81.                                                         myEntryPrice = preEntryPrice + TrailingScale * N;//程序化交易 www.chengxuhuajiaoyi.com
  82.                                                         preEntryPrice = myEntryPrice;
  83.                                                         Buy(Lots,myEntryPrice);
  84.                                                         SendOrderThisBar = True;                                       
  85.                                                 }
  86.                                         }                       
  87.                                         /*///////////////////////////////////止损策略///////////////////////////////*/
  88.                                         If(Low <= preEntryPrice - StopLossSet * N && SendOrderThisBar == false) // 加仓Bar不止损
  89.                                         {
  90.                                                 myExitPrice = preEntryPrice - StopLossSet * N;
  91.                                                 myExitPrice = IIF(myExitPrice > Open, Open,myExitPrice); // 大跳空的时候用开盘价代替
  92.                                                 Sell(0,myExitPrice); // 数量用0的情况下将全部平仓
  93.                                         }
  94.                                 } // www.chengxuhuajiaoyi.com
  95.                         }Else If(MarketPosition ==-1) // 有空仓的情况
  96.                         {
  97.                                 Commentary("ExitHighestPrice="+Text(ExitHighestPrice));
  98.                                 If(High > ExitHighestPrice)
  99.                                 {
  100.                                         myExitPrice = Min(High,ExitHighestPrice);
  101.                                         myExitPrice = IIF(myExitPrice < Open, Open,myExitPrice); // 大跳空的时候用开盘价代替
  102.                                         BuyToCover(0,myExitPrice);    // 数量用0的情况下将全部平仓
  103.                                 }Else
  104.                                 {
  105.                                         If(preEntryPrice!=InvalidNumeric)
  106.                                         {
  107.                                                 If(Open <= preEntryPrice - TrailingScale*N) // 如果开盘就超过设定的1/2N,则直接用开盘价增仓。
  108.                                                 {
  109.                                                         myEntryPrice = Open;
  110.                                                         preEntryPrice = myEntryPrice;
  111.                                                         SellShort(Lots,myEntryPrice);
  112.                                                         SendOrderThisBar = True;
  113.                                                 }
  114.                                                 while(Low <= preEntryPrice - TrailingScale*N) // 以最低价为标准,判断能进行几次增仓
  115.                                                 {
  116.                                                         myEntryPrice = preEntryPrice - TrailingScale * N;
  117.                                                         preEntryPrice = myEntryPrice;
  118.                                                         SellShort(Lots,myEntryPrice);
  119.                                                         SendOrderThisBar = True;
  120.                                                 }
  121.                                 }
  122.                                 /*///////////////////////////////////止损策略///////////////////////////////*/
  123.                                 If(High >= preEntryPrice + StopLossSet * N && SendOrderThisBar==false) // 加仓Bar不止损
  124.                                 {
  125.                                         myExitPrice = preEntryPrice + StopLossSet * N;
  126.                                         myExitPrice = IIF(myExitPrice < Open, Open,myExitPrice); // 大跳空的时候用开盘价代替
  127.                                         BuyToCover(0,myExitPrice); // 数量用0的情况下将全部平仓
  128.                                 }
  129.                         }
  130.                 }
  131. End
复制代码


回复

使用道具 举报

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