Navigation:Home > Content >

ScanWave_Indicator_v.0.2.mq4

Time: 2010-01-28 | Download file:ScanWave_Indicator_v.0.2.mq4

//+------------------------------------------------------------------+
//|                                          ScanWave Indicator.mq4  |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Romario"
#property link      ""
#property indicator_chart_window
#import "user32.dll"
   int GetClientRect(int hWnd, int lpRect[]);
#import

int GetClientHeight(int i = 3)
{
   int rect[4];
   int hWnd = WindowHandle(Symbol(), 0);
   GetClientRect(hWnd, rect);
   return (rect[i]);
}
extern int Lev = -1;
extern int Offset = 0;
extern bool ShowSelectWave = False;
extern bool SaveAnalysisOnExit = False;
extern int RaiseLevelOn = 0;
extern bool ShowChannel = False;
int ArrIndex[20000];
bool ArrBool[20000];
int ArrCount[20000];
int ArrLevel[20000];
datetime LastDate;
double PriceInPixel;
int a, n, shift = 5;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   a = 0;
   n = 0;
   int i = 0;
   string CurrName;
   while(i < ObjectsTotal())
   {
      CurrName = ObjectName(i);
      if((ObjectType(CurrName) == OBJ_TEXT || ObjectType(CurrName) == OBJ_TREND) && 
         (StringFind(CurrName, "ObjText") > -1 || StringFind(CurrName, "ObjChannel") > -1 || StringFind(CurrName, "ObjTrend") > -1))
         ObjectDelete(CurrName);
      else
         i++;
   }
   int MinSize = 6;
   double PriceMax = WindowPriceMax();
   double PriceMin = WindowPriceMin();
   int WindowHeight = GetClientHeight();
   PriceInPixel = (PriceMax - PriceMin) / WindowHeight;
   int Handle = FileOpen("ResultAnalysis" + Symbol() + Period() + ".csv", FILE_CSV | FILE_READ, ' ');
   if(Handle < 1)
   {
      Print("Файл " + "ResultAnalysis" + Symbol() + Period() + ".csv" + " не обнаружен, последняя ошибка ", GetLastError());
      return(false);
   }
   else
   {
      bool b;
      FileReadString(Handle);
      LastDate = StrToTime(FileReadString(Handle) + " " + FileReadString(Handle));
      while(!FileIsEnding(Handle))
      { 
         int Level = FileReadNumber(Handle) + RaiseLevelOn;  // волновой уровень
         int NumWave = FileReadNumber(Handle);
         string Wave = FileReadString(Handle);
         while(!FileIsLineEnding(Handle))
         {  
            string s = FileReadString(Handle);
            if(s == "Select")
               continue;
            if(s == "Targets")
            {
               while(!FileIsLineEnding(Handle))
               {
                  FileReadNumber(Handle);
               }
               continue;
            }
            double PriceValue = FileReadNumber(Handle);
            datetime DateTime = StrToTime(FileReadString(Handle) + " " + FileReadString(Handle));
            int PriceIndex = iBarShift(NULL, 0, DateTime); 
            if(s == "0") continue;
            if(Low[PriceIndex] < PriceValue)
               b = true;
            else
               b = false;
            if(PriceValue > 0)
               Add(PriceIndex, b, Level);
         }
      }
      FileSeek(Handle, 0, SEEK_SET);
      int j = 0;
      int k = 0;
      double vPr[6];
      int iPr[6];
      int Cnt = 0;
      color Color;
      int TextHeightInPixels[] = {13, 15, 18, 22, 23, 26, 30, 32, 36}; // 0 -- Size = 6, 1 -- Size = 8 , 2 -- Size = 10 и т.д.
      double vWave[6];
      int iWave[6];
      int vi;
      FileReadString(Handle);
      LastDate = StrToTime(FileReadString(Handle) + " " + FileReadString(Handle));
      while(!FileIsEnding(Handle))
      { 
         Level = FileReadNumber(Handle) + RaiseLevelOn;  // волновой уровень
         int Size = MinSize + (Level / 3) * 2;           // размер шрифта в соответствии с волновым уровнем
         NumWave = FileReadNumber(Handle);
         Wave = FileReadString(Handle);
         i = 0;
         ArrayInitialize(vWave, 0);
         ArrayInitialize(iWave, 0);
         vi = 0;
         bool Select = false;
         bool NZ = false;
         while(!FileIsLineEnding(Handle))
         {  
            s = FileReadString(Handle);
            if(s == "0") NZ = true;
            if(s == "Select")
            {
               Select = true;
               continue;
            }
            if(s == "Targets")
            {
               int T = 0;
               double Fibo[10];
               while(!FileIsLineEnding(Handle))
               {
                  Fibo[T] = FileReadNumber(Handle);
                  T++;
               }
               continue;
            }
            if((Level / 3) % 2 == 0)
            {
               if(s == "1")      s = "i";
               else if(s == "2") s = "ii";
               else if(s == "3") s = "iii";
               else if(s == "4") s = "iv";
               else if(s == "5") s = "v";
               else if(s == "A") s = "a";
               else if(s == "B") s = "b";
               else if(s == "C") s = "c";
               else if(s == "D") s = "d";
               else if(s == "E") s = "e";
               else if(s == "W") s = "w";
               else if(s == "X") s = "x";
               else if(s == "Y") s = "y";
               else if(s == "Z") s = "z";
            }
            if(Level % 3 == 2)
            {
               Color = Green;
               s = "[" + s + "]";
            }
            if(Level % 3 == 1)
            {
               Color = Blue;
               s = "(" + s + ")";
            }
            if(Level % 3 == 0)
               Color = Red;
            PriceValue = FileReadNumber(Handle);
            DateTime = StrToTime(FileReadString(Handle) + " " + FileReadString(Handle));
            PriceIndex = iBarShift(NULL, 0, DateTime);
            vWave[vi] = PriceValue;
            iWave[vi] = PriceIndex;
            vi++;
            if(PriceValue > 0)
            {
               vPr[i] = PriceValue;
               iPr[i] = PriceIndex;
               i++;
            }
            if(s == "0" || s == "(0)" || s == "[0]" ) continue;
            if(Low[PriceIndex] < PriceValue)
               b = true;
            else
               b = false;
            int IndexInArr = Search(PriceIndex, b);   
            if(b == true)
            {
               int min = ArrLevel[IndexInArr];
               for (k = 0; k <= Level - ArrLevel[IndexInArr];  k++)
               {
                  PriceValue = PriceValue + TextHeightInPixels[min / 3] * PriceInPixel;
                  min = min + 1;
               } 
               PriceValue = PriceValue + Offset * PriceInPixel;
            }
            else
            {
               min = ArrLevel[IndexInArr];
               for (k = 1; k <= Level - ArrLevel[IndexInArr];  k++)
               {
                 PriceValue = PriceValue - TextHeightInPixels[k / 3] * PriceInPixel;
               }
               PriceValue = PriceValue - Offset * PriceInPixel;
            }
            ObjectCreate("ObjText" + j, OBJ_TEXT, 0, Time[PriceIndex], PriceValue);
            ObjectSetText("ObjText" + j, s, Size, "Times New Roman", Color);
            j++;
         }
         if(ShowChannel == true && NZ == true && ((NumWave == 5 && vi < 6) || (NumWave == 3 && vi < 4)))
         {
            for(k = vi - 1; k >= 0; k--)
            {
               if(vWave[k] > 0 && k >= 2)
               {
                  if(vWave[k - 1] > 0 && vWave[k - 2] > 0)
                  {
                     double Point1 = vWave[k - 2];
                     datetime Time1 = iWave[k - 2];
                     double Point2 = vWave[k];
                     datetime Time2 = iWave[k];
                     double Point4 = vWave[k - 1];
                     datetime Time4 = iWave[k - 1];
                     double Point3;
                     datetime Time3 = Time1;
                     double kk = (Point2 - Point1) / (Time2 - Time1);
                     double bb = - kk * Time1 + Point1;
                     Point3 = Point1 + (Point4 - (kk * Time4 + bb));
                     ObjectCreate("ObjChannel" + Cnt, OBJ_TREND, 0, Time[Time1], Point1, Time[Time2], Point2);
                     ObjectSet("ObjChannel" + Cnt, OBJPROP_COLOR, Color);
                     //ObjectSet("ObjChannel" + Cnt, OBJPROP_WIDTH, Level / 3 + 1);
                     Cnt++;
                     ObjectCreate("ObjChannel" + Cnt, OBJ_TREND, 0,Time[Time3], Point3, Time[Time4], Point4);
                     ObjectSet("ObjChannel" + Cnt, OBJPROP_COLOR, Color);
                     //ObjectSet("ObjChannel" + Cnt, OBJPROP_WIDTH, Level / 3 + 1);
                     Cnt++;
                     break;
                  }
               }
            }
         }
         if(Level == Lev || (Select == true && ShowSelectWave == True))
         {
            Select = false;
            for(k = 0; k < i - 1; k++)
            {
               ObjectCreate("ObjTrend" + Cnt, OBJ_TREND, 0, Time[iPr[k]], vPr[k],  Time[iPr[k + 1]], vPr[k + 1]);
               ObjectSet("ObjTrend" + Cnt, OBJPROP_COLOR, Color);
               ObjectSet("ObjTrend" + Cnt, OBJPROP_WIDTH, 2);
               ObjectSet("ObjTrend" + Cnt, OBJPROP_RAY, false);
               Cnt++;
            }
         }
         if(s == "Targets")
         {
            s = '';
            datetime time = Time[0] + Period() * 60 * shift;
            shift += 5;
            ObjectCreate("ObjTrend" + Cnt, OBJ_TREND, 0, time, Fibo[0],  time, Fibo[1]);
            ObjectSet("ObjTrend" + Cnt, OBJPROP_COLOR, Color);
            ObjectSet("ObjTrend" + Cnt, OBJPROP_RAY, false);
            Cnt++;
            for(i = 2; i < T; i++)
            {
               ObjectCreate("ObjTrend" + Cnt, OBJ_TREND, 0, time - Period() * 60 * 2, Fibo[i], time + Period() * 60 * 2, Fibo[i]);
               ObjectSet("ObjTrend" + Cnt, OBJPROP_COLOR, Color);
               //ObjectSet("ObjTrend" + Cnt, OBJPROP_WIDTH, Level);
               ObjectSet("ObjTrend" + Cnt, OBJPROP_RAY, false);
               Cnt++;
            }
         }
      }
   }
   FileClose(Handle);
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   int i = 0;
   string CurrName;
   if(SaveAnalysisOnExit == False)
   {
      while(i < ObjectsTotal())
      {
         CurrName = ObjectName(i);
         if((ObjectType(CurrName) == OBJ_TEXT || ObjectType(CurrName) == OBJ_TREND) && 
            (StringFind(CurrName, "ObjText") > -1 || StringFind(CurrName, "ObjChannel") > -1 || StringFind(CurrName, "ObjTrend") > -1))
            ObjectDelete(CurrName);
         else
            i++;
      }
   }
   else
   {
      while(i < ObjectsTotal())
      {
         CurrName = ObjectName(i);
         if(ObjectType(CurrName) == OBJ_TEXT && StringFind(CurrName, "ObjText") > -1)
         {
            datetime ObjTime = ObjectGet(CurrName, OBJPROP_TIME1);
            double ObjPrice = ObjectGet(CurrName, OBJPROP_PRICE1);
            string Name = "Save" + StringSubstr(CurrName, 3);
            ObjectCreate(Name, OBJ_TEXT, 0, ObjTime, ObjPrice);
            string ObjText = ObjectDescription(CurrName);
            int TextSize = ObjectGet(CurrName, OBJPROP_FONTSIZE);
            color TextColor = ObjectGet(CurrName, OBJPROP_COLOR);
            ObjectSetText(Name, ObjText, TextSize, "Times New Roman", TextColor);
            ObjectDelete(CurrName);
         }
         else
            i++;
      }
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{ 
   int Handle = FileOpen("ResultAnalysis" + Symbol() + Period() + ".csv", FILE_CSV | FILE_READ, ' ');
   if(Handle < 1)
   {
      Print("Файл " + "ResultAnalysis" + Symbol() + Period() + ".csv" + " не обнаружен, последняя ошибка ", GetLastError());
      return(false);
   }
   else
   {
      FileReadString(Handle);
      if(StrToTime(FileReadString(Handle) + " " + FileReadString(Handle)) != LastDate)
         init();
      else
      {
         double PriceMax = WindowPriceMax();
         double PriceMin = WindowPriceMin();
         int WindowHeight = GetClientHeight();
         double CurrPriceInPixel = (PriceMax - PriceMin) / WindowHeight;
         int i = 0;
         string CurrName;
         while(i < ObjectsTotal())
         {
            CurrName = ObjectName(i);
            if(ObjectType(CurrName) == OBJ_TEXT && StringFind(CurrName, "ObjText") > -1)
            {
               
               datetime PriceTime = ObjectGet(CurrName, OBJPROP_TIME1);
               int PriceIndex = iBarShift(NULL, 0, PriceTime);
               double PriceValue = ObjectGet(CurrName, OBJPROP_PRICE1);
               double OffsetInPixel; 
               if(Low[PriceIndex] >= PriceValue)
               {
                  OffsetInPixel = (Low[PriceIndex] - PriceValue) / PriceInPixel;
                  ObjectSet(CurrName, OBJPROP_PRICE1, Low[PriceIndex] - OffsetInPixel * CurrPriceInPixel);
               }
               else if(High[PriceIndex] <= PriceValue)
               {
                  OffsetInPixel = (PriceValue - High[PriceIndex]) / PriceInPixel;
                  ObjectSet(CurrName, OBJPROP_PRICE1, High[PriceIndex] + OffsetInPixel * CurrPriceInPixel);
               }
            }
            i++;
         }
         PriceInPixel =  CurrPriceInPixel;
      } 
   } 
   FileClose(Handle);   
   return(0);
}
//+------------------------------------------------------------------+
void Add(int Index, bool Bool, int Level)
{
   bool b = False;
   for(int i = 0; i < n; i++)
   {
      if((ArrIndex[i] == Index) && ArrBool[i] == Bool)
      {
         ArrCount[i] = ArrCount[i] + 1;
         if(Level < ArrLevel[i])
            ArrLevel[i] = Level;
         b = true;
         break;
      }
   }
   if(b == false)
   {
      ArrIndex[n] = Index;
      ArrBool[n] = Bool;
      ArrCount[n] = 1;
      ArrLevel[n] = Level;
      n = n + 1;
   }   
}
int Search(int Index, bool Bool)
{
   int Result = 0;
   for(int i = 0; i < n; i++)
   { 
      if(ArrIndex[i] == Index && ArrBool[i] == Bool)
      {
        Result = i;
        break;
      }  
   }
   return(Result);
}

Recommend