#property strict // v3.0 // Added Telegram Functionality // Only one trade in 5 mins window #include #include #include CAppDialog OurInterface; #include CEdit LotSize,TPBUY,SLBUY,TPSELL,SLSELL,STARTDIFF; #include CLabel LotLabel,TPBUYLabel,SLBUYLabel,TPSELLLabel,SLSELLLabel,STARTDIFFLabel; CCustomBot bot; double TradeVolume = 1; double sls = 200; // 200 double tps = 50; // 50 best double slb = 300; // 300 best double tpb = 200; // 200 best double tsl= 0; double sd= 25; // 25 best int start_hour = 3; int start_min = 0; int end_hour = 4; int end_min = 01; int second_start_hour = 18; // 18 is best int second_start_min = 0; int second_end_hour = 22; // 22:30 is best int second_end_min = 30; string OK_TO_TRADE= "NG"; string SymbolOrder="CHINA50"; double MaxAccountBalance = AccountBalance(); double MinAccountBalance = 1000000; double InitialAccountBalance = AccountBalance(); const string LotName = "Lot_Volume"; const string TPBUY_Value = "TPBUY_Value"; const string SLBUY_Value = "SLBJY_Value"; const string TPSELL_Value = "TPSELL_Value"; const string SLSELL_Value = "SLSELL_Value"; const string STARTDIFF_Value = "STARTDIFF_Value"; string InpToken= "8181067618:AAGqA6S1y59CG5JgxX-c2rcgEt0kvpgCBvY"; // UK Bot int check_telegram_updates = 2150; //miliseconds int capture_previous_update_id= 0; ulong chat_id = 0; int OnInit() { bot.Token(InpToken); // Random Number Generator MathSrand(GetTickCount()); EventSetMillisecondTimer(check_telegram_updates); telegram_to_mt4(); ChartSetInteger(0,CHART_EVENT_MOUSE_MOVE,true); OurInterface.Create(0,"China50 Trading v3.0",0,180,70,400,300); LotLabel.Create(0,"LotLabel",0,10,10,50,30); OurInterface.Add(LotLabel); LotLabel.Text("Lot Size:"); LotSize.Create(0,LotName,0,90,10,160,30); OurInterface.Add(LotSize); LotSize.Text(string(TradeVolume)); TPBUYLabel.Create(0,"TPBUYLabel",0,10,40,50,60); OurInterface.Add(TPBUYLabel); TPBUYLabel.Text("TP BUY:"); TPBUY.Create(0,TPBUY_Value,0,90,40,160,60); OurInterface.Add(TPBUY); TPBUY.Text(string(tpb)); SLBUYLabel.Create(0,"SLBUYLabel",0,10,70,50,90); OurInterface.Add(SLBUYLabel); SLBUYLabel.Text("SL BUY:"); SLBUY.Create(0,SLBUY_Value,0,90,70,160,90); OurInterface.Add(SLBUY); SLBUY.Text(string(slb)); TPSELLLabel.Create(0,"TPSELLLabel",0,10,100,50,120); OurInterface.Add(TPSELLLabel); TPSELLLabel.Text("TP SELL:"); TPSELL.Create(0,TPSELL_Value,0,90,100,160,120); OurInterface.Add(TPSELL); TPSELL.Text(string(tps)); SLSELLLabel.Create(0,"SLSELLLabel",0,10,130,50,150); OurInterface.Add(SLSELLLabel); SLSELLLabel.Text("SL SELL:"); SLSELL.Create(0,SLSELL_Value,0,90,130,160,150); OurInterface.Add(SLSELL); SLSELL.Text(string(sls)); STARTDIFFLabel.Create(0,"STARTDIFFLabel",0,10,160,50,180); OurInterface.Add(STARTDIFFLabel); STARTDIFFLabel.Text("ST_DIFF:"); STARTDIFF.Create(0,STARTDIFF_Value,0,90,160,160,180); OurInterface.Add(STARTDIFF); STARTDIFF.Text(string(sd)); OurInterface.Run(); return(INIT_SUCCEEDED); } void OnDeinit(const int reason) { EventKillTimer(); // Kill the timer OurInterface.Destroy(reason); } void OnTimer() { if(check_telegram_updates >= 2000) telegram_to_mt4(); else Alert("Get Updates Time in Millisecond Should be Greater"); int filehandle = FileOpen("update_id.txt",FILE_READ); int check_used_update_id = (int) (FileReadString(filehandle)); FileClose(filehandle); if(capture_previous_update_id > check_used_update_id) { Print ("New Message Received"); int filehandle1 = FileOpen("update_id.txt",FILE_WRITE|FILE_TXT); FileWriteString(filehandle1,IntegerToString(capture_previous_update_id)); FileClose(filehandle1); int filehandle2 = FileOpen("chat_id.txt",FILE_READ); string check_chat_id = FileReadString(filehandle2); FileClose(filehandle2); int filehandle3 = FileOpen("message_details.txt",FILE_READ|FILE_CSV); string check_message_complete = FileReadString(filehandle3); FileClose(filehandle3); check_message_complete = toUpper(check_message_complete); string check_message = check_message_complete; int close_ea = StringFind(check_message,"CLOSE"); int stop_ea = StringFind(check_message,"STOP"); int close_cocoa = StringFind(check_message,"COCOA"); int close_china = StringFind(check_message,"CHINA"); if ((close_ea!=-1 || stop_ea!=-1) && close_china!=-1) { Print("EA is shutting down..."); ExpertRemove(); // Terminate the EA } } } // End of On Timer void OnTick() { int MagicBuy =1000; int MagicSell = 2000; int Date = Day(); MagicBuy = MagicBuy+Date; MagicSell = MagicSell+Date; datetime Today = StrToTime(StringConcatenate(Year(), ".", Month(), ".", Day())); datetime start_time = Today+start_hour*3600+start_min*60; datetime end_time = Today+end_hour*3600+end_min*60; datetime second_start_time = Today+second_start_hour*3600+second_start_min*60; datetime second_end_time = Today+second_end_hour*3600+second_end_min*60; if ((TimeCurrent()>start_time && TimeCurrent()second_start_time && TimeCurrent()sd) { double buy = price; double slbuy = buy-slb; //buy-previousclose; double tpbuy = buy+tpb; int orderbuy = OrderSend (SymbolOrder,OP_BUY,TradeVolume,buy,0,slbuy,tpbuy,NULL,MagicBuy,0,clrBlue); Sleep(100000); } } if (OK_TO_TRADE=="OK" && CheckSellOpenOrders(MagicSell)==false) { double price = NormalizeDouble(MarketInfo(SymbolOrder,MODE_BID),4); if (previousclose-previousopen>sd) { double sell = price; double slsell = sell+sls; // sell+previousclose; double tpsell = sell-tps; int ordersell = OrderSend (SymbolOrder,OP_SELL,TradeVolume,sell,0,slsell,tpsell,NULL,MagicSell,0,clrBlue); Sleep(100000); } } double CurrentAccountBalance = AccountBalance(); if (MaxAccountBalance < AccountBalance()) MaxAccountBalance = AccountBalance(); if (MinAccountBalance > AccountBalance()) MinAccountBalance = AccountBalance(); Comment ("Initial Account Balance was ",InitialAccountBalance,", Minimum Balance was ",MinAccountBalance,", Max Balance was ",MaxAccountBalance,", Current Balance is ",CurrentAccountBalance," and Current Equity is ",AccountEquity(), ""); } // End of On Tick // 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){ if(sparam == LotName) { string volumeText = ObjectGetString(0, LotName, OBJPROP_TEXT); TradeVolume = StringToDouble(volumeText); ObjectSetString(0, LotName, OBJPROP_TEXT, string(TradeVolume)); return; } if(sparam == TPBUY_Value) { string volumeText = ObjectGetString(0, TPBUY_Value, OBJPROP_TEXT); tpb = StringToDouble(volumeText); ObjectSetString(0, TPBUY_Value, OBJPROP_TEXT, string(tpb)); return; } if(sparam == SLBUY_Value) { string volumeText = ObjectGetString(0, SLBUY_Value, OBJPROP_TEXT); slb = StringToDouble(volumeText); ObjectSetString(0, SLBUY_Value, OBJPROP_TEXT, string(slb)); return; } if(sparam == TPSELL_Value) { string volumeText = ObjectGetString(0, TPSELL_Value, OBJPROP_TEXT); tps = StringToDouble(volumeText); ObjectSetString(0, TPSELL_Value, OBJPROP_TEXT, string(tps)); return; } if(sparam == SLSELL_Value) { string volumeText = ObjectGetString(0, SLSELL_Value, OBJPROP_TEXT); sls = StringToDouble(volumeText); ObjectSetString(0, SLSELL_Value, OBJPROP_TEXT, string(sls)); return; } if(sparam == STARTDIFF_Value) { string volumeText = ObjectGetString(0, STARTDIFF_Value, OBJPROP_TEXT); sd = StringToDouble(volumeText); ObjectSetString(0, STARTDIFF_Value, OBJPROP_TEXT, string(sd)); return; } } // End of CHARTEVENT_OBJECT_ENDEDIT // if an object was clicked } // End of Event Handling bool CheckBuyOpenOrders(int MagicBuy){ // We need to scan all the open and pending orders to see if there are any. // OrdersTotal returns the total number of market and pending orders. // What we do is scan all orders and check if they are of the same symbol as the one where the EA is running. for( int i = 0 ; i < OrdersTotal() ; i++ ) { // We select the order of index i selecting by position and from the pool of market/pending trades. int OrderBO = OrderSelect( i, SELECT_BY_POS, MODE_TRADES ); // If the pair of the order is equal to the pair where the EA is running. if(OrderType()==OP_BUY && OrderMagicNumber()==MagicBuy) return(true); } // If the loop finishes it mean there were no open orders for that pair. return(false); } bool CheckSellOpenOrders(int MagicSell){ // We need to scan all the open and pending orders to see if there are any. // OrdersTotal returns the total number of market and pending orders. // What we do is scan all orders and check if they are of the same symbol as the one where the EA is running. for( int i = 0 ; i < OrdersTotal() ; i++ ) { // We select the order of index i selecting by position and from the pool of market/pending trades. int OrderSO = OrderSelect( i, SELECT_BY_POS, MODE_TRADES ); // If the pair of the order is equal to the pair where the EA is running. if(OrderType()==OP_SELL && OrderMagicNumber()==MagicSell) return(true); } // If the loop finishes it mean there were no open orders for that pair. return(false); } int telegram_to_mt4() { CJAVal js; string headers; char post[], result[]; int res = WebRequest("GET","https://api.telegram.org/bot"+InpToken+"/getUpdates?offset=-1","", NULL, 10000, post, ArraySize(post), result, headers); string server_response_data = CharArrayToString(result); js.Deserialize(result); string update_id_str = js["result"] [0] ["update_id"].ToStr(); int update_id = StrToInteger(update_id_str); string chat_id_str = js["result"][0]["message"]["chat"]["id"].ToStr(); chat_id=(ulong)chat_id_str; string output[]; string output2[]; if(capture_previous_update_id < update_id) { capture_previous_update_id = update_id; string chat_message_text = js["result"] [0] ["message"]["text"].ToStr(); string chat_message_caption = js["result"] [0] ["message"]["caption"].ToStr(); StringSplit(chat_message_text,StringGetCharacter("\n", 0), output); StringSplit(chat_message_caption,StringGetCharacter("\n", 0), output2); if(res==-1) { Print("Error in WebRequest. Error code =",GetLastError()); //--- Perhaps the URL is not listed, display a message about the necessity to add the address MessageBox("Add the address in the list of allowed URLs on tab 'Expert Advisors'","Error",MB_ICONINFORMATION); } else { int filehandle11 = FileOpen("chat_id.txt",FILE_WRITE|FILE_TXT); FileWriteString(filehandle11,IntegerToString(chat_id)); FileClose(filehandle11); int filehandle2 = FileOpen("message_details.txt",FILE_WRITE|FILE_CSV); int count_lines = ArraySize(output); for(int x=0;x