设为首页收藏本站

 找回密码
 注册
楼主: 追涨杀跌
打印 上一主题 下一主题

跨周期数据转换函数以及跨周期技术指标调用的实现 [复制链接]

Rank: 1

精华
0
UID
121786
积分
40
帖子
19
主题
2
阅读权限
10
注册时间
2013-7-28
最后登录
2016-10-14
232#
发表于 2014-5-11 19:00:39 |只看该作者
本帖最后由 扶老二 于 2014-5-11 19:33 编辑
  1. //------------------------------------------------------------------------
  2. // MtParabolicSAR,跨周期抛物线转向ParabolicSAR函数
  3. // 返回 当前bar的“周期索引”,比如当前在1分钟周期里,折算成15分钟周期的话,从凌晨00:00分钟开始,第1根bar是1,第2根bar是2......第15根bar是15,第16根bar又变成1,......
  4. // 除了函数本身的返回值,还通过oParCl、oParOpen、oPosition、oTransition这四个引用参数把对应Bar的停损值、对应Bar的停损值下一根的停损值、对应Bar建议的持仓状态(1 - 多头,-1 - 空头)、对应Bar的状态是否发生反转(1 或 -1 为反转,0 为保持不变)
  5. // 版本 20140511_183500
  6. //------------------------------------------------------------------------

  7. Params
  8.     Numeric TimeFrame(1440);        //目标时间周期参数,参数说明参见MtBar
  9.     Numeric BarsBack(1);            //目标时间周期BAR偏移参数,说明见MtBar函数
  10.     Numeric AfStep(0.02);
  11.     Numeric AfLimit(0.2);
  12.     NumericRef oParClose;
  13.     NumericRef oParOpen;
  14.     NumericRef oPosition;
  15.     NumericRef oTransition;

  16. Vars
  17.     NumericSeries barCnt;
  18.     NumericSeries CurBar;
  19.     NumericSeries barCntSum;
  20.     NumericSeries HighHT;
  21.     NumericSeries LowHT;
  22.     Numeric CurTime;
  23.     Numeric PreTime;
  24.     bool condition(false);
  25.     Numeric n;
  26.     Numeric i;


  27.     NumericSeries Af(0);   
  28.     NumericSeries ParOpen(0);
  29.     NumericSeries ParClose(0);
  30.     NumericSeries Position(0);  
  31.     NumericSeries HHValue(0);
  32.     NumericSeries LLValue(0);
  33.     NumericSeries Transition(0);
  34.      
  35. Begin
  36.     if(TimeFrame == 40320) {                                        //月线
  37.         CurTime = Month;
  38.         PreTime = Month[1];
  39.     } else if(TimeFrame == 10080) {                                 //周线
  40.         CurTime = IntPart(DateDiff(19700105, TrueDate(0)) / 7);
  41.         PreTime = IntPart(DateDiff(19700105, TrueDate(1)) / 7);
  42.     } else {                                                        //其他时间周期
  43.         CurTime = IntPart((DateDiff(19700105, TrueDate(0)) * 1440 + Hour * 60 + Minute) / TimeFrame);
  44.         PreTime = IntPart((DateDiff(19700105, TrueDate(1)) * 1440 + Hour[1] * 60 + Minute[1]) / TimeFrame);
  45.     }

  46.     condition = CurTime != PreTime;

  47.     if(CurrentBar == 0) {                       //如果是第一根Bar, CurBar=0
  48.         barCnt = 0;
  49.         CurBar = 0;
  50.         HighHT = High[1];
  51.         LowHT = Low[1];
  52.         
  53.         Position = 1;
  54.         Transition = 1;
  55.         Af = AfStep;
  56.         HHValue = HighHT;
  57.         LLValue = LowHT;
  58.         ParClose = LLValue;
  59.         ParOpen = ParClose + Af * (HHValue - ParClose);
  60.         if(ParOpen > LowHT) {
  61.             ParOpen = LowHT;
  62.         }
  63.     } else {
  64.         if(condition) {                         //如果在目标周期下,属于另一根K线,则CurBar加1
  65.             barCnt = 1;
  66.             CurBar = CurBar[1] + 1;
  67.             HighHT = High[1];
  68.             LowHT = Low[1];
  69.             for n = 2 to TimeFrame {
  70.                 HighHT = Max(HighHT, High[n]);
  71.                 LowHT = Min(LowHT, Low[n]);
  72.             }
  73.         } else {                                //如果在目标周期下,属于同一根K线,则CurBar不变,但最高价和最低价要记录价格的变化,成交量要累加
  74.             barCnt = barCnt[1] + 1;
  75.             CurBar = CurBar[1];
  76.             HighHT = Max(HighHT[1], High);
  77.             LowHT = Min(LowHT[1], Low);
  78.         }
  79.         
  80.         HHValue = Max(HHValue[1], HighHT);
  81.         LLValue = Min(LLValue[1], LowHT);

  82.         if(CurBar == 0) {
  83.             Position = 1;
  84.             Transition = 1;
  85.             Af = AfStep;
  86.             ParClose = LLValue;
  87.             ParOpen = ParClose + Af * (HHValue - ParClose);
  88.             if(ParOpen > LowHT) {
  89.                 ParOpen = LowHT;
  90.             }
  91.         } else {        
  92.             if(condition) {                       //正好切换到目标周期的下一根k线
  93.                 Transition = 0;

  94.                 if(Position[TimeFrame] == 1) {
  95.                     if(LowHT <= ParOpen[TimeFrame]) {
  96.                         Position = -1;
  97.                         Transition = -1;              
  98.                         ParClose = HHValue;
  99.                         HHValue = HighHT;
  100.                         LLValue  = LowHT;
  101.             
  102.                         Af = AfStep;
  103.                         ParOpen = ParClose + Af * (LLValue - ParClose);
  104.                            
  105.                         if(ParOpen < HighHT) {
  106.                             ParOpen = HighHT;
  107.                         }
  108.                         
  109.                         if(ParOpen < HighHT[TimeFrame]) {
  110.                             ParOpen = HighHT[TimeFrame];
  111.                         }
  112.                     } else {
  113.                         Position = Position[TimeFrame];
  114.                         ParClose = ParOpen[TimeFrame];                    
  115.                         if(HHValue > HHValue[TimeFrame] && Af[TimeFrame] < AfLimit) {
  116.                             if(Af[TimeFrame] + AfStep > AfLimit) {
  117.                                 Af = AfLimit;
  118.                             } else {
  119.                                 Af = Af[TimeFrame] + AfStep;
  120.                             }               
  121.                         } else {
  122.                             Af = Af[TimeFrame];
  123.                         }   
  124.                         ParOpen = ParClose + Af * (HHValue - ParClose);               
  125.             
  126.                         if(ParOpen > LowHT) {
  127.                             ParOpen = LowHT;
  128.                         }
  129.                         
  130.                         if(ParOpen > LowHT[TimeFrame]) {
  131.                             ParOpen = LowHT[TimeFrame];
  132.                         }
  133.                     }
  134.                 } else {
  135.                     if(HighHT >= ParOpen[TimeFrame]) {
  136.                         Position = 1;
  137.                         Transition = 1;
  138.                         
  139.                         ParClose = LLValue;
  140.                         HHValue = HighHT;
  141.                         LLValue  = LowHT;
  142.                         
  143.                         Af = AfStep;
  144.                         ParOpen = ParClose + Af * (HHValue - ParClose);
  145.                         if(ParOpen > LowHT) {
  146.                             ParOpen = LowHT;
  147.                         }
  148.                         
  149.                         if(ParOpen > LowHT[TimeFrame]) {
  150.                             ParOpen = LowHT[TimeFrame];
  151.                         }
  152.                     } else {
  153.                         Position = Position[TimeFrame];
  154.                         ParClose = ParOpen[TimeFrame];
  155.                            
  156.                         if(LLValue < LLValue[TimeFrame] && Af[TimeFrame] < AfLimit) {
  157.                             if(Af[TimeFrame] + AfStep > AfLimit) {
  158.                                 Af = AfLimit;
  159.                             } else {
  160.                                 Af = Af[TimeFrame] + AfStep;
  161.                             }
  162.                         } else {
  163.                             Af = Af[TimeFrame];
  164.                         }
  165.                         ParOpen = ParClose + Af * (LLValue - ParClose);
  166.             
  167.                         if(ParOpen < HighHT) {
  168.                             ParOpen = HighHT;
  169.                         }
  170.                         
  171.                         if(ParOpen < HighHT[TimeFrame]) {
  172.                             ParOpen = HighHT[TimeFrame];
  173.                         }
  174.                     }
  175.                 }   
  176.             }
  177.         }
  178.     }
  179.    
  180.     //上面的程序,在每根小周期的K线上,记录了它所属的大时间周期下的开高低收等值的变化。
  181.     //接下来,要把在大的时间周期级别上,属于同一根K线的开高低收这些数据,记录在这一组小周期K线的最后一根上。
  182.     barCntSum = barCnt;

  183.     if(BarsBack == 0) {             //如果Bar偏移参数为0,则取每根小周期K线上保留的大时间周期截止到这根小周期K线为止的BAR数据
  184.         barCntSum = 0;
  185.     } else if(BarsBack == 1) {      //如果Bar偏移参数为1,则取大时间周期的上一根K线的BAr数据
  186.         barCntSum = barCnt;
  187.     } else {                        //如果BAR偏移参数为其他,则取大时间周期的指定偏移后的那根K线的BAR数据
  188.         for i = 2 to BarsBack {
  189.             barCntSum = barCntSum + barCnt[barCntSum];
  190.         }
  191.     }

  192.     //最后将相应的K线数据作为引用参数返回
  193.     oParClose = ParClose[barCntSum];
  194.     oParOpen = ParOpen[barCntSum];
  195.     oPosition = Position[barCntSum];
  196.     oTransition = Transition[barCntSum];
  197.    
  198.     Return true;
  199. End
