How to save data OrderOpenPrice and OrderStopLoss

When I open order it’s know data right?
but now I want to save data OpenOrderPrice or save data SL TP and can be print return found the data

How does its done?

If the trade is open for Buy or Sell or pending for BuyStop, SellStop, BuyLimit and SellLimit - you can loop through the trades but this does not save the data.

Here is an example of how to do it:

This is the example from that page:

  int handle=FileOpen("OrdersReport.csv",FILE_WRITE|FILE_CSV,"\t");
  if(handle<0) return(0);
  // write header
  FileWrite(handle,"#","open price","open time","symbol","lots");
  int total=OrdersTotal();
  // write open orders
  for(int pos=0;pos<total;pos++)
    {
     if(OrderSelect(pos,SELECT_BY_POS)==false) continue;
     FileWrite(handle,OrderTicket(),OrderOpenPrice(),OrderOpenTime(),OrderSymbol(),OrderLots());
    }
  FileClose(handle);

You have to name the file:
int handle=FileOpen("OrdersReport.csv",FILE_WRITE|FILE_CSV,"\t");
For your case you could keep it as OrdersReport.csv.
CSV, means comma separated variables…so the file will be “data, data2, data3” with commas between the bits of data. You can use Excel or other spreadsheet code to see the data.

FileWrite(handle,"#","open price","open time","symbol","lots");
This line will put the headings on the first line.

int total=OrdersTotal();
total will hold the number of orders you can see in your account.

  for(int pos=0;pos<total;pos++)
    {
     if(OrderSelect(pos,SELECT_BY_POS)==false) continue;
     FileWrite(handle,OrderTicket(),OrderOpenPrice(),OrderOpenTime(),OrderSymbol(),OrderLots());
    }

Now you loop through the Orders and on each new line you put the correct data under the header line created already.

FileClose(handle);
When you have filled the file, you want to close the file. If you don’t, I think your Excel won’t be able to open it as it is already open.

For your purposes, as you are looping the OrderTotal(), you can add in OrderStopLoss() and OrderTakeProfit(), in new columns and delete the ones that you don’t care about.

1 Like

Additional note:
Your files you create can only be found in:
…AppData/ Roaming / MetaQuotes / Terminal / 8401984_big number with letters / MQL4 / Files
You can save and retrieve your data from this folder and I believe subfolders of this “Files” folder.

You can’t keep it for example in the MQL4 folder as that save is blocked by the programming of MT4.

1 Like