TickSave-01-002-001

Author: Copyright © 2006, komposter + © 2015, KirillovYV
Miscellaneous
It issuies visual alerts to the screenUses files from the file systemIt writes information to file
0 Views
0 Downloads
0 Favorites
TickSave-01-002-001
ÿþ//+------------------------------------------------------------------+

//|                                                  TickSave-01.mq4 |

//|                 Copyright © 2006, komposter + © 2015, KirillovYV |

//|                                      mailto:KirillovYV@Gmail.Com |

//+------------------------------------------------------------------+

#property strict

#property copyright "Copyright © 2006, komposter + © 2015, KirillovYV"

#property link      "mailto:KirillovYV@Gmail.Com"

extern string	SymbolList		= "EURUSD/USDJPY";

extern bool		WriteWarnings  = false;



string 

   SymbolsArray[32], 

   strComment,

   ServerName; 

double 

   preBid[32];

int 

   TickCount,

	CurYear,

	CurMonth,

   FileHandle[32], 

   SymbolsCount=0;

#define strEXPERT_WAS_STOPED  "--------------------------Expert was stoped"

#define strCONNECTION_LOST    "--------------------------Connection lost  "

#define strLEN						45

bool 

   preIsConnected=true, 

   nowIsConnected=true;

//+-----------------------

// OnInit()

//+-----------------------

int OnInit() 

{ 

	CurYear=Year();

	CurMonth=Month();

   ServerName=AccountServer(); 

   // 72;5:05< 87 AB@>:8 SymbolList A?8A>: A8<2>;>2

	{if (!PrepareSymbolList()) 

	{

	   return(INIT_FAILED);

	}}//if

	// @>25@O5< A>AB>O=85 A>548=5=8O

   {if (!ConnectionCheck()) 

   {

      return(INIT_FAILED);

   }}//if

	// B:@K205< D09;K

	{if (!OpenFiles()) 

	{

	   return(INIT_FAILED);  

   }}//if

   return(INIT_SUCCEEDED); 

}//int OnInit()

//

//+-----------------------

// OnDeInit()

//+-----------------------

void OnDeinit(const int reason) 

{ 

	// 0:@K205< D09;K

   //Sleep(100);

	CloseFiles();

	//Comment("\nreason=",reason);

   Sleep(100);

   return; 

}//int OnDeinit()

//

//+-----------------------

// OnTick()

//+-----------------------

void OnTick()

{

	if (!IsStopped())

	{

		// A;8 ?><5=O;AO A5@25@, A@07C ?@5:@0I05< 70?8AL - 4;O =>2>3> A5@25@0 1C45B A>740=0 A2>O ?0?:0

		{if (ServerName!=AccountServer()) 

		{

		   return;

		}}//if

		// A;8 ?><5=O;AO 3>4, A@07C ?@5:@0I05< 70?8AL - 4;O =>2>3> 3>40 1C45B A>740= A2>9 D09;

		{if (CurYear!=Year()) 

		{

		   return;

		}}//if

		// A;8 ?><5=O;AO <5AOF, A@07C ?@5:@0I05< 70?8AL - 4;O =>2>3> <5AOF0 1C45B A>740= A2>9 D09;

		{if (CurMonth!=Month()) 

		{

		   return;

		}}//if

		// A;8 ?@>?0;0 A2O7L,

		{if (!IsConnected())

		{

			// 70?8AK205< ?@54C?@5645=85 2> 2A5 D09;K

			WriteConnectionLost();

			preIsConnected = false;

		}else{

		   // A;8 A2O7L 5ABL,

			// 0?8AK205< ?>ABC?82H85 B8:8

			WriteTick();

			preIsConnected = true;

			TickCount++;

		}}//if

		Sleep(1);

	}

   //Print("TickCount=",TickCount);

	//Comment("TickCount=",TickCount);

}//int OnTick()

//

//+------------------------------------------------------------------+

//| 72;5:05< 87 AB@>:8 SymbolList A?8A>: A8<2>;>2

//+------------------------------------------------------------------+

bool PrepareSymbolList()

