-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnews_trader.mq4
236 lines (211 loc) · 8.92 KB
/
news_trader.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
//+------------------------------------------------------------------+
//| news.mq4 |
//| Copyright � 2016 Tor |
//| http://einvestor.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright � 2016 Tor"
#property link "http://einvestor.ru/"
#property version "1.0"
#property description "This Expert Advisor loads the News from the site Investing.com without using .dll"
#property strict
input int AfterNewsStop=5; // Indent after News, minuts
input int BeforeNewsStop=5; // Indent before News, minuts
input bool NewsLight= false; // Enable light news
input bool NewsMedium=false; // Enable medium news
input bool NewsHard=true; // Enable hard news
input int offset=3; // Your Time Zone, GMT (for news)
input string NewsSymb="USD,EUR,GBP,CHF,CAD,AUD,NZD,JPY"; //Currency to display the news (empty - only the current currencies)
input bool DrawLines=true; // Draw lines on the chart
input bool Next = false; // Draw only the future of news line
input bool Signal = false; // Signals on the upcoming news
color highc = clrRed; // Colour important news
color mediumc = clrBlue; // Colour medium news
color lowc = clrLime; // The color of weak news
int Style = 2; // Line style
int Upd = 86400; // Period news updates in seconds
bool Vhigh = false;
bool Vmedium = false;
bool Vlow = false;
int MinBefore=0;
int MinAfter=0;
int NomNews=0;
string NewsArr[4][1000];
int Now=0;
datetime LastUpd;
string str1;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
if(StringLen(NewsSymb)>1)str1=NewsSymb;
else str1=Symbol();
Vhigh=NewsHard;
Vmedium=NewsMedium;
Vlow=NewsLight;
MinBefore=BeforeNewsStop;
MinAfter=AfterNewsStop;
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
ObjectsDeleteAll(0,OBJ_VLINE);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
double CheckNews=0;
if(AfterNewsStop>0)
{
if(TimeCurrent()-LastUpd>=Upd){Comment("News Loading...");Print("News Loading...");UpdateNews();LastUpd=TimeCurrent();Comment("");}
WindowRedraw();
//---Draw a line on the chart news--------------------------------------------
if(DrawLines)
{
for(int i=0;i<NomNews;i++)
{
string Name=StringSubstr(TimeToStr(TimeNewsFunck(i),TIME_MINUTES)+"_"+NewsArr[1][i]+"_"+NewsArr[3][i],0,63);
if(NewsArr[3][i]!="")if(ObjectFind(Name)==0)continue;
if(StringFind(str1,NewsArr[1][i])<0)continue;
if(TimeNewsFunck(i)<TimeCurrent() && Next)continue;
color clrf = clrNONE;
if(Vhigh && StringFind(NewsArr[2][i],"High")>=0)clrf=highc;
if(Vmedium && StringFind(NewsArr[2][i],"Moderate")>=0)clrf=mediumc;
if(Vlow && StringFind(NewsArr[2][i],"Low")>=0)clrf=lowc;
if(clrf==clrNONE)continue;
if(NewsArr[3][i]!="")
{
ObjectCreate(Name,0,OBJ_VLINE,TimeNewsFunck(i),0);
ObjectSet(Name,OBJPROP_COLOR,clrf);
ObjectSet(Name,OBJPROP_STYLE,Style);
ObjectSetInteger(0,Name,OBJPROP_BACK,true);
}
}
}
//---------------event Processing------------------------------------
int i;
CheckNews=0;
for(i=0;i<NomNews;i++)
{
int power=0;
if(Vhigh && StringFind(NewsArr[2][i],"High")>=0)power=1;
if(Vmedium && StringFind(NewsArr[2][i],"Moderate")>=0)power=2;
if(Vlow && StringFind(NewsArr[2][i],"Low")>=0)power=3;
if(power==0)continue;
if(TimeCurrent()+MinBefore*60>TimeNewsFunck(i) && TimeCurrent()-MinAfter*60<TimeNewsFunck(i) && StringFind(str1,NewsArr[1][i])>=0)
{
CheckNews=1;
break;
}
else CheckNews=0;
}
if(CheckNews==1 && i!=Now && Signal) { Alert("In ",(int)(TimeNewsFunck(i)-TimeCurrent())/60," minutes released news ",NewsArr[1][i],"_",NewsArr[3][i]);Now=i;}
/*** ***/
}
if(CheckNews>0)
{
///// We are doing here if we are in the framework of the news
Comment("News time");
}else{
// We are out of scope of the news release (No News)
Comment("No news");
}
}
//+------------------------------------------------------------------+
//////////////////////////////////////////////////////////////////////////////////
// Download CBOE page source code in a text variable
// And returns the result
//////////////////////////////////////////////////////////////////////////////////
string ReadCBOE()
{
string cookie=NULL,headers;
char post[],result[]; string TXT="";
int res;
//--- to work with the server, you must add the URL "https://www.google.com/finance"
//--- the list of allowed URL (Main menu-> Tools-> Settings tab "Advisors"):
string google_url="http://ec.forexprostools.com/?columns=exc_currency,exc_importance&importance=1,2,3&calType=week&timeZone=15&lang=1";
//---
ResetLastError();
//--- download html-pages
int timeout=5000; //--- timeout less than 1,000 (1 sec.) is insufficient at a low speed of the Internet
res=WebRequest("GET",google_url,cookie,NULL,timeout,post,0,result,headers);
//--- error checking
if(res==-1)
{
Print("WebRequest error, err.code =",GetLastError());
MessageBox("You must add the address ' "+google_url+"' in the list of allowed URL tab 'Advisors' "," Error ",MB_ICONINFORMATION);
//--- You must add the address ' "+ google url"' in the list of allowed URL tab 'Advisors' "," Error "
}
else
{
//--- successful download
//PrintFormat("File successfully downloaded, the file size in bytes =%d.",ArraySize(result));
//--- save the data in the file
int filehandle=FileOpen("news-log.html",FILE_WRITE|FILE_BIN);
//--- �������� ������
if(filehandle!=INVALID_HANDLE)
{
//---save the contents of the array result [] in file
FileWriteArray(filehandle,result,0,ArraySize(result));
//--- close file
FileClose(filehandle);
int filehandle2=FileOpen("news-log.html",FILE_READ|FILE_BIN);
TXT=FileReadString(filehandle2,ArraySize(result));
FileClose(filehandle2);
}else{
Print("Error in FileOpen. Error code =",GetLastError());
}
}
return(TXT);
}
//+------------------------------------------------------------------+
datetime TimeNewsFunck(int nomf)
{
string s=NewsArr[0][nomf];
string time=StringConcatenate(StringSubstr(s,0,4),".",StringSubstr(s,5,2),".",StringSubstr(s,8,2)," ",StringSubstr(s,11,2),":",StringSubstr(s,14,4));
return((datetime)(StringToTime(time) + offset*3600));
}
//////////////////////////////////////////////////////////////////////////////////
void UpdateNews()
{
string TEXT=ReadCBOE();
int sh = StringFind(TEXT,"pageStartAt>")+12;
int sh2= StringFind(TEXT,"</tbody>");
TEXT=StringSubstr(TEXT,sh,sh2-sh);
sh=0;
while(!IsStopped())
{
sh = StringFind(TEXT,"event_timestamp",sh)+17;
sh2= StringFind(TEXT,"onclick",sh)-2;
if(sh<17 || sh2<0)break;
NewsArr[0][NomNews]=StringSubstr(TEXT,sh,sh2-sh);
sh = StringFind(TEXT,"flagCur",sh)+10;
sh2= sh+3;
if(sh<10 || sh2<3)break;
NewsArr[1][NomNews]=StringSubstr(TEXT,sh,sh2-sh);
if(StringFind(str1,NewsArr[1][NomNews])<0)continue;
sh = StringFind(TEXT,"title",sh)+7;
sh2= StringFind(TEXT,"Volatility",sh)-1;
if(sh<7 || sh2<0)break;
NewsArr[2][NomNews]=StringSubstr(TEXT,sh,sh2-sh);
if(StringFind(NewsArr[2][NomNews],"High")>=0 && !Vhigh)continue;
if(StringFind(NewsArr[2][NomNews],"Moderate")>=0 && !Vmedium)continue;
if(StringFind(NewsArr[2][NomNews],"Low")>=0 && !Vlow)continue;
sh=StringFind(TEXT,"left event",sh)+12;
int sh1=StringFind(TEXT,"Speaks",sh);
sh2=StringFind(TEXT,"<",sh);
if(sh<12 || sh2<0)break;
if(sh1<0 || sh1>sh2)NewsArr[3][NomNews]=StringSubstr(TEXT,sh,sh2-sh);
else NewsArr[3][NomNews]=StringSubstr(TEXT,sh,sh1-sh);
NomNews++;
if(NomNews==300)break;
}
}
//+------------------------------------------------------------------+