设为首页收藏本站

 找回密码
 注册
查看: 7963|回复: 10
打印 上一主题 下一主题

“MtParabolicSAR,跨周期抛物线转向ParabolicSAR函数”出炉 [复制链接]

Rank: 1

精华
0
UID
121786
积分
40
帖子
19
主题
2
阅读权限
10
注册时间
2013-7-28
最后登录
2016-10-14
跳转到指定楼层
1#
发表于 2014-5-11 19:09:18 |只看该作者 |倒序浏览
本帖最后由 扶老二 于 2014-5-11 19:34 编辑

“MtParabolicSAR,跨周期抛物线转向ParabolicSAR函数”出炉

有需要的帅哥美女拿去用吧,续贴到“追涨杀跌”的帖子里了:

http://bbs.tb18.net/thread-15184-24-1.html

有问题欢迎交流 QQ: 149561420
  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”。然后以下是调用示例:
  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: 4

精华
0
UID
115197
积分
307
帖子
23
主题
9
阅读权限
50
注册时间
2013-1-28
最后登录
2018-8-6
2#
发表于 2014-5-13 15:12:14 |只看该作者
群主我用rb的日线跟此函数timeframe1440进行了对比发现不匹配

使用道具 举报

Rank: 1

精华
0
UID
121786
积分
40
帖子
19
主题
2
阅读权限
10
注册时间
2013-7-28
最后登录
2016-10-14
3#
发表于 2014-5-13 21:56:37 |只看该作者
duck_arrow 发表于 2014-5-13 15:12
群主我用rb的日线跟此函数timeframe1440进行了对比发现不匹配

跟tb自带的ParabolicSAR函数比对发现不匹配。其实主要是tb自带的ParabolicSAR函数代码里High和Low都用的当前k线的数据导致,这是有问题的,当前k线还没运行结束之前,High和Low都是未来的属性,所以直接使用它,会导致实盘时候信号闪烁和使用了未来属性,我修复了一下tb自带的ParabolicSAR,用High[1]和Low[1]来取代High和Low,取名ParabolicSAR_ximen,请用以下代码新建用户函数,函数名叫ParabolicSAR_ximen,然后在不需要跨周期的时候,建议用ParabolicSAR_ximen来替换ParabolicSAR:
  1. //------------------------------------------------------------------------
  2. // 西门 ParabolicSAR_ximen
  3. // 描述 求抛物线转向,修正系统默认用当前bar会导致闪烁的问题,采用[1]来做判断依据,比系统默认函数更精确
  4. // 返回 True 或 false。除了函数本身的返回值,还通过oParCl、oParOpen、oPosition、oTransition这四个引用参数把对应Bar的停损值、对应Bar的停损值下一根的停损值、对应Bar建议的持仓状态(1 - 多头,-1 - 空头)、对应Bar的状态是否发生反转(1 或 -1 为反转,0 为保持不变)
  5. // 版本 20140511
  6. //------------------------------------------------------------------------


  7. Params
  8.     Numeric AfStep(0.02);
  9.     Numeric AfLimit(0.2);
  10.     NumericRef oParClose;
  11.     NumericRef oParOpen;
  12.     NumericRef oPosition;
  13.     NumericRef oTransition;
  14.    
  15. Vars
  16.     NumericSeries HighHT;
  17.     NumericSeries LowHT;

  18.     NumericSeries Af(0);   
  19.     NumericSeries ParOpen(0);
  20.     NumericSeries ParClose(0);
  21.     NumericSeries Position(0);  
  22.     NumericSeries HHValue(0);
  23.     NumericSeries LLValue(0);
  24.     NumericSeries Transition(0);

  25. Begin               
  26.     if(CurrentBar == 0) {
  27.                 HighHT = High;
  28.                 LowHT = Low;
  29.                
  30.         Position = 1;
  31.         Transition = 1;
  32.         Af = AfStep;
  33.         HHValue = HighHT;
  34.         LLValue = LowHT;
  35.         ParClose = LLValue;
  36.         ParOpen = ParClose + Af * ( HHValue - ParClose);
  37.         if(ParOpen > LowHT) {
  38.             ParOpen = LowHT;
  39.         }
  40.     } else {
  41.                 HighHT = High[1];
  42.                 LowHT = Low[1];
  43.                
  44.                 HHValue = Max(HHValue[1], HighHT);
  45.                 LLValue = Min(LLValue[1], LowHT);

  46.         Transition = 0;

  47.         if(Position[1] == 1) {
  48.             if(LowHT <= ParOpen[1]) {
  49.                 Position = -1;
  50.                 Transition = -1;              
  51.                 ParClose = HHValue;
  52.                 HHValue = HighHT;
  53.                 LLValue  = LowHT;
  54.    
  55.                 Af = AfStep;
  56.                 ParOpen = ParClose + Af * (LLValue - ParClose);
  57.                     
  58.                 if(ParOpen < HighHT) {
  59.                     ParOpen = HighHT;
  60.                 }
  61.                
  62.                 if(ParOpen < HighHT[1]) {
  63.                     ParOpen = HighHT[1];
  64.                 }
  65.             } else {
  66.                 Position = Position[1];
  67.                 ParClose = ParOpen[1];                    
  68.                 if(HHValue > HHValue[1] && Af[1] < AfLimit) {
  69.                     if(Af[1] + AfStep > AfLimit) {
  70.                         Af = AfLimit;
  71.                     } else {
  72.                         Af = Af[1] + AfStep;
  73.                     }               
  74.                 } else {
  75.                     Af = Af[1];
  76.                 }   
  77.                 ParOpen = ParClose + Af * (HHValue - ParClose);               
  78.    
  79.                 if(ParOpen > LowHT) {
  80.                     ParOpen = LowHT;
  81.                 }
  82.                
  83.                 if(ParOpen > LowHT[1]) {
  84.                     ParOpen = LowHT[1];
  85.                 }
  86.             }
  87.         } else {
  88.             if(HighHT >= ParOpen[1]) {
  89.                 Position = 1;
  90.                 Transition = 1;
  91.                
  92.                 ParClose = LLValue;
  93.                 HHValue = HighHT;
  94.                 LLValue  = LowHT;
  95.                
  96.                 Af = AfStep;
  97.                 ParOpen = ParClose + Af * (HHValue - ParClose);
  98.                 if(ParOpen > LowHT) {
  99.                     ParOpen = LowHT;
  100.                 }
  101.                
  102.                 if(ParOpen > LowHT[1]) {
  103.                     ParOpen = LowHT[1];
  104.                 }
  105.             } else {
  106.                 Position = Position[1];
  107.                 ParClose = ParOpen[1];
  108.                     
  109.                 if(LLValue < LLValue[1] && Af[1] < AfLimit) {
  110.                     if(Af[1] + AfStep > AfLimit) {
  111.                         Af = AfLimit;
  112.                     } else {
  113.                         Af = Af[1] + AfStep;
  114.                     }
  115.                 } else {
  116.                     Af = Af[1];
  117.                 }
  118.                 ParOpen = ParClose + Af * (LLValue - ParClose);
  119.    
  120.                 if(ParOpen < HighHT) {
  121.                     ParOpen = HighHT;
  122.                 }
  123.                
  124.                 if(ParOpen < HighHT[1]) {
  125.                     ParOpen = HighHT[1];
  126.                 }
  127.             }
  128.         }  
  129.     }   
  130.    
  131.     oParClose = ParClose;
  132.     oParOpen = ParOpen;
  133.     oPosition = Position;
  134.     oTransition = Transition;

  135.     Return True;
  136. End
