#property copyright "Copyright � 2010-2011, NiX"
#property link      "http://www.the-forex-strategy.com"

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Black

#import "RangeBars2.dll"
   int initDLL(int a0, int a1, string a2);
   string getDLLVer();
   int updateDW(int a0);
   int updateT1(int a0, int a1);
   int processHistoryBar4Range(double a0, double& a1[], double& a2[], double& a3[], int a4);
#import

extern double PipRange = 13.0;
extern int OfflineTimeFrame = 2;
extern bool RefreshWindowOnAskPriceChange = FALSE;
extern int RenderUsing1MhistoryBars = 14400;
extern bool RefreshOnHistoryUpdates = FALSE;
extern bool UseSoundSignalOnNewBar = FALSE;
extern string SoundFile = "clock_tick.wav";
extern bool DisableComment = FALSE;
extern bool Backtesting = FALSE;
string Gs_120 = "";
int G_file_128 = -1;
int Gi_unused_132 = 0;
int Gi_unused_136 = 0;
double G_iclose_140 = 0.0;
double G_iclose_148 = 0.0;
double G_iclose_156 = 0.0;
double G_iclose_164 = 0.0;
double Gd_172 = 0.0;
string G_dbl2str_180 = "1.0";
int Gi_188;
int Gi_192 = 0;
int Gi_196 = 400;
string Gs_200;
string Gs_208;
int Gi_216;
int G_digits_220;
int Gia_224[13];
double Gd_228 = 0.0;
double G_iclose_236;
double Gd_244 = 0.0;
bool Gi_252 = FALSE;
bool Gi_256 = FALSE;
bool Gi_260 = FALSE;
bool Gi_264 = TRUE;
int Gi_268 = 0;
string G_name_272 = "RB_title";
string G_name_280 = "RB_underline";
int Gi_288 = 20;
string Gsa_292[20] = {"RB_aux1", "RB_aux2", "RB_aux3", "RB_aux4", "RB_aux5", "RB_aux6", "RB_aux7", "RB_aux8", "RB_aux9", "RB_aux10", "RB_aux11", "RB_aux12", "RB_aux13", "RB_aux14", "RB_aux15",
      "RB_aux16", "RB_aux17", "RB_aux18", "RB_aux19", "RB_aux20"};
double G_iclose_296;

// 945D754CB0DC06D04243FCBA25FC0802
int f0_4() {
   RefreshRates();
   double bid_0 = MarketInfo(Symbol(), MODE_BID);
   double ask_8 = MarketInfo(Symbol(), MODE_ASK);
   if (bid_0 > 0.0 && ask_8 > 0.0) return (1);
   return (0);
}

// 9B1AEE847CFB597942D106A4135D4FE6
double f0_5() {
   double Lda_0[6] = {1, 10, 100, 1000, 10000, 100000};
   if (Digits > 0) return (0.1 / (Lda_0[Digits - 1]));
   if (Digits == 0) return (1);
   return (0.0);
}

