如何从现有MT4指标代码导出到CSV文件

如何从现有MT4指标代码导出到CSV文件,csv,mql4,mt4,Csv,Mql4,Mt4,我想将结果导出到CSV文件。 这是MT4中的IEExposure指示符(IEExposure.mq4)。 目前,我知道如何将值导出到CSV 这是我尝试使用的代码,可以导出到CSV int h1; h1 = FileOpen("data3.csv", FILE_CSV | FILE_WRITE | FILE_READ, ','); FileSeek( ExtSymbolsSummaries[i][DEALS]=0, ExtSymbolsSummaries[i], [BUY_PRICE]=0,E

我想将结果导出到CSV文件。 这是MT4中的IEExposure指示符(IEExposure.mq4)。 目前,我知道如何将值导出到CSV

这是我尝试使用的代码,可以导出到CSV

int h1;
h1 = FileOpen("data3.csv", FILE_CSV | FILE_WRITE | FILE_READ, ',');
FileSeek( ExtSymbolsSummaries[i][DEALS]=0, ExtSymbolsSummaries[i],

 [BUY_PRICE]=0,ExtSymbolsSummaries[i][SELL_LOTS]=0 ,ExtSymbolsSummaries[i]  
[SELL_PRICE]=0,ExtSymbolsSummaries[i][NET_LOTS]=0, ExtSymbolsSummaries[i][PROFIT]=0, SEEK_END);
FileWrite(h1, Symbols[i]=SymbolName, ExtSymbolsSummaries[i][DEALS]=0, ExtSymbolsSummaries[i]
[BUY_PRICE]=0,ExtSymbolsSummaries[i][SELL_LOTS]=0 ,ExtSymbolsSummaries[i]  
[SELL_PRICE]=0,ExtSymbolsSummaries[i][NET_LOTS]=0, ExtSymbolsSummaries[i][PROFIT]=0 );
FileClose(h1)
这是原始代码:

     //+------------------------------------------------------------------+
//|                                                    iExposure.mq4 |
//|                      Copyright © 2007, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_minimum 0.0
#property indicator_maximum 0.1

#define SYMBOLS_MAX 1024
#define DEALS          0
#define BUY_LOTS       1
#define BUY_PRICE      2
#define SELL_LOTS      3
#define SELL_PRICE     4
#define NET_LOTS       5
#define PROFIT         6

extern color ExtColor=LightSeaGreen;

string ExtName="Exposure";
string ExtSymbols[SYMBOLS_MAX];
int    ExtSymbolsTotal=0;
double ExtSymbolsSummaries[SYMBOLS_MAX][7];
int    ExtLines=-1;
string ExtCols[8]={"Symbol",
                   "Deals",
                   "Buy lots",
                   "Buy price",
                   "Sell lots",
                   "Sell price",
                   "Net lots",
                   "Profit"};
int    ExtShifts[8]={ 10, 80, 130, 180, 260, 310, 390, 460 };
int    ExtVertShift=14;
double ExtMapBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void init()
  {
    IndicatorShortName(ExtName);
   SetIndexBuffer(0,ExtMapBuffer);
   SetIndexStyle(0,DRAW_NONE);
   IndicatorDigits(0);
    SetIndexEmptyValue(0,0.0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void deinit()
  {
   int windex=WindowFind(ExtName);
   if(windex>0) ObjectsDeleteAll(windex);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
void start()
  {
   string name;
   int    i,col,line,windex=WindowFind(ExtName);
//----
   if(windex<0) return;
//---- header line
   if(ExtLines<0)
     {
      for(col=0; col<8; col++)
        {
         name="Head_"+col;
         if(ObjectCreate(name,OBJ_LABEL,windex,0,0))
           {
            ObjectSet(name,OBJPROP_XDISTANCE,ExtShifts[col]);
            ObjectSet(name,OBJPROP_YDISTANCE,ExtVertShift);
            ObjectSetText(name,ExtCols[col],9,"Arial",ExtColor);
           }
        }
      ExtLines=0;
     }
//----
   ArrayInitialize(ExtSymbolsSummaries,0.0);
   int total=Analyze();
   if(total>0)
     {
      line=0;
      for(i=0; i<ExtSymbolsTotal; i++)
        {
         if(ExtSymbolsSummaries[i][DEALS]<=0) continue;
         line++;
         //---- add line
         if(line>ExtLines)
           {
            int y_dist=ExtVertShift*(line+1)+1;
            for(col=0; col<8; col++)
              {
               name="Line_"+line+"_"+col;
               if(ObjectCreate(name,OBJ_LABEL,windex,0,0))
                 {
                  ObjectSet(name,OBJPROP_XDISTANCE,ExtShifts[col]);
                  ObjectSet(name,OBJPROP_YDISTANCE,y_dist);
                 }
              }
            ExtLines++;
           }
         //---- set line
         int    digits=MarketInfo(ExtSymbols[i],MODE_DIGITS);
         double buy_lots=ExtSymbolsSummaries[i][BUY_LOTS];
         double sell_lots=ExtSymbolsSummaries[i][SELL_LOTS];
         double buy_price=0.0;
         double sell_price=0.0;
         if(buy_lots!=0)  buy_price=ExtSymbolsSummaries[i][BUY_PRICE]/buy_lots;
         if(sell_lots!=0) sell_price=ExtSymbolsSummaries[i][SELL_PRICE]/sell_lots;
         name="Line_"+line+"_0";
         ObjectSetText(name,ExtSymbols[i],9,"Arial",ExtColor);
         name="Line_"+line+"_1";
         ObjectSetText(name,DoubleToStr(ExtSymbolsSummaries[i][DEALS],0),9,"Arial",ExtColor);
         name="Line_"+line+"_2";
         ObjectSetText(name,DoubleToStr(buy_lots,2),9,"Arial",ExtColor);
         name="Line_"+line+"_3";
         ObjectSetText(name,DoubleToStr(buy_price,digits),9,"Arial",ExtColor);
         name="Line_"+line+"_4";
         ObjectSetText(name,DoubleToStr(sell_lots,2),9,"Arial",ExtColor);
         name="Line_"+line+"_5";
         ObjectSetText(name,DoubleToStr(sell_price,digits),9,"Arial",ExtColor);
         name="Line_"+line+"_6";
         ObjectSetText(name,DoubleToStr(buy_lots-sell_lots,2),9,"Arial",ExtColor);
         name="Line_"+line+"_7";
         ObjectSetText(name,DoubleToStr(ExtSymbolsSummaries[i][PROFIT],2),9,"Arial",ExtColor);
        }
     }
//---- remove lines
   if(total<ExtLines)
     {
      for(line=ExtLines; line>total; line--)
        {
         name="Line_"+line+"_0                                    ";
         ObjectSetText(name,"");
         name="Line_"+line+"_1";
         ObjectSetText(name,"");
         name="Line_"+line+"_2";
         ObjectSetText(name,"");
         name="Line_"+line+"_3";
         ObjectSetText(name,"");
         name="Line_"+line+"_4";
         ObjectSetText(name,"");
         name="Line_"+line+"_5";
         ObjectSetText(name,"");
         name="Line_"+line+"_6";
         ObjectSetText(name,"","                       ");
         name="Line_"+line+"_7";
         ObjectSetText(name,"","                           ");
        }
     }
//---- to avoid minimum==maximum
   ExtMapBuffer[Bars-1]=-1;
//----
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int Analyze()
  {
   double profit;
   int    i,index,type,total=OrdersTotal();
//----
   for(i=0; i<total; i++)
     {
      if(!OrderSelect(i,SELECT_BY_POS)) continue;
      type=OrderType();
      if(type!=OP_BUY && type!=OP_SELL) continue;
      index=SymbolsIndex(OrderSymbol());
      if(index<0 || index>=SYMBOLS_MAX) continue;
      //----
      ExtSymbolsSummaries[index][DEALS]++;
      profit=OrderProfit()+OrderCommission()+OrderSwap();
      ExtSymbolsSummaries[index][PROFIT]+=profit;
      if(type==OP_BUY)
        {
         ExtSymbolsSummaries[index][BUY_LOTS]+=OrderLots();
         ExtSymbolsSummaries[index][BUY_PRICE]+=OrderOpenPrice()*OrderLots();
        }
      else
        {
         ExtSymbolsSummaries[index][SELL_LOTS]+=OrderLots();
         ExtSymbolsSummaries[index][SELL_PRICE]+=OrderOpenPrice()*OrderLots();
        }
     }
//----
   total=0;
   for(i=0; i<ExtSymbolsTotal; i++)
     {
      if(ExtSymbolsSummaries[i][DEALS]>0) total++;
     }
//----
   return(total);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int SymbolsIndex(string SymbolName)
  {
   bool found=false;
//----
   for(int i=0; i<ExtSymbolsTotal; i++)
     {
      if(SymbolName==ExtSymbols[i])
        {
         found=true;
         break;
        }
     }
//----
   if(found) return(i);
   if(ExtSymbolsTotal>=SYMBOLS_MAX) return(-1);
//----
   i=ExtSymbolsTotal;
   ExtSymbolsTotal++;
   ExtSymbols[i]=SymbolName;
   ExtSymbolsSummaries[i][DEALS]=0;
   ExtSymbolsSummaries[i][BUY_LOTS]=0;
   ExtSymbolsSummaries[i][BUY_PRICE]=0;
   ExtSymbolsSummaries[i][SELL_LOTS]=0;
   ExtSymbolsSummaries[i][SELL_PRICE]=0;
   ExtSymbolsSummaries[i][NET_LOTS]=0;
   ExtSymbolsSummaries[i][PROFIT]=0;




//----
   return(i);
  }



//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//|ieexposure.mq4|
//|版权所有©2007,MetaQuotes软件公司|
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#版权所有“版权所有©2007,MetaQuotes软件公司”
#“财产链接”http://www.metaquotes.net"
#属性指示器\u单独\u窗口
#属性指示符\u缓冲区1
#属性指示器_最小值0.0
#属性指示器_最大值为0.1
#定义符号\u MAX 1024
#定义交易0
#定义购买批次1
#定义购买价格2
#确定销售地块3
#定义销售价格4
#定义净地块5
#定义利润6
extern color EXTCLOR=浅绿色;
字符串ExtName=“曝光”;
字符串ExtSymbols[SYMBOLS_MAX];
int ExtSymbolsTotal=0;
双extsymbols摘要[SYMBOLS_MAX][7];
int ExtLines=-1;
字符串ExtCols[8]={“Symbol”,
“交易”,
“买很多”,
“买入价”,
“卖地”,
“售价”,
“净地块”,
“利润”};
int ExtShifts[8]={10,80,130,180,260,310,390,460};
int ExtVertShift=14;
双ExtMapBuffer[];
//+------------------------------------------------------------------+
//|自定义指示器初始化功能|
//+------------------------------------------------------------------+
void init()
{
指示符短名称(ExtName);
SetIndexBuffer(0,ExtMapBuffer);
SetIndexStyle(0,无绘制);
指标指标(0);
SetIndexEmptyValue(0,0.0);
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
无效脱硝剂()
{
int windex=WindowFind(ExtName);
如果(windex>0)ObjectsDeleteAll(windex);
}
//+------------------------------------------------------------------+
//|自定义指标迭代函数|
//+------------------------------------------------------------------+
void start()
{
字符串名;
int i,col,line,windex=WindowFind(ExtName);
//----

if(windexCSV输出示例:

对于本例,我将描述实现以下目标的方法:

  • 为文件夹和CSV文件名添加输入字符串
  • 创建用于生成CSV的按钮
  • 侦听事件,以便我们可以检测按钮何时被单击
  • 解析对象列表并将对象的名称和说明写入CSV文件
  • 注意:如果您无法完成第2步,请跳到第3步,查看
    OnChartEvent()
    中的代码,然后在您想要编写CSV文件时对其进行自定义。但是,请注意,如果每次发生
    OnCalculate()
    事件时都要编写文件,那么将需要大量的文件写入

    由于默认情况下,
    ieexposure.mq4
    是可用的(至少对于版本:4.0 Build 765),让我们从创建原始版本的副本开始。我将我的示例命名为iExposureTest

    步骤1:为文件夹和CSV文件添加输入字符串

    这是最简单的步骤之一,只需为文件夹和CSV文件添加输入字符串。我将这些字符串添加到另一个输入变量旁边

    //Step 1
    input string  InputFileName="iExposure.csv";      // File name
    input string  InputDirectoryName="Data";     // Folder name
    
    步骤2:创建CSV按钮

    包含
    ChartObjectPanel.mqh的标题。我选择将包含行直接放在输入变量上方

    //Part of Step 2
    #include <ChartObjects\ChartObjectPanel.mqh>
    
    我们有一个按钮类型
    CChartObjectButton
    ,但它还没有实例化。让我们使用从
    OnInit()
    调用的函数来实现这一点

    在OnInit()中添加以下内容:

    //Part of Step 2
    CreateCSVButton(0);
    
    现在我们需要实际的函数,所以在指示符代码的末尾,我们接下来添加这个函数

    //Part of Step 2
    bool CreateCSVButton(const long chart_ID=0)
    { 
         //--- reset the error value
         ResetLastError();
    
         //--- set property values
         if(!CSVButton.Create(0,"CSVButton",0,10,25,75,15))
         {
             //--- display the error message in Experts journal
             Print(__FUNCTION__+", Error Code = ",GetLastError());
             return(false);
         }
    
         CSVButton.Description("CSV");
    
         CSVButton.FontSize(10);
         CSVButton.Corner(CORNER_LEFT_LOWER);
         CSVButton.Anchor(ANCHOR_CENTER);
    
         CSVButton.State(false);
    
         return true;
    } 
    
    此时,当我们编译时,窗口上应该有一个CSV按钮可用。这主要是为了通过单击生成CSV而创建的。它应该类似于下图

    如果您有CSV按钮,那么我们就完成了步骤2

    步骤3:侦听事件并捕获已单击的CSV按钮

    我们可以使用内置的
    OnChartEvent()
    捕获所需的事件

    在我们的
    OnChartEvent()
    中,我们正在检查事件是否为
    CHARTEVENT\u对象\u单击
    事件,然后如果
    sparam
    等于我们的
    CSVButton
    并且按钮状态为true,我们将执行文件工作

    另外,请注意,我们调用的函数
    WriteCSVObjects
    还不存在,我们将
    文件\u句柄
    传递给它。我们将在步骤4中创建该函数

      //step 3
      void OnChartEvent(const int id,
                        const long &lparam,
                        const double &dparam,
                        const string &sparam)
        {
    
         if(id==CHARTEVENT_OBJECT_CLICK)
           {  
               if (sparam=="CSVButton") 
               {
                  if(CSVButton.State() == true)
                  {
    
                     ResetLastError();
    
                     int file_handle=FileOpen(InputDirectoryName+"//"+InputFileName,FILE_READ|FILE_WRITE|FILE_CSV);
    
                     if(file_handle!=INVALID_HANDLE)
                     {
                        PrintFormat("%s file is available for writing",InputFileName);
                        PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH));
    
                        //Call Step 4  
                        WriteCSVObjects(file_handle);   
    
                        //--- close the file
                        FileClose(file_handle);
    
                        PrintFormat("Data is written, %s file is closed",InputFileName);
    
                     }
                     else
                     {
                        PrintFormat("Failed to open %s file, Error code = %d",InputFileName,GetLastError());
                     }
    
                  }
    
                  ChartRedraw();
               }
    
           }
        }
    
    第4步:解析对象列表并将名称和描述写入CSV

    差不多了!这是解决您问题的最后一部分。将此函数添加到代码示例的末尾

      //Step 4
      void WriteCSVObjects(int file_hand)
      {
         int obj_total=ObjectsTotal();
         string name;
         string desc;
    
         for(int i=obj_total;i>=0;i--)
          {
            name = ObjectName(i);
            desc = ObjectDescription(name);
    
            if(name == "")
            {
             name = "Empty Name";
            }
    
            if(desc == "")
            {
             desc = "Empty Desc";
            }
            FileWrite(file_hand, name + "," + desc);    
          }
      }
    
    在上面的函数中,请注意,我们正在循环对象并写出每个对象的名称和描述。输出和对象的顺序可能不是您的目标,因此请相应地修改


    此时,当我们编译并单击CSV按钮时,它将生成存储在
    Files//Data//ieexposure.CSV

    中的CSV文件的值!太棒了!谢谢!
      //Step 4
      void WriteCSVObjects(int file_hand)
      {
         int obj_total=ObjectsTotal();
         string name;
         string desc;
    
         for(int i=obj_total;i>=0;i--)
          {
            name = ObjectName(i);
            desc = ObjectDescription(name);
    
            if(name == "")
            {
             name = "Empty Name";
            }
    
            if(desc == "")
            {
             desc = "Empty Desc";
            }
            FileWrite(file_hand, name + "," + desc);    
          }
      }