TradeReportExporter

Author: Anatoliy Migachyov
Miscellaneous
Uses files from the file systemIt writes information to file
0 Views
0 Downloads
0 Favorites
TradeReportExporter
ÿþ//+------------------------------------------------------------------+

//|                                          TradeReportExporter.mq5 |

//|                                               Anatoliy Migachyov |

//|                                             https://www.mql5.com |

//+------------------------------------------------------------------+

#property copyright "Anatoliy Migachyov"

#property link      "https://www.mql5.com"

#property version   "1.00"

#include <Trade\DealInfo.mqh> // ;O @01>BK A 8AB>@859 A45;>:



// $C=:F8O 4;O D>@<0B8@>20=8O G8A5; (70<5=0 B>G:8 =0 70?OBCN, 5A;8 =C6=>)

string FormatDouble(double value, int digits) {

    string result = DoubleToString(value, digits);

    StringReplace(result, ".", ","); // 0<5=O5< B>G:C =0 70?OBCN

    return result;

}



// $C=:F8O 4;O ?>;CG5=8O :><8AA88 A CG5B>< <8=8<0;L=>3> 7=0G5=8O

double GetCommissionWithMin(double commission) {

    double minCommission = 0.01; // 8=8<0;L=0O :><8AA8O (1 F5=B)

    return (commission < minCommission) ? minCommission : commission;

}



// $C=:F8O 4;O >?@545;5=8O B8?0 8=AB@C<5=B0 (:@8?B>20;NB0 8;8 20;NB0)

bool IsCryptoCurrency(string symbol) {

    // @8?B>20;NBK >1KG=> 8<5NB ACDD8:A "USDT" 8;8 "BTC"

    if (StringFind(symbol, "USDT") != -1 || StringFind(symbol, "BTC") != -1) {

        return true;

    }

    return false;

}



// $C=:F8O 4;O @0AG5B0 :><8AA88 2 7028A8<>AB8 >B B8?0 8=AB@C<5=B0

double CalculateCommission(string symbol, double volume, double price) {

    if (IsCryptoCurrency(symbol)) {

        // ><8AA8O 4;O :@8?B>20;NB: 0.055% 2 4>;;0@0E 70 ;>B

        return 0.00055 * volume * price; // 0.055% = 0.00055

    } else {

        // ><8AA8O 4;O 20;NB: 6$ 70 ;>B

        return 6.0 * volume;

    }

}



