Telegram BOT reply to messages with quote

 

Hi!

I'm coding my very first EA, and i'm stuck with this issue:
I would like that evey time that i'm modifying the TP or SL, the Bot reply to the original message and send out a message with the update.

I've found this topic here, but i haven't understood it...

https://www.mql5.com/en/forum/347358


Do you have any idea on how to manage this issue?
Thanks, have a great day!

telegram.mqh reply to msg - I'm trying to make updates about my trades when they hit a TP and send their messages in Telegram
telegram.mqh reply to msg - I'm trying to make updates about my trades when they hit a TP and send their messages in Telegram
  • 2020.07.23
  • www.mql5.com
What i'm trying to do now is to make updates about my trades when they hit a tp and send a 'tp hit' message in telegram. And since the orderticket is the same when i close the trade, i'm thinking that it should recognise the message in telegram and replies to it right
 
Fabio Brondo:

Hi!

I'm coding my very first EA, and i'm stuck with this issue:
I would like that evey time that i'm modifying the TP or SL, the Bot reply to the original message and send out a message with the update.

I've found this topic here, but i haven't understood it...

https://www.mql5.com/en/forum/347358


Do you have any idea on how to manage this issue?
Thanks, have a great day!

Hi . 

Are you associating your ea trades to telegram message ids ? (and are these stored?)

 
Lorentzos Roussos #:

Hi . 

Are you associating your ea trades to telegram message ids ? (and are these stored?)

Hi Lorentzos!

Currently i'm not associating anything... I'm just checking if the number of total orders (or pending orders) is increased, the BOT  sends out the right message.

Thank you for your fast feedback!

 
Fabio Brondo #:
Hi Lorentzos!

Currently i'm not associating anything... I'm just checking if the number of total orders (or pending orders) is increased, the BOT  sends out the right message.

Thank you for your fast feedback!

In general what it does is this :

  1. You post (anything) it returns a message # (if i recall)
  2. So you can utilize this when you are posting the first time about a trade and you can say i have this trade which is this message number in telegram.
  3. Then when you want to update about the trade you can instruct the bot to reply to the message number your ea has linked to an order.
  4. If you want to update the sl ,and it has already been updated once, you would not prefer to reply to the first message of the trade again but you would maintain the latest message # about that trade so it could make logical sense to the reader.
That is it really , the broker gives you numbers for identifying trades and the telegram gives you numbers for identifying messages . You just "chef" them together.
 
Lorentzos Roussos #:

In general what it does is this :

  1. You post (anything) it returns a message # (if i recall)
  2. So you can utilize this when you are posting the first time about a trade and you can say i have this trade which is this message number in telegram.
  3. Then when you want to update about the trade you can instruct the bot to reply to the message number your ea has linked to an order.
  4. If you want to update the sl ,and it has already been updated once, you would not prefer to reply to the first message of the trade again but you would maintain the latest message # about that trade so it could make logical sense to the reader.
That is it really , the broker gives you numbers for identifying trades and the telegram gives you numbers for identifying messages . You just "chef" them together.

So basically to recap:

  1. Placing an order, the broker is giving me a number 
  2. Than i have to figure out how to find the ID of the message sent by the EA on Telegram and merge this one and the number of the order.
  3. Write the code that if i'm modifying anything on the order, EA replaces the "mixed number" with this new one, with the same rule of point 2.
  4. Send out the message with the updated value modified
Now (aside that i have to figure out how to do all the 4 points above), where i'm storing / how i can store all the "mixed numbers"?
 
Fabio Brondo #:

So basically to recap:

  1. Placing an order, the broker is giving me a number 
  2. Than i have to figure out how to find the ID of the message sent by the EA on Telegram and merge this one and the number of the order.
  3. Write the code that if i'm modifying anything on the order, EA replaces the "mixed number" with this new one, with the same rule of point 2.
  4. Send out the message with the updated value modified
Now (aside that i have to figure out how to do all the 4 points above), where i'm storing / how i can store all the "mixed numbers"?

  1. Yes you open up a trade and the broker responds with a ticket (not -1 assuming mt4) , let's say 15455
  2. You then send a message with the bot and it tells you the message is #11 in the channel or the chat (if i recall ,haven't telegrammed a lot lately)

