MQL4 存储指标值
MQL4 storing indicator value
有谁知道是否可以存储一个指标值的系列,例如 1 年,或在 EA 中计算的布尔函数?
我想存储根据我定义到 EA 中的两个不同 EMA 之间的差异计算的特定函数的值。
我需要存储这个双函数的值,在我的 EA 中计算了几年(比如从 2015 年到 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,双函数是在 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());
}
请提供您的 MCVE 代码以查看您需要什么。您希望如何存储数据以及您可能需要它的原因?指标数据已存储在缓冲区中,因此只需使用 iCustom()
调用它即可。如果您想优化您的 EA 和指标需要花费很多时间来加载和计算缓冲区 - 是的,可以计算一次指标缓冲区然后将它们写入文件或数据库并在新的优化开始之前获取,在这种情况下使用 CArrayObj or CArrayDouble
作为动态数组,用于存储大数组
有谁知道是否可以存储一个指标值的系列,例如 1 年,或在 EA 中计算的布尔函数?
我想存储根据我定义到 EA 中的两个不同 EMA 之间的差异计算的特定函数的值。
我需要存储这个双函数的值,在我的 EA 中计算了几年(比如从 2015 年到 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,双函数是在 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());
}
请提供您的 MCVE 代码以查看您需要什么。您希望如何存储数据以及您可能需要它的原因?指标数据已存储在缓冲区中,因此只需使用 iCustom()
调用它即可。如果您想优化您的 EA 和指标需要花费很多时间来加载和计算缓冲区 - 是的,可以计算一次指标缓冲区然后将它们写入文件或数据库并在新的优化开始之前获取,在这种情况下使用 CArrayObj or CArrayDouble
作为动态数组,用于存储大数组