#property strict /* v3.0 Ready for testing in UK. v4.0 Added Magic Number function V5.0 Ready for testing in US V6.0 Added Second Limit Order and Added pips to points in some currencies V7.0 Added For Three News at a time. Corrected for multiple orders. Format Corrected */ #include "My-Functions.mqh" CAppDialog OurInterface; CEdit LotSize1,LotSize2,LotSize3,SD1,TP1,SL1,TSL1,SD2,TP2,SL2,TSL2,SD3,TP3,SL3,TSL3; CLabel LotLabel1,LotLabel2,LotLabel3, SDLabel1,TPLabel1,SLLabel1,TSLLabel1, SDLabel2,TPLabel2,SLLabel2,TSLLabel2, SDLabel3,TPLabel3,SLLabel3,TSLLabel3, NewsTime,NewsRemaining; struct NewsAlertData { string timeStr; string currency; string impact; string newsnumber; string event; string difference; string result; }; struct NextNewsData { string timeStr; string currency; }; double TradeVolume1 =1; double sd1=0; double tp1=500; double sl1=1000; double tsl1=0; double TradeVolume2 =1.2; double sd2=300; double tp2=1000; double sl2=1000; double tsl2=500; double TradeVolume3 =3; double sd3=0; double tp3=0; double sl3=0; double tsl3=0; string trade_status; string content_msg; string content_file; string SymbolOrder ="None"; string ordertype = "None"; bool basecurrency = true; double diffvalue=0; string OK_TO_TRADE="NG"; int thisnewsnumber = 0; int totalnewsnumber = 0; int MagicNewsTime1=1; int MagicNewsTime2=2; int MagicNewsTime3=3; int MagicNewsTime4=4; int MagicNewsTime5=5; int MagicNewsTime6=6; string CurrentTimeOrderExecFirst = "ExecFirst"; string CurrentTimeOrderTakenFirst = "TakenFirst"; string CurrentTimeOrderExecSecond = "ExecSecond"; string CurrentTimeOrderTakenSecond = "TakenSecond"; string CurrentTimeOrderExecThird = "ExecThird"; string CurrentTimeOrderTakenThird = "TakenThird"; int OnInit() { capture_previous_update_id= 0; chat_id = 0; bot.Token(InpToken); // Random Number Generator MathSrand(GetTickCount()); EventSetMillisecondTimer(900); telegram_to_mt4(); ChartSetInteger(0,CHART_EVENT_MOUSE_MOVE,true); OurInterface.Create(0,"AutoTrade NewsTime 7.1v",0,180,70,600,320); createLabel(LotLabel1, OurInterface,"LotLabel1", "Lot1 :",10,50,10,30); createInputBox(LotSize1, OurInterface,"LotVolume1", string(TradeVolume1),60,130,10,30); createLabel(SDLabel1, OurInterface,"SDLabel1", "SD1:",10,50,40,60); createInputBox(SD1, OurInterface,"SD_Value1", string(sd1),60,130,40,60); createLabel(TPLabel1, OurInterface,"TPLabel1", "TP1:",10,50,70,90); createInputBox(TP1, OurInterface,"TP_Value1", string(tp1),60,130,70,90); createLabel(SLLabel1, OurInterface,"SLLabel1", "SL1:",10,50,100,120); createInputBox(SL1, OurInterface,"SL_Value1", string(sl1),60,130,100,120); createLabel(TSLLabel1, OurInterface,"TSLLabel1", "TSL1:",10,50,130,150); createInputBox(TSL1, OurInterface,"TSL_Value1", string(tsl1),60,130,130,150); createLabel(LotLabel2, OurInterface,"LotLabel2", "Lot2 :",140,180,10,30); createInputBox(LotSize2, OurInterface,"LotVolume2", string(TradeVolume2),190,260,10,30); createLabel(SDLabel2, OurInterface,"SDLabel2", "SD2:",140,180,40,60); createInputBox(SD2, OurInterface,"SD_Value2", string(sd2),190,260,40,60); createLabel(TPLabel2, OurInterface,"TPLabel2", "TP2:",140,180,70,90); createInputBox(TP2, OurInterface,"TP_Value2", string(tp2),190,260,70,90); createLabel(SLLabel2, OurInterface,"SLLabel2", "SL2:",140,180,100,120); createInputBox(SL2, OurInterface,"SL_Value2", string(sl2),190,260,100,120); createLabel(TSLLabel2, OurInterface,"TSLLabel2", "TSL2:",140,180,130,150); createInputBox(TSL2, OurInterface,"TSL_Value2", string(tsl2),190,260,130,150); createLabel(LotLabel3, OurInterface,"LotLabel3", "Lot3 :",270,310,10,30); createInputBox(LotSize3, OurInterface,"LotVolume3", string(TradeVolume3),320,390,10,30); createLabel(SDLabel3, OurInterface,"SDLabel3", "SD3:",270,310,40,60); createInputBox(SD3, OurInterface,"SD_Value3", string(sd3),320,390,40,60); createLabel(TPLabel3, OurInterface,"TPLabel3", "TP3:",270,310,70,90); createInputBox(TP3, OurInterface,"TP_Value3", string(tp3),320,390,70,90); createLabel(SLLabel3, OurInterface,"SLLabel3", "SL3:",270,310,100,120); createInputBox(SL3, OurInterface,"SL_Value3", string(sl3),320,390,100,120); createLabel(TSLLabel3, OurInterface,"TSLLabel3", "TSL3:",270,310,130,150); createInputBox(TSL3, OurInterface,"TSL_Value3", string(tsl3),320,390,130,150); OurInterface.Run(); return(INIT_SUCCEEDED); } void OnDeinit(const int reason) { EventKillTimer(); // Kill the timer OurInterface.Destroy(reason); } void OnTick() { for (int b= OrdersTotal()-1;b>=0;b--) { if (OrderSelect(b,SELECT_BY_POS,MODE_TRADES)) { if((OrderType()==OP_BUYLIMIT || OrderType()==OP_BUYSTOP || OrderType()==OP_SELLLIMIT || OrderType()==OP_SELLSTOP) && OrderMagicNumber()!=0) { datetime ctm= TimeCurrent()-OrderOpenTime(); if (ctm>600) { int OpenDelete = OrderDelete(OrderTicket()); } }}} string filePattern = "News_Alert_*.*"; string foundFile; long handle = FileFindFirst(filePattern, foundFile, 0); if(handle != INVALID_HANDLE) { do { // Print("Found file: ", foundFile); NewsAlertData news = ReadNewsAlert(foundFile); /* Print("Time: ", news.timeStr); Print("Currency: ", news.currency); Print("Impact: ", news.impact); Print("High Impact News Number :", news.newsnumber); Print("Event:", news.event); Print("Difference: ", news.difference); Print("Result: ", news.result); */ string getnewsnumber[]; StringSplit(news.newsnumber,StringGetCharacter("/", 0), getnewsnumber); int count_getnewsnumber = ArraySize(getnewsnumber); if (count_getnewsnumber>1) { thisnewsnumber = StrToInteger(getnewsnumber[0]); totalnewsnumber = StrToInteger(getnewsnumber[1]); } string getdifference[]; StringSplit(news.difference,StringGetCharacter("%", 0), getdifference); int count_getdifference = ArraySize(getdifference); if (count_getdifference>0) { diffvalue = MathAbs(StrToDouble(getdifference[0])); } if (diffvalue>4) OK_TO_TRADE="OK"; if (news.currency=="USD") SymbolOrder ="XAUUSD"; if (news.currency=="JPY") SymbolOrder ="USDJPY"; if (news.currency=="CHF") SymbolOrder ="USDCHF"; if (news.currency=="CAD") SymbolOrder ="USDCAD"; if (news.currency=="AUD") SymbolOrder ="AUDUSD"; if (news.currency=="EUR") SymbolOrder ="XAUEUR"; if (news.currency=="GBP") SymbolOrder ="XAUGBP"; if (news.currency=="NZD") SymbolOrder ="NZDUSD"; basecurrency = isbasecurrency(SymbolOrder,news.currency); if ((news.result=="Better" && basecurrency==true) || (news.result=="Worse" && basecurrency==false)) ordertype="Buy"; if ((news.result=="Worse" && basecurrency==true) || (news.result=="Better" && basecurrency==false)) ordertype="Sell"; if(thisnewsnumber ==1) { MagicNewsTime1 = CreateMagicNumber("News",SymbolOrder,ordertype,1); MagicNewsTime2 = CreateMagicNumber("News",SymbolOrder,ordertype,2); CurrentTimeOrderExecFirst = news.timeStr+news.currency+DoubleToStr(thisnewsnumber); if((CurrentTimeOrderTakenFirst != CurrentTimeOrderExecFirst) && (news.result=="Better" || news.result=="Worse") && IsNewsWithinOneMinute(news.timeStr)<=60 && IsNewsWithinOneMinute(news.timeStr)>0) { if (ordertype=="Buy" && OK_TO_TRADE=="OK") { buyprice = NormalizeDouble(MarketInfo(SymbolOrder,MODE_ASK),4); int orderbuy = OrderSend (SymbolOrder,OP_BUY,TradeVolume1,buyprice,0,0,0,NULL,MagicNewsTime1,0,clrBlue); buylimitprice = buyprice - PipsToPrice(sd2,SymbolOrder); int orderbuylimit = OrderSend(SymbolOrder, OP_BUYLIMIT, TradeVolume2, buylimitprice, 0,0, 0, NULL, MagicNewsTime2, 0, clrBlue); } if (ordertype=="Sell" && OK_TO_TRADE=="OK") { sellprice = NormalizeDouble(MarketInfo(SymbolOrder,MODE_BID),4); int ordersell = OrderSend (SymbolOrder,OP_SELL,TradeVolume1,sellprice,0,0,0,NULL,MagicNewsTime1,0,clrBlue); selllimitprice = sellprice + PipsToPrice(sd2,SymbolOrder); int orderselllimit = OrderSend(SymbolOrder, OP_SELLLIMIT, TradeVolume2, selllimitprice, 0,0, 0, NULL, MagicNewsTime2, 0, clrBlue); } CurrentTimeOrderTakenFirst = CurrentTimeOrderExecFirst; } } if(thisnewsnumber ==2) { MagicNewsTime3 = CreateMagicNumber("News",SymbolOrder,ordertype,3); MagicNewsTime4 = CreateMagicNumber("News",SymbolOrder,ordertype,4); CurrentTimeOrderExecSecond = news.timeStr+news.currency+DoubleToStr(thisnewsnumber); if((CurrentTimeOrderTakenSecond != CurrentTimeOrderExecSecond) && (news.result=="Better" || news.result=="Worse") && IsNewsWithinOneMinute(news.timeStr)<=60 && IsNewsWithinOneMinute(news.timeStr)>0) { if (ordertype=="Buy" && OK_TO_TRADE=="OK") { buyprice = NormalizeDouble(MarketInfo(SymbolOrder,MODE_ASK),4); int orderbuy = OrderSend (SymbolOrder,OP_BUY,TradeVolume1,buyprice,0,0,0,NULL,MagicNewsTime3,0,clrBlue); buylimitprice = buyprice - PipsToPrice(sd2,SymbolOrder); int orderbuylimit = OrderSend(SymbolOrder, OP_BUYLIMIT, TradeVolume2, buylimitprice, 0,0, 0, NULL, MagicNewsTime4, 0, clrBlue); } if (ordertype=="Sell" && OK_TO_TRADE=="OK") { sellprice = NormalizeDouble(MarketInfo(SymbolOrder,MODE_BID),4); int ordersell = OrderSend (SymbolOrder,OP_SELL,TradeVolume1,sellprice,0,0,0,NULL,MagicNewsTime3,0,clrBlue); selllimitprice = sellprice + PipsToPrice(sd2,SymbolOrder); int orderselllimit = OrderSend(SymbolOrder, OP_SELLLIMIT, TradeVolume2, selllimitprice, 0,0, 0, NULL, MagicNewsTime4, 0, clrBlue); } CurrentTimeOrderTakenSecond = CurrentTimeOrderExecSecond; } } if(thisnewsnumber ==3) { MagicNewsTime5 = CreateMagicNumber("News",SymbolOrder,ordertype,5); MagicNewsTime6 = CreateMagicNumber("News",SymbolOrder,ordertype,6); CurrentTimeOrderExecThird = news.timeStr+news.currency+DoubleToStr(thisnewsnumber); if((CurrentTimeOrderTakenThird != CurrentTimeOrderExecThird) && (news.result=="Better" || news.result=="Worse") && IsNewsWithinOneMinute(news.timeStr)<=60 && IsNewsWithinOneMinute(news.timeStr)>0) { if (ordertype=="Buy" && OK_TO_TRADE=="OK") { buyprice = NormalizeDouble(MarketInfo(SymbolOrder,MODE_ASK),4); int orderbuy = OrderSend (SymbolOrder,OP_BUY,TradeVolume1,buyprice,0,0,0,NULL,MagicNewsTime5,0,clrBlue); buylimitprice = buyprice - PipsToPrice(sd2,SymbolOrder); int orderbuylimit = OrderSend(SymbolOrder, OP_BUYLIMIT, TradeVolume2, buylimitprice, 0,0, 0, NULL, MagicNewsTime6, 0, clrBlue); } if (ordertype=="Sell" && OK_TO_TRADE=="OK") { sellprice = NormalizeDouble(MarketInfo(SymbolOrder,MODE_BID),4); int ordersell = OrderSend (SymbolOrder,OP_SELL,TradeVolume1,sellprice,0,0,0,NULL,MagicNewsTime5,0,clrBlue); selllimitprice = sellprice + PipsToPrice(sd2,SymbolOrder); int orderselllimit = OrderSend(SymbolOrder, OP_SELLLIMIT, TradeVolume2, selllimitprice, 0,0, 0, NULL, MagicNewsTime6, 0, clrBlue); } CurrentTimeOrderTakenThird = CurrentTimeOrderExecThird; } } OK_TO_TRADE="NG"; if (IsNewsWithinOneMinute(news.timeStr)>120 ) FileDelete(foundFile); } while(FileFindNext(handle, foundFile)); FileFindClose(handle); } else { // Print("No matching files found."); } for (int b= OrdersTotal()-1;b>=0;b--) { if (OrderSelect(b,SELECT_BY_POS,MODE_TRADES)) { if (OrderMagicNumber()==MagicNewsTime1 || OrderMagicNumber()==MagicNewsTime2 || OrderMagicNumber()==MagicNewsTime3 || OrderMagicNumber()==MagicNewsTime4 || OrderMagicNumber()==MagicNewsTime5 || OrderMagicNumber()==MagicNewsTime6 ) { if (GetMagicSource(OrderMagicNumber())=="News") { if ((OrderType()==OP_BUY) && OrderStopLoss()==0) { double slbuy = OrderOpenPrice()-PipsToPrice(sl1,OrderSymbol()); double tpbuy = OrderOpenPrice()+PipsToPrice(tp1, OrderSymbol()); int Modify = OrderModify (OrderTicket(),OrderOpenPrice(),slbuy,tpbuy,0,clrAqua); } if ((OrderType()==OP_SELL) && OrderStopLoss()==0) { double slsell = OrderOpenPrice()+PipsToPrice(sl1,OrderSymbol()); double tpsell = OrderOpenPrice()-PipsToPrice(tp1, OrderSymbol()); int Modify = OrderModify (OrderTicket(),OrderOpenPrice(),slsell,tpsell,0,clrAqua); } }}}} // Trailing StopLoss for (int b= OrdersTotal()-1;b>=0;b--) { if (OrderSelect(b,SELECT_BY_POS,MODE_TRADES)) { if (OrderMagicNumber()==MagicNewsTime1 || OrderMagicNumber()==MagicNewsTime2 || OrderMagicNumber()==MagicNewsTime3 || OrderMagicNumber()==MagicNewsTime4 || OrderMagicNumber()==MagicNewsTime5 || OrderMagicNumber()==MagicNewsTime6 ) { // Trailing StopLoss for Buy if((OrderType()==OP_BUY) && GetMagicSource(OrderMagicNumber())=="News") { if(tsl1!=0 && Ask> OrderOpenPrice()+ tsl1*_Point) { buynewtsl = Ask - tsl1*_Point; buynewtsl = NormalizeDouble(buynewtsl, _Digits); if (OrderStopLoss()sellnewtsl && OrderStopLoss()!=0) { int SellModify = OrderModify (OrderTicket(),OrderOpenPrice(),sellnewtsl,OrderTakeProfit(),0,clrBlue); }}} }}} } // End of On Tick void OnTimer() { chartopenclose(); showbalance(); if (FileIsExist("Next_News_Alert.txt")) { NewsAlertData nextnews = ReadNewsAlert("Next_News_Alert.txt"); string nextnewstime = nextnews.timeStr; string nextnewscurr = nextnews.currency; datetime myTime = TimeLocal(); // or any datetime value string fullTimeStr = TimeToString(myTime, TIME_SECONDS); // "2025.04.17 14:32:15" string infofirstline = "Next High Imp News at "+nextnewstime+" \n for "+nextnewscurr; string infosecondline = "Calculating......"; int timediff = IsNewsWithinOneMinute(nextnewstime); if (timediff>0 && timediff<=60) infosecondline= "News Windows Active......."; if (timediff>60) { infofirstline="Wait for the next news tomorrow...."; infosecondline= "No More News today"; } if (timediff<0) { int remaining = MathAbs(timediff); infosecondline = "Time Remaining "+SecondsToHMS(remaining); } createLabel(NewsTime, OurInterface,"NewsTime",infofirstline,40,100,160,180); createLabel(NewsRemaining, OurInterface,"NewsRemaing",infosecondline,100,150,190,200); } } // End On Timer // Start of Event Handling void OnChartEvent (const int id,const long &lparam,const double &dparam,const string &sparam) { OurInterface.OnEvent(id,lparam,dparam, sparam); if (id == CHARTEVENT_OBJECT_ENDEDIT){ TradeVolume1 = updatetextboxdouble(sparam, "LotVolume1",TradeVolume1); sd1 = updatetextboxdouble(sparam, "SD_Value1",sd1); tp1 = updatetextboxdouble(sparam, "TP_Value1",tp1); sl1 = updatetextboxdouble(sparam, "SL_Value1",sl1); tsl1 = updatetextboxdouble(sparam, "TSL_Value1",tsl1); TradeVolume2 = updatetextboxdouble(sparam, "LotVolume1",TradeVolume2); sd2 = updatetextboxdouble(sparam, "SD_Value2",sd2); tp2 = updatetextboxdouble(sparam, "TP_Value2",tp2); sl2 = updatetextboxdouble(sparam, "SL_Value2",sl2); tsl2 = updatetextboxdouble(sparam, "TSL_Value2",tsl2); TradeVolume3 = updatetextboxdouble(sparam, "LotVolume1",TradeVolume3); sd3 = updatetextboxdouble(sparam, "SD_Value3",sd3); tp3 = updatetextboxdouble(sparam, "TP_Value3",tp3); sl3 = updatetextboxdouble(sparam, "SL_Value3",sl3); tsl3 = updatetextboxdouble(sparam, "TSL_Value3",tsl3); } // End of CHARTEVENT_OBJECT_ENDEDIT } // End of Event Handling NewsAlertData ReadNewsAlert(string filename) { NewsAlertData data; int fileHandle = FileOpen(filename, FILE_READ | FILE_TXT); if(fileHandle == INVALID_HANDLE) { Print("Failed to open file: ", filename); return data; } string line; while(!FileIsEnding(fileHandle)) { line = FileReadString(fileHandle); if(StringFind(line, "Time:") == 0) data.timeStr = StringTrimLeft(StringSubstr(line, 5)); else if(StringFind(line, "Currency:") == 0) data.currency = StringTrimLeft(StringSubstr(line, 9)); else if(StringFind(line, "Impact:") == 0) data.impact = StringTrimLeft(StringSubstr(line, 7)); else if(StringFind(line, "High Impact News Number :") == 0) data.newsnumber = StringTrimLeft(StringSubstr(line, 25)); else if(StringFind(line, "Event:") == 0) data.event = StringTrimLeft(StringSubstr(line, 6)); else if(StringFind(line, "Difference:") == 0) data.difference = StringTrimLeft(StringSubstr(line, 11)); else if(StringFind(line, "Result:") == 0) data.result = StringTrimLeft(StringSubstr(line, 7)); } FileClose(fileHandle); return data; } NextNewsData ReadNextNews(string filename) { NextNewsData data; int fileHandle = FileOpen(filename, FILE_READ | FILE_TXT); if(fileHandle == INVALID_HANDLE) { Print("Failed to open file: ", filename); return data; } string line; while(!FileIsEnding(fileHandle)) { line = FileReadString(fileHandle); if(StringFind(line, "Time:") == 0) data.timeStr = StringTrimLeft(StringSubstr(line, 5)); else if(StringFind(line, "Currency:") == 0) data.currency = StringTrimLeft(StringSubstr(line, 9)); } FileClose(fileHandle); return data; } bool MagicNumberUsed(int magic, int N = 10) { int total = OrdersHistoryTotal(); int startIndex = MathMax(0, total - N); for(int i = total - 1; i >= startIndex; i--) { if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) { if(OrderMagicNumber() == magic) { return true; } } } return false; } int IsNewsWithinOneMinute(string timeStr) { // Trim whitespace string raw = StringTrimLeft(StringTrimRight(timeStr)); // Print("Raw time from file: ", raw); // Find colon int colonPos = StringFind(raw, ":"); if (colonPos < 0) { Print("Invalid time format: no colon found"); return false; } // Extract hour, minute, am/pm string hourStr = StringSubstr(raw, 0, colonPos); string minuteStr = StringSubstr(raw, colonPos + 1, 2); string ampm = StringSubstr(raw, StringLen(raw) - 2, 2); int hour = StrToInteger(hourStr); int minute = StrToInteger(minuteStr); // Convert to 24-hour format if (ampm == "pm" && hour < 12) hour += 12; if (ampm == "am" && hour == 12) hour = 0; // Build datetime datetime now = TimeLocal(); datetime todayStart = StringToTime(TimeToString(now, TIME_DATE)); datetime newsTime = todayStart + (hour * 3600) + (minute * 60); int diff = (int)(now - newsTime); // Print("Time difference (in seconds): ", diff); return diff; }