{

ushort

   curchar=0;

int

   len=StringLen(SymbolList),

   curSymbol;

string

	cur_symbol=""; 

	SymbolsCount=0;

	//#AB0=02;8205< @07<5@ <0AA820 A8<2>;>2 ?> C<>;G0=8N

	ArrayResize(SymbolsArray,32);

	//5@518@05< 2A5 A8<2>;K 2 AB@>:5 A> A?8A:>< 3@0D8:>2

	{for (int pos=0;pos<=len;pos++)

	{

		curchar=StringGetCharacter(SymbolList,pos);

   	//Print("pos=",pos," curchar (code)=",CharToString(uchar(curchar))," (",curchar,")");

		//A;8 B5:CI89 A8<2>; - :>A0O 8;8 ?>A;54=89 A8<2>; 2 AB@>:5,

		{if ((curchar=='/')||(curchar==0)||(pos>=len))

		{

			// ?5@5<5==>9 cur_symbol - ?>;=>5 8<O A8<2>;0. @>25@O5< 53>:

			ResetLastError();

			MarketInfo(cur_symbol,MODE_BID);

			{if (GetLastError()==4106)

			{

				Alert("58725AB=K9 A8<2>; <<",cur_symbol,">> !!!");

				return(false);

			}}//if (GetLastError()==4106)

			//A;8 A8<2>; @50;L=> ACI5AB2C5B, ?@>25@O5<, =5B ;8 B0:>3> A8<>;0 2 =0H5< A?8A:5:

			bool Uniq = true;

			{for (curSymbol=0;curSymbol<SymbolsCount;curSymbol++)

			{

				{if (cur_symbol==SymbolsArray[curSymbol])

				{

					Uniq = false;

					break;

				}}//if (cur_symbol==SymbolsArray[curSymbol])

			}}//for (curSymbol=0;curSymbol<SymbolsCount;curSymbol++)

			//A;8 B0:>3> A8<>;0 2 =0H5< A?8A:5 =5B, 70?8AK205< 53>, 8 AG8B05< >1I55 :>;8G5AB2>:

			{if (Uniq)

			{

				SymbolsArray[SymbolsCount]=cur_symbol;

				SymbolsCount++;

				{if (SymbolsCount>31)

				{

					Alert( "!;8H:>< <=>3> A8<2>;>2! B:@KBL <>6=> <0:A8<C< 32 D09;0!" );

					return(false);

				}}//if (SymbolsCount>31)

			}}//if (Uniq)

			//1=C;O5< 7=0G5=85 B5:CI53> A8<2>;0

			cur_symbol="";

		}else{

		   //1@010BK205< >G5@54=>9 A8<2>; 8=AB@C<5=B0 3@0D8:0

			cur_symbol=cur_symbol+CharToString(uchar(curchar));

			//Print(" cur_symbol=",cur_symbol);

		}}//if ((curchar=='/')||(curchar==0)||(pos>=len)) -else 

	}}//for (int pos=0;pos<=len;pos++)

	//---- 5A;8 =8 >48= A8<2>; =5 1K; =0945=, 2KE>48<

	{if (SymbolsCount<=0)

	{

		Alert("5 >?@545;5=> =8 >4=>3> A8<2>;0!!!");

		return(false);

	}}//if (SymbolsCount<=0)

	//---- CAB0=02;8205< @07<5@ 2A5E <0AA82>2 ?>4 :>;-2> A8<2>;>2:

	ArrayResize		(SymbolsArray, SymbolsCount);

	ArrayResize		(preBid		, SymbolsCount );

	ArrayInitialize(preBid		, -1 				);

	ArrayResize		(FileHandle	, SymbolsCount );

	ArrayInitialize(FileHandle	, -1 				);

	//---- K2>48< 8=D>@<0F8N:

	string uniq_symbols_list=SymbolsArray[0];

	{for (curSymbol=1;curSymbol<SymbolsCount;curSymbol++)

	{

		{if (curSymbol==SymbolsCount-1)

		{ 

		   uniq_symbols_list=uniq_symbols_list+" 8 "+SymbolsArray[curSymbol];

		}else{

		   uniq_symbols_list=uniq_symbols_list+", "+SymbolsArray[curSymbol]; 

		}}//if (curSymbol==SymbolsCount-1)

	}}//for (curSymbol=1; curSymbol<SymbolsCount; curSymbol++)

	strComment=StringConcatenate(AccountServer(),": >1@010BK205BAO ",SymbolsCount," A8<2>;(-0,->2):\n",uniq_symbols_list,"\n" );

	Comment(strComment);

	return(true);

}//bool PrepareSymbolList()

