-
Notifications
You must be signed in to change notification settings - Fork 23
/
WatchDog.mq4
158 lines (126 loc) · 4.45 KB
/
WatchDog.mq4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//+------------------------------------------------------------------+
//| WatchDog.mq4 |
//| Copyright, Kirill |
//| http://www.ForexBoat.com |
//+------------------------------------------------------------------+
#property copyright "Copyright, Kirill"
#property link "http://www.ForexBoat.com"
#property copyright "Copyright 2021, Vladimir Zhbanko"
#property link "https://vladdsm.github.io/myblog_attempt/"
#property version "2.00"
#property strict
/*
v.2.00
Added option Account Monitoring - purpose is to write a file with current account profit (balance and equity)
*/
extern int AccountRow = 1;
extern int DelayMinutes = 5;
extern bool eMailAlert = False;
extern bool OptAccountMonitoring = True;
// AccountRow - Your favourites account number
// DelayMinutes - Delay in minutes, has to be greater than the chart timeframe
#include <18_EmailFromMT4.mqh>
#include <WinUser32.mqh>
#include <08_TerminalNumber.mqh>
#import "user32.dll"
int GetParent(int hWnd);
int GetDlgItem(int hDlg, int nIDDlgItem);
int GetLastActivePopup(int hWnd);
#import
#define VK_HOME 0x24
#define VK_DOWN 0x28
#define VK_ENTER 0x0D
#define PAUSE 1000
datetime Old_Time=0;
bool is_reconect = true;
void OnInit()
{
Old_Time=iTime(NULL,0,0);
//send email on Sunday Morning once platform re-starts to work...
if(eMailAlert)
{
EmailFromMT4(AccountRow);
}
OnTick();
}
void OnTick()
{
//log account amount to file
if(OptAccountMonitoring)
{
InfoAccountToCSV(T_Num());
}
if (!IsDllsAllowed())
{
Alert("Watchdog: DLLs not alllowed!");
return;
}
while (!IsStopped())
{
RefreshRates();
if (Old_Time == iTime(NULL,0,0)) is_reconect=true;
else is_reconect=false;
Old_Time=iTime(NULL,0,0);
if (is_reconect)
{
Print("Watchdog: The chart has not been updated in " + string(DelayMinutes) + " minutes. Initating reconnection procedure...");
Login(AccountRow);
}
//log account amount to file
if(OptAccountMonitoring)
{
InfoAccountToCSV(T_Num());
}
Sleep(DelayMinutes*60*1000);
}
return;
}
void Login(int Num)
{
int hwnd = WindowHandle(Symbol(), Period());
int hwnd_parent = 0;
while (!IsStopped())
{
hwnd = GetParent(hwnd);
if (hwnd == 0) break;
hwnd_parent = hwnd;
}
if (hwnd_parent != 0)
{
hwnd = GetDlgItem(hwnd_parent, 0xE81C);
hwnd = GetDlgItem(hwnd, 0x52);
hwnd = GetDlgItem(hwnd, 0x8A70);
PostMessageA(hwnd, WM_KEYDOWN, VK_HOME,0);
while (Num > 1)
{
PostMessageA(hwnd, WM_KEYDOWN,VK_DOWN, 0);
Num--;
}
PostMessageA(hwnd, WM_KEYDOWN, VK_ENTER, 0);
Sleep(PAUSE);
hwnd = GetLastActivePopup(hwnd_parent);
PostMessageA(hwnd, WM_KEYDOWN, VK_ENTER, 0);
}
return;
}
//+------------------------------------------------------------------+
//| FUNCTION ACCOUNT PROFIT TO CSV
//+------------------------------------------------------------------+
void InfoAccountToCSV(int TermNumber)
{
//*3*_Logging account status to the file csv for further account management in R
// record info to the file csv
string fileName = "AccountResultsT"+string(TermNumber)+".csv";//create the name of the file same for all symbols...
datetime TIME = iTime(Symbol(), PERIOD_CURRENT, 0); //Time of the bar of the applied chart symbol
RefreshRates();
double myBalance = AccountBalance();
double myEquity = AccountEquity();
double myProfit = DoubleToStr(myEquity - myBalance);
Comment(fileName);
// open file handle
int handle = FileOpen(fileName,FILE_CSV|FILE_WRITE|FILE_SHARE_READ|FILE_SHARE_WRITE);
FileSeek(handle,0,SEEK_END);
string data = string(TIME) + "," + string(myBalance) + "," + string(myEquity) + "," + string(myProfit);
FileWrite(handle,data); //write data to the file during each for loop iteration
FileClose(handle); //close file when data write is over
}