查看: 34027|回复: 461

分形线的构造(画出分形线来)

 火... [复制链接]

41

主题

142

回帖

856

积分

积分
856
发表于 2008-4-8 15:31:39 | 显示全部楼层 |阅读模式
Construction of Fractal Lines

Introduction
Fractals are used by practically all traders. However, if you ask them what a fractal is, they will at best answer that it is an indicator in the Bill Williams' system. The more advanced traders will say that it is a sequence of 5 bars where, if the middle bar's High is higher than those of other bars in the sequence, it is an Up fractal and, if the middle bar's Low is lower than those of other bars, it is a Down fractal. As the phrase goes, "This is all I can tell about the war”.

A brief description of fractals, particularly of their nature and use, is given in the Bill Williams' book named New Trading Dimensions: How to Profit from Chaos in Stocks, Bonds, and Commodities. Something can also be found in the Chekulaev's article named Fractals (in Russian). Mathematical formulas are well described in Shiryaev's book named Fundamentals of Stochastic Mathematics (in Russian).

Use of Fractals

There are two kinds of fractal penetration to be marked – a simple one is where the price exceeds the Up fractal level (falls under the Down fractal level). In this case, it would be better to wait for the close price and open a position at opening the next bar.

fractals.gif

The respective fractals Buy and Sell are marked with arrows in the figure above where we consider the simple fractal penetration. A complex penetration uses 2 fractals – the last and the last but one. They are joint by a straight line that is expected to be penetrated by the close price.

fractal%20lines.gif

Entering points when the fractal line is penetrated are marked by blue and red arrows.
The development environment MQL4 will help us to know the fractal ideology better.

Let us define a problem to test fractals:
  • draw fractals buy/sell;
  • draw horizontal penetrating levels;
  • draw fractal lines;
  • mark the expected entering points with arrows.

