Telegram BOT reply to messages with quote - page 2

 
Fabio Brondo #:

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

i'll try that now since thats the method you want to utilize and let you know .  ☕️

 
Fabio Brondo #:

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

@Fabio Brondo try this , you needed to seek to the end of file and open it for read + write when it already existed 

#property copyright "Read the discussion"
#property link      "https://www.mql5.com/en/forum/440593"
#property strict

string system_folder="TELYBOT_",csv_file="the_file.csv";
int tr=0;
datetime barstamp=0;
int OnInit()
  {
//---
  tr=0;
  barstamp=0;
//---
  return(INIT_SUCCEEDED);
  }

//function to write to file 
  void write_order_to_csv(int order_ticket,string _path){
  if(OrderSelect(order_ticket,SELECT_BY_TICKET)){
  //if file exists add line 
  if(FileIsExist(_path)){
  //open file 
    int f=FileOpen(_path,FILE_WRITE|FILE_READ|FILE_CSV,',');
    if(f!=INVALID_HANDLE){
    ulong bytes=FileSize(f);
    FileSeek(f,bytes,SEEK_SET);
    int sydigs=(int)SymbolInfoInteger(OrderSymbol(),SYMBOL_DIGITS);
    FileWrite(f,OrderTicket(), 
                DoubleToString(OrderOpenPrice(),sydigs), 
                OrderOpenTime(), 
                OrderSymbol(), 
                DoubleToString(OrderLots(), 2), 
                DoubleToString(OrderStopLoss(),sydigs), 
                DoubleToString(OrderTakeProfit(),sydigs));    
    FileClose(f);
    }else{
    Print("Cannot open file "+_path);
    }
  }
  //if file does not exist create it and write header 
  else{
  int f=FileOpen(_path,FILE_WRITE|FILE_CSV,',');
  if(f!=INVALID_HANDLE){
  FileWrite(f, "Order ID", "Open Price", "Open Time", "Symbol", "Lots", "Stop Loss", "Take Profit");
  int sydigs=(int)SymbolInfoInteger(OrderSymbol(),SYMBOL_DIGITS);
  FileWrite(f,OrderTicket(), 
              DoubleToString(OrderOpenPrice(),sydigs), 
              OrderOpenTime(), 
              OrderSymbol(), 
              DoubleToString(OrderLots(), 2), 
              DoubleToString(OrderStopLoss(),sydigs), 
              DoubleToString(OrderTakeProfit(),sydigs));
  FileClose(f);
  }else{
  Print("Cannot create file "+_path);
  }
  }
  
  }else{
  Print("Cannot select order "+IntegerToString(order_ticket));
  }
  }
void OnTick()
  {
//---
  if(Time[0]>barstamp)
    {
    barstamp=Time[0];
    int ticket=OrderSend(_Symbol,OP_BUY,0.01,Ask,1000,0,0,NULL,0,0,clrBlue);
    if(ticket!=-1){
      write_order_to_csv(ticket,system_folder+"\\"+csv_file);
      }
    }
  }

 
Lorentzos Roussos #:

@Fabio Brondo try this , you needed to seek to the end of file and open it for read + write when it already existed 

@Lorentzos Thank you! Sorry for the stupid questions below, but to be shure, i think that i have to modify the code in those points:

