有您的持续关注
我们会做得更好

MT4交易历史转换为EXCEL的脚本



该脚本可以将MT4的账号历史保存成EXCEL表格以方便来进行各种统计分析。用法请参考下面两张图。

第一步在MT4账号历史选择需要保持的交易历史时间段,然后将脚本加载到图表,出来确认窗口点击是就行。如果不能自动打开保存的表格,那么看第二步。

MT4脚本

第二步到MT的MQL4\Files目录下就能看到已经保存的EXCEL表格,打开后就可以看到历史记录完整保存好了。

MT4脚本

该脚本的MQL4源码如下:

//+------------------------------------------------------------------+
//|                                       MT4交易历史转换为EXCEL.mq4 |
//|                                     Copyright 2014, QQ:125808047 |
//+------------------------------------------------------------------+
#property copyright "www.125808047.com"
#property link      "http://www.125808047.com/"
#property show_inputs

#include <WinUser32.mqh>

#import "shell32.dll"
   int ShellExecuteA(int a0, string a1, string a2, string a3, string a4, int a5);
#import

extern bool DisplayClosedTrades = TRUE;
extern bool DisplayLiveTrades = TRUE;

int start() {
   int l_mb_code_0;
   string ls_4;
   int l_file_12;
   int 订单号;
   int l_datetime_24;
   int l_cmd_28;
   double  手数;
   string  商品名;
   int l_digits_48;
   double l_str2dbl_52;
   double l_str2dbl_60;
   double l_str2dbl_68;
   int l_datetime_76;
   int li_80;
   double l_str2dbl_84;
   double l_point_92;
   int li_unused_100;
   string  佣金;
   string  利息;
   string  盈利 ;
   double  点数;
   string  评论;
   string l_dbl2str_144;
   string  买卖类型;
   string 订单日期;
   string  平单日期;
   string  订单时间;
   string  平单时间;
   string l_str_concat_192;
   int l_mb_code_200;
   string ls_204;
   string l_str_concat_212;
   if (!IsDllsAllowed()) {
      l_mb_code_0 = MessageBox("DLL Calls are not allowed. Please click on Tools 
\\ Options \\ Expert Advisor and select \"Allow DLL Imports\"", "交易历史转换为EXCEL", MB_ICONEXCLAMATION);
      return (0);
   }
   if (AccountNumber() != 0 && IsConnected()) {
      ls_4 = "商号" + DoubleToStr(AccountNumber(), 0) + TimeDay(D'') + "历史"+".csv";
      l_file_12 = CreateFileHeader(ls_4);
      if (l_file_12 < 0) {
         Alert("Error - COULD NOT CREATE FILE");
         return (0);
      }
      if (DisplayClosedTrades) {
         for (int l_pos_16 = OrdersHistoryTotal() - 1; l_pos_16 >= 0; l_pos_16--) {
            if (OrderSelect(l_pos_16, SELECT_BY_POS, MODE_HISTORY) == TRUE) {
               订单号 = OrderTicket();
               l_datetime_24 = OrderOpenTime();
               l_cmd_28 = OrderType();
                手数 = OrderLots();
                商品名 = OrderSymbol();
               l_digits_48 = MarketInfo( 商品名, MODE_DIGITS);
               l_str2dbl_52 = StrToDouble(DoubleToStr(OrderOpenPrice(), l_digits_48));
               l_str2dbl_60 = StrToDouble(DoubleToStr(OrderStopLoss(), l_digits_48));
               l_str2dbl_68 = StrToDouble(DoubleToStr(OrderTakeProfit(), l_digits_48));
               l_datetime_76 = OrderCloseTime();
               li_80 = l_datetime_76 - l_datetime_24;
               l_str2dbl_84 = StrToDouble(DoubleToStr(OrderClosePrice(), l_digits_48));
               l_point_92 = MarketInfo( 商品名, MODE_POINT);
               li_unused_100 = 1;
               if (l_point_92 == 0.001) {
                  l_point_92 = 0.01;
                  li_unused_100 = 10;
               } else {
                  if (l_point_92 == 0.00001) {
                     l_point_92 = 0.0001;
                     li_unused_100 = 10;
                  }
               }
                佣金 = DoubleToStr(OrderCommission(), 2);
                利息 = DoubleToStr(OrderSwap(), 2);
                盈利  = DoubleToStr(OrderProfit(), 2);
                点数 = 0.0;
                评论 = OrderComment();
               if ( 评论 == "")  评论 = "None";
               l_dbl2str_144 = DoubleToStr(OrderMagicNumber(), 0);
                买卖类型 = "";
               if (l_cmd_28 == OP_BUY) {
                   买卖类型 = "BUY";
                   点数 = (l_str2dbl_84 - l_str2dbl_52) / l_point_92;
               } else {
                  if (l_cmd_28 == OP_SELL) {
                      买卖类型 = "SELL";
                      点数 = (l_str2dbl_52 - l_str2dbl_84) / l_point_92;
                  } else {
                     if (l_cmd_28 == OP_BUYLIMIT) {
                         买卖类型 = "BUY_LIMIT";
                         点数 = 0.0;
                     } else {
                        if (l_cmd_28 == OP_SELLLIMIT) {
                            买卖类型 = "SELL_LIMIT";
                            点数 = 0.0;
                        } else {
                           if (l_cmd_28 == OP_BUYSTOP) {
                               买卖类型 = "BUY_STOP";
                               点数 = 0.0;
                           } else {
                              if (l_cmd_28 == OP_SELLSTOP) {
                                  买卖类型 = "SELL_STOP";
                                  点数 = 0.0;
                              } else
                                 if (l_cmd_28 == 6)  买卖类型 = "Deposit/WithDrawal";
                           }
                        }
                     }
                  }
               }
订单日期 = TimeMonth(l_datetime_24) + "/" + TimeDay(l_datetime_24) + "/" + TimeYear(l_datetime_24);
平单日期 = TimeMonth(l_datetime_76) + "/" + TimeDay(l_datetime_76) + "/" + TimeYear(l_datetime_76);
订单时间 = TimeHour(l_datetime_24) + ":" + TimeMinute(l_datetime_24) + ":" + TimeSeconds(l_datetime_24);
平单时间 = TimeHour(l_datetime_76) + ":" + TimeMinute(l_datetime_76) + ":" + TimeSeconds(l_datetime_76);
l_str_concat_192 = StringConcatenate(订单号, ",", 订单日期, ",",  订单时间, ",",  
买卖类型, ",",  手数, ",",  商品名, ",", DoubleToStr(l_str2dbl_52, l_digits_48), ",", 
DoubleToStr(l_str2dbl_60, l_digits_48), ",", DoubleToStr(l_str2dbl_68, l_digits_48), ",",  平单日期, ",",  
平单时间, ",", DoubleToStr(l_str2dbl_84, l_digits_48), ",",  佣金, ",",  利息, ",",  盈利 , ",",  点数, ",", 
在交易用的时间(li_80), ",",  评论, ",", l_dbl2str_144, ",");
               FileWrite(l_file_12, l_str_concat_192);
            }
         }
      }
      if (DisplayLiveTrades) {
         for (l_pos_16 = OrdersTotal() - 1; l_pos_16 >= 0; l_pos_16--) {
            if (OrderSelect(l_pos_16, SELECT_BY_POS, MODE_TRADES) == TRUE) {
               订单号 = OrderTicket();
               l_datetime_24 = OrderOpenTime();
               l_cmd_28 = OrderType();
                手数 = OrderLots();
                商品名 = OrderSymbol();
               l_digits_48 = MarketInfo( 商品名, MODE_DIGITS);
               l_str2dbl_52 = StrToDouble(DoubleToStr(OrderOpenPrice(), l_digits_48));
               l_str2dbl_60 = StrToDouble(DoubleToStr(OrderStopLoss(), l_digits_48));
               l_str2dbl_68 = StrToDouble(DoubleToStr(OrderTakeProfit(), l_digits_48));
               l_datetime_76 = OrderCloseTime();
               li_80 = Time[0] - l_datetime_24;
               l_str2dbl_84 = StrToDouble(DoubleToStr(OrderClosePrice(), l_digits_48));
               l_point_92 = MarketInfo( 商品名, MODE_POINT);
               li_unused_100 = 1;
               if (l_point_92 == 0.001) {
                  l_point_92 = 0.01;
                  li_unused_100 = 10;
               } else {
                  if (l_point_92 == 0.00001) {
                     l_point_92 = 0.0001;
                     li_unused_100 = 10;
                  }
               }
                佣金 = DoubleToStr(OrderCommission(), 2);
                利息 = DoubleToStr(OrderSwap(), 2);
                盈利  = DoubleToStr(OrderProfit(), 2);
                点数 = 0.0;
                评论 = OrderComment();
               if ( 评论 == "")  评论 = "None";
               l_dbl2str_144 = DoubleToStr(OrderMagicNumber(), 0);
                买卖类型 = "";
               if (l_cmd_28 == OP_BUY) {
                   买卖类型 = "BUY";
                   点数 = NormalizeDouble((iClose( 商品名, PERIOD_M1, 0) - l_str2dbl_52) / l_point_92, 2);
               } else {
                  if (l_cmd_28 == OP_SELL) {
                      买卖类型 = "SELL";
                      点数 = NormalizeDouble((l_str2dbl_52 - iClose( 商品名, PERIOD_M1, 0)) / l_point_92, 2);
                  } else {
                     if (l_cmd_28 == OP_BUYLIMIT) {
                         买卖类型 = "BUY_LIMIT";
                         点数 = 0.0;
                     } else {
                        if (l_cmd_28 == OP_SELLLIMIT) {
                            买卖类型 = "SELL_LIMIT";
                            点数 = 0.0;
                        } else {
                           if (l_cmd_28 == OP_BUYSTOP) {
                               买卖类型 = "BUY_STOP";
                               点数 = 0.0;
                           } else {
                              if (l_cmd_28 == OP_SELLSTOP) {
                                  买卖类型 = "SELL_STOP";
                                  点数 = 0.0;
                              } else
                                 if (l_cmd_28 == 6)  买卖类型 = "Deposit/WithDrawal";
                           }
                        }
                     }
                  }
               }
订单日期 = TimeMonth(l_datetime_24) + "/" + TimeDay(l_datetime_24) + "/" + TimeYear(l_datetime_24);
平单日期 = "OPEN";
订单时间 = TimeHour(l_datetime_24) + ":" + TimeMinute(l_datetime_24) + ":" + TimeSeconds(l_datetime_24);
                平单时间 = "OPEN";
               l_str_concat_192 = StringConcatenate(订单号, ",", 订单日期, ",",  订单时间, ",",  
买卖类型, ",",  手数, ",",  商品名, ",", 
DoubleToStr(l_str2dbl_52, l_digits_48), ",", DoubleToStr(l_str2dbl_60, l_digits_48), ",",
 DoubleToStr(l_str2dbl_68, l_digits_48), ",",  平单日期, ",",  平单时间, ",", 
DoubleToStr(l_str2dbl_84, l_digits_48), ",",  佣金, ",",  利息, ",",  盈利 , ",",  点数, ",", 
在交易用的时间(li_80), ",",  评论, ",", l_dbl2str_144, ",");
               FileWrite(l_file_12, l_str_concat_192);
            }
         }
      }
      FileClose(l_file_12);
      l_mb_code_200 = MessageBox("已创建CSV文件。你想打开它吗?", "交易历史转换为EXCEL", MB_YESNO|MB_ICONQUESTION);
      if (l_mb_code_200 == IDYES) {
         ls_204 = TerminalPath();
         l_str_concat_212 = StringConcatenate(ls_204, "/experts/files/", ls_4);
         ShellExecuteA(0, "open", l_str_concat_212, 0, 0, 0);
      }
   }
   return (0);
}

string 在交易用的时间(int ai_0) {
   int l_str2int_4 = StrToInteger(DoubleToStr(MathFloor(ai_0 / 3600.0), 0));
   int l_str2int_8 = StrToInteger(DoubleToStr(MathFloor(ai_0 - 3600.0 * l_str2int_4) / 60.0, 2));
   int li_12 = ai_0 - (3600.0 * l_str2int_4 + 60.0 * l_str2int_8);
   if (ai_0 < 60.0) return ("00:00:" + ai_0);
   if (ai_0 < 3600.0) return ("00:" + l_str2int_8 + ":" + li_12);
   return (l_str2int_4 + ":" + l_str2int_8 + ":" + li_12);
}

int CreateFileHeader(string a_name_0) {
   string ls_12;
   int li_ret_8 = FileOpen(a_name_0, FILE_CSV|FILE_WRITE, ";");
   if (li_ret_8 <= 0) {
      li_ret_8 = GetLastError();
      Alert(li_ret_8);
   } else {
      ls_12 = "订单号, 订单日期, 订单时间, 买卖类型, 手数, 商品, 价格,止损, 获利, 平单日期, 平单时间, 
平单价格, 佣金, 利息, 盈利 , 点数, 在交易用的时间, 评论, MagicNumber";
      FileWrite(li_ret_8, ls_12);
   }
   return (li_ret_8);
}
如您喜欢此文章请点下面分享按钮↴峰汇在线 » MT4交易历史转换为EXCEL的脚本
上一篇:
下一篇:
分享到:更多 ()