“MtParabolicSAR,跨周期抛物线转向ParabolicSAR函数”出炉 [开拓者 TB]
- 咨询内容:
本帖最后由 扶老二 于 2014-5-11 19:34 编辑
“MtParabolicSAR,跨周期抛物线转向ParabolicSAR函数”出炉
有需要的帅哥美女拿去用吧,续贴到“追涨杀跌”的帖子里了:
http://bbs.tb18.net/thread-15184-24-1.html
有问题欢迎交流 QQ: 149561420- //------------------------------------------------------------------------
- // MtParabolicSAR,跨周期抛物线转向ParabolicSAR函数
- // 返回 当前bar的“周期索引”,比如当前在1分钟周期里,折算成15分钟周期的话,从凌晨00:00分钟开始,第1根bar是1,第2根bar是2......第15根bar是15,第16根bar又变成1,......
- // 除了函数本身的返回值,还通过oParCl、oParOpen、oPosition、oTransition这四个引用参数把对应Bar的停损值、对应Bar的停损值下一根的停损值、对应Bar建议的持仓状态(1 - 多头,-1 - 空头)、对应Bar的状态是否发生反转(1 或 -1 为反转,0 为保持不变)
- // 版本 20140511_183500
- //------------------------------------------------------------------------
- Params
- Numeric TimeFrame(1440); //目标时间周期参数,参数说明参见MtBar
- Numeric BarsBack(1); //目标时间周期BAR偏移参数,说明见MtBar函数
- Numeric AfStep(0.02);
- Numeric AfLimit(0.2);
- NumericRef oParClose;
- NumericRef oParOpen;
- NumericRef oPosition;
- NumericRef oTransition;
- Vars
- NumericSeries barCnt;
- NumericSeries CurBar;
- NumericSeries barCntSum;
- NumericSeries HighHT;
- NumericSeries LowHT;
- Numeric CurTime;
- Numeric PreTime;
- bool condition(false);
- Numeric n;
- Numeric i;
- NumericSeries Af(0);
- NumericSeries ParOpen(0);
- NumericSeries ParClose(0);
- NumericSeries Position(0);
- NumericSeries HHValue(0);
- NumericSeries LLValue(0);
- NumericSeries Transition(0);
-
- Begin
- if(TimeFrame == 40320) { //月线
- CurTime = Month;
- PreTime = Month[1];
- } else if(TimeFrame == 10080) { //周线
- CurTime = IntPart(DateDiff(19700105, TrueDate(0)) / 7);
- PreTime = IntPart(DateDiff(19700105, TrueDate(1)) / 7);
- } else { //其他时间周期
- CurTime = IntPart((DateDiff(19700105, TrueDate(0)) * 1440 + Hour * 60 + Minute) / TimeFrame);
- PreTime = IntPart((DateDiff(19700105, TrueDate(1)) * 1440 + Hour[1] * 60 + Minute[1]) / TimeFrame);
- }
- condition = CurTime != PreTime;
- if(CurrentBar == 0) { //如果是第一根Bar, CurBar=0
- barCnt = 0;
- CurBar = 0;
- HighHT = High[1];
- LowHT = Low[1];
-
- Position = 1;
- Transition = 1;
- Af = AfStep;
- HHValue = HighHT;
- LLValue = LowHT;
- ParClose = LLValue;
- ParOpen = ParClose + Af * (HHValue - ParClose);
- if(ParOpen > LowHT) {
- ParOpen = LowHT;
- }
- } else {
- if(condition) { //如果在目标周期下,属于另一根K线,则CurBar加1
- barCnt = 1;
- CurBar = CurBar[1] + 1;
- HighHT = High[1];
- LowHT = Low[1];
- for n = 2 to TimeFrame {
- HighHT = Max(HighHT, High[n]);
- LowHT = Min(LowHT, Low[n]);
- }
- } else { //如果在目标周期下,属于同一根K线,则CurBar不变,但最高价和最低价要记录价格的变化,成交量要累加
- barCnt = barCnt[1] + 1;
- CurBar = CurBar[1];
- HighHT = Max(HighHT[1], High);
- LowHT = Min(LowHT[1], Low);
- }
-
- HHValue = Max(HHValue[1], HighHT);
- LLValue = Min(LLValue[1], LowHT);
- if(CurBar == 0) {
- Position = 1;
- Transition = 1;
- Af = AfStep;
- ParClose = LLValue;
- ParOpen = ParClose + Af * (HHValue - ParClose);
- if(ParOpen > LowHT) {
- ParOpen = LowHT;
- }
- } else {
- if(condition) { //正好切换到目标周期的下一根k线
- Transition = 0;
- if(Position[TimeFrame] == 1) {
- if(LowHT <= ParOpen[TimeFrame]) {
- Position = -1;
- Transition = -1;
- ParClose = HHValue;
- HHValue = HighHT;
- LLValue = LowHT;
-
- Af = AfStep;
- ParOpen = ParClose + Af * (LLValue - ParClose);
-
- if(ParOpen < HighHT) {
- ParOpen = HighHT;
- }
-
- if(ParOpen < HighHT[TimeFrame]) {
- ParOpen = HighHT[TimeFrame];
- }
- } else {
- Position = Position[TimeFrame];
- ParClose = ParOpen[TimeFrame];
- if(HHValue > HHValue[TimeFrame] && Af[TimeFrame] < AfLimit) {
- if(Af[TimeFrame] + AfStep > AfLimit) {
- Af = AfLimit;
- } else {
- Af = Af[TimeFrame] + AfStep;
- }
- } else {
- Af = Af[TimeFrame];
- }
- ParOpen = ParClose + Af * (HHValue - ParClose);
-
- if(ParOpen > LowHT) {
- ParOpen = LowHT;
- }
-
- if(ParOpen > LowHT[TimeFrame]) {
- ParOpen = LowHT[TimeFrame];
- }
- }
- } else {
- if(HighHT >= ParOpen[TimeFrame]) {
- Position = 1;
- Transition = 1;
-
- ParClose = LLValue;
- HHValue = HighHT;
- LLValue = LowHT;
-
- Af = AfStep;
- ParOpen = ParClose + Af * (HHValue - ParClose);
- if(ParOpen > LowHT) {
- ParOpen = LowHT;
- }
-
- if(ParOpen > LowHT[TimeFrame]) {
- ParOpen = LowHT[TimeFrame];
- }
- } else {
- Position = Position[TimeFrame];
- ParClose = ParOpen[TimeFrame];
-
- if(LLValue < LLValue[TimeFrame] && Af[TimeFrame] < AfLimit) {
- if(Af[TimeFrame] + AfStep > AfLimit) {
- Af = AfLimit;
- } else {
- Af = Af[TimeFrame] + AfStep;
- }
- } else {
- Af = Af[TimeFrame];
- }
- ParOpen = ParClose + Af * (LLValue - ParClose);
-
- if(ParOpen < HighHT) {
- ParOpen = HighHT;
- }
-
- if(ParOpen < HighHT[TimeFrame]) {
- ParOpen = HighHT[TimeFrame];
- }
- }
- }
- }
- }
- }
-
- //上面的程序,在每根小周期的K线上,记录了它所属的大时间周期下的开高低收等值的变化。
- //接下来,要把在大的时间周期级别上,属于同一根K线的开高低收这些数据,记录在这一组小周期K线的最后一根上。
- barCntSum = barCnt;
- if(BarsBack == 0) { //如果Bar偏移参数为0,则取每根小周期K线上保留的大时间周期截止到这根小周期K线为止的BAR数据
- barCntSum = 0;
- } else if(BarsBack == 1) { //如果Bar偏移参数为1,则取大时间周期的上一根K线的BAr数据
- barCntSum = barCnt;
- } else { //如果BAR偏移参数为其他,则取大时间周期的指定偏移后的那根K线的BAR数据
- for i = 2 to BarsBack {
- barCntSum = barCntSum + barCnt[barCntSum];
- }
- }
- //最后将相应的K线数据作为引用参数返回
- oParClose = ParClose[barCntSum];
- oParOpen = ParOpen[barCntSum];
- oPosition = Position[barCntSum];
- oTransition = Transition[barCntSum];
-
- Return true;
- End
- //------------------------------------------------------------------------
- TB技术人员:
群主我用rb的日线跟此函数timeframe1440进行了对比发现不匹配
- TB客服:
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:- //------------------------------------------------------------------------
- // 西门 ParabolicSAR_ximen
- // 描述 求抛物线转向,修正系统默认用当前bar会导致闪烁的问题,采用[1]来做判断依据,比系统默认函数更精确
- // 返回 True 或 false。除了函数本身的返回值,还通过oParCl、oParOpen、oPosition、oTransition这四个引用参数把对应Bar的停损值、对应Bar的停损值下一根的停损值、对应Bar建议的持仓状态(1 - 多头,-1 - 空头)、对应Bar的状态是否发生反转(1 或 -1 为反转,0 为保持不变)
- // 版本 20140511
- //------------------------------------------------------------------------
- Params
- Numeric AfStep(0.02);
- Numeric AfLimit(0.2);
- NumericRef oParClose;
- NumericRef oParOpen;
- NumericRef oPosition;
- NumericRef oTransition;
-
- Vars
- NumericSeries HighHT;
- NumericSeries LowHT;
- NumericSeries Af(0);
- NumericSeries ParOpen(0);
- NumericSeries ParClose(0);
- NumericSeries Position(0);
- NumericSeries HHValue(0);
- NumericSeries LLValue(0);
- NumericSeries Transition(0);
- Begin
- if(CurrentBar == 0) {
- HighHT = High;
- LowHT = Low;
-
- Position = 1;
- Transition = 1;
- Af = AfStep;
- HHValue = HighHT;
- LLValue = LowHT;
- ParClose = LLValue;
- ParOpen = ParClose + Af * ( HHValue - ParClose);
- if(ParOpen > LowHT) {
- ParOpen = LowHT;
- }
- } else {
- HighHT = High[1];
- LowHT = Low[1];
-
- HHValue = Max(HHValue[1], HighHT);
- LLValue = Min(LLValue[1], LowHT);
- Transition = 0;
- if(Position[1] == 1) {
- if(LowHT <= ParOpen[1]) {
- Position = -1;
- Transition = -1;
- ParClose = HHValue;
- HHValue = HighHT;
- LLValue = LowHT;
-
- Af = AfStep;
- ParOpen = ParClose + Af * (LLValue - ParClose);
-
- if(ParOpen < HighHT) {
- ParOpen = HighHT;
- }
-
- if(ParOpen < HighHT[1]) {
- ParOpen = HighHT[1];
- }
- } else {
- Position = Position[1];
- ParClose = ParOpen[1];
- if(HHValue > HHValue[1] && Af[1] < AfLimit) {
- if(Af[1] + AfStep > AfLimit) {
- Af = AfLimit;
- } else {
- Af = Af[1] + AfStep;
- }
- } else {
- Af = Af[1];
- }
- ParOpen = ParClose + Af * (HHValue - ParClose);
-
- if(ParOpen > LowHT) {
- ParOpen = LowHT;
- }
-
- if(ParOpen > LowHT[1]) {
- ParOpen = LowHT[1];
- }
- }
- } else {
- if(HighHT >= ParOpen[1]) {
- Position = 1;
- Transition = 1;
-
- ParClose = LLValue;
- HHValue = HighHT;
- LLValue = LowHT;
-
- Af = AfStep;
- ParOpen = ParClose + Af * (HHValue - ParClose);
- if(ParOpen > LowHT) {
- ParOpen = LowHT;
- }
-
- if(ParOpen > LowHT[1]) {
- ParOpen = LowHT[1];
- }
- } else {
- Position = Position[1];
- ParClose = ParOpen[1];
-
- if(LLValue < LLValue[1] && Af[1] < AfLimit) {
- if(Af[1] + AfStep > AfLimit) {
- Af = AfLimit;
- } else {
- Af = Af[1] + AfStep;
- }
- } else {
- Af = Af[1];
- }
- ParOpen = ParClose + Af * (LLValue - ParClose);
-
- if(ParOpen < HighHT) {
- ParOpen = HighHT;
- }
-
- if(ParOpen < HighHT[1]) {
- ParOpen = HighHT[1];
- }
- }
- }
- }
-
- oParClose = ParClose;
- oParOpen = ParOpen;
- oPosition = Position;
- oTransition = Transition;
- Return True;
- End
- Params
- Vars
- Numeric oParCl;
- Numeric oParOp;
- Numeric oPosition;
- Numeric oTransition;
-
- begin
- ParabolicSAR_ximen(0.02, 0.2, oParCl, oParOp, oPosition, oTransition);
-
- Commentary("oParCl:" + Text(oParCl));
- Commentary("oParOp:" + Text(oParOp));
- Commentary("oPosition:" + Text(oPosition));
- Commentary("oTransition:" + Text(oTransition));
- End
- //------------------------------------------------------------------------
有思路,想编写各种指标公式,程序化交易模型,选股公式,预警公式的朋友
可联系技术人员 QQ: 1145508240 进行 有偿 编写!(不贵!点击查看价格!)
相关文章
-
没有相关内容