Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 11
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
How can I tell from the EA that an indicator alert has arrived ?
Do you need an alert?
Usually with an alert some signal is written to the buffer, if so, you need to read the value from the buffer.
But you need to see the code to tell exactly. Or data window ctrl+D with indicator values on bar with alert.
In the appendix to that article, it is called sHistoryExport.mq5. My compiler does not accept functions whose names begin with "HistoryDeal..." and identifiers beginning with "DEAL_...". Here is the code:
#property link "https://www.mql5.com"
#property version "1.00"
#property script_show_inputs
input bool UseDateFrom = false; // Указывать дату начала
input datetime DateFrom=0; // Дата начала
input bool UseDateTo=false; // Указывать дату окончания
input datetime DateTo=0; // Дата окончания
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart(){
datetime from,to;
if(UseDateFrom){
from=DateFrom;
}
else{
from=0;
}
if(UseDateTo){
to=DateTo;
}
else{
to=TimeCurrent();
}
if(!HistorySelect(from,to)){
Alert("Ошибка выделение истории");
return;
}
string FileName="history.csv";
int h=FileOpen(FileName,FILE_WRITE|FILE_ANSI|FILE_CSV,";");
if(h==INVALID_HANDLE){
Alert("Ошибка открытия файла");
return;
}
// первая строка, что бы знать, где что находится
FileWrite(h,"Time","Deal","Order","Symbol","Type","Direction","Volume","Price","Comission","Swap","Profit","Comment");
// по всем сделкам
for(int i=0;i<HistoryDealsTotal();i++){
ulong ticket=HistoryDealGetTicket(i);
if(ticket!=0){
long type=HistoryDealGetInteger(ticket,DEAL_TYPE);
if(type==DEAL_TYPE_BUY || type==DEAL_TYPE_SELL){
long entry=HistoryDealGetInteger(ticket,DEAL_ENTRY);
FileWrite(h,(datetime)HistoryDealGetInteger(ticket,DEAL_TIME),
ticket,
HistoryDealGetInteger(ticket,DEAL_ORDER),
HistoryDealGetString(ticket,DEAL_SYMBOL),
(type==DEAL_TYPE_BUY?"buy":"sell"),
(entry==DEAL_ENTRY_IN?"in":(entry==DEAL_ENTRY_OUT?"out":"in/out")),
DoubleToString(HistoryDealGetDouble(ticket,DEAL_VOLUME),2),
HistoryDealGetDouble(ticket,DEAL_PRICE),
DoubleToString(HistoryDealGetDouble(ticket,DEAL_COMMISSION),2),
DoubleToString(HistoryDealGetDouble(ticket,DEAL_SWAP),2),
DoubleToString(HistoryDealGetDouble(ticket,DEAL_PROFIT),2),
HistoryDealGetString(ticket,DEAL_COMMENT)
);
}
}
else{
Alert("Ошибка выделения сделки, повторите попытку");
FileClose(h);
return;
}
}
FileClose(h);
Alert("Сохранение выполнено, см. файл "+FileName);
}
//+------------------------------------------------------------------+
To be honest, I didn't check it at all - I don't have MT4 history with trades handy - I just wrote it "on my knees" looking at Dmitry's script...
#property link "https://www.mql5.com"
#property version "1.00"
#property script_show_inputs
input bool UseDateFrom = false; // Указывать дату начала
input datetime DateFrom=0; // Дата начала
input bool UseDateTo=false; // Указывать дату окончания
input datetime DateTo=0; // Дата окончания
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart(){
//---
datetime from,to;
if(UseDateFrom) from=DateFrom;
else from=0;
if(UseDateTo) to=DateTo;
else to=TimeCurrent();
//---
string FileName="history.csv";
int h=FileOpen(FileName,FILE_WRITE|FILE_ANSI|FILE_CSV,";");
if(h==INVALID_HANDLE){
Alert("Ошибка открытия файла");
return;
}
//--- первая строка, что бы знать, где что находится
FileWrite(h,"OpenTime","CloseTime","Ticket","Symbol","Type","Volume","OpenPrice","ClosePrice","Comission","Swap","Profit","Comment");
//--- по всем сделкам
for(int i=OrdersHistoryTotal()-1; i>=0; i--) {
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) {
if(OrderOpenTime()<from) continue;
if(OrderOpenTime()>to) break;
int type=OrderType();
if(type>OP_SELL) continue;
int dg=(int)SymbolInfoInteger(OrderSymbol(),SYMBOL_DIGITS);
int ticket=OrderTicket();
FileWrite(h,(datetime)OrderOpenTime(),
(datetime)OrderCloseTime(),
ticket,
OrderSymbol(),
(type==OP_BUY?"buy":"sell"),
DoubleToString(OrderLots(),2),
DoubleToString(OrderOpenPrice(),dg),
DoubleToString(OrderClosePrice(),dg),
DoubleToString(OrderCommission(),2),
DoubleToString(OrderSwap(),2),
DoubleToString(OrderProfit(),2),
OrderComment()
);
}
else{
Alert("Ошибка выделения сделки, повторите попытку");
FileClose(h);
return;
}
}
//---
FileClose(h);
Alert("Сохранение выполнено, см. файл "+FileName);
}
//+------------------------------------------------------------------+
Thanks for the clarification, Artem!
But there is one more question.
Sometimes an EA opens an order immediately after the SL has triggered. Thus, it can drain the entire deposit on one candle.
How to correctly specify the delay for opening the next order after the closing of the current order? The delay time should be equal to the candlestick time and be calculated automatically.
Thank you!
Thanks for the clarification, Artem!
But there is one more question.
Sometimes an EA opens an order immediately after the SL triggering. Thus, we can let the entire deposit to be drained on one candle.
How to correctly specify the delay for the opening of the next order after the closure of the current order? The delay time should be equal to the candlestick time and be calculated automatically.
Thank you!
The logic is as follows:
Well, to be honest, I haven't checked it at all - I don't have MT4 deal history handy - I just wrote it "on my knees" looking at Dimitri's script...
It works. At first glance everything seems fine, opening/closing only writes not the date, but I know how to fix that. Thank you very much.
Help with this advisor on the previous build worked and now it doesn't!!!!
What do you need help with? Is there something you can't do yourself?
There is help and discussion here, but not gratuitous wish fulfillment![](https://c.mql5.com/3/109/pardon.gif)
What do you need help with? Is there something you can't do yourself?
Help and discussion here, but not gratuitous wish fulfilment
I don't understand what the mistake is?