
#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 getR2();
   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 td_0 = 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;

int f0_0() {
   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);
}

double f0_1() {
   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);
}

int init() {
   gs_120 = f0_7();
   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_1() != 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);
   }
   td_0 = NormalizeDouble(PipRange * Point, Digits);
   if (PipRange <= 0.0) return (0);
   return (0);
}

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);
}

int f0_2() {
   string ls_4;
   while (!f0_0()) Sleep(100);
   int li_ret_0 = initDLL(Backtesting, WindowHandle(gs_208, Period()), gs_120);
   if (li_ret_0 == 2 || li_ret_0 == 3) {
      Print("Indicator not ready or invalid key...");
      return (li_ret_0);
   }
   if (li_ret_0 == 0) return (0);
   if (li_ret_0 == 1) {
      ls_4 = getDLLVer();
      if (ls_4 == "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_5();
   for (gi_188 = li_12; gi_188 > 0; gi_188--) {
      gd_244 += iVolume(Symbol(), PERIOD_M1, gi_188);
      f0_6(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_6(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);
}

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_2();
      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_6(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);
}

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);
      }
   }
}

void f0_4(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;
}

void f0_5() {
   g_iclose_140 = 0;
   g_iclose_148 = 0;
   g_iclose_156 = 0;
   g_iclose_164 = 0;
   gd_172 = 0;
}

int f0_6(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(td_0, 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_4(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(td_0, lda_52, lda_56, lda_48, 0);
   }
   if (li_60 == 5 || li_60 == 10) {
      f0_4(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_5();
      return (0);
   }
   return (1);
}

string f0_7() {
   //int file_0 = FileOpen("RangeBar.key", FILE_BIN|FILE_READ);
   //if (file_0 < 0) return (gs_120);
   string ls_ret_4 = "supadupa";//FileReadString(file_0, 1024);
   //if (file_0 >= 0) FileClose(file_0);
   return (ls_ret_4);
}