//+------------------------------------------------------------------+ //| Telegram.mq5 | //| Copyright 2020, QUADRELION | //| https://www.quadrelion.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2020, QUADRELION" #property link "https://www.quadrelion.com" #property version "2.06" #define KEYBOARD1 "[[\"\xF5A8 Reporting\"],[\"\xF4B9 Trading\"]]" input uint percentage = 20; //[Margin change in percentage] input double equity_change = 1.0; //[Equity change in percentage] /************************************************ 1093872535:AAHxmV_VJ_opVnEM3Qyg0zzU79sKi6dpw6s MT5 Notification bot uses: 1072805610:AAEUeJJZ-mMr_7-2erUOuC6MVuyC1Fdr6QE *************************************************/ input string botToken = "1145259342:AAFHCX6AMM7a3vgNVD9bOQ5fsyTXhX7-_Wg"; input string channelName = "VINITERON:Varchev"; // [Channel or Group name] /* input string botToken = "951743402:AAFk5Wqzg5X4hvba-nSQGKy6nXtCfslFhYs"; input string channelName = "VINITERON:BenchMark"; // [Channel or Group name] */ /* input string botToken = "1116038453:AAGF-dvzHZxKlCZpwuJa3MGqjldn4pFFzh8"; input string channelName = "Nick:BenchMark"; // [Channel or Group name] */ double old_margin = 0; double old_equity = 0; bool trading_clicked = false; bool show_weekly_report = false; struct output_data_array { string symbol; double net_lots; double profit; double open_price; double short_price_volume ; double short_volume; double long_price_volume ; double long_volume; string tickets; }; struct TelegramMessage { long MessageId; string Text; datetime TimestampGMT; }; #import "Telegram Connector MT5.ex5" bool TelegramInit(const string botToken, const long channelId); bool TelegramInit(const string botToken, const string channelName); void TelegramGetUpdates(TelegramMessage& messages[]); string BuildReplyMarkup(const string keyboard, const bool resize = false,const bool selective = false); string BuildReplyKeyboardHide(); bool TelegramSendText(const string text, const bool htmlMode = false, const string replyMarkup = NULL); bool TelegramSendPhoto(const string path, const string title, const bool useCommonFlag = false, const int timeout = 10000); #import //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { if(!TelegramInit(botToken, channelName)) { return INIT_FAILED; } TelegramSendText("---=== "+AccountInfoString(ACCOUNT_NAME)+" ===---", false, BuildReplyMarkup(KEYBOARD1)); //OnTick(); EventSetTimer(3); return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- destroy timer EventKillTimer(); } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //double margin = AccountInfoDouble(ACCOUNT_MARGIN_LEVEL); double margin_level = AccountInfoDouble(ACCOUNT_MARGIN_LEVEL); double changeMargin = 0; if(margin_level==0) { if(show_weekly_report) { datetime end_date = TimeCurrent(); datetime start_date =iTime(_Symbol,PERIOD_W1,0); out_deals(start_date,end_date); show_weekly_report = false; } } else { changeMargin = MathAbs(1 - double(old_margin/margin_level)); show_weekly_report = true; } double balance = AccountInfoDouble(ACCOUNT_BALANCE); double equity = AccountInfoDouble(ACCOUNT_EQUITY); double account_pnl = equity - balance; if(old_equity ==0) old_equity = equity; double changeEq = MathAbs(equity/old_equity - 1); //Print("change: ",changeMargin); // Print("percentage: ",percentage); double perc = percentage/100.0; //Print(perc); //Print("Send:",(changeMargin >= perc || changeEq >= perc)); //Print("changeMargin: ",changeMargin); //Print("changeEq: ",changeEq); if(changeMargin >= perc || changeEq >= (double)(equity_change/100.0) || trading_clicked == true) { output_data_array data_array[]; int sizer = 1; int totalPositions = PositionsTotal(); for(int i =0; i < totalPositions; i++) { bool exists = false; string currentSym = PositionGetSymbol(i); for(int j =0; j0) data_array[j].open_price = data_array[j].long_price_volume/data_array[j].long_volume; else if(data_array[j].net_lots<0) data_array[j].open_price = data_array[j].short_price_volume/data_array[j].short_volume; output+= StringFormat("\n[%+.2f] %s ",data_array[j].profit*toEUR, data_array[j].symbol); if(data_array[j].net_lots!=0) { string helper_digits = StringFormat("\n........................%%.2f @ %%.%df",decimalPlacesForPairs(data_array[j].symbol)); output+= StringFormat(helper_digits,data_array[j].net_lots,data_array[j].open_price); } } output+= StringFormat("\n==========================\n%+.2f [Floating]", account_pnl*toEUR); old_margin = margin_level; old_equity = equity; trading_clicked = false; TelegramSendText(output); } } //+------------------------------------------------------------------+ //| Timer function | //+------------------------------------------------------------------+ void OnTimer() { TelegramMessage messages[]; TelegramGetUpdates(messages); for(int i = 0; i < ArraySize(messages); i++) { if(StringFind(messages[i].Text, " Trading") >= 0) { trading_clicked = true; //OnTick(); //TelegramSendText("Trading callback"); } if(StringFind(messages[i].Text, " Reporting") >= 0) { datetime end_date = TimeCurrent(); datetime start_date =iTime(_Symbol,PERIOD_W1,0); //Print(start_date+"-"+end_date); out_deals(start_date,end_date); //TelegramSendText("", false, BuildReplyKeyboardHide()); } } } //+------------------------------------------------------------------+ //| Trade function | //+------------------------------------------------------------------+ void OnTrade() { //--- } //+------------------------------------------------------------------+ //| TradeTransaction function | //+------------------------------------------------------------------+ void OnTradeTransaction(const MqlTradeTransaction& trans, const MqlTradeRequest& request, const MqlTradeResult& result) { //--- } //+------------------------------------------------------------------+ //| ChartEvent function | //+------------------------------------------------------------------+ void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam) { //--- } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ double EUR_ConversionRate() { string CCY = AccountInfoString(ACCOUNT_CURRENCY); //Print("Account CCY: ", CCY); if(CCY == "USD") return(1/ SymbolInfoDouble("EURUSD", SYMBOL_ASK)); else if(CCY == "BGN") return(1/1.95583); else return(1); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ long decimalPlacesForPairs(string sPair) { return SymbolInfoInteger(sPair,SYMBOL_DIGITS); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void sort(output_data_array& array[]) { bool sorted = false; while(!sorted) { sorted = true; for(int i = 1; i= array[i-1].profit) { output_data_array tmp = array[i]; array[i] = array[i-1]; array[i-1] = tmp; sorted = false; } } } } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void out_deals(datetime start_date, datetime end_date) { output_data_array out_deals_array[]; HistorySelect(start_date,end_date); int total = HistoryDealsTotal(); for(int i =0; i