复制代码
大家一直期待的“MtParabolicSAR,跨周期抛物线转向ParabolicSAR函数”出炉了,有需要的帅哥美女拿去用吧,有问题欢迎交流 QQ: 149561420







上面代码请新建用户函数,取名“MtParabolicSAR”。然后以下是调用示例:
  1. Params

  2. Vars       
  3.         Numeric oParCl;
  4.         Numeric oParOp;
  5.         Numeric oPosition;
  6.         Numeric oTransition;
  7.           
  8. begin
  9.         MtParabolicSAR(5, 1, 0.02, 0.2, oParCl, oParOp, oPosition, oTransition);
  10.          
  11.         Commentary("oParCl:" + Text(oParCl));
  12.         Commentary("oParOp:" + Text(oParOp));
  13.         Commentary("oPosition:" + Text(oPosition));
  14.         Commentary("oTransition:" + Text(oTransition));
  15. End                     
复制代码

使用道具 举报

Rank: 1

精华
0
UID
181691
积分
1
帖子
1
主题
0
阅读权限
10
注册时间
2014-3-4
最后登录
1970-1-1
231#
发表于 2014-4-14 10:39:42 |只看该作者
很强大

使用道具 举报

Rank: 2

精华
0
UID
112394
积分
85
帖子
39
主题
14
阅读权限
30
注册时间
2012-9-7
最后登录
2018-5-24
230#
发表于 2014-3-24 16:59:57 |只看该作者
好贴。

