黄金
3shadeopen
回复:0  浏览:500
  • 楼主admin 圈主
  • 2019-05-05 05:06
EURUSDM30.png

  1. //+------------------------------------------------------------------+
  2. //|                                                      3shadeopen.mq4 |
  3. //|                                         Copyright © 2006, sx ted |
  4. //| Purpose: shade New York or other sessions for chart time frames  |
  5. //|          M1 to H4 (at a push).                                   |
  6. //| version: 2 - enhanced for speed but with MT4 beeing so fast no   |
  7. //|              difference will be noticed, all the sessions are    |
  8. //|              shaded in the init(), last session if it is current |
  9. //|              is widened in the start() in lieu of repainting all.|
  10. //+------------------------------------------------------------------+
  11. #property copyright "Copyright © 2006, sx ted"
  12. #property link      ""

  13. #property indicator_chart_window

  14. //---- input parameters
  15. extern color     ShadeColor=Gold;







  16. // if in New York
  17. #define NY_OPEN_HH   06 // NY session open hour
  18. #define NY_OPEN_MM   30 // NY session open minutes
  19. #define NY_CLOSE_HH  13 // NY session close hour
  20. #define NY_CLOSE_MM  05 // NY session close minutes


  21. #define MAX_DAYS_TO_SHADE  5 // maximum number of days back from last chart date to be shaded

  22. //---- global variables to program
  23. string obj[]; //array of object names
  24. int    iPrevious=0, iStart=-1, iEnd;
  25. double dLow, dHigh;

  26. //+------------------------------------------------------------------+
  27. //| Custom indicator initialization function                         |
  28. //+------------------------------------------------------------------+
  29. int init()
  30.   {
  31.    if(Period()>PERIOD_H4) return(0); // no shading required
  32.    int iMaxBarsonChart=iBars(NULL,0), i, iBarDay, iBarTime;
  33.    // find approximate start of first day to shade
  34.    int iBarsToDo=MathMin((MAX_DAYS_TO_SHADE*PERIOD_D1)/Period(),iMaxBarsOnChart);
  35.    // find start of first day to shade
  36.    for(i=iBarsToDo; i<iMaxBarsOnChart; i++)
  37.      {
  38.       iBarDay=TimeYear(Time[i])*PERIOD_MN1*12+TimeMonth(Time[i])*PERIOD_MN1+TimeDay(Time[i])*PERIOD_D1;
  39.       iBarTime=iBarDay+TimeHour(Time[i])*60+TimeMinute(Time[i]);
  40.       if(iBarTime>=iBarDay+NY_OPEN_HH*60+NY_OPEN_MM && iBarTime<=iBarDay+NY_CLOSE_HH*60+NY_CLOSE_MM) iStart=i;
  41.       else if(iStart>-1) break;
  42.      }
  43.    if(iStart>-1) iBarsToDo=iStart;
  44.    iStart=-1;
  45.    // shade previous sessions and current session if started
  46.    for(i=iBarsToDo; i>=0; i--)
  47.      {
  48.       iBarDay=TimeYear(Time[i])*PERIOD_MN1*12+TimeMonth(Time[i])*PERIOD_MN1+TimeDay(Time[i])*PERIOD_D1;
  49.       iBarTime=iBarDay+TimeHour(Time[i])*60+TimeMinute(Time[i]);
  50.       if(iBarTime>=iBarDay+NY_OPEN_HH*60+NY_OPEN_MM && iBarTime<=iBarDay+NY_CLOSE_HH*60+NY_CLOSE_MM)
  51.         {
  52.          if(iBarDay==iPrevious)   // current NY session
  53.            {
  54.             dLow =MathMin(dLow,  Low[i]);
  55.             dHigh=MathMax(dHigh, High[i]);
  56.            }
  57.          else                     // new NY session
  58.            {
  59.             dLow=Low[i];
  60.             dHigh=High[i];
  61.             iStart=i;
  62.             iPrevious=iBarDay;
  63.            }      
  64.          iEnd=i;
  65.         }
  66.       else if(iStart>-1)
  67.         {
  68.          PaintRectangle();
  69.          iStart=-1;
  70.         }  
  71.      }
  72.    if(iStart>-1) PaintRectangle(); // paint the last one if session not closed
  73.    return(0);
  74.   }
  75. //+------------------------------------------------------------------+
  76. //| Custom indicator deinitialization function                       |
  77. //+------------------------------------------------------------------+
  78. int deinit()
  79.   {
  80.    int iaCount=ArraySize(obj);
  81.    for(int i=0; i<iaCount; i++)
  82.      {
  83.       if(ObjectFind(obj[i])>-1) ObjectDelete(obj[i]);
  84.      }
  85.    return(0);
  86.   }
  87. //+------------------------------------------------------------------+
  88. //| Custom indicator iteration function                              |
  89. //+------------------------------------------------------------------+
  90. int start()
  91.   {
  92.    int i=0, iBarDay, iBarTime;
  93.    iBarDay=TimeYear(Time[i])*PERIOD_MN1*12+TimeMonth(Time[i])*PERIOD_MN1+TimeDay(Time[i])*PERIOD_D1;
  94.    iBarTime=iBarDay+TimeHour(Time[i])*60+TimeMinute(Time[i]);
  95.    if(iBarTime>=iBarDay+NY_OPEN_HH*60+NY_OPEN_MM && iBarTime<=iBarDay+NY_CLOSE_HH*60+NY_CLOSE_MM)
  96.      {
  97.       if(iBarDay==iPrevious)   // current NY session
  98.         {
  99.          dLow =MathMin(dLow,  Low[i]);
  100.          dHigh=MathMax(dHigh, High[i]);
  101.         }
  102.       else                     // new NY session
  103.         {
  104.          dLow=Low[i];
  105.          dHigh=High[i];
  106.          iStart=i;
  107.          iPrevious=iBarDay;
  108.         }      
  109.       iEnd=i;
  110.       PaintRectangle();
  111.      }
  112.    return(0);
  113.   }
  114. //+------------------------------------------------------------------+
  115. //| Paint rectangle                                                  |
  116. //+------------------------------------------------------------------+
  117. void PaintRectangle()
  118.   {
  119.    string sObj="ShadeNY_"+DoubleToStr(iPrevious, 0); // name for the object
  120.    if(ObjectFind(sObj)>-1)
  121.      {
  122.       // current session object found, so just widen it
  123.       ObjectSet(sObj,OBJPROP_PRICE1,dLow-Point);
  124.       ObjectSet(sObj,OBJPROP_TIME2 ,Time[iEnd]);
  125.       ObjectSet(sObj,OBJPROP_PRICE2,dHigh+Point);
  126.      }
  127.    else
  128.      {
  129.       // otherwise create new object for the session
  130.       int iaCount=ArraySize(obj);
  131.       ArrayResize(obj, iaCount+1);
  132.       obj[iaCount]=sObj;
  133.       ObjectCreate(sObj,OBJ_RECTANGLE,0,Time[iStart],dLow-Point,Time[iEnd],dHigh+Point);
  134.       ObjectSet(sObj,OBJPROP_COLOR,ShadeColor);
  135.      }
  136.   }     
  137. //+------------------------------------------------------------------+