Excel_to_MT4

Author: Copyright � 2008, komposter
Price Data Components
Series array that contains open time of each barSeries array that contains open prices of each barSeries array that contains open prices of each barSeries array that contains close prices for each barSeries array that contains close prices for each barSeries array that contains the highest prices of each barSeries array that contains the highest prices of each barSeries array that contains the lowest prices of each barSeries array that contains the lowest prices of each bar
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
Excel_to_MT4
//+------------------------------------------------------------------+
//|                                              EqualVolumeBars.mq4 |
//|                                      Copyright © 2008, komposter |
//|                                      mailto:komposterius@mail.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, komposter"
#property link      "mailto:komposterius@mail.ru"
#property show_inputs

#include <WinUser32.mqh>

#import "mt4excel.dll"
   bool  ExcelOpenFile(string FileName);//Îòêðûâàåò ôàéë Excel
   double  ExcelGetValueCell(int X,int Y);//Ñ÷èòàòü ÷èñëî èç ÿ÷åéêè
   string ExcelGetLastErrorText();//Òåêñò ïîñëåäíåé îøèáêè Excel
#import
//---- Êîëè÷åñòâî òèêîâ â îäíîì áàðå
extern string ExcelFile="D:\Êíèãà1.xlsx";
extern int		TicksInBar	= 1;

double Open_[],
       Close_[],
       High_[],
       Low_[];
int Time_[];
int HistoryHandle = -1, HistoryFilePos = 0, hwnd = 0, TicksFilePos = 0, now_time;
double now_close, now_open, now_low, now_high, now_volume;
string _Symbol;

int _Period, _PeriodSec, pre_time, last_fpos = 0;
double pre_close;

int init()
{
	int    _GetLastError = 0, cnt_ticks = 0, cnt_bars = 0, temp[13];
	// çàïîìèíàåì ñèìâîë ãðàôèêà, îáíóëÿåì õýíäë îêíà off-line ãðàôèêà
	_Symbol = Symbol();
   hwnd = 0;

	// îòêðûâàåì ôàéë, â êîòîðûé áóäåì çàïèñûâàòü èñòîðèþ
	string file_name = StringConcatenate( "!Eqv", _Symbol, TicksInBar, ".hst" );
	int sd_=iBars("!Eqv"+ _Symbol,TicksInBar)-1;
   double Open_[],
          Close_[],
          High_[],
          Low_[];
   int Time_[];
   ArrayResize(Open_,sd_+1);
   ArrayResize(High_,sd_+1);
   ArrayResize(Low_,sd_+1);
   ArrayResize(Close_,sd_+1);
   ArrayResize(Time_,sd_+1);
	for(int sd=iBars("!Eqv"+ _Symbol,TicksInBar)-1;sd>=0;sd--)
	{
	   Time_[sd]=iTime("!Eqv"+ _Symbol,TicksInBar,sd);
	   Open_[sd]=iOpen("!Eqv"+ _Symbol,TicksInBar,sd);
	   Close_[sd]=iClose("!Eqv"+ _Symbol,TicksInBar,sd);
	   High_[sd]=iHigh("!Eqv"+ _Symbol,TicksInBar,sd);
	   Low_[sd]=iLow("!Eqv"+ _Symbol,TicksInBar,sd);
	   //Print(sd," ",GetLastError()," ",Time_[sd]," ",Low_[sd]," ",High_[sd]," ",Close_[sd]," ",Open_[sd]);
	}
	HistoryHandle = FileOpenHistory( file_name, FILE_BIN | FILE_WRITE );
	if ( HistoryHandle < 0 )
	{
		_GetLastError = GetLastError();
		Alert( "FileOpenHistory( \"", file_name, "\", FILE_BIN | FILE_WRITE )", " - Error #", _GetLastError );
		return(-1);
	}

	//---- Çàïèñûâàåì çàãîëîâîê ôàéëà
	FileWriteInteger	( HistoryHandle, 400, LONG_VALUE );
	FileWriteString	( HistoryHandle, "Copyright © 2008, komposter", 64 );
	FileWriteString	( HistoryHandle, StringConcatenate( "!Eqv", _Symbol ), 12 );
	FileWriteInteger	( HistoryHandle, TicksInBar, LONG_VALUE );
	FileWriteInteger	( HistoryHandle, Digits, LONG_VALUE );
	FileWriteInteger	( HistoryHandle, 0, LONG_VALUE );       //timesign
	FileWriteInteger	( HistoryHandle, 0, LONG_VALUE );       //last_sync
	FileWriteArray		( HistoryHandle, temp, 0, 13 );

   for(int sd1=sd_;sd1>=0;sd1--)
   {
   	FileWriteInteger	( HistoryHandle, Time_[sd1],		LONG_VALUE	);
		FileWriteDouble	( HistoryHandle, Open_[sd1],		DOUBLE_VALUE);
		FileWriteDouble	( HistoryHandle, Low_[sd1],		DOUBLE_VALUE);
		FileWriteDouble	( HistoryHandle, High_[sd1],		DOUBLE_VALUE);
		FileWriteDouble	( HistoryHandle, Close_[sd1],	DOUBLE_VALUE);
		FileWriteDouble	( HistoryHandle, TicksInBar,	DOUBLE_VALUE);
	   FileFlush			( HistoryHandle );
	   //Print(sd1," ",GetLastError()," ",Time_[sd1]," ",Low_[sd1]," ",High_[sd1]," ",Close_[sd1]," ",Open_[sd1]);
   }
   now_time=Time_[0];
	// çàïîìèíàåì ìåñòî â ôàéëå, ïåðåä çàïèñüþ 0-ãî áàðà
	HistoryFilePos = FileTell( HistoryHandle);

	// çàïèñûâàåì 0-é áàð äëÿ îòîáàæåíèÿ íà ãðàôèêå
	FileWriteInteger	( HistoryHandle, now_time,		LONG_VALUE	);
	FileWriteDouble	( HistoryHandle, now_open,		DOUBLE_VALUE);
	FileWriteDouble	( HistoryHandle, now_low,		DOUBLE_VALUE);
	FileWriteDouble	( HistoryHandle, now_high,		DOUBLE_VALUE);
	FileWriteDouble	( HistoryHandle, now_close,	DOUBLE_VALUE);
	FileWriteDouble	( HistoryHandle, now_volume,	DOUBLE_VALUE);
	FileFlush			( HistoryHandle );

	// âûâîäèì ñòàòèñòèêó
	Print( "< - - - Îáðàáîòàíî áàðîâ: ", sd_+1, ", ïîñòðîåíî ïîëíûõ áàðîâ: ", sd_+1, " - - - >" );
	Print( "< - - - Äëÿ ïðîñìîòðà ðåçóëüòàòîâ îòêðîéòå ãðàôèê \"!Eqv", _Symbol, TicksInBar, "\" - - - >" );
	
	// îáíîâëÿåì ãðàôèê
	RefreshWindow();
   ArrayResize(Open_,1);
   ArrayResize(High_,1);
   ArrayResize(Low_,1);
   ArrayResize(Close_,1);
   ArrayResize(Time_,1);

	return(0);
}