// E37F0136AA3FFAF149B351F6A4C948E9
int init() {
   Gs_120 = f0_2();
   if (Gs_120 == "") {
      Gi_260 = TRUE;
      Alert("Please run the RangeBarsUnlock script first.\nIt is located in your Navigator window (Scripts branch)");
      Comment("RangeBarChart DISABLED.\nPlease REMOVE the indicator from this chart and unlock first.");
      return (0);
   }
   Gi_260 = FALSE;
   G_dbl2str_180 = DoubleToStr(PipRange, 1);
   if (Digits % 2 > 0) PipRange = 10.0 * PipRange;
   if (f0_5() != MarketInfo(Symbol(), MODE_TICKSIZE)) PipRange /= MarketInfo(Symbol(), MODE_TICKSIZE);
   if (!DisableComment) {
      if (ObjectFind(G_name_272) == -1) ObjectCreate(G_name_272, OBJ_LABEL, 0, 0, 0);
      if (ObjectFind(G_name_280) == -1) ObjectCreate(G_name_280, OBJ_LABEL, 0, 0, 0);
      ObjectSetText(G_name_272, "RangeBar Plugin v" + "2.03e" + " for MT4 (c)2010 NiX", 11, "Terminal", RoyalBlue);
      ObjectSet(G_name_272, OBJPROP_CORNER, 1);
      ObjectSet(G_name_272, OBJPROP_XDISTANCE, 5);
      ObjectSet(G_name_272, OBJPROP_YDISTANCE, 20);
      ObjectSetText(G_name_280, "__________________________________________", 11, "Terminal", Blue);
      ObjectSet(G_name_280, OBJPROP_CORNER, 1);
      ObjectSet(G_name_280, OBJPROP_XDISTANCE, 5);
      ObjectSet(G_name_280, OBJPROP_YDISTANCE, 25);
   }
   Gs_208 = Symbol();
   Gi_216 = OfflineTimeFrame;
   G_digits_220 = Digits;
   if (!Backtesting) {
      switch (OfflineTimeFrame) {
      case 0:
      case 1:
      case 5:
      case 15:
      case 30:
      case 60:
      case 240:
      case 1440:
      case 10080:
      case 43200:
         Alert(OfflineTimeFrame + " is a Metatrader4 reserver time frame. Please choose a different value (i.e. 2,3,4,6,7,8,9,10.... Exiting...");
         Gi_256 = TRUE;
         return (0);
      }
   }
   if (!IsDllsAllowed()) Alert("The live range bar feed will not work if DLL calls are disabled!");
   if (!DisableComment) {
      while (ObjectFind(Gsa_292[Gi_268]) == 0) Gi_268++;
      if (Gi_268 >= Gi_288) {
         Alert("Indicator limit reached. Exiting...");
         Gi_256 = TRUE;
         return (0);
      }
      ObjectCreate(Gsa_292[Gi_268], OBJ_LABEL, 0, 0, 0);
      ObjectSetText(Gsa_292[Gi_268], G_dbl2str_180 + " pip range bars @ offline " + Symbol() + ",M" + OfflineTimeFrame, 11, "Terminal", Gray);
      ObjectSet(Gsa_292[Gi_268], OBJPROP_CORNER, 1);
      ObjectSet(Gsa_292[Gi_268], OBJPROP_XDISTANCE, 5);
      ObjectSet(Gsa_292[Gi_268], OBJPROP_YDISTANCE, 13 * Gi_268 + 40);
   }
   Gd_228 = NormalizeDouble(PipRange * Point, Digits);
   if (PipRange <= 0.0) return (0);
   return (0);
}

// 52D46093050F38C27267BCE42543EF60
int deinit() {
   if (G_file_128 >= 0) {
      FileClose(G_file_128);
      G_file_128 = -1;
   }
   int count_0 = 0;
   for (int index_4 = 0; index_4 < Gi_288; index_4++)
      if (ObjectFind(Gsa_292[index_4]) == 0) count_0++;
   ObjectDelete(Gsa_292[Gi_268]);
   if (count_0 == 1) {
      ObjectDelete(G_name_272);
      ObjectDelete(G_name_280);
   }
   if (Gi_260 == TRUE) Comment("");
   return (0);
}