string system_folder="TELYBOT_",csv_file="the_file.csv"; //TELYBOT to be replaced with a path like C:\Users etc... and the_file.csv to be replaced with the name of the file?
int OnInit() // I currently don't have it on my code, where i have to put it?
 void write_order_to_csv(int order_ticket,string _path){  //_path have to be modified with something again like C:/Users... ?
    int f=FileOpen(_path,FILE_WRITE|FILE_READ|FILE_CSV,','); //instead of "f" I have to use the file name?
Print("Cannot select order "+IntegerToString(order_ticket)); // Order Ticket will be recovered automatically?
 write_order_to_csv(ticket,system_folder+"\\"+csv_file); // Something- or what to modify here?
FileWrite(f,OrderTicket(), 
              DoubleToString(OrderOpenPrice(),sydigs),  //sydigs, what is that? I've never see it before :(
              OrderOpenTime(), 
              OrderSymbol(), 
              DoubleToString(OrderLots(), 2), 
              DoubleToString(OrderStopLoss(),sydigs), 
              DoubleToString(OrderTakeProfit(),sydigs));

Compiling your file returns 0 errors, but it's not recording nothig yet, not even generating the file...

 
Fabio Brondo #:

@Lorentzos Thank you! Sorry for the stupid questions below, but to be shure, i think that i have to modify the code in those points:

Compiling your file returns 0 errors, but it's not recording nothig yet, not even generating the file...

My bad i did not give instructions of what it does . It places a trade on every new bar so to test it place it on an M1 chart and in 3 minutes you will have your test completed.

  1. The system_folder is a subfolder inside "MQL4\Files\" , i am rarely using the common folder .
  2. Is it a script you are running to execute trades ? (it has start?)
  3. You don't have to modify the _path , you just send the file you want to write along with the folder if you wish to have files in a subfolder . 
  4. f is the file handle variable , if you are writing in one snap , that means the function runs opens the file writes and closes it (without it remaining open) , then there is no need to name it in a way that stands out from other file handles . Unless it helps you.
  5. The order ticket must be sent to the function by you after you verify it was succesfully opened
 
Lorentzos Roussos #:

My bad i did not give instructions of what it does . It places a trade on every new bar so to test it place it on an M1 chart and in 3 minutes you will have your test completed.

  1. The system_folder is a subfolder inside "MQL4\Files\" , i am rarely using the common folder .
  2. Is it a script you are running to execute trades ? (it has start?)
  3. You don't have to modify the _path , you just send the file you want to write along with the folder if you wish to have files in a subfolder . 
  4. f is the file handle variable , if you are writing in one snap , that means the function runs opens the file writes and closes it (without it remaining open) , then there is no need
  5.  to name it in a way that stands out from other file handles . Unless it helps you.
  6. The order ticket must be sent to the function by you after you verify it was succesfully opened


So if i've got your suggestion this part will look like this:

1:

string system_folder="C:\Users\[...]\MQL4\Files",csv_file="OrderLog.csv";
int tr=0;
datetime barstamp=0;

2.  Yes, it has Start,init, deinit.

3.Ok i will keep _path

4. I don't care of the name, if it works :)

5. It will be like this:

if(Time[0]>barstamp)
    {
    barstamp=Time[0];
    int ticket=OrderSend(_Symbol,OP_BUY,0.01,Ask,1000,0,0,NULL,0,0,clrBlue);
    if(ticket!=-1){
      write_order_to_csv(ticket,C:\Users\[...]\MQL4\Files+"\\"+OrderLog.csv");
      }

 
Fabio Brondo #:


So if i've got your suggestion this part will look like this:

1:

2.  Yes, it has Start,init, deinit.

3.Ok i will keep _path

4. I don't care of the name, if it works :)

5. It will be like this:

You don't need to send the C:\users... stuff , it will take care of it on its own.

 

Hi @Lorentzos Roussos, thanks for your help, now i'm able to store all the data in the CSV file!

I have one more question to you, i'm still not able to get the ID of the message that is sent each time a new order is opened or closed.

I'm currently using the bot.SendMessage function, do you know how i can get the ID of this message with this function?

Thanks for your support!

 
Fabio Brondo #:

Hi @Lorentzos Roussos, thanks for your help, now i'm able to store all the data in the CSV file!

I have one more question to you, i'm still not able to get the ID of the message that is sent each time a new order is opened or closed.

I'm currently using the bot.SendMessage function, do you know how i can get the ID of this message with this function?

Thanks for your support!

Hello . 

Are you using the telegramL.mqh library ? 

If you do there is a function :

   int SendMessageGetID(bool replying_to_message,
                        long message_id_replying_to,
                        long    _chat_id,//same as send message
                        string  _text,//same as send message
                        string  _reply_markup,//same as send message
                        bool    _as_HTML,//same as send message
                        bool    _silently,//same as send message
                        long &return_id)

which can be called like so :

    string txt="i wear my sunglasses at nigh";
    long last_message_id=-1;
    int res=bot.SendMessageGetID(false,0,chat_id,txt,NULL,false,false,last_message_id);

The above will fill the variable "last_message_id" with the location of the newly posted message on the telegram chat (i.e. first second third post etc)

You can also check if it succeeded if it is bigger than -1 (if last_message_id >-1)

Conveniently , when you want to reply to a message you can use the same function , turn replying_to_message to true and dump the message id you have storred in message_id_replying_to

 
Lorentzos Roussos #:

Hello . 

Are you using the telegramL.mqh library ? 

If you do there is a function :

which can be called like so :

The above will fill the variable "last_message_id" with the location of the newly posted message on the telegram chat (i.e. first second third post etc)

You can also check if it succeeded if it is bigger than -1 (if last_message_id >-1)

Conveniently , when you want to reply to a message you can use the same function , turn replying_to_message to true and dump the message id you have storred in message_id_replying_to

Yes, i'm using the Telegram.mqh library but i'm starting thinking that i've found an already edited version...
You can find it attached...

Do you have a "clean" version?

In mine there is no function SendMessageGetID

Files:
Telegram.mqh  64 kb
 
Fabio Brondo #:

Yes, i'm using the Telegram.mqh library but i'm starting thinking that i've found an already edited version...
You can find it attached...

Do you have a "clean" version?

In mine there is no function SendMessageGetID

Oh , the TelegramL.mqh The clean version does not have the get message id modification .

Here is the latest 

Files:
TelegramL.mqh  84 kb
Reason: