8评论

8收藏

non lag ma mtf histo alerts arrows

 

avatar 360 | 3065 人阅读 | 8 人评论 | 2018-03-28

nlma histo filter 2.png

  1. //------------------------------------------------------------------
  2. #property copyright "www.forex-station.com"
  3. #property link      "www.forex-station.com"
  4. //------------------------------------------------------------------

  5. #property indicator_separate_window
  6. #property indicator_buffers 2
  7. #property indicator_minimum 0
  8. #property indicator_maximum 1
  9. #property strict

  10. //
  11. //
  12. //
  13. //
  14. //

  15. enum enPrices
  16. {
  17.    pr_close,      // Close
  18.    pr_open,       // Open
  19.    pr_high,       // High
  20.    pr_low,        // Low
  21.    pr_median,     // Median
  22.    pr_typical,    // Typical
  23.    pr_weighted,   // Weighted
  24.    pr_average,    // Average (high+low+open+close)/4
  25.    pr_medianb,    // Average median body (open+close)/2
  26.    pr_tbiased,    // Trend biased price
  27.    pr_tbiased2,   // Trend biased (extreme) price
  28.    pr_haclose,    // Heiken ashi close
  29.    pr_haopen ,    // Heiken ashi open
  30.    pr_hahigh,     // Heiken ashi high
  31.    pr_halow,      // Heiken ashi low
  32.    pr_hamedian,   // Heiken ashi median
  33.    pr_hatypical,  // Heiken ashi typical
  34.    pr_haweighted, // Heiken ashi weighted
  35.    pr_haaverage,  // Heiken ashi average
  36.    pr_hamedianb,  // Heiken ashi median body
  37.    pr_hatbiased,  // Heiken ashi trend biased price
  38.    pr_hatbiased2  // Heiken ashi trend biased (extreme) price
  39. };
  40. enum enFilterWhat
  41. {
  42.    flt_prc,  // Filter the price
  43.    flt_val,  // Filter the non lag ma value
  44.    flt_both  // Filter both
  45. };

  46. extern ENUM_TIMEFRAMES TimeFrame         = PERIOD_CURRENT;    // Time frame to use
  47. extern int             NlmPeriod         = 25;                // Non lag ma period
  48. extern enPrices        NlmPrice          = pr_close;          // Price to use
  49. extern double          Filter            =  0;                // Filter to use (<=0 no filter)
  50. extern int             FilterPeriod      =  0;                // Filter period (<=0 use Non lag ma period)
  51. extern enFilterWhat    FilterOn          = flt_prc;           // Apply filter to:
  52. extern int             Shift             = 0;                 // Shift
  53. extern int             HistoWidth        = 3;                 // Histogram bars width
  54. extern color           UpHistoColor      = clrLimeGreen;      // Up Histogram color
  55. extern color           DnHistoColor      = clrRed;            // Down histogram color
  56. extern bool            alertsOn          = true;              // Turn alerts on?
  57. extern bool            alertsOnCurrent   = false;             // Alerts on current (still opened) bar?
  58. extern bool            alertsMessage     = true;              // Alerts should display a message?
  59. extern bool            alertsSound       = false;             // Alerts should play a sound?
  60. extern bool            alertsEmail       = false;             // Alerts should send an email?
  61. extern bool            alertsNotify      = false;             // Alerts should send notification?
  62. extern bool            arrowsVisible     = false;             // Arrows visible?
  63. extern bool            arrowsOnNewest    = false;             // Arrows drawn on newst bar of higher time frame bar?
  64. extern string          arrowsIdentifier  = "nlm Arrows1";     // Unique ID for arrows
  65. extern double          arrowsUpperGap    = 1.0;               // Upper arrow gap
  66. extern double          arrowsLowerGap    = 1.0;               // Lower arrow gap
  67. extern color           arrowsUpColor     = clrLimeGreen;      // Up arrow color
  68. extern color           arrowsDnColor     = clrOrange;         // Down arrow color
  69. extern int             arrowsUpCode      = 241;               // Up arrow code
  70. extern int             arrowsDnCode      = 242;               // Down arrow code
  71. extern int             arrowsSize        = 2;                 // Arrows size

  72. //
  73. //
  74. //
  75. //
  76. //

  77. double nlmDa[];
  78. double nlmDb[];
  79. double trend[];
  80. double nlm[];
  81. string indicatorFileName;
  82. bool   returnBars;

  83. //------------------------------------------------------------------
  84. //
  85. //------------------------------------------------------------------
  86. //
  87. //
  88. //
  89. //
  90. //

  91. int init()
  92. {
  93.    IndicatorBuffers(4);
  94.    SetIndexBuffer(0,nlmDa);  SetIndexStyle(0, DRAW_HISTOGRAM,EMPTY,HistoWidth,UpHistoColor);
  95.    SetIndexBuffer(1,nlmDb);  SetIndexStyle(1, DRAW_HISTOGRAM,EMPTY,HistoWidth,DnHistoColor);
  96.    SetIndexBuffer(2,nlm);
  97.    SetIndexBuffer(3,trend);

  98.       //
  99.       //
  100.       //
  101.       //
  102.       //
  103.          
  104.           indicatorFileName = WindowExpertName();
  105.           returnBars        = TimeFrame==-99;
  106.           TimeFrame         = fmax(TimeFrame,_Period);
  107.           for (int i=0; i<7; i++) SetIndexShift(i,Shift*TimeFrame/Period());
  108.    
  109.       //
  110.       //
  111.       //
  112.       //
  113.       //
  114.       
  115. return(0);
  116. }
  117. int deinit()
  118. {
  119.    string lookFor       = arrowsIdentifier+":";
  120.    int    lookForLength = StringLen(lookFor);
  121.    for (int i=ObjectsTotal()-1; i>=0; i--)
  122.    {
  123.       string objectName = ObjectName(i);
  124.          if (StringSubstr(objectName,0,lookForLength) == lookFor) ObjectDelete(objectName);
  125.    }
  126. return(0);
  127. }

  128. //------------------------------------------------------------------
  129. //
  130. //------------------------------------------------------------------
  131. //
  132. //
  133. //
  134. //
  135. //

  136. int start()
  137. {
  138.    int i,limit,counted_bars=IndicatorCounted();
  139.       if(counted_bars<0) return(-1);
  140.       if(counted_bars>0) counted_bars--;
  141.          limit = fmin(Bars-counted_bars,Bars-1);
  142.          if (returnBars) { nlmDa[0] = limit+1; return(0); }

  143.    //
  144.    //
  145.    //
  146.    //
  147.    //

  148.    if (TimeFrame == Period())
  149.    {
  150.       for(i=limit; i>=0; i--)
  151.       {  
  152.          int    tperiod = FilterPeriod; if (tperiod<=0)  tperiod=(int)NlmPeriod;
  153.          double pfilter = Filter; if (FilterOn==flt_val) pfilter=0;
  154.          double vfilter = Filter; if (FilterOn==flt_prc) vfilter=0;
  155.          double price   = iFilter(getPrice(NlmPrice,Open,Close,High,Low,i),pfilter,tperiod,i,0);
  156.          nlm[i]   = iFilter(iNoLagMa(price,NlmPeriod,i),vfilter,tperiod,i,1);
  157.          nlmDa[i] = EMPTY_VALUE;
  158.          nlmDb[i] = EMPTY_VALUE;
  159.          trend[i] = (i<Bars-1) ? (nlm[i]>nlm[i+1]) ? 1 : (nlm[i]<nlm[i+1]) ? -1 : trend[i+1] : 0;
  160.          if (trend[i] == 1) nlmDa[i] = 1;
  161.          if (trend[i] ==-1) nlmDb[i] = 1;
  162.          
  163.          //
  164.          //
  165.          //
  166.          //
  167.          //
  168.          
  169.          if (arrowsVisible)
  170.           {
  171.             string lookFor = arrowsIdentifier+":"+(string)Time[i]; ObjectDelete(lookFor);            
  172.             if (i<(Bars-1) && trend[i] != trend[i+1])
  173.             {
  174.                if (trend[i] == 1) drawArrow(i,arrowsUpColor,arrowsUpCode,false);
  175.                if (trend[i] ==-1) drawArrow(i,arrowsDnColor,arrowsDnCode, true);
  176.             }
  177.          }
  178.      }
  179.    manageAlerts();
  180.    return(0);
  181.    }
  182.    
  183.    //
  184.    //
  185.    //
  186.    //
  187.    //
  188.    
  189.    limit = (int)fmax(limit,fmin(Bars-1,iCustom(NULL,TimeFrame,indicatorFileName,-99,0,0)*TimeFrame/Period()));
  190.    for (i=limit; i>=0; i--)
  191.    {
  192.       int y = iBarShift(NULL,TimeFrame,Time[i]);
  193.          nlmDa[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,NlmPeriod,NlmPrice,Filter,FilterPeriod,FilterOn,0,HistoWidth,UpHistoColor,DnHistoColor,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsEmail,alertsNotify,arrowsVisible,arrowsOnNewest,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,arrowsSize,0,y);
  194.          nlmDb[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,NlmPeriod,NlmPrice,Filter,FilterPeriod,FilterOn,0,HistoWidth,UpHistoColor,DnHistoColor,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsEmail,alertsNotify,arrowsVisible,arrowsOnNewest,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,arrowsSize,1,y);         
  195.    }
  196. return(0);      
  197. }

  198. //------------------------------------------------------------------
  199. //                                                                  
  200. //------------------------------------------------------------------
  201. //
  202. //
  203. //
  204. //
  205. //

  206. #define _length  0
  207. #define _len     1
  208. #define _weight  2

  209. #define numOfSeparateCalculations 3
  210. double  nlm_values[3][numOfSeparateCalculations];
  211. double  nlm_prices[ ][numOfSeparateCalculations];
  212. double  nlm_alphas[ ][numOfSeparateCalculations];

  213. double iNoLagMa(double price, int length, int r, int instanceNo=0)
  214. {
  215.    r = Bars-r-1;
  216.    if (ArrayRange(nlm_prices,0) != Bars(Symbol(),0)) ArrayResize(nlm_prices,Bars(Symbol(),0));
  217.                                nlm_prices[r][instanceNo]=price;
  218.    if (length<3 || r<3) return(nlm_prices[r][instanceNo]);
  219.    
  220.    //
  221.    //
  222.    //
  223.    //
  224.    //
  225.    
  226.    if (nlm_values[_length][instanceNo] != length)
  227.    {
  228.       double Cycle = 4.0;
  229.       double Coeff = 3.0*M_PI;
  230.       int    Phase = length-1;
  231.       
  232.          nlm_values[_length][instanceNo] = length;
  233.          nlm_values[_len   ][instanceNo] = length*4 + Phase;  
  234.          nlm_values[_weight][instanceNo] = 0;

  235.          if (ArrayRange(nlm_alphas,0) < nlm_values[_len][instanceNo]) ArrayResize(nlm_alphas,(int)nlm_values[_len][instanceNo]);
  236.          for (int k=0; k<nlm_values[_len][instanceNo]; k++)
  237.          {
  238.             double t;
  239.             if (k<=Phase-1)
  240.                  t = 1.0 * k/(Phase-1);
  241.             else t = 1.0 + (k-Phase+1)*(2.0*Cycle-1.0)/(Cycle*length-1.0);
  242.             double beta = MathCos(M_PI*t);
  243.             double g = 1.0/(Coeff*t+1); if (t <= 0.5 ) g = 1;
  244.       
  245.             nlm_alphas[k][instanceNo]        = g * beta;
  246.             nlm_values[_weight][instanceNo] += nlm_alphas[k][instanceNo];
  247.          }
  248.    }
  249.    
  250.    //
  251.    //
  252.    //
  253.    //
  254.    //
  255.    
  256.    if (nlm_values[_weight][instanceNo]>0)
  257.    {
  258.       double sum = 0;
  259.            for (int k=0; k < nlm_values[_len][instanceNo] && (r-k)>=0; k++) sum += nlm_alphas[k][instanceNo]*nlm_prices[r-k][instanceNo];
  260.            return( sum / nlm_values[_weight][instanceNo]);
  261.    }
  262.    else return(0);           
  263. }

  264. //------------------------------------------------------------------
  265. //                                                                  
  266. //------------------------------------------------------------------
  267. //
  268. //
  269. //
  270. //
  271. //

  272. #define filterInstances 2
  273. double workFil[][filterInstances*3];

  274. #define _fchange 0
  275. #define _fachang 1
  276. #define _fprice  2

  277. double iFilter(double tprice, double filter, int period, int i, int instanceNo=0)
  278. {
  279.    if (filter<=0 || period<=0) return(tprice);
  280.    if (ArrayRange(workFil,0)!= Bars) ArrayResize(workFil,Bars); i = Bars-i-1; instanceNo*=3;
  281.    
  282.    //
  283.    //
  284.    //
  285.    //
  286.    //
  287.    
  288.    workFil[i][instanceNo+_fprice]  = tprice; if (i<1) return(tprice);
  289.    workFil[i][instanceNo+_fchange] = fabs(workFil[i][instanceNo+_fprice]-workFil[i-1][instanceNo+_fprice]);
  290.    workFil[i][instanceNo+_fachang] = workFil[i][instanceNo+_fchange];

  291.    for (int k=1; k<period && (i-k)>=0; k++) workFil[i][instanceNo+_fachang] += workFil[i-k][instanceNo+_fchange];
  292.                                             workFil[i][instanceNo+_fachang] /= period;
  293.    
  294.    double stddev = 0; for (int k=0;  k<period && (i-k)>=0; k++) stddev += MathPow(workFil[i-k][instanceNo+_fchange]-workFil[i-k][instanceNo+_fachang],2);
  295.           stddev = MathSqrt(stddev/(double)period);
  296.    double filtev = filter * stddev;
  297.    if( MathAbs(workFil[i][instanceNo+_fprice]-workFil[i-1][instanceNo+_fprice]) < filtev ) workFil[i][instanceNo+_fprice]=workFil[i-1][instanceNo+_fprice];
  298.    return(workFil[i][instanceNo+_fprice]);
  299. }

  300. //------------------------------------------------------------------
  301. //
  302. //------------------------------------------------------------------
  303. //
  304. //
  305. //
  306. //
  307. //
  308. //

  309. #define priceInstances 1
  310. double workHa[][priceInstances*4];
  311. double getPrice(int tprice, const double& open[], const double& close[], const double& high[], const double& low[], int i, int instanceNo=0)
  312. {
  313.   if (tprice>=pr_haclose)
  314.    {
  315.       if (ArrayRange(workHa,0)!= Bars) ArrayResize(workHa,Bars); instanceNo*=4;
  316.          int r = Bars-i-1;
  317.          
  318.          //
  319.          //
  320.          //
  321.          //
  322.          //
  323.          
  324.          double haOpen;
  325.          if (r>0)
  326.                 haOpen  = (workHa[r-1][instanceNo+2] + workHa[r-1][instanceNo+3])/2.0;
  327.          else   haOpen  = (open[i]+close[i])/2;
  328.          double haClose = (open[i] + high[i] + low[i] + close[i]) / 4.0;
  329.          double haHigh  = fmax(high[i],fmax(haOpen,haClose));
  330.          double haLow   = fmin(low[i] ,fmin(haOpen,haClose));

  331.          if(haOpen  <haClose) { workHa[r][instanceNo+0] = haLow;  workHa[r][instanceNo+1] = haHigh; }
  332.          else                 { workHa[r][instanceNo+0] = haHigh; workHa[r][instanceNo+1] = haLow;  }
  333.                                 workHa[r][instanceNo+2] = haOpen;
  334.                                 workHa[r][instanceNo+3] = haClose;
  335.          //
  336.          //
  337.          //
  338.          //
  339.          //
  340.          
  341.          switch (tprice)
  342.          {
  343.             case pr_haclose:     return(haClose);
  344.             case pr_haopen:      return(haOpen);
  345.             case pr_hahigh:      return(haHigh);
  346.             case pr_halow:       return(haLow);
  347.             case pr_hamedian:    return((haHigh+haLow)/2.0);
  348.             case pr_hamedianb:   return((haOpen+haClose)/2.0);
  349.             case pr_hatypical:   return((haHigh+haLow+haClose)/3.0);
  350.             case pr_haweighted:  return((haHigh+haLow+haClose+haClose)/4.0);
  351.             case pr_haaverage:   return((haHigh+haLow+haClose+haOpen)/4.0);
  352.             case pr_hatbiased:
  353.                if (haClose>haOpen)
  354.                      return((haHigh+haClose)/2.0);
  355.                else  return((haLow+haClose)/2.0);        
  356.             case pr_hatbiased2:
  357.                if (haClose>haOpen)  return(haHigh);
  358.                if (haClose<haOpen)  return(haLow);
  359.                                     return(haClose);        
  360.          }
  361.    }
  362.    
  363.    //
  364.    //
  365.    //
  366.    //
  367.    //
  368.    
  369.    switch (tprice)
  370.    {
  371.       case pr_close:     return(close[i]);
  372.       case pr_open:      return(open[i]);
  373.       case pr_high:      return(high[i]);
  374.       case pr_low:       return(low[i]);
  375.       case pr_median:    return((high[i]+low[i])/2.0);
  376.       case pr_medianb:   return((open[i]+close[i])/2.0);
  377.       case pr_typical:   return((high[i]+low[i]+close[i])/3.0);
  378.       case pr_weighted:  return((high[i]+low[i]+close[i]+close[i])/4.0);
  379.       case pr_average:   return((high[i]+low[i]+close[i]+open[i])/4.0);
  380.       case pr_tbiased:   
  381.                if (close[i]>open[i])
  382.                      return((high[i]+close[i])/2.0);
  383.                else  return((low[i]+close[i])/2.0);        
  384.       case pr_tbiased2:   
  385.                if (close[i]>open[i]) return(high[i]);
  386.                if (close[i]<open[i]) return(low[i]);
  387.                                      return(close[i]);        
  388.    }
  389.    return(0);
  390. }   

  391. //-------------------------------------------------------------------
  392. //
  393. //-------------------------------------------------------------------
  394. //
  395. //
  396. //
  397. //
  398. //

  399. string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
  400. int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

  401. string timeFrameToString(int tf)
  402. {
  403.    for (int i=ArraySize(iTfTable)-1; i>=0; i--)
  404.          if (tf==iTfTable[i]) return(sTfTable[i]);
  405.                               return("");
  406. }

  407. //-------------------------------------------------------------------
  408. //                                                                  
  409. //-------------------------------------------------------------------
  410. //
  411. //
  412. //
  413. //
  414. //

  415. void manageAlerts()
  416. {
  417.    if (alertsOn)
  418.    {
  419.       int whichBar = 1; if (alertsOnCurrent) whichBar = 0;
  420.       if (trend[whichBar] != trend[whichBar+1])
  421.       {
  422.          if (trend[whichBar] ==  1) doAlert(whichBar,"up");
  423.          if (trend[whichBar] == -1) doAlert(whichBar,"down");
  424.       }
  425.    }
  426. }

  427. //
  428. //
  429. //
  430. //
  431. //

  432. void doAlert(int forBar, string doWhat)
  433. {
  434.    static string   previousAlert="nothing";
  435.    static datetime previousTime;
  436.    string message;
  437.    
  438.    if (previousAlert != doWhat || previousTime != Time[forBar]) {
  439.        previousAlert  = doWhat;
  440.        previousTime   = Time[forBar];

  441.        //
  442.        //
  443.        //
  444.        //
  445.        //

  446.        message =  StringConcatenate(Symbol()," ",timeFrameToString(_Period)," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," NonLag MA slope changed to ",doWhat);
  447.           if (alertsMessage) Alert(message);
  448.           if (alertsSound)   PlaySound("alert2.wav");
  449.           if (alertsEmail)   SendMail(StringConcatenate(Symbol(),"NonLag MA"),message);
  450.           if (alertsNotify)  SendNotification(message);
  451.    }
  452. }

  453. //-------------------------------------------------------------------
  454. //                                                                  
  455. //-------------------------------------------------------------------
  456. //
  457. //
  458. //
  459. //
  460. //

  461. void drawArrow(int i,color theColor,int theCode,bool up)
  462. {
  463.    string name = arrowsIdentifier+":"+(string)Time[i];
  464.    double gap  = iATR(NULL,0,20,i);   
  465.    
  466.       //
  467.       //
  468.       //
  469.       //
  470.       //

  471.       datetime time = Time[i]; if (arrowsOnNewest) time += _Period*60-1;      
  472.       ObjectCreate(name,OBJ_ARROW,0,time,0);
  473.          ObjectSet(name,OBJPROP_ARROWCODE,theCode);
  474.          ObjectSet(name,OBJPROP_WIDTH,arrowsSize);
  475.          ObjectSet(name,OBJPROP_COLOR,theColor);
  476.          if (up)
  477.                ObjectSet(name,OBJPROP_PRICE1,High[i] + arrowsUpperGap * gap);
  478.          else  ObjectSet(name,OBJPROP_PRICE1,Low[i]  - arrowsLowerGap * gap);
  479. }
复制代码


non lag ma mtf histo alerts arrows.mq4
""
还没有人打赏,支持一下

评论|共 8 个

yaotou133

发表于 2018-5-12 22:05:32 | 显示全部楼层

1111111111111111

XAPxap520

发表于 2018-6-3 17:09:13 | 显示全部楼层

22222222222222222222222

至今还没对象

发表于 2018-6-11 18:07:44 | 显示全部楼层

谢谢分享谢谢分享谢谢分享谢谢分享谢谢分享谢谢分享谢谢分享谢谢分享谢谢分享

axhjz

发表于 2018-6-21 15:44:10 | 显示全部楼层

吃饭都是高峰的高峰的海景房卡和见了客户来客家罗湖绿林豪客

刘刚

发表于 2019-6-16 09:38:32 | 显示全部楼层

享谢谢分享谢谢分享谢谢

QQ1397379127

发表于 2020-6-4 15:35:07 | 显示全部楼层

前排支持下

ttofx

发表于 2020-7-23 21:38:36 | 显示全部楼层

谢谢楼主分享

美嘉晨风

发表于 2021-8-3 20:04:12 | 显示全部楼层

顶下

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

EA之家评论守则