// 2569208C5E61CB15E209FFE323DB48B7
int f0_1() {
   string Ls_0;
   while (!f0_4()) Sleep(100);
   int Li_ret_8 = initDLL(Backtesting, WindowHandle(Gs_208, Period()), Gs_120);
   if (Li_ret_8 == 2 || Li_ret_8 == 3) {
      Print("Indicator not ready or invalid key...");
      return (Li_ret_8);
   }
   if (Li_ret_8 == 0) return (0);
   if (Li_ret_8 == 1) {
      Ls_0 = getDLLVer();
      if (Ls_0 == "20110620.1") Print("(ok) RangeBar Plug-in " + "2.03e" + " [" + getDLLVer() + "] for MT4 is licensed to " + AccountNumber());
      else {
         Print("(error) Incorrect DLL Version!");
         return (0);
      }
   }
   if (G_file_128 >= 0) {
      FileFlush(G_file_128);
      FileClose(G_file_128);
      G_file_128 = -1;
   }
   G_file_128 = FileOpenHistory(Gs_208 + Gi_216 + ".hst", FILE_BIN|FILE_WRITE);
   if (G_file_128 < 0) return (-1);
   Gs_200 = "(C)opyright 2010, NiX";
   FileWriteInteger(G_file_128, Gi_196, LONG_VALUE);
   FileWriteString(G_file_128, Gs_200, 64);
   FileWriteString(G_file_128, Gs_208, 12);
   FileWriteInteger(G_file_128, Gi_216, LONG_VALUE);
   FileWriteInteger(G_file_128, G_digits_220, LONG_VALUE);
   FileWriteInteger(G_file_128, 0, LONG_VALUE);
   FileWriteInteger(G_file_128, 0, LONG_VALUE);
   FileWriteArray(G_file_128, Gia_224, 0, 13);
   FileFlush(G_file_128);
   int Li_12 = 0;
   int Li_16 = iBars(Symbol(), PERIOD_M1);
   if (Li_16 > RenderUsing1MhistoryBars) {
      if (RenderUsing1MhistoryBars == 0) Li_12 = Li_16;
      else Li_12 = RenderUsing1MhistoryBars;
   } else Li_12 = Li_16;
   Li_12--;
   f0_6();
   for (Gi_188 = Li_12; Gi_188 > 0; Gi_188--) {
      Gd_244 += iVolume(Symbol(), PERIOD_M1, Gi_188);
      f0_7(iTime(Symbol(), PERIOD_M1, Gi_188), iOpen(Symbol(), PERIOD_M1, Gi_188), iLow(Symbol(), PERIOD_M1, Gi_188), iHigh(Symbol(), PERIOD_M1, Gi_188), iClose(Symbol(),
         PERIOD_M1, Gi_188), Gd_244, 1);
   }
   if (Backtesting) {
      ObjectSetText(Gsa_292[Gi_268], G_dbl2str_180 + " pip range bars ready for backtesting @ " + Symbol() + ",M" + OfflineTimeFrame, 11, "Terminal", Lime);
      Gi_256 = TRUE;
      Print("Backtesting chart");
      if (G_file_128 >= 0) {
         FileClose(G_file_128);
         G_file_128 = -1;
      }
      updateDW(WindowHandle(Gs_208, OfflineTimeFrame));
   }
   RefreshRates();
   Gi_252 = TRUE;
   Gi_264 = FALSE;
   Gd_244 += iVolume(Symbol(), PERIOD_M1, 0);
   f0_7(iTime(Symbol(), PERIOD_M1, 0), iOpen(Symbol(), PERIOD_M1, 0), iLow(Symbol(), PERIOD_M1, 0), iHigh(Symbol(), PERIOD_M1, 0), iClose(Symbol(), PERIOD_M1, 0), Gd_244,
      1);
   updateDW(WindowHandle(Gs_208, OfflineTimeFrame));
   return (0);
}

// EA2B2676C28C0DB26D39331A336C6B92
int start() {
   if (Gi_260 == TRUE) return (-2);
   if ((!IsConnected()) && !Backtesting) return (-1);
   int ind_counted_0 = IndicatorCounted();
   if (ind_counted_0 < 0) return (-1);
   if (ind_counted_0 == 0) {
      if (RefreshOnHistoryUpdates == TRUE) {
         Print("More history downloaded - resynchronizing with market");
         Gi_252 = FALSE;
         Gi_192 = FALSE;
         Gi_264 = TRUE;
      }
   }
   if (Gi_264 == TRUE) {
      f0_1();
      if (Gi_264) return (-1);
   }
   if (Gi_256) return (0);
   G_iclose_296 = iClose(Symbol(), PERIOD_M1, 0);
   if (G_iclose_236 != G_iclose_296) {
      G_iclose_236 = G_iclose_296;
      if (G_iclose_140 != 0.0) {
         if (G_iclose_156 < G_iclose_296) G_iclose_156 = G_iclose_296;
         if (G_iclose_148 > G_iclose_296) G_iclose_148 = G_iclose_296;
         G_iclose_164 = G_iclose_296;
         Gd_172 += 1.0;
      } else {
         G_iclose_156 = G_iclose_296;
         G_iclose_148 = G_iclose_296;
         G_iclose_140 = G_iclose_296;
         G_iclose_164 = G_iclose_296;
         Gd_172 = 1;
      }
      f0_7(iTime(Symbol(), PERIOD_M1, 0), G_iclose_140, G_iclose_148, G_iclose_156, G_iclose_164, Gd_172, 1);
      updateDW(WindowHandle(Gs_208, OfflineTimeFrame));
   }
   if (RefreshWindowOnAskPriceChange) updateDW(WindowHandle(Gs_208, OfflineTimeFrame));
   return (0);
}