void OnStart() {

    string symbol = Symbol(); // "5:CI89 A8<2>; 3@0D8:0

    string timeStamp = StringReplace(TimeToString(TimeCurrent(), TIME_DATE|TIME_SECONDS), ":", "."); // 0<5=O5< 42>5B>G8O

    string fileName = "trades_" + symbol + "_" + timeStamp + ".csv"; // <O D09;0 A A8<2>;><

    

    // B:@K205< D09; A @0745;8B5;5<-70?OB>9 8 :>48@>2:>9 UTF-8

    int fileHandle = FileOpen(fileName, FILE_WRITE|FILE_CSV|FILE_ANSI, ";");



    if (fileHandle == INVALID_HANDLE) {

        Print("H81:0 ?@8 >B:@KB88 D09;0: ", fileName, " >4 >H81:8: ", GetLastError());

        return;

    }



    // 03>;>2:8 B01;8FK

    FileWrite(fileHandle, "0B0 8 2@5<O", ":B82", ""8? A45;:8", "&5=0", ">;8G5AB2>", "><8AA8O", "@81K;L/#1KB>:");



    // #:07K205< 480?07>= 2@5<5=8 (?>A;54=89 3>4)

    datetime startTime = iTime(symbol, PERIOD_D1, 365); // 48= 3>4 =0704

    datetime endTime = TimeCurrent(); // "5:CI55 2@5<O



    // 03@C605< 8AB>@8N A45;>: 70 C:070==K9 ?5@8>4

    if (!HistorySelect(startTime, endTime)) {

        Print("H81:0 ?@8 703@C7:5 8AB>@88 A45;>:. >4 >H81:8: ", GetLastError());

        FileClose(fileHandle);

        return;

    }



    int totalDeals = HistoryDealsTotal();

    Print("A53> A45;>: 2 8AB>@88: ", totalDeals);



    if (totalDeals == 0) {

        Print("AB>@8O A45;>: ?CAB0. #1548B5AL, GB> 8AB>@8O 703@C65=0.");

        FileClose(fileHandle);

        return;

    }



    // 5@5<5==K5 4;O 8B>3>2KE 7=0G5=89

    double totalProfit = 0.0;

    double totalCommission = 0.0;

    int totalFilteredDeals = 0;



    // 5@518@05< A45;:8

    for (int i = 0; i < totalDeals; i++) {

        ulong dealTicket = HistoryDealGetTicket(i);

        if (dealTicket > 0) {

            CDealInfo deal;

            deal.Ticket(dealTicket);



            string dealSymbol = deal.Symbol();

            ENUM_DEAL_TYPE dealType = deal.DealType();

            double dealPrice = deal.Price();

            double dealVolume = deal.Volume();

            double dealCommission = deal.Commission();

            double dealProfit = deal.Profit();

            ENUM_DEAL_ENTRY dealEntry = deal.Entry();



            // $8;LB@ ?> A8<2>;C, >1J5<C, F5=5 8 B8?C A45;:8

            if (dealSymbol == symbol && dealVolume > 0 && dealPrice > 0 && dealProfit != 0 && (dealEntry == DEAL_ENTRY_IN || dealEntry == DEAL_ENTRY_OUT)) {

                // ?@545;O5< B8? A45;:8

                string dealTypeStr;

                if (dealType == DEAL_TYPE_BUY) {

                    dealTypeStr = "@>4060"; // A;8 DEAL_TYPE_BUY, B> MB> "@>4060"

                } else if (dealType == DEAL_TYPE_SELL) {

                    dealTypeStr = ">:C?:0"; // A;8 DEAL_TYPE_SELL, B> MB> ">:C?:0"

                } else {

                    dealTypeStr = "58725AB=>"; // 0 A;CG09, 5A;8 B8? A45;:8 =5 >?@545;5=

                }



                // >;CG05< 40BC 8 2@5<O A45;:8 (2 1>;55 :><?0:B=>< D>@<0B5)

                datetime dealTime = deal.Time();

                string dealTimeStr = TimeToString(dealTime, TIME_DATE|TIME_MINUTES); // $>@<0B: YYYY.MM.DD HH:MM



                // A;8 :><8AA8O 87 8AB>@88 @02=0 0, @0AAG8BK205< 55 2@CG=CN

                if (dealCommission == 0) {

                    dealCommission = CalculateCommission(dealSymbol, dealVolume, dealPrice);

                }



                // @8<5=O5< <8=8<0;L=CN :><8AA8N (1 F5=B)

                dealCommission = GetCommissionWithMin(dealCommission);



                // $>@<0B8@C5< G8A;0

                string formattedPrice = FormatDouble(dealPrice, 4);

                string formattedVolume = FormatDouble(dealVolume, 2);

                string formattedCommission = FormatDouble(dealCommission, 2);

                string formattedProfit = FormatDouble(dealProfit, 2);



                // 0?8AK205< 40==K5 2 B01;8FC

                if (FileWrite(fileHandle, dealTimeStr, dealSymbol, dealTypeStr, formattedPrice, formattedVolume, formattedCommission, formattedProfit) <= 0) {

                    Print("H81:0 ?@8 70?8A8 A45;:8 2 D09;. >4 >H81:8: ", GetLastError());

                    FileClose(fileHandle);

                    return;

                }



                // !C<<8@C5< 8B>3>2K5 7=0G5=8O

                totalProfit += dealProfit;

                totalCommission += dealCommission;

                totalFilteredDeals++;



                Print("!45;:0 4>102;5=0 2 B01;8FC: ", dealSymbol, " "8?: ", dealTypeStr, " @81K;L/#1KB>:: ", formattedProfit);

            }

        }

    }



    // 0?8AK205< 8B>3>2K5 7=0G5=8O ?>4 B01;8F59

    FileWrite(fileHandle, ""); // CAB0O AB@>:0 4;O @0745;5=8O

    FileWrite(fileHandle, "B>3>:", "", "", "", "", "><8AA8O:", FormatDouble(totalCommission, 2));

    FileWrite(fileHandle, "", "", "", "", "", "@81K;L/#1KB>::", FormatDouble(totalProfit, 2));

    FileWrite(fileHandle, "", "", "", "", "", ">;8G5AB2> A45;>::", IntegerToString(totalFilteredDeals));



    // >102;O5< A>>1I5=85 4;O ?>445@6:8 02B>@0

    FileWrite(fileHandle, "");

    FileWrite(fileHandle, "**********************************************");

    FileWrite(fileHandle, "=Ø¡Ü >445@68 02B>@0 A:@8?B0!");

    FileWrite(fileHandle, "A;8 B515 ?>=@028;AO MB>B 8=AB@C<5=B, BK <>65HL A:070BL A?0A81>!");

    FileWrite(fileHandle, "@>AB> 22548 :>4 **45980** ?@8 @538AB@0F88 =0 Bybit.");

    FileWrite(fileHandle, "-B> 709<5B 2A53> ?0@C A5:C=4, => >G5=L ?><>65B 02B>@C! =Ø
Þ");

    FileWrite(fileHandle, "!?0A81> 70 ?>445@6:C! d'þ");

    FileWrite(fileHandle, "**********************************************");



    FileClose(fileHandle);

    Print("0==K5 CA?5H=> 70?8A0=K 2 D09;: ", fileName);

}

Comments