//+------------------------------------------------------------------+ //| RenkoLiveChart.mq4 | //| Inspired from Renco script by "e4" (renko_live_scr.mq4) | //| Written from scratch 2009 LastViking | //+------------------------------------------------------------------+ #property copyright "" #property show_inputs //+------------------------------------------------------------------+ #include //+------------------------------------------------------------------+ extern int BoxSize = 5; extern bool VolumeInHistory = false; //+------------------------------------------------------------------+ int start() { int i, LastFPos = 0, HstHandle = -1; //---- History header int HstVersion = 400; string HstCopyright = ""; string HstSymbol = Symbol(); int HstPeriod = BoxSize + 2; int HstDigits = Digits; int HstUnused[13]; HstHandle = FileOpenHistory(HstSymbol + HstPeriod + ".hst", FILE_BIN|FILE_WRITE); if(HstHandle < 0) return(-1); //---- write history file header FileWriteInteger(HstHandle, HstVersion, LONG_VALUE); FileWriteString(HstHandle, HstCopyright, 64); FileWriteString(HstHandle, HstSymbol, 12); FileWriteInteger(HstHandle, HstPeriod, LONG_VALUE); FileWriteInteger(HstHandle, HstDigits, LONG_VALUE); FileWriteInteger(HstHandle, 0, LONG_VALUE); //timesign FileWriteInteger(HstHandle, 0, LONG_VALUE); //last_sync FileWriteArray(HstHandle, HstUnused, 0, 13); Comment("RenkoLiveChart: Open Offline ", HstSymbol, "M", HstPeriod, " to view chart"); //---- write history file double BoxPoints = NormalizeDouble(BoxSize*Point, Digits); double PrevLow = NormalizeDouble(MathFloor(Close[Bars-1]/BoxPoints)*BoxPoints, Digits); double PrevHigh = PrevLow + BoxPoints; double PrevOpen = PrevLow; double PrevClose = PrevHigh; double CurVolume = 1; datetime PrevTime = Time[Bars-1]; //---- begin historical data i = Bars-2; while(i >= 0) { //get price based on closed bar PrevTime = Time[i]; CurVolume = CurVolume + Volume[i]; if(LastFPos != 0 && VolumeInHistory) { FileSeek(HstHandle, LastFPos, SEEK_SET); // Resore last pos in file then updating volume FileWriteDouble(HstHandle, CurVolume, DOUBLE_VALUE); } while(Close[i] >= PrevHigh+BoxPoints) { PrevHigh = PrevHigh + BoxPoints; PrevLow = PrevLow + BoxPoints; PrevOpen = PrevLow; PrevClose = PrevHigh; CurVolume = 1; FileWriteInteger(HstHandle, PrevTime, LONG_VALUE); FileWriteDouble(HstHandle, PrevOpen, DOUBLE_VALUE); FileWriteDouble(HstHandle, PrevLow, DOUBLE_VALUE); FileWriteDouble(HstHandle, PrevHigh, DOUBLE_VALUE); FileWriteDouble(HstHandle, PrevClose, DOUBLE_VALUE); LastFPos = FileTell(HstHandle); // Remeber Last pos in file FileWriteDouble(HstHandle, CurVolume, DOUBLE_VALUE); PrevTime++; } while(Close[i] <= PrevLow-BoxPoints) { PrevHigh = PrevHigh - BoxPoints; PrevLow = PrevLow - BoxPoints; PrevOpen = PrevHigh; PrevClose = PrevLow; CurVolume = 1; FileWriteInteger(HstHandle, PrevTime, LONG_VALUE); FileWriteDouble(HstHandle, PrevOpen, DOUBLE_VALUE); FileWriteDouble(HstHandle, PrevLow, DOUBLE_VALUE); FileWriteDouble(HstHandle, PrevHigh, DOUBLE_VALUE); FileWriteDouble(HstHandle, PrevClose, DOUBLE_VALUE); LastFPos = FileTell(HstHandle); // Remeber Last pos in file FileWriteDouble(HstHandle, CurVolume, DOUBLE_VALUE); PrevTime++; } i--; } FileFlush(HstHandle); //----- end historical data //------ begin live data feed int hwnd = 0; CurVolume = 0; datetime LastTime = TimeCurrent(); while(!IsStopped()) { if(RefreshRates()) { if(hwnd == 0) { hwnd = WindowHandle(Symbol(),HstPeriod); if(hwnd != 0) Print("Chart window detected"); } if(Bid >= PrevHigh+BoxPoints) { PrevHigh = PrevHigh + BoxPoints; PrevLow = PrevLow + BoxPoints; PrevOpen = PrevLow; PrevClose = PrevHigh; PrevTime = TimeCurrent(); CurVolume = 1; FileWriteInteger(HstHandle, PrevTime, LONG_VALUE); FileWriteDouble(HstHandle, PrevOpen, DOUBLE_VALUE); FileWriteDouble(HstHandle, PrevLow, DOUBLE_VALUE); FileWriteDouble(HstHandle, PrevHigh, DOUBLE_VALUE); FileWriteDouble(HstHandle, PrevClose, DOUBLE_VALUE); LastFPos = FileTell(HstHandle); // Remeber Last pos in file FileWriteDouble(HstHandle, CurVolume, DOUBLE_VALUE); FileFlush(HstHandle); if(hwnd != 0) { LastTime = TimeCurrent(); PostMessageA(hwnd, WM_COMMAND, 33324, 0); } } else if(Bid <= PrevLow-BoxPoints) { PrevHigh = PrevHigh - BoxPoints; PrevLow = PrevLow - BoxPoints; PrevOpen = PrevHigh; PrevClose = PrevLow; PrevTime = TimeCurrent(); CurVolume = 1; FileWriteInteger(HstHandle, PrevTime, LONG_VALUE); FileWriteDouble(HstHandle, PrevOpen, DOUBLE_VALUE); FileWriteDouble(HstHandle, PrevLow, DOUBLE_VALUE); FileWriteDouble(HstHandle, PrevHigh, DOUBLE_VALUE); FileWriteDouble(HstHandle, PrevClose, DOUBLE_VALUE); LastFPos = FileTell(HstHandle); // Remeber last pos in file FileWriteDouble(HstHandle, CurVolume, DOUBLE_VALUE); FileFlush(HstHandle); if(hwnd != 0) { LastTime = TimeCurrent(); PostMessageA(hwnd, WM_COMMAND, 33324, 0); } } else { CurVolume++; FileSeek(HstHandle, LastFPos, SEEK_SET); // Resore last pos in file then updating volume FileWriteDouble(HstHandle, CurVolume, DOUBLE_VALUE); FileFlush(HstHandle); if(hwnd != 0 && (TimeCurrent()-LastTime) >= 2) { LastTime = TimeCurrent(); PostMessageA(hwnd, WM_COMMAND, 33324, 0); } } } Sleep(50); } if(HstHandle >= 0) FileClose(HstHandle); Comment(""); // remove comment from main chart return(0); } //+------------------------------------------------------------------+