//+------------------------------------------------------------------+
//| SandyEr7-31.mq4 |
//| Copyright 2014, owner. |
//| https://www.mql5.com/ru/users/owner |
//+------------------------------------------------------------------+
#property copyright "Copyright 2012-2015, owner89 - southtrader."
#property link "www.mql5.com/ru/users/owner"
#property link "www.mql4.com/ru/users/owner89"
#property description "ñåêóíäíûé êîíâåðòåð òèêîâ äëÿ îáíîâëåííîãî òåðìèíàëà"
#property description "çàïèñü ïðîèçâîäèòñÿ â ôàéë ñ èìåíåì ñèìâîëà-èíñòðóìåíòà, ñ ïðèñòàâêîé 'any'."
#property description "âåðñèÿ 7-30 äëÿ 509 ñåðèè òåðìèíàëà"
#property description "\n"
#property description "÷òîáû ïðîäîëæèòü çàïèñü ïîñëå íåçàïëàíèðîâàííîãî îòêëþ÷åíèÿ òåðìèíàëà"
#property description "óäàëèòü ãëîá.ïåðåìåííóþ Symbol eR (F3)"
#property description "\n"
#property description "southtrader@yandex.ru"
#property strict
#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";
extern bool refresh_candle = true;
int MT4InternalMsg;
bool initw;
int error = -1;
int hand1e = -1;
ulong 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;
datetime 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(INIT_FAILED);
}
RefreshRates();
digit = Digits ;
//----
gvar = (symbol+"eR:"+(string)seconds);
if(GlobalVariableCheck(gvar) == false)
{
initw = true;
GlobalVariableSet(gvar,1.0);
}
else{
Print("out: second copy");
initw = false;
flag = false;
return(INIT_FAILED);
}
//----
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);
FileClose(hand1e);
hand1e = FileOpenHistory(fp,FILE_BIN|FILE_WRITE|FILE_SHARE_WRITE|FILE_SHARE_READ|FILE_ANSI);
error = GetLastError();
if(hand1e<0)
{
Print("out: error(",error,"): ",ErrorDescription(error));
flag = false;
}
else{
int unused[13];
ArrayInitialize(unused,0);
FileWriteInteger(hand1e, 400, LONG_VALUE);
FileWriteString (hand1e, "", 64);
FileWriteString (hand1e, sn, 12);
FileWriteInteger(hand1e, seconds, LONG_VALUE);
FileWriteInteger(hand1e, digit+1, 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 = (int)Volume[a];
}
else{
DATE = Time[a];
}
BQ = Low [a];
AQ = High [a];
ask = isNewCell();
if(ask == true)
{
onNewCell();
}
else{
onNonCell();
}
}
//----
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| 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 = (int)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 ++;
if(refresh_candle == true)
{
refreshHSTcell(hand1e, (int)t, wRO, wRL, wRH, wRC,MS);
UpdateChartWindow();
}
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, (int)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 refreshHSTcell(int handle,int wt,double wo,double wl,double wh,double wc,double wv)
//----
{
if(FileSeek(handle,-44,SEEK_END) == true)
{
FileWriteInteger(handle, (int)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);
}
else{
error = GetLastError();
Print("error: (",error,"): ",ErrorDescription(error));
}
//----
return;
}
//+------------------------------------------------------------------+
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, (int)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