sandyer7-30-anysecond

Author: Owner
Miscellaneous
It writes information to file
0 Views
0 Downloads
0 Favorites
sandyer7-30-anysecond
//+------------------------------------------------------------------+
//|                                          SandyEr7-Any-Second.mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Owner"
#property link      "Owner89@bk.ru"

#property indicator_chart_window

#import "user32.dll"
            int  RegisterWindowMessageA(string lpString);
  

  
#include <stdlib.mqh>
#include <WinUser32.mqh>

extern      int seconds       = 35;
extern      int price_source  = 1;
         string symbol_name   = "default";
            
            int MT4InternalMsg;
            
           bool initw;
            int error                  =                  -1;
            int hand1e                 =                  -1;
            int fpos                   =                  -1;
            int digit;
         string symbol;
      
           bool flag = true;
         string gvar;                   
         string fp;     
         string sn;  
       
            int last = -1;
            
            int     MS;
       datetime     DATE;
         double     AQ,BQ; 
         
         double     wRO,wRL,wRH,wRC;
            int     t=0;   
           bool     mode=true;
           bool     ask;   
  
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators

symbol = Symbol();


if(StringFind(symbol,"tick",0) >= 0)
  {
  symbol = StringSubstr(symbol, 4);  
  mode = true;
  }
  else{
  mode = false;
  flag = false;
  Print("out: need tick chart");
  return(-1);
  }
RefreshRates();  
digit  = Digits ;
//----
gvar = (symbol+"eR:"+seconds);
     
if(GlobalVariableCheck(gvar) == false)
  {
  initw = true;
  GlobalVariableSet(gvar,1.0);   
  }
  else{
  Print("out: second copy");
  initw = false;
  flag = false;
  return(-1);
  }
//----

if(symbol_name == "default")
  {
  sn = StringConcatenate("any", symbol) ;
  fp = StringConcatenate(sn, seconds,  ".hst") ;
  }
  else{
  sn = symbol_name ;
  fp = StringConcatenate(symbol_name, seconds, ".hst") ;  
  }
//----  

  hand1e = FileOpenHistory(fp, FILE_BIN|FILE_WRITE);
   error = GetLastError();
  if(hand1e<0)
    {
    Print("out: error(",error,"): ",ErrorDescription(error));
    flag = false;
    }
    else{
    int unused[13];
	 FileWriteInteger(hand1e, 400, LONG_VALUE); 			 
	 FileWriteString (hand1e, "", 64);				 
	 FileWriteString (hand1e, sn, 12); 
	 FileWriteInteger(hand1e, seconds, LONG_VALUE);	 
	 FileWriteInteger(hand1e, digit, LONG_VALUE);		 
	 FileWriteInteger(hand1e, 0, LONG_VALUE);		  
	 FileWriteInteger(hand1e, 0, LONG_VALUE);		 
	 FileWriteArray  (hand1e, unused, 0, 13);
	 FileFlush       (hand1e);
	 
	 if(FileSeek(hand1e, 0, SEEK_END) == false)
	   { 
	   error = GetLastError();
	   Print("out: error(",error,"): ",ErrorDescription(error));
	   flag = false;
	   }
	   else{
	   fpos = FileTell(hand1e);  
	   }
    }  
    
 for(int a = (Bars -1); a >= 0; a --)
   {
   if(mode == true)
     {
     DATE = Volume[a];
     }
     else{
     DATE = Time[a];
     }
   BQ   = Low   [a];
   AQ   = High  [a];   
  
   ask = isNewCell();
   if(ask == true)
     {  
     onNewCell(); 
     }
     else{
     onNonCell();
     }
 
   }   

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
if(initw == true)
  {
  GlobalVariableDel(gvar);  
  }
if(hand1e >0)
  {
  FileClose(hand1e);
  }
error = UninitializeReason(); 
string temp;
 
