|
|
@@ -0,0 +1,629 @@
|
|
|
1
|
+//+------------------------------------------------------------------+
|
|
|
2
|
+//| blTelegramToMT4.mq4 |
|
|
|
3
|
+//| Copyright 2025, MQL Development |
|
|
|
4
|
+//| https://www.mqldevelopment.com/ |
|
|
|
5
|
+//+------------------------------------------------------------------+
|
|
|
6
|
+#property copyright "Copyright 2025, MQL Development"
|
|
|
7
|
+#property link "https://www.mqldevelopment.com/"
|
|
|
8
|
+#property version "1.1"
|
|
|
9
|
+#property strict
|
|
|
10
|
+
|
|
|
11
|
+#define buy "buy"
|
|
|
12
|
+#define sell "sell"
|
|
|
13
|
+#define MaxOrders 10000
|
|
|
14
|
+
|
|
|
15
|
+struct msgDetails
|
|
|
16
|
+ {
|
|
|
17
|
+ int msgid;
|
|
|
18
|
+ msgDetails()
|
|
|
19
|
+ {
|
|
|
20
|
+ msgid = -1;
|
|
|
21
|
+ }
|
|
|
22
|
+ };
|
|
|
23
|
+
|
|
|
24
|
+msgDetails od[MaxOrders];
|
|
|
25
|
+
|
|
|
26
|
+input string Settings = " ------------- General Settings ------------- "; //_
|
|
|
27
|
+input int magic_no = 333; // Magic no
|
|
|
28
|
+input string symbolMatch = "GOLD:XAUUSD,BitCoin:BTCUSD"; // Symbol Mapping (Telegram:MT4)
|
|
|
29
|
+input string suffix = ""; // Account Suffix
|
|
|
30
|
+input string prefix = ""; // Account Prefix
|
|
|
31
|
+input double lotSize = 0.1; // Lot Size
|
|
|
32
|
+
|
|
|
33
|
+//+------------------------------------------------------------------+
|
|
|
34
|
+//| Expert initialization function |
|
|
|
35
|
+//+------------------------------------------------------------------+
|
|
|
36
|
+// Global Variables
|
|
|
37
|
+string url1 = "http://127.0.0.1"; // "http://myapp.local";
|
|
|
38
|
+string header = "Content-Type: application/json\r\nAccept: application/json\r\n";
|
|
|
39
|
+
|
|
|
40
|
+string symbolChart[];
|
|
|
41
|
+string symbolSnd[];
|
|
|
42
|
+string symbolsListUserInput[];
|
|
|
43
|
+uchar sym1[];
|
|
|
44
|
+uchar sym2[];
|
|
|
45
|
+int last_message_id = INT_MIN;
|
|
|
46
|
+int OnInit()
|
|
|
47
|
+ {
|
|
|
48
|
+//--- create timer
|
|
|
49
|
+
|
|
|
50
|
+ ushort u_sep = StringGetCharacter(",",0);
|
|
|
51
|
+ StringSplit(symbolMatch,u_sep,symbolsListUserInput);
|
|
|
52
|
+ ArrayResize(symbolChart,0);
|
|
|
53
|
+ ArrayResize(symbolSnd,0);
|
|
|
54
|
+
|
|
|
55
|
+ for(int i = 0; i < ArraySize(symbolsListUserInput); i++)
|
|
|
56
|
+ {
|
|
|
57
|
+
|
|
|
58
|
+ string str = symbolsListUserInput[i];
|
|
|
59
|
+
|
|
|
60
|
+ int index = StringFind(str,":");
|
|
|
61
|
+ int index2 = StringLen(str);
|
|
|
62
|
+
|
|
|
63
|
+ ArrayResize(sym1,0);
|
|
|
64
|
+ ArrayResize(sym2,0);
|
|
|
65
|
+
|
|
|
66
|
+ for(int j = 0; j < index; j++)
|
|
|
67
|
+ {
|
|
|
68
|
+ ArrayResize(sym1,ArraySize(sym1)+1);
|
|
|
69
|
+ sym1[j] = uchar(str[j]);
|
|
|
70
|
+ }
|
|
|
71
|
+
|
|
|
72
|
+ int k = 0;
|
|
|
73
|
+
|
|
|
74
|
+ for(int j = index + 1 ; j < index2; j++)
|
|
|
75
|
+ {
|
|
|
76
|
+ ArrayResize(sym2,ArraySize(sym2)+1);
|
|
|
77
|
+ sym2[k] = uchar(str[j]);
|
|
|
78
|
+ k++;
|
|
|
79
|
+ }
|
|
|
80
|
+
|
|
|
81
|
+ ArrayResize(symbolChart,ArraySize(symbolChart)+1);
|
|
|
82
|
+ ArrayResize(symbolSnd,ArraySize(symbolSnd)+1);
|
|
|
83
|
+
|
|
|
84
|
+ symbolChart[i] = CharArrayToString(sym1);
|
|
|
85
|
+ symbolSnd[i] = CharArrayToString(sym2);
|
|
|
86
|
+
|
|
|
87
|
+ }
|
|
|
88
|
+
|
|
|
89
|
+ string jsonString = GET_function(url1 + "/get-latest-message-id", header);
|
|
|
90
|
+ StringReplace(jsonString,"},", "*");
|
|
|
91
|
+ last_message_id = (int)getJsonStringValue(jsonString, "id") != 0 ? (int)getJsonStringValue(jsonString, "id") : INT_MIN;
|
|
|
92
|
+
|
|
|
93
|
+ string message = getJsonStringValue(jsonString, "message");
|
|
|
94
|
+
|
|
|
95
|
+ if(last_message_id != INT_MIN)
|
|
|
96
|
+ {
|
|
|
97
|
+ Print(" latest_message_id = ",last_message_id);
|
|
|
98
|
+ Print(" result found against get-latest-message-id = ",message);
|
|
|
99
|
+ }
|
|
|
100
|
+
|
|
|
101
|
+ EventSetTimer(1);
|
|
|
102
|
+
|
|
|
103
|
+//---
|
|
|
104
|
+ return(INIT_SUCCEEDED);
|
|
|
105
|
+ }
|
|
|
106
|
+//+------------------------------------------------------------------+
|
|
|
107
|
+//| Expert deinitialization function |
|
|
|
108
|
+//+------------------------------------------------------------------+
|
|
|
109
|
+void OnDeinit(const int reason)
|
|
|
110
|
+ {
|
|
|
111
|
+//--- destroy timer
|
|
|
112
|
+ EventKillTimer();
|
|
|
113
|
+
|
|
|
114
|
+ }
|
|
|
115
|
+//+------------------------------------------------------------------+
|
|
|
116
|
+//| Expert tick function |
|
|
|
117
|
+//+------------------------------------------------------------------+
|
|
|
118
|
+void OnTick()
|
|
|
119
|
+ {
|
|
|
120
|
+//---
|
|
|
121
|
+
|
|
|
122
|
+ }
|
|
|
123
|
+//+------------------------------------------------------------------+
|
|
|
124
|
+//| Timer function |
|
|
|
125
|
+//+------------------------------------------------------------------+
|
|
|
126
|
+void OnTimer()
|
|
|
127
|
+ {
|
|
|
128
|
+//---
|
|
|
129
|
+ if(last_message_id == INT_MIN)
|
|
|
130
|
+ {
|
|
|
131
|
+
|
|
|
132
|
+ string jsonString = GET_function(url1 + "/get-latest-message-id", header);
|
|
|
133
|
+ StringReplace(jsonString,"},", "*");
|
|
|
134
|
+ last_message_id = (int)getJsonStringValue(jsonString, "id") != 0 ? (int)getJsonStringValue(jsonString, "id") : INT_MIN;
|
|
|
135
|
+
|
|
|
136
|
+
|
|
|
137
|
+ if(last_message_id != INT_MIN)
|
|
|
138
|
+ {
|
|
|
139
|
+
|
|
|
140
|
+ string message = getJsonStringValue(jsonString, "message");
|
|
|
141
|
+
|
|
|
142
|
+ if(last_message_id != INT_MIN)
|
|
|
143
|
+ {
|
|
|
144
|
+ Print(" latest_message_id = ",last_message_id);
|
|
|
145
|
+ Print(" result found against get-latest-message-id = ",message);
|
|
|
146
|
+ }
|
|
|
147
|
+ execute_functionality_on_new_message(last_message_id);
|
|
|
148
|
+
|
|
|
149
|
+ }
|
|
|
150
|
+
|
|
|
151
|
+ }
|
|
|
152
|
+ else
|
|
|
153
|
+ {
|
|
|
154
|
+ string jsonString = GET_function(url1 + "/get-latest-message-id", header);
|
|
|
155
|
+ StringReplace(jsonString,"},", "*");
|
|
|
156
|
+ int latest_message_id = (int)getJsonStringValue(jsonString, "id") != 0 ? (int)getJsonStringValue(jsonString, "id") : INT_MIN;
|
|
|
157
|
+
|
|
|
158
|
+ if(last_message_id != latest_message_id)
|
|
|
159
|
+ {
|
|
|
160
|
+ for(int i = latest_message_id; i > last_message_id; i--)
|
|
|
161
|
+ {
|
|
|
162
|
+ execute_functionality_on_new_message(i);
|
|
|
163
|
+ }
|
|
|
164
|
+ last_message_id = latest_message_id;
|
|
|
165
|
+ }
|
|
|
166
|
+ }
|
|
|
167
|
+ }
|
|
|
168
|
+//+------------------------------------------------------------------+
|
|
|
169
|
+//| |
|
|
|
170
|
+//+------------------------------------------------------------------+
|
|
|
171
|
+string GET_function(string url,string headers)
|
|
|
172
|
+ {
|
|
|
173
|
+
|
|
|
174
|
+ string result_string = NULL;
|
|
|
175
|
+ int timeout = 10; // Set the timeout value in seconds
|
|
|
176
|
+
|
|
|
177
|
+ char result[],data[];
|
|
|
178
|
+ string resultHeaders;
|
|
|
179
|
+ int res = WebRequest("GET",url,headers,timeout,data,result,resultHeaders);
|
|
|
180
|
+
|
|
|
181
|
+ if(res == 200)
|
|
|
182
|
+ {
|
|
|
183
|
+ result_string = CharArrayToString(result);
|
|
|
184
|
+ }
|
|
|
185
|
+ else
|
|
|
186
|
+ {
|
|
|
187
|
+ Print("content GETT: ", result_string," Error: ",GetLastError(), " res: ",res);
|
|
|
188
|
+ }
|
|
|
189
|
+
|
|
|
190
|
+ return result_string;
|
|
|
191
|
+ }
|
|
|
192
|
+//+------------------------------------------------------------------+
|
|
|
193
|
+//| |
|
|
|
194
|
+//+------------------------------------------------------------------+
|
|
|
195
|
+bool checkExistingTrade(int message_id)
|
|
|
196
|
+ {
|
|
|
197
|
+ for(int i=0; i < MaxOrders; i++)
|
|
|
198
|
+ {
|
|
|
199
|
+ if(od[i].msgid == message_id)
|
|
|
200
|
+ {
|
|
|
201
|
+ return true;
|
|
|
202
|
+ }
|
|
|
203
|
+ }
|
|
|
204
|
+ return false;
|
|
|
205
|
+ }
|
|
|
206
|
+
|
|
|
207
|
+//+------------------------------------------------------------------+
|
|
|
208
|
+//| |
|
|
|
209
|
+//+------------------------------------------------------------------+
|
|
|
210
|
+string getJsonStringValue(string json,string key,int addUp,string endSign)
|
|
|
211
|
+ {
|
|
|
212
|
+ int indexStart = StringFind(json,key)+StringLen(key)+addUp;
|
|
|
213
|
+ int indexEnd = StringFind(json,endSign,indexStart);
|
|
|
214
|
+ return StringSubstr(json,indexStart,indexEnd-indexStart);
|
|
|
215
|
+ }
|
|
|
216
|
+//+------------------------------------------------------------------+
|
|
|
217
|
+//| |
|
|
|
218
|
+//+------------------------------------------------------------------+
|
|
|
219
|
+string getJsonStringValue(string json, string key)
|
|
|
220
|
+ {
|
|
|
221
|
+ int start = StringFind(json, "\""+key+"\"");
|
|
|
222
|
+ if(start == -1)
|
|
|
223
|
+ return "";
|
|
|
224
|
+
|
|
|
225
|
+// Find colon after key
|
|
|
226
|
+ int colon = StringFind(json, ":", start);
|
|
|
227
|
+ if(colon == -1)
|
|
|
228
|
+ return "";
|
|
|
229
|
+
|
|
|
230
|
+// Find next comma or closing brace
|
|
|
231
|
+ int endComma = StringFind(json, ",", colon);
|
|
|
232
|
+ int endBrace = StringFind(json, "}", colon);
|
|
|
233
|
+ int end = (endComma != -1 && (endComma < endBrace || endBrace == -1)) ? endComma : endBrace;
|
|
|
234
|
+ if(end == -1)
|
|
|
235
|
+ end = StringLen(json);
|
|
|
236
|
+
|
|
|
237
|
+ string value = trim(StringSubstr(json, colon+1, end-colon-1));
|
|
|
238
|
+// remove quotes if exist
|
|
|
239
|
+ StringReplace(value, "\"", "");
|
|
|
240
|
+ return value;
|
|
|
241
|
+ }
|
|
|
242
|
+//+------------------------------------------------------------------+
|
|
|
243
|
+//| |
|
|
|
244
|
+//+------------------------------------------------------------------+
|
|
|
245
|
+string trim(string text)
|
|
|
246
|
+ {
|
|
|
247
|
+ StringTrimLeft(text);
|
|
|
248
|
+ StringTrimRight(text);
|
|
|
249
|
+ return text;
|
|
|
250
|
+ }
|
|
|
251
|
+//+------------------------------------------------------------------+
|
|
|
252
|
+//| |
|
|
|
253
|
+//+------------------------------------------------------------------+
|
|
|
254
|
+void execute_functionality_on_new_message(int i)
|
|
|
255
|
+ {
|
|
|
256
|
+
|
|
|
257
|
+ string url = url1 + "/get-message/" + IntegerToString(i);
|
|
|
258
|
+ string jsonString = GET_function(url, header);
|
|
|
259
|
+ StringReplace(jsonString,"},", "*");
|
|
|
260
|
+
|
|
|
261
|
+ string message = getJsonStringValue(jsonString,"message",4,"\"");
|
|
|
262
|
+
|
|
|
263
|
+ int group_message_id = (int)getJsonStringValue(jsonString, "message_id");
|
|
|
264
|
+
|
|
|
265
|
+ StringToLower(message);
|
|
|
266
|
+ StringReplace(message,"~","#");
|
|
|
267
|
+
|
|
|
268
|
+ if(checkExistingTrade(group_message_id) == false)
|
|
|
269
|
+ {
|
|
|
270
|
+
|
|
|
271
|
+ string isReplyValue = getJsonStringValue(jsonString, "is_reply");
|
|
|
272
|
+
|
|
|
273
|
+ if(isReplyValue == "True")
|
|
|
274
|
+ {
|
|
|
275
|
+
|
|
|
276
|
+ int reply_to_msg_id = (int)getJsonStringValue(jsonString, "reply_to_msg_id");
|
|
|
277
|
+ message = getJsonStringValue(jsonString, "message");
|
|
|
278
|
+ StringToLower(message);
|
|
|
279
|
+
|
|
|
280
|
+ Print(" ================ Replied Message found of message_id ================ ",reply_to_msg_id);
|
|
|
281
|
+ Print(" ================ Message: ================ ",message);
|
|
|
282
|
+
|
|
|
283
|
+ }
|
|
|
284
|
+
|
|
|
285
|
+ else
|
|
|
286
|
+ {
|
|
|
287
|
+ if(checkExistingTrade(group_message_id) == false)
|
|
|
288
|
+ {
|
|
|
289
|
+ Print(" --------------- New Trade Message Found ----------------- ", " Message Id: ", group_message_id);
|
|
|
290
|
+ StringReplace(message,"~","#");
|
|
|
291
|
+ StringReplace(message,"##", "#");
|
|
|
292
|
+ StringToLower(message);
|
|
|
293
|
+ message = removeExtraSpaces(message);
|
|
|
294
|
+ Print("Message is ",message);
|
|
|
295
|
+
|
|
|
296
|
+ string result[];
|
|
|
297
|
+ string tempResult[];
|
|
|
298
|
+ int indexTemp = 0;
|
|
|
299
|
+
|
|
|
300
|
+ StringSplit(message,'#',tempResult);
|
|
|
301
|
+
|
|
|
302
|
+ for(int j=0; j<ArraySize(tempResult); j++)
|
|
|
303
|
+ {
|
|
|
304
|
+ //result[i] = StringTrimLeft(result[i]);
|
|
|
305
|
+ //result[i] = StringTrimRight(result[i]);
|
|
|
306
|
+ //Print("Temp Result : ", tempResult[i], " index is: ", i);
|
|
|
307
|
+ if(HasAlphanumeric(tempResult[j]))
|
|
|
308
|
+ {
|
|
|
309
|
+ //Print(" contains alphanumeric characters.");
|
|
|
310
|
+ ArrayResize(result,ArraySize(result)+1);
|
|
|
311
|
+ result[indexTemp] = tempResult[j];
|
|
|
312
|
+ indexTemp++;
|
|
|
313
|
+ }
|
|
|
314
|
+ else
|
|
|
315
|
+ {
|
|
|
316
|
+ //Print(" does not contain alphanumeric characters.");
|
|
|
317
|
+ //ArrayResize(indexToDelete,ArraySize(indexToDelete)+1);
|
|
|
318
|
+ //indexToDelete[indexTemp] = i;
|
|
|
319
|
+ //indexTemp++;
|
|
|
320
|
+ }
|
|
|
321
|
+ }
|
|
|
322
|
+
|
|
|
323
|
+ addtoMessageStructure(group_message_id,message);
|
|
|
324
|
+
|
|
|
325
|
+ message(result,message,group_message_id);
|
|
|
326
|
+ }
|
|
|
327
|
+ }
|
|
|
328
|
+ }
|
|
|
329
|
+ }
|
|
|
330
|
+//+------------------------------------------------------------------+
|
|
|
331
|
+//| |
|
|
|
332
|
+//+------------------------------------------------------------------+
|
|
|
333
|
+void message(string &result[], string message, int message_id)
|
|
|
334
|
+ {
|
|
|
335
|
+ string lineOne[]; // = result[0];
|
|
|
336
|
+ int lineIndex = 0;
|
|
|
337
|
+ string direction = "";
|
|
|
338
|
+ int direction_index = -1;
|
|
|
339
|
+ string symbol = "";
|
|
|
340
|
+
|
|
|
341
|
+ if(ArraySize(result) >= 1)
|
|
|
342
|
+ {
|
|
|
343
|
+ StringSplit(result[0], ' ', lineOne);
|
|
|
344
|
+ if(((StringFind(result[0], "buy", 0) != -1) || (StringFind(result[0], "sell", 0) != -1)))
|
|
|
345
|
+ {
|
|
|
346
|
+
|
|
|
347
|
+ for(int i=0; i<ArraySize(lineOne); i++)
|
|
|
348
|
+ {
|
|
|
349
|
+ if(HasAlphanumeric(lineOne[i]))
|
|
|
350
|
+ {
|
|
|
351
|
+ ArrayResize(lineOne,ArraySize(lineOne)+1);
|
|
|
352
|
+ lineOne[lineIndex] = lineOne[i];
|
|
|
353
|
+ Print("Direction and Symbol: ", lineOne[lineIndex], " index is: ", lineIndex);
|
|
|
354
|
+
|
|
|
355
|
+ if(lineOne[lineIndex] == buy || lineOne[lineIndex] == sell)
|
|
|
356
|
+ {
|
|
|
357
|
+ direction = lineOne[lineIndex];
|
|
|
358
|
+ direction_index = lineIndex;
|
|
|
359
|
+ //Print(" Direction is: ", direction, " Direction Index: ", direction_index);
|
|
|
360
|
+ }
|
|
|
361
|
+ lineIndex++;
|
|
|
362
|
+ }
|
|
|
363
|
+ }
|
|
|
364
|
+ if(ArraySize(lineOne) >= 2)
|
|
|
365
|
+ {
|
|
|
366
|
+ if(direction_index == 0)
|
|
|
367
|
+ {
|
|
|
368
|
+ symbol = lineOne[1];
|
|
|
369
|
+ StringToUpper(symbol);
|
|
|
370
|
+
|
|
|
371
|
+ //Print(" This is Message format One (1). Where Direction is: ", direction, " Symbol: ", symbol);
|
|
|
372
|
+ }
|
|
|
373
|
+ else
|
|
|
374
|
+ if(direction_index > 0)
|
|
|
375
|
+ {
|
|
|
376
|
+ symbol = lineOne[0];
|
|
|
377
|
+ StringToUpper(symbol);
|
|
|
378
|
+
|
|
|
379
|
+ //Print(" This is Message format One (1). Where Direction is: ", direction, " Symbol: ", symbol);
|
|
|
380
|
+ }
|
|
|
381
|
+ }
|
|
|
382
|
+ symbol = symbolMapping(symbol);
|
|
|
383
|
+
|
|
|
384
|
+ double sl = 0;
|
|
|
385
|
+ double tp = 0; // = result[0];
|
|
|
386
|
+ for(int i=0 ; i < ArraySize(result); i++)
|
|
|
387
|
+ {
|
|
|
388
|
+ // result[i] = StringTrimLeft(result[i]);
|
|
|
389
|
+ // result[i] = StringTrimRight(result[i]);
|
|
|
390
|
+ // Print("Result : ", result[i], " index is: ", i);
|
|
|
391
|
+ if((StringFind(result[i], "sl", 0) != -1))
|
|
|
392
|
+ {
|
|
|
393
|
+ string tempSl[];
|
|
|
394
|
+ StringReplace(result[i],":", " ");
|
|
|
395
|
+ result[i] = trimString(result[i]);
|
|
|
396
|
+ result[i] = spaceRemove(result[i]);
|
|
|
397
|
+ //Print(" Sl String: ", result[i]);
|
|
|
398
|
+ StringSplit(result[i], ' ', tempSl);
|
|
|
399
|
+ if(ArraySize(tempSl) >= 2)
|
|
|
400
|
+ sl = (double) tempSl[1];
|
|
|
401
|
+ Print("Sl : ", sl);
|
|
|
402
|
+ }
|
|
|
403
|
+ if((StringFind(result[i], "tp", 0) != -1))
|
|
|
404
|
+ {
|
|
|
405
|
+ Print("Tp : ", result[i], " index is: ", i);
|
|
|
406
|
+ string tempTp[];
|
|
|
407
|
+ StringReplace(result[i],":", " ");
|
|
|
408
|
+ result[i] = trimString(result[i]);
|
|
|
409
|
+ result[i] = spaceRemove(result[i]);
|
|
|
410
|
+ Print("Tp After String Replace : ", result[i], " index is: ", i);
|
|
|
411
|
+ StringSplit(result[i], ' ', tempTp);
|
|
|
412
|
+ //double tp = (double) tempTp[1];
|
|
|
413
|
+ for(int j=0 ; j < ArraySize(tempTp); j++)
|
|
|
414
|
+ {
|
|
|
415
|
+ Print(" Data is: ", tempTp[j]);
|
|
|
416
|
+ }
|
|
|
417
|
+ if(ArraySize(tempTp) >= 2)
|
|
|
418
|
+ tp = (double) tempTp[1];
|
|
|
419
|
+ Print("Tp : ", tp);
|
|
|
420
|
+ }
|
|
|
421
|
+ }
|
|
|
422
|
+ Print("Side:", direction, " Symbol: ", symbol, " Tp: ", tp, " Sl: ", sl, " Message Id: ", message_id);
|
|
|
423
|
+
|
|
|
424
|
+ if(direction == buy)
|
|
|
425
|
+ {
|
|
|
426
|
+ placeBuyTrade(symbol, tp, sl, message_id, lotSize);
|
|
|
427
|
+ }
|
|
|
428
|
+
|
|
|
429
|
+ if(direction == sell)
|
|
|
430
|
+ {
|
|
|
431
|
+ placeSellTrade(symbol, tp, sl, message_id, lotSize);
|
|
|
432
|
+ }
|
|
|
433
|
+ }
|
|
|
434
|
+ }
|
|
|
435
|
+ }
|
|
|
436
|
+//+------------------------------------------------------------------+
|
|
|
437
|
+//| |
|
|
|
438
|
+//+------------------------------------------------------------------+
|
|
|
439
|
+void placeBuyTrade(string symbol,double tp,double sl, int messageId, double lot_size)
|
|
|
440
|
+ {
|
|
|
441
|
+
|
|
|
442
|
+ double ask = SymbolInfoDouble(symbol,SYMBOL_ASK);
|
|
|
443
|
+ double bid = SymbolInfoDouble(symbol,SYMBOL_BID);
|
|
|
444
|
+ double buySl = sl;
|
|
|
445
|
+ double buyTp = tp;
|
|
|
446
|
+
|
|
|
447
|
+
|
|
|
448
|
+ int ticket = OrderSend(symbol, OP_BUY, lot_size, ask, 3, buySl, buyTp, "Buy Trade Placed.", magic_no, 0, clrBlue);
|
|
|
449
|
+ Print("Buy order Print: Stop Loss: ", buySl, " Take profit: ", buyTp);
|
|
|
450
|
+ if(ticket < 0)
|
|
|
451
|
+ {
|
|
|
452
|
+ Print("Buy Order Failed ", GetLastError());
|
|
|
453
|
+ }
|
|
|
454
|
+ else
|
|
|
455
|
+ {
|
|
|
456
|
+ Print(" Buy Order Is Placed Sucessfully ");
|
|
|
457
|
+ }
|
|
|
458
|
+ }
|
|
|
459
|
+//+------------------------------------------------------------------+
|
|
|
460
|
+//| |
|
|
|
461
|
+//+------------------------------------------------------------------+
|
|
|
462
|
+void placeSellTrade(string symbol,double tp,double sl, int messageId, double lot_size)
|
|
|
463
|
+ {
|
|
|
464
|
+
|
|
|
465
|
+ double ask = SymbolInfoDouble(symbol,SYMBOL_ASK);
|
|
|
466
|
+ double bid = SymbolInfoDouble(symbol,SYMBOL_BID);
|
|
|
467
|
+ double sellSl = sl;
|
|
|
468
|
+ double sellTp = tp;
|
|
|
469
|
+
|
|
|
470
|
+ int ticket = OrderSend(symbol, OP_SELL, lot_size, bid, 3, sellSl, sellTp, "Sell Trade Placed.", magic_no, 0, clrRed);
|
|
|
471
|
+ if(ticket < 0)
|
|
|
472
|
+ {
|
|
|
473
|
+ Print("Sell Order Failed ", GetLastError());
|
|
|
474
|
+ }
|
|
|
475
|
+ else
|
|
|
476
|
+ {
|
|
|
477
|
+ Print(" Sell Order Is Placed Sucessfully ");
|
|
|
478
|
+ }
|
|
|
479
|
+
|
|
|
480
|
+ }
|
|
|
481
|
+//+------------------------------------------------------------------+
|
|
|
482
|
+//| |
|
|
|
483
|
+//+------------------------------------------------------------------+
|
|
|
484
|
+void addtoMessageStructure(int message_id,string message)
|
|
|
485
|
+ {
|
|
|
486
|
+ for(int i=0; i < MaxOrders; i++)
|
|
|
487
|
+ {
|
|
|
488
|
+ if(od[i].msgid == -1)
|
|
|
489
|
+ {
|
|
|
490
|
+ od[i].msgid = message_id;
|
|
|
491
|
+ StringToLower(message);
|
|
|
492
|
+ Print(" Message ID ",message_id," of Message = ",message," is added To Structure :: ");
|
|
|
493
|
+ break;
|
|
|
494
|
+ }
|
|
|
495
|
+ }
|
|
|
496
|
+ }
|
|
|
497
|
+//+------------------------------------------------------------------+
|
|
|
498
|
+//| |
|
|
|
499
|
+//+------------------------------------------------------------------+
|
|
|
500
|
+string trimString(string inputt)
|
|
|
501
|
+ {
|
|
|
502
|
+// Remove spaces from the left and right sides
|
|
|
503
|
+ int startt = 0;
|
|
|
504
|
+ int end = StringLen(inputt) - 1;
|
|
|
505
|
+
|
|
|
506
|
+// Find the first non-space character
|
|
|
507
|
+ while(startt <= end && StringGetCharacter(inputt, startt) == ' ')
|
|
|
508
|
+ startt++;
|
|
|
509
|
+
|
|
|
510
|
+// Find the last non-space character
|
|
|
511
|
+ while(end >= startt && StringGetCharacter(inputt, end) == ' ')
|
|
|
512
|
+ end--;
|
|
|
513
|
+
|
|
|
514
|
+// Extract the substring without leading or trailing spaces
|
|
|
515
|
+ return StringSubstr(inputt, startt, end - startt + 1);
|
|
|
516
|
+ }
|
|
|
517
|
+//+------------------------------------------------------------------+
|
|
|
518
|
+//| |
|
|
|
519
|
+//+------------------------------------------------------------------+
|
|
|
520
|
+string spaceRemove(string inputt)
|
|
|
521
|
+ {
|
|
|
522
|
+ int len = StringLen(inputt);
|
|
|
523
|
+ string out = "";
|
|
|
524
|
+ bool inSpace = false;
|
|
|
525
|
+
|
|
|
526
|
+ for(int i = 0; i < len; i++)
|
|
|
527
|
+ {
|
|
|
528
|
+ ushort ch = StringGetCharacter(inputt, i);
|
|
|
529
|
+ // treat space, tab, CR, LF as whitespace
|
|
|
530
|
+ bool isSpace = (ch == 32 || ch == 9 || ch == 10 || ch == 13);
|
|
|
531
|
+
|
|
|
532
|
+ if(isSpace)
|
|
|
533
|
+ {
|
|
|
534
|
+ // mark that we are inside a whitespace run, but don't append yet
|
|
|
535
|
+ inSpace = true;
|
|
|
536
|
+ continue;
|
|
|
537
|
+ }
|
|
|
538
|
+
|
|
|
539
|
+ // when we hit a non-space after whitespace, add a single space (if out not empty)
|
|
|
540
|
+ if(inSpace && StringLen(out) > 0)
|
|
|
541
|
+ out += " ";
|
|
|
542
|
+
|
|
|
543
|
+ // append the non-space character (use substr to preserve unicode chars)
|
|
|
544
|
+ out += StringSubstr(inputt, i, 1);
|
|
|
545
|
+
|
|
|
546
|
+ inSpace = false;
|
|
|
547
|
+ }
|
|
|
548
|
+
|
|
|
549
|
+ return(out);
|
|
|
550
|
+ }
|
|
|
551
|
+//+------------------------------------------------------------------+
|
|
|
552
|
+//| |
|
|
|
553
|
+//+------------------------------------------------------------------+
|
|
|
554
|
+string removeExtraSpaces(string str)
|
|
|
555
|
+ {
|
|
|
556
|
+ string result = "";
|
|
|
557
|
+ int len = StringLen(str);
|
|
|
558
|
+ bool lastWasSpace = false;
|
|
|
559
|
+
|
|
|
560
|
+ for(int i = 0; i < len; i++)
|
|
|
561
|
+ {
|
|
|
562
|
+ string currentChar = StringSubstr(str, i, 1);
|
|
|
563
|
+
|
|
|
564
|
+ if(currentChar == " ")
|
|
|
565
|
+ {
|
|
|
566
|
+ // Skip adding this space if the last character was also a space
|
|
|
567
|
+ if(lastWasSpace)
|
|
|
568
|
+ continue;
|
|
|
569
|
+
|
|
|
570
|
+ lastWasSpace = true;
|
|
|
571
|
+ }
|
|
|
572
|
+ else
|
|
|
573
|
+ {
|
|
|
574
|
+ lastWasSpace = false;
|
|
|
575
|
+ }
|
|
|
576
|
+
|
|
|
577
|
+ result += currentChar;
|
|
|
578
|
+ }
|
|
|
579
|
+
|
|
|
580
|
+ return result;
|
|
|
581
|
+ }
|
|
|
582
|
+//+------------------------------------------------------------------+
|
|
|
583
|
+//| |
|
|
|
584
|
+//+------------------------------------------------------------------+
|
|
|
585
|
+bool HasAlphanumeric(string str)
|
|
|
586
|
+ {
|
|
|
587
|
+//Print("String Length: ", StringLen(str));
|
|
|
588
|
+ for(int i = 0; i <= StringLen(str); i++)
|
|
|
589
|
+ {
|
|
|
590
|
+ //Print("Here ", StringLen(str));
|
|
|
591
|
+ if(IsAlphanumeric((char)str[i]))
|
|
|
592
|
+ {
|
|
|
593
|
+ return true;
|
|
|
594
|
+ break;
|
|
|
595
|
+ }
|
|
|
596
|
+ }
|
|
|
597
|
+ return false;
|
|
|
598
|
+ }
|
|
|
599
|
+//+------------------------------------------------------------------+
|
|
|
600
|
+//| |
|
|
|
601
|
+//+------------------------------------------------------------------+
|
|
|
602
|
+bool IsAlphanumeric(char c)
|
|
|
603
|
+ {
|
|
|
604
|
+ if((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
|
|
|
605
|
+ return true;
|
|
|
606
|
+ return false;
|
|
|
607
|
+ }
|
|
|
608
|
+//+------------------------------------------------------------------+
|
|
|
609
|
+//| |
|
|
|
610
|
+//+------------------------------------------------------------------+
|
|
|
611
|
+string symbolMapping(string symbol)
|
|
|
612
|
+ {
|
|
|
613
|
+ for(int k = 0; k < ArraySize(symbolChart); k++)
|
|
|
614
|
+ {
|
|
|
615
|
+ StringToUpper(symbolChart[k]);
|
|
|
616
|
+ if(symbol == symbolChart[k])
|
|
|
617
|
+ {
|
|
|
618
|
+ symbol = symbolSnd[k];
|
|
|
619
|
+ }
|
|
|
620
|
+ }
|
|
|
621
|
+ symbol = prefix + symbol + suffix;
|
|
|
622
|
+ return symbol;
|
|
|
623
|
+ }
|
|
|
624
|
+//+------------------------------------------------------------------+
|
|
|
625
|
+//| |
|
|
|
626
|
+//+------------------------------------------------------------------+
|
|
|
627
|
+//+------------------------------------------------------------------+
|
|
|
628
|
+
|
|
|
629
|
+//+------------------------------------------------------------------+
|