#property copyright "Copyright � 2008, MQL Service"
#property link      "http://www.mqlservice.net"
//#property show_inputs

#include <WinUser32.mqh>
#import "user32.dll"
   int RegisterWindowMessageA(string a0);
#import

extern int BarsRange = 7;
extern int TimeFrame = 2;
extern int MaxBars = 500000;
extern bool RescaleFor5DigitsBroker = TRUE;
extern bool ShowWicks = TRUE;
extern double AnchorPrice = 0.0;
int G_file_104 = -1;

double f0_1(int Ai_0) {
   return (iVolume(Symbol(), PERIOD_M1, Ai_0));
}

double f0_5(int Ai_0) {
   return (iOpen(Symbol(), PERIOD_M1, Ai_0));
}

double f0_3(int Ai_0) {
   return (iClose(Symbol(), PERIOD_M1, Ai_0));
}

double f0_6(int Ai_0) {
   return (iHigh(Symbol(), PERIOD_M1, Ai_0));
}

double f0_2(int Ai_0) {
   return (iLow(Symbol(), PERIOD_M1, Ai_0));
}

int f0_0(int Ai_0) {
   return (Ai_0);
}

int init() {
   Comment(WindowExpertName(), " started.   Offline chart to open is ", Symbol(), ",M", TimeFrame, 
   "\nRange = ", BarsRange);
   return (0);
}