// 6ABA3523C7A75AAEA41CC0DEC7953CC5
void f0_3(int Ai_0, double Ad_4, double Ad_12, double Ad_20, double Ad_28, double Ad_36, int Ai_44) {
   int Li_48;
   if (G_file_128 >= 0) {
      if (Ai_44 == 1) {
         Li_48 = updateT1(Ai_0, Gi_192);
         FileSeek(G_file_128, 0, SEEK_END);
         FileWriteInteger(G_file_128, Li_48, LONG_VALUE);
         Gi_192 = Li_48;
      } else FileSeek(G_file_128, -40, SEEK_END);
      if (Ad_36 == 0.0) Ad_36++;
      FileWriteDouble(G_file_128, Ad_4, DOUBLE_VALUE);
      FileWriteDouble(G_file_128, Ad_12, DOUBLE_VALUE);
      FileWriteDouble(G_file_128, Ad_20, DOUBLE_VALUE);
      FileWriteDouble(G_file_128, Ad_28, DOUBLE_VALUE);
      FileWriteDouble(G_file_128, Ad_36, DOUBLE_VALUE);
      FileFlush(G_file_128);
      if (Ai_44 == 1 && Gi_252 && UseSoundSignalOnNewBar == TRUE) {
         if (SoundFile == "alert_box") Alert("New bar on " + G_dbl2str_180 + "pip chart for " + Symbol());
         else PlaySound(SoundFile);
      }
   }
}

// 09CBB5F5CE12C31A043D5C81BF20AA4A
void f0_0(double A_iclose_0, double A_iclose_8, double A_iclose_16, double A_iclose_24, double Ad_32) {
   G_iclose_140 = A_iclose_0;
   G_iclose_148 = A_iclose_8;
   G_iclose_156 = A_iclose_16;
   G_iclose_164 = A_iclose_24;
   Gd_172 = Ad_32;
}

// A9B24A824F70CC1232D1C2BA27039E8D
void f0_6() {
   G_iclose_140 = 0;
   G_iclose_148 = 0;
   G_iclose_156 = 0;
   G_iclose_164 = 0;
   Gd_172 = 0;
}

// D1DDCE31F1A86B3140880F6B1877CBF8
int f0_7(int Ai_0, double Ad_4, double Ad_12, double Ad_20, double Ad_28, double Ad_36, int Ai_44) {
   double Lda_48[5] = {0, 0, 0, 0, 0};
   double Lda_52[5] = {0, 0, 0, 0, 0};
   double Lda_56[5] = {0, 0, 0, 0, 0};
   Lda_52[0] = Ad_4;
   Lda_52[1] = Ad_12;
   Lda_52[2] = Ad_20;
   Lda_52[3] = Ad_28;
   Lda_52[4] = Ad_36;
   Lda_56[0] = G_iclose_140;
   Lda_56[1] = G_iclose_148;
   Lda_56[2] = G_iclose_156;
   Lda_56[3] = G_iclose_164;
   Lda_56[4] = Gd_172;
   int Li_60 = processHistoryBar4Range(Gd_228, Lda_52, Lda_56, Lda_48, Ai_44);
   while (true) {
      if (!(((Li_60 >= 1 && Li_60 <= 4) || (Li_60 >= 6 && Li_60 <= 9)))) break;
      f0_3(Ai_0, Lda_48[0], Lda_48[1], Lda_48[2], Lda_48[3], Lda_48[4], 0);
      f0_0(Lda_56[0], Lda_56[1], Lda_56[2], Lda_56[3], Lda_56[4]);
      f0_3(Ai_0, Lda_56[0], Lda_56[1], Lda_56[2], Lda_56[3], Lda_56[4], 1);
      Lda_52[0] = G_iclose_140;
      Lda_52[1] = G_iclose_148;
      Lda_52[2] = G_iclose_156;
      Lda_52[3] = G_iclose_164;
      Lda_52[4] = 1;
      Li_60 = processHistoryBar4Range(Gd_228, Lda_52, Lda_56, Lda_48, 0);
   }
   if (Li_60 == 5 || Li_60 == 10) {
      f0_0(Lda_56[0], Lda_56[1], Lda_56[2], Lda_56[3], Lda_56[4]);
      if (Gi_252) f0_3(Ai_0, Lda_56[0], Lda_56[1], Lda_56[2], Lda_56[3], Lda_56[4], 0);
      else Gd_244 = Lda_56[4];
      return (1);
   }
   if (Li_60 == 100 || Li_60 == 101) {
      f0_6();
      return (0);
   }
   return (1);
}

// 5710F6E623305B2C1458238C9757193B
string f0_2() {
   string Ls_ret_0 = "supadupa";
   return (Ls_ret_0);
}