复制代码
下面是不跨周期的ParabolicSAR_ximen调用的例子:
  1. Params

  2. Vars       
  3.         Numeric oParCl;
  4.         Numeric oParOp;
  5.         Numeric oPosition;
  6.         Numeric oTransition;
  7.        
  8. begin
  9.         ParabolicSAR_ximen(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: 4

精华
0
UID
115197
积分
307
帖子
23
主题
9
阅读权限
50
注册时间
2013-1-28
最后登录
2018-8-6
4#
发表于 2014-5-14 10:20:55 |只看该作者
扶老二 发表于 2014-5-13 21:56
跟tb自带的ParabolicSAR函数比对发现不匹配。其实主要是tb自带的ParabolicSAR函数代码里High和Low都用的 ...

如果原始的ParabolicSAR中直接用High[1] 和 Low[1] 来和ParOpen[1]比较可以吗?
类似:if(High[1] >= ParOpen[1]) {
                Position = 1;
                Transition = 1;

使用道具 举报

Rank: 1

精华
0
UID
121786
积分
40
帖子
19
主题
2
阅读权限
10
注册时间
2013-7-28
最后登录
2016-10-14
5#
发表于 2014-5-20 23:20:29 |只看该作者
duck_arrow 发表于 2014-5-14 10:20
如果原始的ParabolicSAR中直接用High[1] 和 Low[1] 来和ParOpen[1]比较可以吗?
类似:if(High[1] >= Par ...

这个也可以的

使用道具 举报

Rank: 1

精华
0
UID
187138
积分
1
帖子
1
主题
0
阅读权限
10
注册时间
2014-5-20
最后登录
1970-1-1
6#
发表于 2014-5-21 08:45:40 |只看该作者
学习,学习,暂时还无法理解

使用道具 举报

Rank: 6Rank: 6

精华
0
UID
174690
积分
2359
帖子
15
主题
2
阅读权限
70
注册时间
2013-10-29
最后登录
2014-6-25
7#
发表于 2014-6-5 14:35:09 |只看该作者
有个疑问,这个,代码中[barCntSum]应该是 吧,请楼主明示
附件: 你需要登录才可以下载或查看附件。没有帐号?注册

使用道具 举报

Rank: 6Rank: 6

精华
0
UID
174690
积分
2359
帖子
15
主题
2
阅读权限
70
注册时间
2013-10-29
最后登录
2014-6-25
8#
发表于 2014-6-6 11:54:50 |只看该作者
还有一个问题,这个是不是只针对从1分钟转换为其他周期有效的

使用道具 举报

Rank: 1

精华
0
UID
121786
积分
40
帖子
19
主题
2
阅读权限
10
注册时间
2013-7-28
最后登录
2016-10-14
9#
发表于 2014-6-7 20:47:45 |只看该作者
TBLearning 发表于 2014-6-6 11:54
还有一个问题,这个是不是只针对从1分钟转换为其他周期有效的

不只一分钟转换为其他周期有效,理论上各个周期都有效

使用道具 举报

Rank: 6Rank: 6

精华
0
UID
174690
积分
2359
帖子
15
主题
2
阅读权限
70
注册时间
2013-10-29
最后登录
2014-6-25
10#
发表于 2014-6-9 10:07:03 |只看该作者
TBLearning 发表于 2014-6-5 14:35
有个疑问,这个,代码中应该是 吧,请楼主明示

哦,终于明白了

使用道具 举报

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

bottom

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

GMT+8, 2024-5-22 22:45

Powered by Discuz! X2 LicensedChrome插件扩展

© 2011-2012 交易开拓者 Inc.

回顶部