int start() {
   int Li_4;
   double Ld_8;
   int Lia_16[13];
   double Ld_20;
   int Li_28;
   if (f0_4(BarsRange, 0, "BarsRange")) return (0);
   if (f0_4(TimeFrame, 1, "TimeFrame")) return (0);
   if (!IsDllsAllowed()) {
      Alert(WindowExpertName(), ": Please Allow DLL imports to run this script");
      Comment(WindowExpertName(), " aborted due to disabled DLL calls");
      return (-1);
   }
   int Li_32 = RegisterWindowMessageA("MetaTrader4_Internal_Message");
   string Ls_36 = Symbol();
   int Li_44 = 0;
   int count_48 = 0;
   int Li_52 = 400;
   int Li_56 = TimeFrame;
   int digits_60 = Digits;
   for (int Li_64 = StringFind(Ls_36, ".", 0); Li_64 > 0; Li_64 = StringFind(Ls_36, ".", 0)) Ls_36 = StringSubstr(Ls_36, 0, Li_64) + StringSubstr(Ls_36, Li_64 + 1);
   G_file_104 = FileOpenHistory(Ls_36 + Li_56 + ".hst", FILE_BIN|FILE_WRITE|FILE_SHARE_WRITE|FILE_SHARE_READ|FILE_ANSI);
   if (G_file_104 < 0) {
      Alert(WindowExpertName(), ": Cannot create ", Ls_36 + TimeFrame + ".hst Error=",GetLastError());
      Comment(WindowExpertName(), " aborted: Cannot create ", Ls_36 + TimeFrame + ".hst");
      return (-1);
   }
   Print("$Id: //mqlservice/mt4files/experts/scripts/s-Renko bars.mq4#10 $");
   string Ls_68 = "(C)opyright 2008, MQL Service";
   FileWriteInteger(G_file_104, Li_52, LONG_VALUE);
   FileWriteString(G_file_104, Ls_68, 64);
   FileWriteString(G_file_104, Ls_36, 12);
   FileWriteInteger(G_file_104, Li_56, LONG_VALUE);
   FileWriteInteger(G_file_104, digits_60, LONG_VALUE);
   FileWriteInteger(G_file_104, 0, LONG_VALUE);
   FileWriteInteger(G_file_104, 0, LONG_VALUE);
   FileWriteArray(G_file_104, Lia_16, 0, 13);
   FileFlush(G_file_104);
   Comment("RenkoAshi Brk--(" + BarsRange + "): Open Offline ", Ls_36, ",M", Li_56, " to view chart");
   int Li_76 = 60 * Li_56;
   int Li_80 = MathMin(MaxBars, iBars(Symbol(), PERIOD_M1) - 1);
   if (Li_80 <= 0)
      if (MessageBox("No 1M bars available, there will be no history bars!\n\nContinue?", WindowExpertName(), MB_YESNO|MB_ICONQUESTION) == IDNO) return (0);
   if (Period() != PERIOD_M1)
      if (MessageBox("It should be run on 1M chart!\n\nContinue?", WindowExpertName(), MB_YESNO|MB_ICONQUESTION) == IDNO) return (0);
   int Li_84 = 1;
   if (Digits == 3 || Digits > 4) {
      if (RescaleFor5DigitsBroker) {
         BarsRange = 10 * BarsRange;
         Li_84 = 10;
      }
   }
   int Li_88 = f0_0(iTime(Symbol(), PERIOD_M1, Li_80));
   Print("Starting from ", TimeToStr(Li_88, TIME_DATE|TIME_MINUTES|TIME_SECONDS));
   int Li_92 = FileTell(G_file_104);
   double Ld_96 = f0_3(Li_80);
   if (AnchorPrice >= 0.0) Ld_96 = MathRound(Ld_96 / Point / BarsRange) * Point * BarsRange + MathMod(AnchorPrice, BarsRange * Point);
   double Ld_104 = Ld_96;
   double Ld_112 = 1.0;
   double Ld_120 = Ld_96;
   double Ld_128 = Ld_96;
   double Ld_136 = Ld_96;
   int Li_144 = 1;
   int Li_148 = 1;
   int Li_152 = 1;
   for (Li_64 = Li_80 - 1; Li_64 >= 0; Li_64--) {
      Li_4 = f0_0(iTime(Symbol(), PERIOD_M1, Li_64));
      if (Volume[Li_64] > 0.0) Li_152 = MathMax(Li_84, MathCeil((f0_6(Li_64) - f0_2(Li_64)) / Point / Volume[Li_64]));
      else Li_152 = Li_84;
      Li_152 = MathRound(Li_152 / Li_84) * Li_84;
      if (f0_5(Li_64) > f0_3(Li_64)) {
         for (int Li_0 = f0_6(Li_64) / Point; Li_0 >= f0_2(Li_64) / Point; Li_0 -= Li_152) {
            Ld_20 = NormalizeDouble(Li_0 * Point, Digits);
            Ld_96 = MathMin(Ld_96, Ld_20);
            Ld_104 = MathMax(Ld_104, Ld_20);
            Ld_112 += MathRound(MathAbs(Ld_120 - Ld_20) / Point);
            Ld_120 = Ld_20;
            if (MathRound((Ld_104 - Ld_136) / Point) >= Li_144 * BarsRange || MathRound((Ld_136 - Ld_96) / Point) >= Li_148 * BarsRange) {
               while (true) {
                  Li_92 = FileTell(G_file_104);
                  if (MathRound((Ld_136 - Ld_96) / Point) >= Li_148 * BarsRange) {
                     if (Li_148 == 2) Ld_136 -= BarsRange * Point;
                     Ld_120 = Ld_136 - BarsRange * Point;
                     if (!ShowWicks) {
                        Ld_96 = Ld_120;
                        Ld_104 = Ld_136;
                     }
                     Ld_128 = Ld_136;
                     Li_148 = 1;
                     Li_144 = 2;
                  }
                  if (MathRound((Ld_104 - Ld_136) / Point) >= Li_144 * BarsRange) {
                     if (Li_144 == 2) Ld_136 += BarsRange * Point;
                     Ld_120 = Ld_136 + BarsRange * Point;
                     if (!ShowWicks) {
                        Ld_104 = Ld_120;
                        Ld_96 = Ld_136;
                     }
                     Ld_128 = Ld_136;
                     Li_144 = 1;
                     Li_148 = 2;
                  }
                  Ld_96 = MathMin(Ld_128, MathMin(Ld_104, MathMin(Ld_96, Ld_120)));
                  Ld_104 = MathMax(Ld_128, MathMax(Ld_104, MathMax(Ld_96, Ld_120)));
                  FileWriteInteger(G_file_104, Li_88, LONG_VALUE);
                  FileWriteDouble(G_file_104, Ld_128, DOUBLE_VALUE);
                  FileWriteDouble(G_file_104, Ld_96, DOUBLE_VALUE);
                  FileWriteDouble(G_file_104, Ld_104, DOUBLE_VALUE);
                  FileWriteDouble(G_file_104, Ld_120, DOUBLE_VALUE);
                  FileWriteDouble(G_file_104, Ld_112, DOUBLE_VALUE);
                  Li_88++;
                  FileFlush(G_file_104);
                  count_48++;
                  Ld_136 = Ld_120;
                  Li_88 = MathMax(Li_4, Li_88);
                  Ld_128 = Ld_120;
                  Ld_96 = MathMin(Ld_20, Ld_120);
                  Ld_104 = MathMax(Ld_20, Ld_120);
                  Ld_120 = Ld_20;
                  Ld_112 = 1.0;
                  if (MathRound((Ld_104 - Ld_136) / Point) >= Li_144 * BarsRange || MathRound((Ld_136 - Ld_96) / Point) >= Li_148 * BarsRange) continue;
                  break;
               }
            }
         }
      } else {
         for (Li_0 = f0_2(Li_64) / Point; Li_0 <= f0_6(Li_64) / Point; Li_0 += Li_152) {
            Ld_20 = NormalizeDouble(Li_0 * Point, Digits);
            Ld_96 = MathMin(Ld_96, Ld_20);
            Ld_104 = MathMax(Ld_104, Ld_20);
            Ld_112 += MathRound(MathAbs(Ld_120 - Ld_20) / Point);
            Ld_120 = Ld_20;
            if (MathRound((Ld_104 - Ld_136) / Point) >= Li_144 * BarsRange || MathRound((Ld_136 - Ld_96) / Point) >= Li_148 * BarsRange) {
               while (true) {
                  Li_92 = FileTell(G_file_104);
                  if (MathRound((Ld_136 - Ld_96) / Point) >= Li_148 * BarsRange) {
                     if (Li_148 == 2) Ld_136 -= BarsRange * Point;
                     Ld_120 = Ld_136 - BarsRange * Point;
                     if (!ShowWicks) {
                        Ld_96 = Ld_120;
                        Ld_104 = Ld_136;
                     }
                     Ld_128 = Ld_136;
                     Li_148 = 1;
                     Li_144 = 2;
                  }
                  if (MathRound((Ld_104 - Ld_136) / Point) >= Li_144 * BarsRange) {
                     if (Li_144 == 2) Ld_136 += BarsRange * Point;
                     Ld_120 = Ld_136 + BarsRange * Point;
                     if (!ShowWicks) {
                        Ld_104 = Ld_120;
                        Ld_96 = Ld_136;
                     }
                     Ld_128 = Ld_136;
                     Li_144 = 1;
                     Li_148 = 2;
                  }
                  Ld_96 = MathMin(Ld_128, MathMin(Ld_104, MathMin(Ld_96, Ld_120)));
                  Ld_104 = MathMax(Ld_128, MathMax(Ld_104, MathMax(Ld_96, Ld_120)));
                  FileWriteInteger(G_file_104, Li_88, LONG_VALUE);
                  FileWriteDouble(G_file_104, Ld_128, DOUBLE_VALUE);
                  FileWriteDouble(G_file_104, Ld_96, DOUBLE_VALUE);
                  FileWriteDouble(G_file_104, Ld_104, DOUBLE_VALUE);
                  FileWriteDouble(G_file_104, Ld_120, DOUBLE_VALUE);
                  FileWriteDouble(G_file_104, Ld_112, DOUBLE_VALUE);
                  Li_88++;
                  FileFlush(G_file_104);
                  count_48++;
                  Ld_136 = Ld_120;
                  Li_88 = MathMax(Li_4, Li_88);
                  Ld_128 = Ld_120;
                  Ld_96 = MathMin(Ld_20, Ld_120);
                  Ld_104 = MathMax(Ld_20, Ld_120);
                  Ld_120 = Ld_20;
                  Ld_112 = 1.0;
                  if (MathRound((Ld_104 - Ld_136) / Point) >= Li_144 * BarsRange || MathRound((Ld_136 - Ld_96) / Point) >= Li_148 * BarsRange) continue;
                  break;
               }
            }
         }
      }
   }
   Li_92 = FileTell(G_file_104);
   Ld_120 = Close[0];
   FileWriteInteger(G_file_104, TimeCurrent(), LONG_VALUE);
   FileWriteDouble(G_file_104, Ld_128, DOUBLE_VALUE);
   FileWriteDouble(G_file_104, Ld_96, DOUBLE_VALUE);
   FileWriteDouble(G_file_104, Ld_104, DOUBLE_VALUE);
   FileWriteDouble(G_file_104, Ld_120, DOUBLE_VALUE);
   FileWriteDouble(G_file_104, Ld_112, DOUBLE_VALUE);
   FileFlush(G_file_104);
   Print(count_48, " record(s) written");
   if (Li_44 == 0) {
      Li_44 = WindowHandle(Ls_36, Li_56);
      if (Li_44 != 0) Print("Chart window detected");
   }
   f0_7(Li_44, Li_32);
   Li_88 = MathMax(TimeCurrent(), Li_88);
   while (IsStopped() == FALSE) {
      if (RefreshRates()) {
         FileSeek(G_file_104, Li_92, SEEK_SET);
         Ld_96 = MathMin(Ld_96, Close[0]);
         Ld_104 = MathMax(Ld_104, Close[0]);
         Ld_112 += MathRound(MathAbs(Ld_120 - f0_3(0)) / Point);
         if (MathRound((Ld_104 - Ld_136) / Point) < Li_144 * BarsRange && MathRound((Ld_136 - Ld_96) / Point) < Li_148 * BarsRange) {
            Ld_8 = f0_1(0);
            Ld_120 = f0_3(0);
         } else {
            if (MathRound((Ld_104 - Ld_136) / Point) >= Li_144 * BarsRange || MathRound((Ld_136 - Ld_96) / Point) >= Li_148 * BarsRange) {
               while (true) {
                  if (MathRound((Ld_136 - Ld_96) / Point) >= Li_148 * BarsRange) {
                     if (Li_148 == 2) Ld_136 -= BarsRange * Point;
                     Ld_120 = Ld_136 - BarsRange * Point;
                     if (!ShowWicks) {
                        Ld_96 = Ld_120;
                        Ld_104 = Ld_136;
                     }
                     Ld_128 = Ld_136;
                     Li_148 = 1;
                     Li_144 = 2;
                  }
                  if (MathRound((Ld_104 - Ld_136) / Point) >= Li_144 * BarsRange) {
                     if (Li_144 == 2) Ld_136 += BarsRange * Point;
                     Ld_120 = Ld_136 + BarsRange * Point;
                     if (!ShowWicks) {
                        Ld_104 = Ld_120;
                        Ld_96 = Ld_136;
                     }
                     Ld_128 = Ld_136;
                     Li_144 = 1;
                     Li_148 = 2;
                  }
                  Ld_96 = MathMin(Ld_128, MathMin(Ld_104, MathMin(Ld_96, Ld_120)));
                  Ld_104 = MathMax(Ld_128, MathMax(Ld_104, MathMax(Ld_96, Ld_120)));
                  FileWriteInteger(G_file_104, Li_88, LONG_VALUE);
                  FileWriteDouble(G_file_104, Ld_128, DOUBLE_VALUE);
                  FileWriteDouble(G_file_104, Ld_96, DOUBLE_VALUE);
                  FileWriteDouble(G_file_104, Ld_104, DOUBLE_VALUE);
                  FileWriteDouble(G_file_104, Ld_120, DOUBLE_VALUE);
                  FileWriteDouble(G_file_104, Ld_112, DOUBLE_VALUE);
                  Li_92 = FileTell(G_file_104);
                  Li_88++;
                  Li_88 = MathMax(Li_88, TimeCurrent());
                  Ld_136 = Ld_120;
                  Li_88 = MathMax(Li_4, Li_88);
                  Ld_128 = Ld_120;
                  Ld_96 = MathMin(Close[0], Ld_120);
                  Ld_104 = MathMax(Close[0], Ld_120);
                  Ld_120 = Close[0];
                  Ld_112 = 1.0;
                  if (MathRound((Ld_104 - Ld_136) / Point) >= Li_144 * BarsRange || MathRound((Ld_136 - Ld_96) / Point) >= Li_148 * BarsRange) continue;
                  break;
               }
            }
         }
         FileWriteInteger(G_file_104, Li_88, LONG_VALUE);
         FileWriteDouble(G_file_104, Ld_128, DOUBLE_VALUE);
         FileWriteDouble(G_file_104, Ld_96, DOUBLE_VALUE);
         FileWriteDouble(G_file_104, Ld_104, DOUBLE_VALUE);
         FileWriteDouble(G_file_104, Ld_120, DOUBLE_VALUE);
         FileWriteDouble(G_file_104, Ld_112, DOUBLE_VALUE);
         FileFlush(G_file_104);
         Li_28 = WindowHandle(Ls_36, Li_56);
         if (Li_44 != Li_28) Print("Chart window detected");
         Li_44 = Li_28;
         f0_7(Li_44, Li_32);
      } else Sleep(100);
      Comment(WindowExpertName(), " started. Offline chart to open is ", Symbol(), ",M", TimeFrame, 
         "\nRange = ", BarsRange, 
      "\nOff-line window handle = ", Li_28);
   }
   return (0);
}

void f0_7(int Ai_0, int Ai_4) {
   if (Ai_0 != 0) {
      PostMessageA(Ai_0, WM_COMMAND, 33324, 1);
      PostMessageA(Ai_0, Ai_4, 2, 1);
   }
}

void deinit() {
   if (G_file_104 >= 0) {
      FileClose(G_file_104);
      G_file_104 = -1;
   }
   Comment(WindowExpertName(), " finished");
}

int f0_4(int Ai_0, int Ai_4, string As_8) {
   if (Ai_0 <= Ai_4) {
      Alert(WindowExpertName() + " " + Symbol() + Period() + " invalid parameter ", As_8, ": ", Ai_0, ". Must be grater than ", Ai_4);
      return (1);
   }
   return (0);
}