So now you can construct your own variable 

struct myTradeInfo{
long order_id,message_id
void reset(){order_id=-1;message_id=-1;}
void set_order_ticket(long _new_order_id){order_id=_new_order_id;}
void set_message_id(long _new_message_id){message_id=_new_message_id;}
void save(int file_handle){
FileWriteLong(file_handle,order_id);
FileWriteLong(file_handle,message_id);
}
void load(int file_handle){
reset();
order_id=(long)FileReadLong(file_handle);
message_id=(long)FileReadLong(file_handle);
}
};

Structures, Classes and Interfaces - Data Types - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5

And then you can have an array(list) of these variables and add and remove from it as trades come and go , and also save it to the hard drive with an identifier unique to the ea , symbol and timeframe.

Array Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5

File Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5


 

 
Lorentzos Roussos #:

  1. Yes you open up a trade and the broker responds with a ticket (not -1 assuming mt4) , let's say 15455
  2. You then send a message with the bot and it tells you the message is #11 in the channel or the chat (if i recall ,haven't telegrammed a lot lately)

So now you can construct your own variable 

Structures, Classes and Interfaces - Data Types - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5

And then you can have an array(list) of these variables and add and remove from it as trades come and go , and also save it to the hard drive with an identifier unique to the ea , symbol and timeframe.

Array Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5

File Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5


 

Wow @Lorentzos Roussos thank you!
Now i have a little bit to study, I will type to you again and let you know if everything is working (i hope!) or not!

Thanks again

 
Fabio Brondo #:

Wow @Lorentzos Roussos thank you!
Now i have a little bit to study, I will type to you again and let you know if everything is working (i hope!) or not!

Thanks again

awesome

 

 
Lorentzos Roussos #:

awesome

 

Hi @Lorentzos!
Quick update:

I've had a look to the documentation you provided, and this is the result:

//FILE CREATION 
    string OrderLog = "OrderLog.csv";
    int OrderLogHandle = FileOpen(OrderLog, FILE_WRITE|FILE_CSV, ',');

    // Print error if file don't work correctly
    if(OrderLogHandle < 0)
    {
        Print("Error: ", GetErrorDescription(OrderLogHandle));
        Sleep(10000);
                
        return(0);
    }    
    
    // write header
    FileWrite(OrderLogHandle, "Order ID", "Open Price", "Open Time", "Symbol", "Lots", "Stop Loss", "Take Profit");
  
   [...]
   Order Messages management and Text
   [...]
            
            // Write position on CSV file
            FileWrite(OrderLogHandle, 
                        OrderTicket(), 
                        DoubleToString(OrderOpenPrice(), _Digits), 
                        OrderOpenTime(), 
                        OrderSymbol(), 
                        DoubleToString(OrderLots(), 2), 
                        DoubleToString(OrderStopLoss(), Digits), 
                        DoubleToString(OrderTakeProfit(), Digits)
                    );


        FileClose(OrderLogHandle);

Now the problem is that the file is overwriting itself all the time, basically only updating the first line after the header.

How can I add rows, instead of overwriting them every time?

It's okay if it's deleting closed positions but I need to add the others....


Also I'm still trying to figure out how to get the id of the message I'm sending, do you have any advice on this?


Thank you!

 
Fabio Brondo #:

Hi @Lorentzos!
Quick update:

I've had a look to the documentation you provided, and this is the result:

Now the problem is that the file is overwriting itself all the time, basically only updating the first line after the header.

How can I add rows, instead of overwriting them every time?

It's okay if it's deleting closed positions but I need to add the others....


Also I'm still trying to figure out how to get the id of the message I'm sending, do you have any advice on this?


Thank you!

Nice , so you will store your trades on csv only ?

That will make it more complex vs having it in a structure and saving it as binary when needed.(and loading it)

I will review the telegram code later , i'm really rusty on this one and let you know where the id is returned.

 
Lorentzos Roussos #:

Nice , so you will store your trades on csv only ?

That will make it more complex vs having it in a structure and saving it as binary when needed.(and loading it)

I will review the telegram code later , i'm really rusty on this one and let you know where the id is returned.


Thank you! Do you have any idea on how to solve the problem of the append of the CSV file btw?

Reason: