【问题标题】:MQL4 storing indicator valueMQL4 存储指标值
【发布时间】:2017-12-22 21:20:40
【问题描述】:

有谁知道是否可以存储指标的一系列值,例如 1 年,或在 EA 中计算的布尔函数的值?

我想存储根据我在 EA 中定义的两个不同 EMA 之间的差异计算得出的特定函数的值。

我需要存储这个双重函数的值,在我的 EA 中计算了几年(比如从 2​​015 年到 2017 年),并将其打印到一些输出文件(.txt 或一些其他格式)中

    int s15_60;
double B_M15_H1(int i) {

                          if (i>=0  && i<4  ) s15_60=0;
                     else if (i>=4  && i<8  ) s15_60=1;
                     else if (i>=8  && i<12 ) s15_60=2;
                     else if (i>=12 && i<16 ) s15_60=3;
                     else if (i>=16 && i<20 ) s15_60=4;

                     return NormalizeDouble(MathAbs(M15(i) - H1(s15_60)),Digits);

其中 M15 是在 M15 时间范围内计算的简单 EMA,H1 是在 H1 时间范围内计算的同一个 EMA,double 函数是以 M15 时间步长计算的这两个指标之间的距离。

我的目标是将此值存储在输出文件中,以便对此函数进行一些统计研究。

编辑:

此代码适用于我的目的:

#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//--- show the window of input parameters when launching the script
#property script_show_inputs
//--- parameters for writing data to file
input string             InpFileName="BOX.csv";      // File name
input string             InpDirectoryName="Data";     // Folder name
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+



double H1  (int shift) {double val = iCustom(NULL,PERIOD_H1, "my_funct",100,2.0,30.0,2.0,2.0,0,1,0,shift); return(val);}
double H4  (int shift) {double val = iCustom(NULL,PERIOD_H4, "my_funct",100,2.0,30.0,2.0,2.0,0,1,0,shift); return(val);}


int s60_240;
double B_H1_H4(int i) {

                          if (i>=0  &&  i<4  ) s60_240=0;
                     else if (i>=4  &&  i<8  ) s60_240=1;
                     else if (i>=8  &&  i<12 ) s60_240=2;
                     else if (i>=12 &&  i<16 ) s60_240=3;
                     else if (i>=16 &&  i<20 ) s60_240=4;

                     return NormalizeDouble( 10000*MathAbs( H1(i) - H4(s60_240) ) , Digits);

                     } 


void OnStart()
  {

   double   box_buff[]; // array of indicator values
   datetime date_buff[]; // array of indicator dates
//--- copying the time from last 1000 bars
   int copied=CopyTime(NULL,PERIOD_H1,0,1000,date_buff);
   ArraySetAsSeries(date_buff,true);
//--- prepare box_buff array
   ArrayResize(box_buff,copied);


//--- copy the values of main line of the iCustom indicator
   for(int i=0;i<copied;i++)
     {
      box_buff[i]=B_H1_H4(i);
     }


//--- open the file for writing the indicator values (if the file is absent, it will be created automatically)
   ResetLastError();
   int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_WRITE|FILE_CSV);
   if(file_handle!=INVALID_HANDLE)
     {
      PrintFormat("%s file is available for writing",InpFileName);
      PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH));
      //--- first, write the number of signals
      FileWrite(file_handle,copied);
      //--- write the time and values of signals to the file
      for(int i=0;i<copied;i++)
         FileWrite(file_handle,date_buff[i],box_buff[i]);
      //--- close the file
      FileClose(file_handle);
      PrintFormat("Data is written, %s file is closed",InpFileName);
     }
   else
      PrintFormat("Failed to open %s file, Error code = %d",InpFileName,GetLastError());
  }

【问题讨论】:

    标签: mql4


    【解决方案1】:

    请提供您的 MCVE 代码以查看您需要什么。您希望如何存储数据以及为什么需要它?指标数据已存储在缓冲区中,因此只需使用iCustom() 调用它。如果您想优化您的 EA 并且指标需要花费大量时间来加载和计算缓冲区 - 是的,可以计算指标缓冲区一次,然后将它们写入文件或数据库并在新的优化开始之前获取,在这种情况下使用 CArrayObj or CArrayDouble 作为用于存储大型数组的动态数组

    【讨论】:

    • 好吧,你表明你需要存储一些双精度值。您尝试做什么(如果有的话) - 为什么您不想查看 mql4.docs 并使用 FileOpen() FileWriteString() FileFlush() FileClose()
    • 对不起,我出去度假了。我已经在脚本和 EA 中尝试过这些命令,但它不输出任何文件。在哪里使用它们更好?在脚本中还是在 EA 中?
    • 尝试调试,并尝试确保 FileOpen() 返回有效句柄
    • 如何打印数据?从最后到最早?您的指标数据从最后一个(0 或 1)到最早(第 1000 个柱,即 1000 个柱之前)给出,当前柱为零。尝试检查日期时间,如果相反,则更改脚本以使最后一个数据与最后一个值匹配,或者尝试为每一行编写FileWrite(file_handle,TimeToStr(iTime(_Symbol,0,i),box_buff[i]);
    • 如果你有这么大的历史,那么你可以得到它。对于 ea - 将 OnStart 更改为 OnTick() 并添加 isNewBar() ,其余的就像你在这里一样。如果您有大量历史记录 - 增加图表然后运行脚本,当然更容易
    猜你喜欢
    • 2015-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多