//

//+------------------------------------------------------------------+

//| @>25@O5< A>AB>O=85 A>548=5=8O

//+------------------------------------------------------------------+

bool ConnectionCheck()

{

	while (!IsConnected())

	{

		Comment(AccountServer(),": " !/ ! !  !!!");

		if (IsStopped()) {return(false);}

		Sleep(10);

	}

	return(true);

}//bool ConnectionCheck()

//

//+------------------------------------------------------------------+

//| B:@K205< D09;K, 2 :>B>@K5 1C45< 70?8AK20BL B8:8

//+------------------------------------------------------------------+

bool OpenFiles()

{

	int _GetLastError;

	for (int curSymbol=0;curSymbol<SymbolsCount;curSymbol++)

	{

		string FileName=StringConcatenate("[Ticks]\\",AccountServer(),"\\",SymbolsArray[curSymbol],"_",Year(),".",strMonth(),".csv");

		FileHandle[curSymbol]=FileOpen(FileName,FILE_READ|FILE_WRITE);

		{if (FileHandle[curSymbol]<0)

		{

			_GetLastError=GetLastError();

			Alert( "FileOpen("+FileName+", FILE_READ|FILE_WRITE) - Error #",_GetLastError);

			return(false);

		}}//if

		{if (!FileSeek(FileHandle[curSymbol],0,SEEK_END))

		{

			_GetLastError=GetLastError();

			Alert("FileSeek("+IntegerToString(FileHandle[curSymbol])+",0,SEEK_END) - Error #",_GetLastError );

			return(false);

		}}//if

		{if (WriteWarnings)

		{

			if (FileWrite(FileHandle[curSymbol],strEXPERT_WAS_STOPED)<=0)

			{

				_GetLastError = GetLastError();

				Alert( "Ticks("+Symbol()+") - FileWrite() Error #",_GetLastError);

				return(false);

			}

			FileFlush(FileHandle[curSymbol]);

		}}//if

		preBid[curSymbol]=MarketInfo(SymbolsArray[curSymbol],MODE_BID);

	}

	return(true);

}//bool OpenFiles()

//

//+------------------------------------------------------------------+

//| WriteConnectionLost() - A;8 ?@>?0;0 A2O7L, 70?8AK205< ?@54C?@5645=8O 2 D09;K

//+------------------------------------------------------------------+

int WriteConnectionLost()

{

int 

	_GetLastError;

	{if (!preIsConnected)

	{ 

	   return(0);

   }}//if

	Comment(strComment,"" !/ ! !  !!!");

	{if (!WriteWarnings) 

	{ 

	   return(0); 

	}}//if

	{for (int curSymbol=0;curSymbol<SymbolsCount;curSymbol++)

	{

		{if (FileHandle[curSymbol]<0)

		{

		   continue;

		}}//if

		{if (!FileSeek(FileHandle[curSymbol],-strLEN,SEEK_END))

		{

			_GetLastError=GetLastError();

			Alert("FileSeek("+IntegerToString(FileHandle[curSymbol])+",-strLEN,SEEK_END) - Error #",_GetLastError );

			continue;

		}}//if

		{if (FileWrite(FileHandle[curSymbol],strCONNECTION_LOST)<=0)

		{

			_GetLastError=GetLastError();

			Alert("FileWrite() Error #",_GetLastError);

		}}//if

		{if (FileWrite(FileHandle[curSymbol],strEXPERT_WAS_STOPED)<=0)

		{

			_GetLastError=GetLastError();

			Alert("FileWrite() Error #",_GetLastError);

		}}//if

		FileFlush(FileHandle[curSymbol]);

	}}//for

   return(1);

}//void WriteConnectionLost()

//

//+------------------------------------------------------------------+

//| WriteTick() - 0?8AK205< ?>ABC?82H85 B8:8

//+------------------------------------------------------------------+

void WriteTick()

{

int 

   _GetLastError; 

double 

   curBid; 

int 

   curDigits;

//----

   Print("strComment=",strComment);

	Comment(strComment);

	{for (int curSymbol=0;curSymbol<SymbolsCount;curSymbol++)

	{

		{if (FileHandle[curSymbol]<0) 

		{ 

		   continue; 

		}}//if

		curBid=MarketInfo(SymbolsArray[curSymbol],MODE_BID);

		curDigits=int(MarketInfo(SymbolsArray[curSymbol],MODE_DIGITS)+0.5);

		{if ((NormalizeDouble(curBid-preBid[curSymbol],curDigits)<0.00000001)

		     &&(NormalizeDouble(preBid[curSymbol]-curBid,curDigits)<0.00000001))

		{

		   continue;

		}}//if

		preBid[curSymbol]=curBid;

		{if (WriteWarnings)

		{

			//5@5<5I05< D09;>2K9 C:070B5;L =0 :>=5F D09;0

			{if (!FileSeek(FileHandle[curSymbol],-strLEN,SEEK_END))

			{

				_GetLastError=GetLastError();

				Alert("FileSeek("+IntegerToString(FileHandle[curSymbol])+",-strLEN,SEEK_END) - Error #",_GetLastError);

				continue;

			}}//if (!FileSeek(FileHandle[curSymbol],-strLEN,SEEK_END))

		}else{

			//5@5<5I05< D09;>2K9 C:070B5;L =0 :>=5F D09;0

			{if (!FileSeek(FileHandle[curSymbol],0,SEEK_END))

			{

				_GetLastError=GetLastError();

				Alert("FileSeek("+IntegerToString(FileHandle[curSymbol])+",0,SEEK_END) - Error #",_GetLastError);

				continue;

			}}//if (!FileSeek(FileHandle[curSymbol],0,SEEK_END))

		}}//if (WriteWarnings) -else

   	//0?8AK205< AB@>:C 7=0G5=89 2 D09;

		{if (FileWrite(FileHandle[curSymbol],TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS),DoubleToStr(curBid,curDigits))<=0)

		{

			_GetLastError=GetLastError();

			Alert("FileWrite() Error #",_GetLastError);

		}}//if

		{if (WriteWarnings)

		{

			{if (FileWrite(FileHandle[curSymbol],strEXPERT_WAS_STOPED)<=0)

			{

				_GetLastError=GetLastError();

				Alert("FileWrite() Error #",_GetLastError);

			}}//if (FileWrite

		}}//if (WriteWarnings)

		FileFlush(FileHandle[curSymbol]);

	}}//for (int curSymbol=0;curSymbol<SymbolsCount;curSymbol++)

}//void WriteTick()

//

//+------------------------------------------------------------------+

//| CloseFiles() - 0:@K205< 2A5 D09;K

//+------------------------------------------------------------------+

void CloseFiles()

{

	{for (int curSymbol=0;curSymbol<SymbolsCount;curSymbol++)

	{

		{if (FileHandle[curSymbol]>0)

		{

         Print("Close>>",curSymbol);

	      //Comment("Close>>",curSymbol);

			FileClose(FileHandle[curSymbol]);

			FileHandle[curSymbol]=-1;

		}}//if (FileHandle[curSymbol]>0)

	}}//for (int curSymbol=0;curSymbol<SymbolsCount;curSymbol++)

}//void CloseFiles()

//

//+-----------------------

// strMonth()

//+-----------------------

string strMonth()

{

	{if (Month()<10) 

	{

	   return(StringConcatenate("0",Month()));

	}}//if

	return(IntegerToString(Month()));

}//string strMonth()

//

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