📅 财经日历 📊 实时波动 📈 大盘云图 📶 行情走势 🆚 投机情绪 🚀 今日热点

    Phase Adaptive_v1.0

    admin LV20
    2018-03-28 · 3145 阅读

    phase_adaptive_v1.0_600__2.png


    1. //+------------------------------------------------------------------+
    2. //|                                     Phase Adaptive_v1.0 600+.mq4 |
    3. //|                                Copyright © 2016, TrendLaboratory |
    4. //|            http://finance.groups.yahoo.com/group/TrendLaboratory |
    5. //|                                   E-mail: igorad2003@yahoo.co.uk |
    6. //+------------------------------------------------------------------+
    7. #property copyright "Copyright © 2016, TrendLaboratory"
    8. #property link      "http://finance.groups.yahoo.com/group/TrendLaboratory"
    9. #property link      "http://newdigital-world.com/forum.php"


    10. #property indicator_separate_window
    11. #property indicator_buffers   1
    12. #property indicator_color1    clrDeepSkyBlue
    13. #property indicator_width1    2

    14. #property indicator_level1    90
    15. #property indicator_level2    180
    16. #property indicator_level3    270

    17. #property indicator_minimum   0
    18. #property indicator_maximum   360   

    19. enum ENUM_PRICE
    20. {
    21.    close,               // Close
    22.    open,                // Open
    23.    high,                // High
    24.    low,                 // Low
    25.    median,              // Median
    26.    typical,             // Typical
    27.    weightedClose,       // Weighted Close
    28.    heikenAshiClose,     // Heiken Ashi Close
    29.    heikenAshiOpen,      // Heiken Ashi Open
    30.    heikenAshiHigh,      // Heiken Ashi High   
    31.    heikenAshiLow,       // Heiken Ashi Low
    32.    heikenAshiMedian,    // Heiken Ashi Median
    33.    heikenAshiTypical,   // Heiken Ashi Typical
    34.    heikenAshiWeighted   // Heiken Ashi Weighted Close   
    35. };

    36. #define pi 3.14159265358979323846



    37. //---- input parameters
    38. input ENUM_TIMEFRAMES   TimeFrame            =     0;       // Timeframe
    39. input ENUM_PRICE        Price                =     0;       // Price
    40. input double            CyclePeriod          =    15;       // Cycle Period Ratio
    41. input bool              AdaptiveModeOn       = false;       // Adaptive Mode On/Off
    42. input double            Alpha                =  0.07;       // Cycle Smoothing Factor(eg. 0.07)
    43. input int               MedianPeriod         =     5;       // Period of Moving Median
    44. input int               DCsmooth             =     5;       // Period of Dominant Cycle Smoothing



    45. //---- buffers
    46. double phase[];
    47. double iprice[];

    48. int      timeframe, draw_begin;
    49. string   TF, IndicatorName;
    50. //+------------------------------------------------------------------+
    51. //| Custom indicator initialization function                         |
    52. //+------------------------------------------------------------------+
    53. int init()
    54. {
    55.    timeframe = TimeFrame;
    56.    if(timeframe <= Period()) timeframe = Period();
    57.    TF = tf(timeframe);
    58.    
    59.    IndicatorDigits(5);
    60.    //---- indicators
    61.    IndicatorBuffers(2);
    62.    SetIndexBuffer(0, phase); SetIndexStyle(0,DRAW_LINE);
    63.    SetIndexBuffer(1,iprice);
    64. //----
    65.    IndicatorName = WindowExpertName();
    66.    IndicatorShortName(IndicatorName+"["+TF+"]("+Price+","+DoubleToStr(CyclePeriod,2)+")");
    67.    SetIndexLabel(0,"Phase");
    68.    
    69. //----   
    70.    draw_begin = MathMax(2,Bars - iBars(NULL,timeframe)*timeframe/Period() + 50);

    71.    SetIndexDrawBegin(0,draw_begin);
    72.    
    73.    return(0);
    74. }

    75. int deinit()
    76. {
    77.    return(0);
    78. }
    79. //+------------------------------------------------------------------+
    80. //| Phase Adaptive_v1.0 600+                                         |
    81. //+------------------------------------------------------------------+
    82. int start()
    83. {
    84.    int   shift, limit, counted_bars=IndicatorCounted();
    85.    
    86. //----
    87.    if(counted_bars > 0) limit = Bars - counted_bars - 1;
    88.    if(counted_bars < 0) return(0);
    89.    if(counted_bars < 1)
    90.    {
    91.    limit = Bars - 1;   
    92.       for(int i=limit;i>=0;i--)
    93.       {
    94.       phase[i] = EMPTY_VALUE;
    95.       }
    96.    }   
    97.    
    98.    if(timeframe != Period())
    99.         {
    100.    limit = MathMax(limit,timeframe/Period());   
    101.       
    102.       for(shift = 0;shift < limit;shift++)
    103.       {       
    104.       int y = iBarShift(NULL,timeframe,Time[shift]);
    105.       
    106.       phase[shift] = iCustom(NULL,TimeFrame,IndicatorName,0,Price,CyclePeriod,AdaptiveModeOn,Alpha,MedianPeriod,DCsmooth,0,y);  
    107.       }
    108.        
    109.         return(0);
    110.         }
    111.         else _adaptivePhase(limit);
    112.        
    113.        
    114. //----
    115.    return(0);
    116. }
    117. //+------------------------------------------------------------------+
    118. void _adaptivePhase(int limit)
    119. {   
    120.    double   ;
    121.    
    122.    
    123.    for(int shift=limit;shift>=0;shift--)
    124.    {
    125.       if(Price <= 6) iprice[shift] = iMA(NULL,0,1,0,0,(int)Price,shift);   
    126.       else
    127.       if(Price > 6 && Price <= 13) iprice[shift] = HeikenAshi(0,(int)Price-7,shift);
    128.       
    129.    if(AdaptiveModeOn)
    130.    {
    131.    double cyclelen = cyclePeriod(iprice,Alpha,MedianPeriod,DCsmooth,shift);  
    132.    int domcycle    = MathMax(1,MathFloor(CyclePeriod*cyclelen));
    133.    }
    134.    else domcycle   = MathMax(1,MathFloor(CyclePeriod));
    135.    
    136.    double realPart = 0;
    137.    double imagPart = 0;
    138.    
    139.       for(int i=0;i<domcycle;i++)
    140.       {
    141.          if(domcycle > 0)
    142.          {
    143.          double weight = iprice[shift+i];
    144.          realPart += MathCos(2*pi*i/domcycle)*weight;
    145.          imagPart += MathSin(2*pi*i/domcycle)*weight;
    146.          }
    147.       }
    148.       
    149.    if(MathAbs(realPart) > 0.001) phase[shift] = MathArctan(imagPart/realPart); else phase[shift] = 0.5*pi*Sign(imagPart);
    150.    
    151.    if(realPart < 0) phase[shift] = phase[shift] + pi;
    152.    phase[shift] = phase[shift] + pi/2;   
    153.    
    154.    if(phase[shift] <    0) phase[shift] = phase[shift] + 2*pi;
    155.    if(phase[shift] > 2*pi) phase[shift] = phase[shift] - 2*pi;   
    156.    
    157.    phase[shift] = 180*phase[shift]/pi;
    158.    }
    159. }




    160. double Sign(double value)
    161. {
    162.    if(value > 0) return( 1);
    163.    if(value < 0) return(-1);
    164.   
    165.    return(0);
    166. }        

    167.                

    168. double   DeltaPhase[], aPrice[4], smooth[3], cycle[7], Q1[2], I1[2], iPeriod[2], cPeriod[4];
    169. datetime prevcptime;

    170. double cyclePeriod(double& price[],double alpha,int median,int dcsmooth,int bar)
    171. {      
    172.    double DC, MedianDelta = 0;
    173.    
    174.    
    175.    if(ArraySize(DeltaPhase) != Bars)
    176.    {
    177.    ArraySetAsSeries(DeltaPhase,false);
    178.    ArrayResize(DeltaPhase,Bars);
    179.    ArraySetAsSeries(DeltaPhase,true);
    180.    }   
    181.         
    182.       if(prevcptime != Time[bar])
    183.       {
    184.          for(int i=6;i>=1;i--)
    185.          {
    186.          cycle[i] = cycle[i-1];
    187.             if(i < 4)
    188.             {
    189.             aPrice[i]  = aPrice[i-1];
    190.             cPeriod[i] = cPeriod[i-1];
    191.             }
    192.          if(i < 3) smooth[i] = smooth[i-1];
    193.          }
    194.       I1[1] = I1[0];
    195.       Q1[1] = Q1[0];
    196.       iPeriod[1] = iPeriod[0];
    197.       
    198.       prevcptime = Time[bar];
    199.       }   

    200.    aPrice[0] = price[bar];   
    201.          
    202.    if(bar < Bars - 3) smooth[0] = (aPrice[0] + 2*aPrice[1] + 2*aPrice[2] + aPrice[3])/6;
    203.         cycle[0] = (1 - 0.5*alpha)*(1 - 0.5*alpha)*(smooth[0] - 2*smooth[1] + smooth[2]) + 2*(1-alpha)*cycle[1] - (1-alpha)*(1-alpha)*cycle[2];
    204.         if(bar > Bars - 7) cycle[0] = (aPrice[0] - 2*aPrice[1] + aPrice[2])/4;  
    205.           
    206.           
    207.    Q1[0] = (0.0962*cycle[0] + 0.5769*cycle[2] - 0.5769*cycle[4] - 0.0962*cycle[6])*(0.5 + 0.08*iPeriod[1]);
    208.    I1[0] = cycle[3];  
    209.        
    210.         if(bar < Bars- median)
    211.         {
    212.         if(Q1[0] != 0 && Q1[1] != 0) DeltaPhase[bar] = (I1[0]/Q1[0] - I1[1]/Q1[1])/(1 + I1[0]*I1[1]/(Q1[0]*Q1[1]));               
    213.    if(DeltaPhase[bar] < 0.1) DeltaPhase[bar] = 0.1;  
    214.    if(DeltaPhase[bar] > 1.1) DeltaPhase[bar] = 1.1;
    215.    MedianDelta = MedianOnArray(DeltaPhase,median,bar);
    216.    
    217.       if(MedianDelta == 0) DC = 15; else DC = 6.28318/MedianDelta + 0.5;
    218.        
    219.    iPeriod[0] = 0.33*DC + (1 - 2.0/(dcsmooth + 1))*iPeriod[1];
    220.    cPeriod[0] = 0.15*iPeriod[0] + 0.85*cPeriod[1];
    221.    }
    222.    
    223.    if(cPeriod[3] == 0) return(0);     
    224.    return(cPeriod[0]);
    225. }   


    226. double MedianOnArray(double& price[],int per,int bar)
    227. {
    228.    double median, array[];
    229.    ArrayResize(array,per);
    230.    
    231.    for(int i=0;i<per;i++) array[i] = price[bar+i];
    232.    ArraySort(array,WHOLE_ARRAY,0,MODE_DESCEND);
    233.    
    234.    int num = (int)MathRound((per - 1)*0.5);
    235.    if(MathMod(per,2) > 0) median = array[num]; else median = 0.5*(array[num] + array[num+1]);
    236.    
    237.    return(median);
    238. }



    239. // HeikenAshi Price
    240. double   haClose[2][2], haOpen[2][2], haHigh[2][2], haLow[2][2];
    241. datetime prevhatime[2];

    242. double HeikenAshi(int index,int price,int bar)
    243. {
    244.    if(prevhatime[index] != Time[bar])
    245.    {
    246.    haClose[index][1] = haClose[index][0];
    247.    haOpen [index][1] = haOpen [index][0];
    248.    haHigh [index][1] = haHigh [index][0];
    249.    haLow  [index][1] = haLow  [index][0];
    250.    prevhatime[index] = Time[bar];
    251.    }
    252.    
    253.    if(bar == Bars - 1)
    254.    {
    255.    haClose[index][0] = Close[bar];
    256.    haOpen [index][0] = Open [bar];
    257.    haHigh [index][0] = High [bar];
    258.    haLow  [index][0] = Low  [bar];
    259.    }
    260.    else
    261.    {
    262.    haClose[index][0] = (Open[bar] + High[bar] + Low[bar] + Close[bar])/4;
    263.    haOpen [index][0] = (haOpen[index][1] + haClose[index][1])/2;
    264.    haHigh [index][0] = MathMax(High[bar],MathMax(haOpen[index][0],haClose[index][0]));
    265.    haLow  [index][0] = MathMin(Low [bar],MathMin(haOpen[index][0],haClose[index][0]));
    266.    }
    267.    
    268.    switch(price)
    269.    {
    270.    case  0: return(haClose[index][0]); break;
    271.    case  1: return(haOpen [index][0]); break;
    272.    case  2: return(haHigh [index][0]); break;
    273.    case  3: return(haLow  [index][0]); break;
    274.    case  4: return((haHigh[index][0] + haLow[index][0])/2); break;
    275.    case  5: return((haHigh[index][0] + haLow[index][0] +   haClose[index][0])/3); break;
    276.    case  6: return((haHigh[index][0] + haLow[index][0] + 2*haClose[index][0])/4); break;
    277.    default: return(haClose[index][0]); break;
    278.    }
    279. }     


    280. string tf(int itimeframe)
    281. {
    282.    string result = "";
    283.    
    284.    switch(itimeframe)
    285.    {
    286.    case PERIOD_M1:   result = "M1" ;
    287.    case PERIOD_M5:   result = "M5" ;
    288.    case PERIOD_M15:  result = "M15";
    289.    case PERIOD_M30:  result = "M30";
    290.    case PERIOD_H1:   result = "H1" ;
    291.    case PERIOD_H4:   result = "H4" ;
    292.    case PERIOD_D1:   result = "D1" ;
    293.    case PERIOD_W1:   result = "W1" ;
    294.    case PERIOD_MN1:  result = "MN1";
    295.    default:          result = "N/A";
    296.    }
    297.    
    298.    if(result == "N/A")
    299.    {
    300.    if(itimeframe <  PERIOD_H1 ) result = "M"  + (string)itimeframe;
    301.    if(itimeframe >= PERIOD_H1 ) result = "H"  + (string)(itimeframe/PERIOD_H1);
    302.    if(itimeframe >= PERIOD_D1 ) result = "D"  + (string)(itimeframe/PERIOD_D1);
    303.    if(itimeframe >= PERIOD_W1 ) result = "W"  + (string)(itimeframe/PERIOD_W1);
    304.    if(itimeframe >= PERIOD_MN1) result = "MN" + (string)(itimeframe/PERIOD_MN1);
    305.    }
    306.    
    307.    return(result);
    308. }

    复制代码


    Phase Adaptive_v1.0 600u.mq4
    ""
    还没有人打赏,支持一下
    回复

    举报

     

    回答|共 11 个

    `小玲/^ LV4

    发表于 2018-9-24 10:00:14 | 显示全部楼层

    路过,支持一下啦

    太阳当空照 LV3

    发表于 2020-6-11 22:41:02 | 显示全部楼层

    过来看看的

    街边捡幸福 LV3

    发表于 2020-6-12 11:43:22 | 显示全部楼层

    我是个凑数的。。。

    财汇国际 LV3

    发表于 2020-6-21 22:27:45 | 显示全部楼层

    难得一见的好帖

    空白式想念 LV4

    发表于 2020-6-26 20:19:37 | 显示全部楼层

    小手一抖,积分到手!

    yushhbxh LV3

    发表于 2020-7-25 16:44:18 | 显示全部楼层

    学习了,不错

    赵山河海 LV3

    发表于 2020-11-21 18:28:11 | 显示全部楼层

    谢谢

    严沃底 LV4

    发表于 2021-7-8 10:57:20 | 显示全部楼层

    谢谢

    老鬼兄 LV3

    发表于 2021-7-9 21:26:30 | 显示全部楼层

    支持下
    12下一页
    您需要登录后才可以回帖 登录 | 注册

    提醒: 禁止引战、谩骂、灌水内容

    微信二维码

    有问题联系客服