switch(error)
      {
      case 0: temp = "REASON_independently"; break;
      case 1: temp = "REASON_REMOVE"; break;
      case 2: temp = "REASON_RECOMPILE"; break;
      case 3: temp = "REASON_CHARTCHANGE"; break;
      case 4: temp = "REASON_CHARTCLOSE"; break;
      case 5: temp = "REASON_PARAMETERS"; break;
      case 6: temp = "REASON_ACCOUNT"; break;
      }
Print(StringConcatenate("out: ",temp));     
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{   
if(flag == true)
  {

  if(mode == true)
    {
    DATE = Volume[0];
    }
    else{
    DATE = Time[0];
    }
  BQ = Low  [0];
  AQ = High [0];   
  
  ask = isNewCell();
  if(ask == true)
    {  
    onNewCell(); 
    }
    else{
    onNonCell();
    }
  }   
//----
return(0);
}
//+------------------------------------------------------------------+
void onNonCell()
{
if((getQuote()< wRL) || (wRL==0))
  {
  wRL=getQuote();
  }
if((getQuote()> wRH) || (wRH==0))
  {
  wRH=getQuote();
  } 

wRC = getQuote();
MS ++;

return;
}

void onNewCell()
{
//--- default mt chart filtre 

if(wRC <=0)
  {
  wRC = wRO;
  }
if((wRH < wRC) || (wRH == 0))
  {
  wRH = wRC;
  }  
if((wRH < wRO) || (wRH == 0))
  {
  wRH = wRO;
  }   
if((wRL > wRO) || (wRL == 0))
  {
  wRL = wRO;
  }
if(MS <= 0)
  {
  MS = 1;
  }   
//---                             
writeHSTcell(hand1e, t, wRO, wRL, wRH, wRC,  MS ); 
wRO = getQuote(); 
  t = (DATE / seconds);             
  t*= seconds; 
//---
wRH=0;
wRL=0;
wRC=0;
 MS=0;


UpdateChartWindow(); 
 
return;
}

bool isNewCell()
{
if(wRO == 0)
  {
  wRO = getQuote();
  wRC = getQuote();
  t   = (DATE / seconds);
  t  *= seconds;
  }
//---     
if(DATE >= (t + seconds))
  {
  return(true);
  }
  else{
  return(false);
  }
}
//+------------------------------------------------------------------+
double getQuote()
{
switch(price_source)
      {
      case 0: return(BQ);
      case 1: return(AQ);
      case 2: return(((BQ+AQ)/2));
     default: return(BQ);
      }
}
//+------------------------------------------------------------------+
void writeHSTcell(int handle,int wt,double wo,double wl,double wh,double wc,double wv)
//----
{                       
if(FileSeek(handle,fpos,SEEK_SET) == true)
  {
  FileWriteInteger(handle,  wt,   LONG_VALUE);		 
  FileWriteDouble (handle,  wo, DOUBLE_VALUE);          
  FileWriteDouble (handle,  wl, DOUBLE_VALUE);		 
  FileWriteDouble (handle,  wh, DOUBLE_VALUE);	 
  FileWriteDouble (handle,  wc, DOUBLE_VALUE);				 
  FileWriteDouble (handle,  wv, DOUBLE_VALUE);	 		
  FileFlush (handle); 
  fpos = FileTell (hand1e);
  }
  else{
  error = GetLastError();
  Print("error: (",error,"): ",ErrorDescription(error));	      
  }    
//----   
return;
}
//+------------------------------------------------------------------+
void UpdateChartWindow() {
	static int hwnd = 0;
 
	if(hwnd == 0) {
		hwnd = WindowHandle(sn, seconds);
		//if(hwnd != 0) Print("Chart window detected");
	}

	if(true && MT4InternalMsg == 0) 
		MT4InternalMsg = RegisterWindowMessageA("MetaTrader4_Internal_Message");

	if(hwnd != 0) if(PostMessageA(hwnd, WM_COMMAND, 0x822c, 0) == 0) hwnd = 0;
	if(hwnd != 0 && MT4InternalMsg != 0) PostMessageA(hwnd, MT4InternalMsg, 2, 1);

	return;
}

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