RenkoLiveChart_v3.0

Author:
RenkoLiveChart_v3.0
Miscellaneous
It writes information to fileIt writes information to fileIt writes information to fileIt writes information to file
0 Views
0 Downloads
0 Favorites
RenkoLiveChart_v3.0
//+---------------------------------------------------------------------------+
//|   EA VERSION
//|   RenkoLiveChart_v3.0.mq4
//|   Inspired from Renko script by "e4" (renko_live_scr.mq4)
//|   Copyleft 2009 LastViking
//|     
//|   Sep 2009 - Optional wicks added
//|            - Conversion back to EA 
//|            - Auto adjust for 5 and 6 dec brokers added
//|               enter RenkoBoxSize as "actual" size e.g. "10" for 10 pips
//|            - compensation for "zero compare" problem added
//+---------------------------------------------------------------------------+
#property copyright "" 
//+------------------------------------------------------------------+
#include <WinUser32.mqh>
#include <stdlib.mqh>
//+------------------------------------------------------------------+
extern int RenkoBoxSize = 10;
extern int BoxOffset = 0;
extern int RenkoTimeFrame = 2;      // What time frame to use for the offline renko chart
extern bool StrangeSymbolName = false;
extern bool ShowWicks = true;
int BoxSize; 
//+------------------------------------------------------------------+
int HstHandle = -1;
string SymbolName;
//+------------------------------------------------------------------+
void UpdateChartWindow() {
	static int hwnd = 0;
 
	if(hwnd == 0) {
		hwnd = WindowHandle(SymbolName, RenkoTimeFrame);
		if(hwnd != 0) Print("Chart window detected");
	}
	if(hwnd != 0) if(PostMessageA(hwnd, WM_COMMAND, 0x822c, 0) == 0) hwnd = 0;
}
int init()  {

   BoxSize = RenkoBoxSize;
   if (Digits == 5)
   {BoxSize = BoxSize*10;}
   if (Digits == 6)
   {BoxSize = BoxSize*100;}

   HstHandle = -1;
   
   return(0);
}   
//+------------------------------------------------------------------+
int start() {

	static int LastFPos = 0;
	static double BoxPoints, PrevLow, PrevHigh, PrevOpen, PrevClose, CurVolume, CurHigh, CurLow;
	static double CurOpen, CurClose;
	static datetime PrevTime;

   static double UpWick = 0;	
   static double DnWick = EMPTY_VALUE;
   	
	if(HstHandle < 0) {
		// Init

		// Error checking	
		if(!IsConnected()) {
			Print("Waiting for connection...");
			return(0);
		}							
		if(!IsDllsAllowed()) {
			Print("Error: Dll calls must be allowed!");
			return(-1);
		}		
		if(MathAbs(BoxOffset) >= BoxSize) {
			Print("Error: |BoxOffset| should be less then BoxSize!");
			return(-1);
		}
		switch(RenkoTimeFrame) {
		case 1: case 5: case 15: case 30: case 60: case 240:
		case 1440: case 10080: case 43200: case 0:
			Print("Error: Invald time frame used for offline renko chart (RenkoTimeFrame)!");
			return(-1);
		}
		//
		
		if(StrangeSymbolName) SymbolName = StringSubstr(Symbol(), 0, 6);
		else SymbolName = Symbol();
   		BoxPoints = NormalizeDouble(BoxSize*Point, Digits);
   		PrevLow = NormalizeDouble(BoxOffset*Point + MathFloor(Close[Bars-1]/BoxPoints)*BoxPoints, Digits);
   		PrevHigh = PrevLow + BoxPoints;
   		PrevOpen = PrevLow;
   		PrevClose = PrevHigh;
		CurVolume = 1;
		PrevTime = Time[Bars-1];
	
		// create / open hst file		
   		HstHandle = FileOpenHistory(SymbolName + RenkoTimeFrame + ".hst", FILE_BIN|FILE_WRITE);
   		if(HstHandle < 0) return(-1);
   		//
   	
		// write hst file header
		int HstUnused[13];
   		FileWriteInteger(HstHandle, 400, LONG_VALUE); 			// Version
   		FileWriteString(HstHandle, "", 64);					// Copyright
   		FileWriteString(HstHandle, SymbolName, 12);			// Symbol
   		FileWriteInteger(HstHandle, RenkoTimeFrame, LONG_VALUE);	// Period
   		FileWriteInteger(HstHandle, Digits, LONG_VALUE);		// Digits
   		FileWriteInteger(HstHandle, 0, LONG_VALUE);			// Time Sign
   		FileWriteInteger(HstHandle, 0, LONG_VALUE);			// Last Sync
   		FileWriteArray(HstHandle, HstUnused, 0, 13);			// Unused
   		//
   	
 		// process historical data
  		int i = Bars-2;
//---------------------------------------------------------------------------
  		while(i >= 0) {
  		
			CurVolume += Volume[i];
		
         UpWick = MathMax(UpWick,High[i]);
         DnWick = MathMin(DnWick,Low[i]);

			bool UpTrend = High[i]+Low[i] > PrevHigh+PrevLow;
			// update low before high or the revers depending on is closest to prev. renko bar
		
			while(UpTrend && (Low[i] < PrevLow-BoxPoints || CompareDoubles(Low[i],PrevLow-BoxPoints))) {
  				PrevHigh = PrevHigh - BoxPoints;
  				PrevLow = PrevLow - BoxPoints;
  				PrevOpen = PrevHigh;
  				PrevClose = PrevLow;

				FileWriteInteger(HstHandle, PrevTime, LONG_VALUE);
				FileWriteDouble(HstHandle, PrevOpen, DOUBLE_VALUE);
				FileWriteDouble(HstHandle, PrevLow, DOUBLE_VALUE);

            if (ShowWicks && UpWick > PrevHigh)
				{FileWriteDouble(HstHandle, UpWick, DOUBLE_VALUE);}
				else
				{FileWriteDouble(HstHandle, PrevHigh, DOUBLE_VALUE);}
            UpWick = 0;
            DnWick = EMPTY_VALUE;
  				  			
				FileWriteDouble(HstHandle, PrevClose, DOUBLE_VALUE);
				FileWriteDouble(HstHandle, CurVolume, DOUBLE_VALUE);

            CurVolume = 0;
				
				if(PrevTime < Time[i]) PrevTime = Time[i];
				else PrevTime++;
			}
		
			while(UpTrend && (High[i] > PrevHigh+BoxPoints || CompareDoubles(High[i],PrevHigh+BoxPoints))) {
  				PrevHigh = PrevHigh + BoxPoints;
  				PrevLow = PrevLow + BoxPoints;
  				PrevOpen = PrevLow;
  				PrevClose = PrevHigh;
  			
				FileWriteInteger(HstHandle, PrevTime, LONG_VALUE);
				FileWriteDouble(HstHandle, PrevOpen, DOUBLE_VALUE);

            if (ShowWicks && DnWick < PrevLow)
				{FileWriteDouble(HstHandle, DnWick, DOUBLE_VALUE);}
				else
				{FileWriteDouble(HstHandle, PrevLow, DOUBLE_VALUE);}
            UpWick = 0;
            DnWick = EMPTY_VALUE;
  				  			
				FileWriteDouble(HstHandle, PrevHigh, DOUBLE_VALUE);
				FileWriteDouble(HstHandle, PrevClose, DOUBLE_VALUE);
				FileWriteDouble(HstHandle, CurVolume, DOUBLE_VALUE);

            CurVolume = 0;
				
				if(PrevTime < Time[i]) PrevTime = Time[i];
				else PrevTime++;
			}
		
			while(!UpTrend && (Low[i] < PrevLow-BoxPoints || CompareDoubles(Low[i],PrevLow-BoxPoints))) {
  				PrevHigh = PrevHigh - BoxPoints;
  				PrevLow = PrevLow - BoxPoints;
  				PrevOpen = PrevHigh;
  				PrevClose = PrevLow;
  			
				FileWriteInteger(HstHandle, PrevTime, LONG_VALUE);
				FileWriteDouble(HstHandle, PrevOpen, DOUBLE_VALUE);
				FileWriteDouble(HstHandle, PrevLow, DOUBLE_VALUE);

            if (ShowWicks && UpWick > PrevHigh)
				{FileWriteDouble(HstHandle, UpWick, DOUBLE_VALUE);}
				else
				{FileWriteDouble(HstHandle, PrevHigh, DOUBLE_VALUE);}
            UpWick = 0;
            DnWick = EMPTY_VALUE;
  				  			
				FileWriteDouble(HstHandle, PrevClose, DOUBLE_VALUE);
				FileWriteDouble(HstHandle, CurVolume, DOUBLE_VALUE);
				
            CurVolume = 0;
				
				if(PrevTime < Time[i]) PrevTime = Time[i];
				else PrevTime++;
			}		
			i--;
		} 
		FileFlush(HstHandle);

   	LastFPos = FileTell(HstHandle);   // Remember Last pos in file			

		//
			
		Comment("RenkoLiveChart(" + BoxSize + "): Open Offline ", SymbolName, ",M", RenkoTimeFrame, " to view chart");
		
      CurOpen = PrevClose;
      if (UpWick > PrevHigh)
      {CurHigh = UpWick;}
      else
      {CurHigh = PrevHigh;}
      if (DnWick < PrevLow)
      {CurLow = DnWick;}
      else
      {CurLow = PrevLow;}
      CurClose = Close[0];
      
		FileWriteInteger(HstHandle, PrevTime, LONG_VALUE);		// Time
		FileWriteDouble(HstHandle, CurOpen, DOUBLE_VALUE);         	// Open
		FileWriteDouble(HstHandle, CurLow, DOUBLE_VALUE);		// Low
		FileWriteDouble(HstHandle, CurHigh, DOUBLE_VALUE);		// High
		FileWriteDouble(HstHandle, CurClose, DOUBLE_VALUE);		// Close
		FileWriteDouble(HstHandle, CurVolume, DOUBLE_VALUE);			// Volume				
            FileFlush(HstHandle);
            
      UpdateChartWindow();
		
		return(0);
 		// End historical data / Init		
	} 		
//----------------------------------------------------------------------------
 	// HstHandle not < 0 so we always enter here after history done
 	// Begin live data feed
   //
   			
      UpWick = MathMax(UpWick,Bid);
      DnWick = MathMin(DnWick,Bid);

		CurVolume++;   			
		FileSeek(HstHandle, LastFPos, SEEK_SET);

 	// up box	   				
 	//-------------------------------------------------------------------------	   				
   	if(Bid > PrevHigh+BoxPoints || CompareDoubles(Bid,PrevHigh+BoxPoints)) {
     	PrevHigh = PrevHigh + BoxPoints;
		PrevLow = PrevLow + BoxPoints;
  		PrevOpen = PrevLow;
  		PrevClose = PrevHigh;
  				  			
		FileWriteInteger(HstHandle, PrevTime, LONG_VALUE);
		FileWriteDouble(HstHandle, PrevOpen, DOUBLE_VALUE);

      if (ShowWicks && DnWick < PrevLow)
		{FileWriteDouble(HstHandle, DnWick, DOUBLE_VALUE);}
		else
		{FileWriteDouble(HstHandle, PrevLow, DOUBLE_VALUE);}
      UpWick = 0;
      DnWick = EMPTY_VALUE;
		  			
		FileWriteDouble(HstHandle, PrevHigh, DOUBLE_VALUE);
		FileWriteDouble(HstHandle, PrevClose, DOUBLE_VALUE);
		FileWriteDouble(HstHandle, CurVolume, DOUBLE_VALUE);
      	FileFlush(HstHandle);

  	  	LastFPos = FileTell(HstHandle);   // Remeber Last pos in file				  							
      	
		if(PrevTime < TimeCurrent()) PrevTime = TimeCurrent();
		else PrevTime++;
            		
  		CurVolume = 0;
      CurOpen = PrevClose;
		CurHigh = PrevHigh;
		CurLow = PrevHigh;  
		
		UpdateChartWindow();				            		
  	}

 	// down box
 	//-------------------------------------------------------------------------	   				
	else if(Bid < PrevLow-BoxPoints || CompareDoubles(Bid,PrevLow-BoxPoints)) {

  		PrevHigh = PrevHigh - BoxPoints;
  		PrevLow = PrevLow - BoxPoints;
  		PrevOpen = PrevHigh;
  		PrevClose = PrevLow;
  				  			
		FileWriteInteger(HstHandle, PrevTime, LONG_VALUE);
		FileWriteDouble(HstHandle, PrevOpen, DOUBLE_VALUE);
		FileWriteDouble(HstHandle, PrevLow, DOUBLE_VALUE);

      if (ShowWicks && UpWick > PrevHigh)
		{FileWriteDouble(HstHandle, UpWick, DOUBLE_VALUE);}
		else
		{FileWriteDouble(HstHandle, PrevHigh, DOUBLE_VALUE);}
      UpWick = 0;
      DnWick = EMPTY_VALUE;
		  			
		FileWriteDouble(HstHandle, PrevClose, DOUBLE_VALUE);
		FileWriteDouble(HstHandle, CurVolume, DOUBLE_VALUE);
      	FileFlush(HstHandle);

  	  	LastFPos = FileTell(HstHandle);   // Remeber Last pos in file				  							
      	
		if(PrevTime < TimeCurrent()) PrevTime = TimeCurrent();
		else PrevTime++;      	
            		
  		CurVolume = 0;
      CurOpen = PrevClose;
		CurHigh = PrevLow;
		CurLow = PrevLow;  
		
		UpdateChartWindow();						
     	} 
	else {
   // no box - high/low not hit				
 	//-------------------------------------------------------------------------	   				
		
		if(Bid > CurHigh) CurHigh = Bid;
		if(Bid < CurLow) CurLow = Bid;
		
		CurClose = Bid;
		
		FileWriteInteger(HstHandle, PrevTime, LONG_VALUE);		// Time
		FileWriteDouble(HstHandle, CurOpen, DOUBLE_VALUE);         	// Open
		FileWriteDouble(HstHandle, CurLow, DOUBLE_VALUE);		// Low
		FileWriteDouble(HstHandle, CurHigh, DOUBLE_VALUE);		// High
		FileWriteDouble(HstHandle, CurClose, DOUBLE_VALUE);		// Close
		FileWriteDouble(HstHandle, CurVolume, DOUBLE_VALUE);			// Volume				
            FileFlush(HstHandle);
            
		UpdateChartWindow();            
     	}
     	return(0);
}
//+------------------------------------------------------------------+
int deinit() {
	if(HstHandle >= 0) {
		FileClose(HstHandle);
		HstHandle = -1;
	}
   	Comment("");
	return(0);
}
//+------------------------------------------------------------------+
   

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---