int start()
{
   if (ExcelOpenFile(ExcelFile)) 
      Print("Ócïåøíî îòêðûëè Excel"); 
   else
   { 
      Print("Íå îòêðûâàåòñÿ Excel:",ExcelGetLastErrorText());//Îòêðûâàåì Excel        
      //return;
   }
   double Bid_=ExcelGetValueCell(1,1);
   //ExcelGetValueCell(1,1)
   while(!IsStopped())
   {
   RefreshRates();
	if ( HistoryHandle < 0 ) continue;//(-1);
	if(ExcelGetValueCell(1,1)==Bid_)
	{
	   Sleep(11);
	   continue;
	}
   Bid_=ExcelGetValueCell(1,1);
	//+------------------------------------------------------------------+
	//| Îáðàáàòûâàåì ïîñòóïàþùèå òèêè
	//+------------------------------------------------------------------+
	//---- ñòàâèì "êóðñîð" ïåðåä ïîñëåäíèì áàðîì
	FileSeek( HistoryHandle, HistoryFilePos, SEEK_SET );

	now_volume ++;

	// áàð ïðîäîëæàåòñÿ
	if ( now_volume <= TicksInBar )
	{
		if ( Bid_ < now_low  ) now_low  = Bid_;
		if ( Bid_ > now_high ) now_high = Bid_;
		now_close = Bid_;

		// çàïèñûâàåì 0-é áàð äëÿ îòîáàæåíèÿ íà ãðàôèêå
		FileWriteInteger	( HistoryHandle, now_time,		LONG_VALUE	);
		FileWriteDouble	( HistoryHandle, now_open,		DOUBLE_VALUE);
		FileWriteDouble	( HistoryHandle, now_low,		DOUBLE_VALUE);
		FileWriteDouble	( HistoryHandle, now_high,		DOUBLE_VALUE);
		FileWriteDouble	( HistoryHandle, now_close,	DOUBLE_VALUE);
		FileWriteDouble	( HistoryHandle, now_volume,	DOUBLE_VALUE);
		FileFlush			( HistoryHandle );
	}
	// ïðèøåë òèê íîâîãî áàðà
	else
	{
		// çàïèñûâàåì 1-é áàð
		FileWriteInteger	( HistoryHandle, now_time,		LONG_VALUE	);
		FileWriteDouble	( HistoryHandle, now_open,		DOUBLE_VALUE);
		FileWriteDouble	( HistoryHandle, now_low,		DOUBLE_VALUE);
		FileWriteDouble	( HistoryHandle, now_high,		DOUBLE_VALUE);
		FileWriteDouble	( HistoryHandle, now_close,	DOUBLE_VALUE);
		FileWriteDouble	( HistoryHandle, now_volume-1,DOUBLE_VALUE);
		FileFlush			( HistoryHandle );

//		now_time		= TimeCurrent();
		now_time		+= 60;	// âðåìÿ ïîäñòàâëÿåì óñëîâíîå, ÷òîá íå ïîëó÷èëîñü 2 áàðà â îäíó ìèíóòó
		now_open		= Bid_;
		now_low		= Bid_;
		now_high		= Bid_;
		now_close	= Bid_;
		now_volume	= 1;

		// çàïîìèíàåì ìåñòî â ôàéëå, ïåðåä çàïèñüþ 0-ãî áàðà
		HistoryFilePos = FileTell( HistoryHandle);

		// çàïèñûâàåì 0-é áàð
		FileWriteInteger	( HistoryHandle, now_time,		LONG_VALUE	);
		FileWriteDouble	( HistoryHandle, now_open,		DOUBLE_VALUE);
		FileWriteDouble	( HistoryHandle, now_low,		DOUBLE_VALUE);
		FileWriteDouble	( HistoryHandle, now_high,		DOUBLE_VALUE);
		FileWriteDouble	( HistoryHandle, now_close,	DOUBLE_VALUE);
		FileWriteDouble	( HistoryHandle, now_volume,	DOUBLE_VALUE);
		FileFlush			( HistoryHandle );
	}
/*
// Print( "now_time = ", now_time, ", now_open = ", now_open, ", now_low = ", now_low, ", now_high = ", now_high, ", now_close = ", now_close, ", now_volume = ", now_volume );
// Print( "5:  file_pos = ", FileTell( HistoryHandle) );
	// çàïèñûâàåì äëÿ îòîáàæåíèÿ íà ãðàôèêå
	FileWriteInteger	( HistoryHandle, now_time,		LONG_VALUE	);
	FileWriteDouble	( HistoryHandle, now_open,		DOUBLE_VALUE);
	FileWriteDouble	( HistoryHandle, now_low,		DOUBLE_VALUE);
	FileWriteDouble	( HistoryHandle, now_high,		DOUBLE_VALUE);
	FileWriteDouble	( HistoryHandle, now_close,	DOUBLE_VALUE);
	FileWriteDouble	( HistoryHandle, now_volume,	DOUBLE_VALUE);
	FileFlush			( HistoryHandle );

// Print( "6:  file_pos = ", FileTell( HistoryHandle) );
*/
	// îáíîâëÿåì ãðàôèê
	RefreshWindow();
	Sleep(100);
	}
	deinit();
}

void RefreshWindow()
{
	//---- íàõîäèì îêíî, â êîòîðîå áóäåì "îòïðàâëÿòü" ñâåæèå êîòèðîâêè
	if ( hwnd == 0 )
	{
		hwnd = WindowHandle( StringConcatenate( "!Eqv", _Symbol ), TicksInBar );
		if ( hwnd != 0 ) { Print( "< - - - Ãðàôèê ", "!Eqv", _Symbol, TicksInBar, " íàéäåí! - - - >" ); }
	}
	//---- è, åñëè íàøëè, îáíîâëÿåì åãî
	if ( hwnd != 0 ) { PostMessageA( hwnd, WM_COMMAND, 33324, 0 ); }
}

int deinit()
{
	if ( HistoryHandle > 0 )
	{
		//---- çàêðûâàåì ôàéë
		FileClose( HistoryHandle );
		HistoryHandle = -1;
	}
	return(0);
}

string strMonth( int m )
{
	if ( m < 10 ) return( StringConcatenate( "0", m ) );
	return(m);
}

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 ---