Fractals Buy/Sell
This is the most elementary part. We should also consider indicator iFractal available in MQL4 (in Omega, I had to write this indicator myself, and it was rather difficult due to Omega's properties). The examples of how to write this indicator can be found in Code Base.

Horizontal Levels of Penetration

Let us use standard horizontal lines. We will specify the the price of a fractal as a price coordinate, the date of forming the fractal and the current time will be given as time coordinates.

ObjectCreate("SimpleUp"+Up,OBJ_TREND,0,bufUpDate[Up], bufUpPrice[Up],Time[i-1],bufUpPrice[Up]);

ObjectSet("SimpleUp"+Up,OBJPROP_COLOR,Aqua);
ObjectSet("SimpleUp"+Up,OBJPROP_RAY,True);

Fractal Lines
The simplest way seems to be drawing of trend lines through 2 points. We make a ray of the line and wait for penetration. However, indeed, it does not seem to be possible to compare the close price and value of the price on the fractal line since function ObjectGet can return only values of points that have formed the fractal line. So what are we to do?
We should remember analytic geometry. We have 2 points, so we have a straight-line equation. And, since we know the time coordinate, we can easily obtain the price value from the straight-line equation. The canonical straight-line equation looks like this:


formula_2.jpg
We will substitute price and time instead of х and у. The realization thereof is given in function LevelCalculate that calculates the penetration level and, along with this, defines new coordinates of the fractal line to be set using function ObjectSet.

ObjectCreate("LineUp"+Up,OBJ_TREND,0,bufUpDate[Up], bufUpPrice[Up],bufUpDate[Up-1],bufUpPrice[Up-1]);

ObjectSet("LineUp"+Up,OBJPROP_COLOR,Blue);
ObjectSet("LineUp"+Up,OBJPROP_RAY,False);

Placing Arrows

We build all necessary lines in the loop and then comapre them to the current prices. If it penetrates a simple line, we put a yellow arrow. If it penetrates a fractal line, arrow for Buy will be blue, for Sell – red.

All this is realized as indicator FractalLines.mq4.

//+------------------------------------------------------------------+//|                                                 FractalLines.mq4 |//|                      Copyright © 2006, MetaQuotes Software Corp. |//|                                        http://www.metaquotes.net/ |//+------------------------------------------------------------------+#property copyright "Copyright © 2006, MetaQuotes Software Corp."#property link      "http://www.metaquotes.net/" #property indicator_chart_window#property indicator_buffers 2#property indicator_color1 Blue#property indicator_color2 Red//---- input parametersextern int       lines=5;  //The amount of visible fractal linesextern int       MaxFractals=10000; // extern bool       ShowHorisontalLines=true;extern bool       ShowFractalLines=true; //---- buffersdouble ExtMapBuffer1[];double ExtMapBuffer2[];//--- my variablesdouble bufUpPrice[10000];  //price array of Up fractalsdouble bufUpDate[10000];   //date array of Up fractalsdouble bufDownPrice[10000];   //price array of Down fractalsdouble bufDownDate[10000]; //date array of Down fractalsint Up = 0; //counter of Up fractalsint Down = 0;  //counter of Down fractals //The function calculates the price value of penetration of the fractal line by the simplest//equations of analytic geometrydouble LevelCalculate(double Price1, double Time1, double Price2,                      double Time2, double NewTime){   double level;   if (Time2!=Time1)// Just in case, to avoid zero divide.   {      level=(NewTime-Time1)*(Price2-Price1)/(Time2-Time1)+Price1;   }   else   {       return(Price2);   }   return(level);} //+------------------------------------------------------------------+//| Custom indicator initialization function                         |//+------------------------------------------------------------------+int init()  {//---- indicators   SetIndexStyle(0,DRAW_ARROW);   SetIndexArrow(0,217);   SetIndexBuffer(0,ExtMapBuffer1);   SetIndexEmptyValue(0,0.0);   SetIndexStyle(1,DRAW_ARROW);   SetIndexArrow(1,218);   SetIndexBuffer(1,ExtMapBuffer2);   SetIndexEmptyValue(1,0.0);//----   return(0);  }//+------------------------------------------------------------------+//| Custom indicator deinitialization function                       |//+------------------------------------------------------------------+int deinit()  {//----   //----   return(0);  }//+------------------------------------------------------------------+//| Custom indicator iteration function                              |//+------------------------------------------------------------------+int start()  {   int    counted_bars=IndicatorCounted();//---- the last calculated bar will be recalculated      if(counted_bars > 0)        counted_bars--;   int limit = Bars - counted_bars;// We will rather place arrows at the moment of penetration of fractal lines, // estimate efficiency // The idea was borrowed from Rosh, hopefully he will not be offended by this       string arrowName; // here, we will give the arrow a unique name    //The number of the penetrated fractal  //Penetration of the fractal line  int FractalUp = 0;  int FractalDown = 0;  //Simple penetration of a fractal  int SimpleFractalUp = 0;  int SimpleFractalDown = 0;    double BuyFractalLevel = 0;  //penetration level of the Up fractal line  double SellFractalLevel = 0; //penetration level of the Down fractal line  double buf = 0; // buffer value of fractal being available; if it is 0, there is no fractal at all //---- the main loop          for(int i = limit; i>0; i--)   {             //Draw simple fractal levels              //Define the current fractal levels           BuyFractalLevel=LevelCalculate(bufUpPrice[Up],bufUpDate[Up],                   bufUpPrice[Up-1],bufUpDate[Up-1],Time);         //Move the second coordinate of the Up fractal line                                       ObjectSet("LineUp"+Up,OBJPROP_TIME1,Time);         ObjectSet("LineUp"+Up,OBJPROP_PRICE1,BuyFractalLevel);          SellFractalLevel=LevelCalculate(bufDownPrice[Down],                          bufDownDate[Down],bufDownPrice[Down-1],                          bufDownDate[Down-1],Time);         //Move the second coordinate of the Down fractal line                                        ObjectSet("LineDown"+Down,OBJPROP_TIME1,Time);         ObjectSet("LineDown"+Down,OBJPROP_PRICE1,SellFractalLevel);               //Search for a simple penetration         if (Close>ObjectGet("SimpleUp"+Up,OBJPROP_PRICE1)&&                          (Up>SimpleFractalUp))         {            arrowName="SimleUpArrow"+Up;            ObjectCreate(arrowName,OBJ_ARROW,0,Time[i-1],                         Low[i-1]-Point*10);            ObjectSet(arrowName,OBJPROP_ARROWCODE,241);            ObjectSet(arrowName,OBJPROP_COLOR,Yellow);            SimpleFractalUp=Up;                      }                  if (Close<ObjectGet("SimpleDown"+Down,OBJPROP_PRICE1)&&                          (Down>SimpleFractalDown))         {            arrowName="SimleUpArrow"+Down;            ObjectCreate(arrowName,OBJ_ARROW,0,Time[i-1],                         High[i-1]+Point*10);            ObjectSet(arrowName,OBJPROP_ARROWCODE,242);            ObjectSet(arrowName,OBJPROP_COLOR,Yellow);            SimpleFractalDown=Down;                      }                                                                                                                                 //Search for a complex penetration        if ((Close>BuyFractalLevel)&&(Up>FractalUp))          {            //Put an up-arrow            arrowName="UpArrow"+Up;            ObjectCreate(arrowName,OBJ_ARROW,0,Time[i-1],                         Low[i-1]-Point*10);            ObjectSet(arrowName,OBJPROP_ARROWCODE,241);            ObjectSet(arrowName,OBJPROP_COLOR,Blue);            FractalUp=Up;                 }                                                  if ((Close<SellFractalLevel)&&(Down>FractalDown))         {            //Put a down-arrow            arrowName="DownArrow"+Down;            ObjectCreate(arrowName,OBJ_ARROW,0,Time[i-1],                         High[i-1]+Point*10);            ObjectSet(arrowName,OBJPROP_ARROWCODE,242);            ObjectSet(arrowName,OBJPROP_COLOR,Red);             FractalDown=Down;                }        //Draw the Up fractal itself        ExtMapBuffer1 = iFractals(NULL, 0, MODE_UPPER, i);                //If it is available, place it in the array of fractals        buf = iFractals(NULL, 0, MODE_UPPER, i);        if (buf!=0)        {            Up++;            bufUpPrice[Up]=iFractals(NULL, 0, MODE_UPPER, i);            bufUpDate[Up]=Time;            //The current fractal penetration level - fractal itself            BuyFractalLevel=bufUpPrice[Up];                        if (Up>1)                        {               //Simple fractal               ObjectCreate("SimpleUp"+Up,OBJ_TREND,0,bufUpDate[Up],                   bufUpPrice[Up],Time[i-1],bufUpPrice[Up]);      ObjectSet("SimpleUp"+Up,OBJPROP_COLOR,Aqua);      ObjectSet("SimpleUp"+Up,OBJPROP_RAY,True);                  //Draw fractal lines on 2 coordinates               ObjectCreate("LineUp"+Up,OBJ_TREND,0,bufUpDate[Up],                  bufUpPrice[Up],bufUpDate[Up-1],bufUpPrice[Up-1]);       ObjectSet("LineUp"+Up,OBJPROP_COLOR,Blue);      ObjectSet("LineUp"+Up,OBJPROP_RAY,False);               //Remove the outdated lines               if (Up>lines+1)               {                  ObjectDelete("LineUp"+(Up-lines));                  ObjectDelete("SimpleUp"+(Up-lines));                                 }            }             }         //A similar block, but for Down fractals        ExtMapBuffer2 = iFractals(NULL, 0, MODE_LOWER, i);        buf = iFractals(NULL, 0, MODE_LOWER, i);            if (buf!=0)        {            Down++;            bufDownPrice[Down]=iFractals(NULL, 0, MODE_LOWER, i);            bufDownDate[Down]=Time;            SellFractalLevel=bufDownPrice[Down];                                                     if (Down>1)                        {               ObjectCreate("SimpleDown"+Down,OBJ_TREND,0,bufDownDate[Down],                   bufDownPrice[Down],Time[i-1],bufDownPrice[Down]);                       ObjectSet("SimpleDown"+Down,OBJPROP_COLOR,LightCoral);               ObjectSet("SimpleDown"+Down,OBJPROP_RAY,True);                                                 ObjectCreate("LineDown"+Down,OBJ_TREND,0,                            bufDownDate[Down],bufDownPrice[Down],                            bufDownDate[Down-1],bufDownPrice[Down-1]);                       ObjectSet("LineDown"+Down,OBJPROP_COLOR,Red);               ObjectSet("LineDown"+Down,OBJPROP_RAY,False);               if (Down>lines+1)               {                  ObjectDelete("LineDown"+(Down-lines));                  ObjectDelete("SimpleDown"+(Down-lines));               }                        }             }                   if (!ShowHorisontalLines)        {               ObjectDelete("SimpleDown"+Down);                          ObjectDelete("SimpleUp"+Up);                        }        if (!ShowFractalLines)        {            ObjectDelete("LineDown"+Down);                    ObjectDelete("LineUp"+Up);        }               }   //----   return(0);  }//+-----------------------------------------------------------------


Old lines must be removed, otherwise the chart will look like a palette. There is a couple of additional settings provided in the indicator, such as visibility of lines or their amount. The result of the indicator's activities is given below.

result.gif
This touches the spot for those who love fractals!


Attachments:附件:
游客,如果您要查看本帖隐藏内容请回复


猴吻.gif

[ 本帖最后由 gget 于 2008-4-9 22:42 编辑 ]

5

主题

1330

回帖

3843

积分

积分
3843

周年纪念水鬼混世魔王暴发户

发表于 2013-9-13 10:38:07 | 显示全部楼层
本帖最后由 hezhigang6 于 2013-9-13 10:41 编辑

分形适用于30F周期以上的大结构,小周期没有必要用分形技术......哈哈......

4

主题

1148

回帖

3044

积分

积分
3044
发表于 2013-8-26 08:02:58 | 显示全部楼层
如果您要查看本帖隐藏内容请回复

38

主题

703

回帖

1752

积分

小兵

积分
1752
发表于 2013-10-18 09:03:30 | 显示全部楼层
看看啊看啊啊  

29

主题

115

回帖

594

积分

积分
594
发表于 2008-4-9 06:06:31 | 显示全部楼层
有没有好心人翻译一下啊

0

主题

88

回帖

416

积分

积分
416
发表于 2008-4-9 09:23:55 | 显示全部楼层
顶一个,lz真是有心人,知识面如此之广!佩服!
不过我觉得这个分形线指标信号太多,杂乱无章。正如他自己说,像个“调色板”。
这是因为没有过滤掉众多的次分形而只保留主分形。
这样既不能提供有效的交易信号,又会造成过多的失误单。
这里面并没有清晰地揭示处分形市场的根本结构,及其多分型行走的模式及秩序。
更没有量化这种秩序从而适合于建模。

[ 本帖最后由 hjgc 于 2008-4-9 10:08 编辑 ]

0

主题

19

回帖

52

积分

积分
52
发表于 2008-4-9 09:33:45 | 显示全部楼层
See~~~~~~

41

主题

142

回帖

856

积分

积分
856
 楼主| 发表于 2008-4-9 17:38:22 | 显示全部楼层
今日(2008.04.09)EUR/USD 这个指标的多条分形线共振同时给出的信号

fractallines.gif

[ 本帖最后由 gget 于 2008-4-9 17:54 编辑 ]

0

主题

88

回帖

416

积分

积分
416
发表于 2008-4-9 19:09:00 | 显示全部楼层
也传2张图,是俺的分形模式EA系统的测试做单记录图:
分形模式Ea交易系统欧元做单图.gif
分形模式Ea交易系统黄金做单图.gif

0

主题

88

回帖

416

积分

积分
416
发表于 2008-4-9 19:18:47 | 显示全部楼层
分形模式Ea交易系统指标信号图图.gif

21

主题

129

回帖

451

积分

积分
451
QQ
发表于 2008-4-12 13:18:08 | 显示全部楼层
See~~~~~~

5

主题

536

回帖

1572

积分

积分
1572
发表于 2008-4-12 19:14:45 | 显示全部楼层
看看!回复!

22

主题

99

回帖

490

积分

积分
490
发表于 2008-4-12 20:51:54 | 显示全部楼层

6

主题

116

回帖

327

积分

积分
327
发表于 2008-4-12 21:00:37 | 显示全部楼层
ZIGZAG指标和这个分形指标有什么区别

12

主题

316

回帖

1829

积分

积分
1829
发表于 2008-4-13 16:15:10 | 显示全部楼层
898989 8989888888888888888888

2

主题

17

回帖

93

积分

积分
93
发表于 2008-4-13 22:48:44 | 显示全部楼层

回复 1# 的帖子

dddddddddddddddddddddddd

8

主题

84

回帖

387

积分

▄︻┻═┳一

积分
387
QQ
发表于 2008-4-15 20:37:34 | 显示全部楼层

0

主题

88

回帖

416

积分

积分
416
发表于 2008-4-16 17:33:26 | 显示全部楼层
原帖由 aaazhy585 于 2008-4-12 21:00 发表
ZIGZAG指标和这个分形指标有什么区别

---------------------------------------------
实际应用是是需要和zigzag及fibo联合使用才有实战意义。
分形线、zigzag、fibo图.gif
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

风险提示:本站内容仅供学习交流,不构成投资建议;禁止开户导流、喊单与代客理财。系统已启用敏感词自动进审/禁发。 · 客服/举报
小黑屋|手机版|Archiver|联系我们|法律声明|智慧网

Powered by Discuz! X5.0 Licensed © 2001-2026 Discuz! Team.|京ICP备15025159号-2

QQ客服返回顶部