Adwizard/Utils/ExpertHistory.mqh
212 行
16 KiB
MQL5
Yuriy Bykov
2025-04-11 13:28:40 +03:00
<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| ExportHistory.mqh |
//| Copyright 2021-2024, Yuriy Bykov |
//| https://www.mql5.com/ru/users/antekov |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021-2024, Yuriy Bykov"
#property link "https://www.mql5.com/ru/users/antekov"
#property version "1.00"
#include "Macros.mqh"
//+------------------------------------------------------------------+
//| -:A?>@B 8AB>@88 A45;>: 2 D09; |
//+------------------------------------------------------------------+
class CExpertHistory {
private:
static string s_sep; // !8<2>;-@0745;8B5;L
static int s_file; // %5=4; D09;0 4;O 70?8A8
static string s_columnNames[]; // 0AA82 =0720=89 AB>;1F>2
// 0?8AL 8AB>@88 A45;>: 2 D09;
static void WriteDealsHistory();
// 0?8AL >4=>9 AB@>:8 8AB>@88 A45;>: 2 D09;
static void WriteDealsHistoryRow(const string &fields[]);
// >;CG5=85 40BK ?5@2>9 A45;:8
static datetime GetStartDate();
// $>@<8@>20=85 8<5=8 D09;0
static string GetHistoryFileName();
public:
// -:A?>@B 8AB>@88 A45;>:
static void Export(
string exportFileName = "", // <O D09;0 4;O M:A?>@B0. A;8 ?CAB>5, B> 8<O 1C45B A35=5@8@>20=>
int commonFlag = FILE_COMMON // !>E@0=OBL D09; 2 >1I59 ?0?:5 40==KE
);
};
// !B0B8G5A:85 ?5@5<5==K5 :;0AA0
string CExpertHistory::s_sep = ",";
int CExpertHistory::s_file;
string CExpertHistory::s_columnNames[] = {"DATE", "TICKET", "TYPE",
"SYMBOL", "VOLUME", "ENTRY", "PRICE",
"STOPLOSS", "TAKEPROFIT", "PROFIT",
"COMMISSION", "FEE", "SWAP",
"MAGIC", "COMMENT"
};
//+------------------------------------------------------------------+
//| -:A?>@B 8AB>@88 A45;>: |
//+------------------------------------------------------------------+
void CExpertHistory::Export(string exportFileName = "", int commonFlag = FILE_COMMON) {
// A;8 8<O D09;0 =5 7040=>, B> A35=5@8@C5< 53>
if(exportFileName == "") {
exportFileName = GetHistoryFileName();
}
// B:@K205< D09; =0 70?8AL 2 =C6=>9 ?0?:5 40==KE
s_file = FileOpen(exportFileName, commonFlag | FILE_WRITE | FILE_CSV | FILE_ANSI, s_sep);
// A;8 D09; >B:@KB, B>
if(s_file > 0) {
// 0?8AK205< 8AB>@8N A45;>:
WriteDealsHistory();
// 0:@K205< D09;
FileClose(s_file);
} else {
PrintFormat(__FUNCTION__" | ERROR: Can't open file [%s]. Last error: %d", exportFileName, GetLastError());
}
}
//+------------------------------------------------------------------+
//| 0?8AL 8AB>@88 A45;>: 2 D09; |
//+------------------------------------------------------------------+
void CExpertHistory::WriteDealsHistory() {
// 0?8AK205< 703>;>2>: A =0720=8O<8 AB>;1F>2
WriteDealsHistoryRow(s_columnNames);
// 5@5<5==K5 4;O A2>9AB2 :064>9 A45;:8
uint total;
ulong ticket = 0;
long entry;
double price;
double sl, tp;
double profit, commission, fee, swap;
double volume;
datetime time;
string symbol;
long type, magic;
string comment;
// 5@Q< 2AN 8AB>@8N
HistorySelect(0, TimeCurrent());
total = HistoryDealsTotal();
// ;O 2A5E A45;>:
for(uint i = 0; i < total; i++) {
// A;8 A45;:0 CA?5H=> 2K1@0=0, B>
if((ticket = HistoryDealGetTicket(i)) > 0) {
// >;CG05< 7=0G5=8O 5Q A2>9AB2
time = (datetime)HistoryDealGetInteger(ticket, DEAL_TIME);
type = HistoryDealGetInteger(ticket, DEAL_TYPE);