使用道具 举报

Rank: 1

精华
0
UID
126927
积分
1
帖子
1
主题
0
阅读权限
10
注册时间
2011-2-21
最后登录
2014-3-17
229#
发表于 2014-3-10 14:29:08 |只看该作者
追涨杀跌 发表于 2011-6-13 01:57
编译好上面的MtMacd函数后,以后就可以随时通过调用这个函数来显示跨周期的MACD指标,以及在小周期上根据大 ...

      “If (MacdDiff[PreBar]<0 and MacdDiff>0)
        {
                Buy(Lots,Open);
        }
        If (MacdDiff[PreBar]>0 and MacdDiff<0)
        {
                SellShort(Lots,Open);
        }”

这里的MacdDiff[PreBar]>0是什么意思?

使用道具 举报

Rank: 2

精华
0
UID
114624
积分
61
帖子
19
主题
6
阅读权限
30
注册时间
2013-1-5
最后登录
2016-6-6
228#
发表于 2014-3-3 12:20:05 |只看该作者
追涨杀跌老师,能否帮助写一下SAR的跨周期指标?最近跟帖在学习,一直没弄明白如何编写SAR的。

使用道具 举报

Rank: 1

精华
0
UID
121786
积分
40
帖子
19
主题
2
阅读权限
10
注册时间
2013-7-28
最后登录
2016-10-14
227#
发表于 2014-2-3 13:14:39 |只看该作者
读书山林 发表于 2011-7-14 13:39
发现一个问题,还请老师帮忙解决一下
   mtBarCnt = MtBar(TimeFrame,BarsBack,refCurBar,refOpen,refHigh, ...


你可以这样子取:

if(BarType == 0) {                          //日线
        BarInterval_ = BarInterval * 1440;
    } else if(BarType == 1) {                   //分钟线
        BarInterval_ = BarInterval;
    } else if(BarType == 4) {                   //周线
        BarInterval_ = BarInterval * 10080;
    } else if(BarType == 5) {                   //月线
        BarInterval_ = BarInterval * 40320;
    }

PlotNumeric("mtClose[10]:", mtClose[10 * TimeFrame / BarInterval_]);

使用道具 举报

Rank: 2

精华
0
UID
121282
积分
76
帖子
39
主题
14
阅读权限
30
注册时间
2013-7-15
最后登录
2018-1-5
226#
发表于 2014-2-1 19:53:40 |只看该作者
收藏,待水平提高后慢慢研究

使用道具 举报

Rank: 5Rank: 5

精华
0
UID
147862
积分
1086
帖子
369
主题
11
阅读权限
60
注册时间
2011-1-1
最后登录
2019-4-1
225#
发表于 2014-1-11 23:50:31 |只看该作者

使用道具 举报

Rank: 1

精华
0
UID
177070
积分
17
帖子
13
主题
4
阅读权限
10
注册时间
2013-12-7
最后登录
2018-10-27
224#
发表于 2013-12-30 19:08:11 |只看该作者
楼主你好  能否写一下:TICK周期里的行情数据
连续3个TICK主动买单,且买的数量大于50就做多;
连续6个TICK,里面有4个主动买单且数量大于30就做多;

使用道具 举报

Rank: 1

精华
0
UID
177755
积分
12
帖子
10
主题
2
阅读权限
10
注册时间
2013-12-18
最后登录
2014-3-11
223#
发表于 2013-12-21 17:35:36 |只看该作者

使用道具 举报

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

bottom

静态版|手机版|联系我们|交易开拓者 ( 粤ICP备07044698   

GMT+8, 2024-5-4 13:04

Powered by Discuz! X2 LicensedChrome插件扩展

© 2011-2012 交易开拓者 Inc.

回顶部