【MT5】自定网格工具

 
//+----------------------------------------+
//|                           网格.mq5     |
//+----------------------------------------+
#property copyright  "QQ: 195484688"
#property strict
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1

input  int      横线取价天数 =  5;
input  int          整距基数 = 50;                             // .   整距基数
input  color           颜色1 = clrDimGray;                     // .      颜色1
input  color           颜色2 = clrDarkGreen;                   // .      颜色2
input  string           分隔 = "===========================";  //===========================
input  int      竖线绘画天数 = 1;
input  int          钟点间隔 = 240;                            // .   钟点间隔
input  color        竖线颜色 = clrDimGray;                     // .   竖线颜色
input  int          彩线钟点 = 12;                             // .   彩线钟点
input  color        彩线颜色 = clrDarkGreen;                   // .   彩线颜色

string  ObjPrefix = "Wg_-_";
double  Jianju;
//=============================================================================================================================
void DeLline()
  {
   for(int i=ObjectsTotal(0)-1;i>=0;i--)
     {
      string n=ObjectName(0,i);
      if(StringFind(n, ObjPrefix)>-1)  ObjectDelete(0,n);
     }
  }
//=============================================================================================================================
int OnInit()
  {
   switch(_Period)
     {
      case PERIOD_M1  :  Jianju= 1* 整距基数 * _Point;  break;
      case PERIOD_M5  :  Jianju= 3* 整距基数 * _Point;  break;
      case PERIOD_M15 :  Jianju= 5* 整距基数 * _Point;  break;
      case PERIOD_M30 :  Jianju= 7* 整距基数 * _Point;  break;
      case PERIOD_H1  :  Jianju=10* 整距基数 * _Point;  break;
      default         :  Jianju= 0;
     }
   DeLline();
   return(INIT_SUCCEEDED);
  }
//=============================================================================================================================
void OnDeinit(const int reason)
  {
   DeLline();
  }
//=============================================================================================================================
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[]) 
  {
   if(Period()<PERIOD_M30)
     {
      double    HighN, LowN, S1,  S2;
      int       Sshu,  days, Ti,  S3,  S4=100;
      string    name;
      datetime  TimeN;

      if(横线取价天数>0 && Jianju>0)
        {
         HighN=iHigh(NULL, PERIOD_D1, 0);
         LowN = iLow(NULL, PERIOD_D1, 0);

         for(int i=1; i<横线取价天数; i++) 
           {
            HighN=MathMax(HighN, iHigh(NULL,PERIOD_D1, i));
            LowN =MathMin(LowN,  iLow(NULL, PERIOD_D1, i));
           }
         S1=HighN+Jianju-MathMod(HighN,Jianju);
         S2=LowN-MathMod(LowN,Jianju);

         for(double r=S2; r<=S1; r += Jianju)
           {
            name=ObjPrefix+DoubleToString(r, _Digits);
            DrawHVline(name,OBJ_HLINE,0, r,STYLE_DOT);
            
            S3=(int)MathRound(r / _Point);
            if(S3 % S4==0)    ObjectSetInteger(0,name,OBJPROP_COLOR,颜色2);
            else              ObjectSetInteger(0,name,OBJPROP_COLOR,颜色1);
           }
        }

      if(钟点间隔>0)
        {
         MqlDateTime  str0, str1, str2;
         TimeN=iTime(_Symbol,0,0);
         Sshu=60 * 钟点间隔;
         TimeN=TimeN-TimeN % Sshu;

         TimeToStruct(TimeN,str0);
         days=str0.day;

         Ti=竖线绘画天数-1;
         while(Ti>=0)
           {
            name=ObjPrefix+IntegerToString(TimeN);
            DrawHVline(name,OBJ_VLINE,TimeN,0,STYLE_DASHDOTDOT);

            TimeToStruct(TimeN,str1);
            if(str1.hour==彩线钟点)     ObjectSetInteger(0,name,OBJPROP_COLOR,彩线颜色);
            else                        ObjectSetInteger(0,name,OBJPROP_COLOR,竖线颜色);

            TimeN -=Sshu;
            TimeToStruct(TimeN,str2);
            if(str2.day != days)      { Ti--;  days=str2.day; }
           }
        }
     }
   return(rates_total);
  }
//============================================================================================================================
bool DrawHVline(string n,ENUM_OBJECT type,datetime x,double y,ENUM_LINE_STYLE s)
  {
   ObjectCreate(0, n, type, 0, x, y); 
   ObjectSetInteger(0,n,OBJPROP_STYLE,s);
   ObjectSetInteger(0,n,OBJPROP_WIDTH,0);
   ObjectSetInteger(0,n,OBJPROP_BACK,true);
   ObjectSetInteger(0,n,OBJPROP_HIDDEN,true);
   ObjectSetInteger(0,n,OBJPROP_SELECTABLE,false);
   ObjectSetInteger(0,n,OBJPROP_SELECTED,false);
   ObjectSetInteger(0,n,OBJPROP_ZORDER,0);
   return(true);
  }
//=============================================================================================================================