196 lines
13 KiB
MQL5
196 lines
13 KiB
MQL5
//+------------------------------------------------------------------+
|
|
//| Create_Initial_Data.mq5 |
|
|
//| Copyright 2021, MetaQuotes Ltd. |
|
|
//| https://www.mql5.com/en/users/dng |
|
|
//+------------------------------------------------------------------+
|
|
//| Script creates training and testing datasets |
|
|
//+------------------------------------------------------------------+
|
|
#property copyright "Copyright 2021, MetaQuotes Ltd."
|
|
#property link "https://www.mql5.com/en/users/dng"
|
|
#property version "1.00"
|
|
#property script_show_inputs
|
|
//+------------------------------------------------------------------+
|
|
//| External parameters for script operation |
|
|
//+------------------------------------------------------------------+
|
|
input datetime Start = D'2022.01.01 00:00:00'; // Start of the population period
|
|
input datetime End = D'2024.07.01 23:59:00'; // End of the population period
|
|
input ENUM_TIMEFRAMES TimeFrame = PERIOD_H1; // Timeframe for loading data
|
|
//input int BarsToLine = 5; // Number of historical bars in one pattern
|
|
input string StudyFileName = "study_pricedelt_data_norm.csv";// File name to write the training dataset
|
|
input string TestFileName = "test_pricedelt_data_norm.csv"; // File name to write the testing dataset
|
|
input bool NormalizeData = true; // Data normalization flag
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| Script program start |
|
|
//+------------------------------------------------------------------+
|
|
void OnStart(void)
|
|
{
|
|
double close[];
|
|
if(CopyClose(_Symbol, TimeFrame, Start, End,close) <= 0)
|
|
return;
|
|
double open[];
|
|
if(CopyOpen(_Symbol, TimeFrame, Start, End, open) <= 0)
|
|
return;
|
|
double high[];
|
|
if(CopyHigh(_Symbol, TimeFrame, Start, End, high) <= 0)
|
|
return;
|
|
double low[];
|
|
if(CopyLow(_Symbol, TimeFrame, Start, End, low) <= 0)
|
|
return;
|
|
|
|
int target1 = 0, target2 = 0;
|
|
double target3 = 0;
|
|
int total = ArraySize(close);
|
|
int test[];
|
|
double A[], B[], C[], D[];
|
|
|
|
if(ArrayResize(test, total) <= 0 || ArrayResize(A, total) <= 0||
|
|
ArrayResize(B, total) <= 0 || ArrayResize(C, total) <= 0 ||
|
|
ArrayResize(D, total) <= 0)
|
|
return;
|
|
for(int i = 0; i<total; i++)
|
|
{
|
|
if(i > (total-4))
|
|
{
|
|
A[i] = A[i-1];
|
|
B[i] = B[i-1];
|
|
C[i] = C[i-1];
|
|
D[i] = D[i-1];
|
|
}
|
|
else
|
|
{
|
|
A[i] = open[i]-high[i+1];
|
|
B[i] = high[i+1]-close[i+1];
|
|
C[i] = open[i+1]-low[i+1];
|
|
D[i] = low[i+1]-open[i+3];
|
|
}
|
|
}
|
|
|
|
if(NormalizeData)
|
|
{
|
|
|
|
double Asr, Bsr, Csr, Dsr;
|
|
|
|
for(int i = 0; i<total-20; i++)
|
|
{
|
|
Asr = MathMax(A[ArrayMaximum(A, i, 20)], MathAbs(A[ArrayMinimum(A, i,20)]));
|
|
|
|
Bsr = MathMax(B[ArrayMaximum(B, i, 20)], MathAbs(B[ArrayMinimum(B, i,20)]));;
|
|
|
|
Csr = MathMax(C[ArrayMaximum(C, i, 20)], MathAbs(C[ArrayMinimum(C, i,20)]));
|
|
|
|
Dsr = MathMax(D[ArrayMaximum(D, i, 20)], MathAbs(D[ArrayMinimum(D, i,20)]));
|
|
|
|
|
|
A[i] /= Asr;
|
|
B[i] /= Bsr;
|
|
C[i] /= Csr;
|
|
D[i] /= Dsr;
|
|
|
|
}
|
|
for(int i = total-20; i<total; i++)
|
|
{
|
|
A[i] /= Asr;
|
|
B[i] /= Bsr;
|
|
C[i] /= Csr;
|
|
D[i] /= Dsr;
|
|
|
|
}
|
|
|
|
}
|
|
ArrayInitialize(test, 0);
|
|
|
|
//--- Randomly generate data indexes for the test dataset
|
|
int for_test = (int)(total * 0.05);
|
|
|
|
for(int i = 0; i < for_test; i++)
|
|
{
|
|
int t = (int)((double)(MathRand() * MathRand()) / MathPow(32767.0, 2) * (total - 5)) + 5;
|
|
if(test[t] == 1)
|
|
{
|
|
i--;
|
|
continue;
|
|
}
|
|
test[t] = 1;
|
|
//test[i] = 1;
|
|
}
|
|
//--- Open the training dataset file for writing
|
|
int Study = FileOpen(StudyFileName, FILE_WRITE | FILE_CSV | FILE_ANSI, ",", CP_UTF8);
|
|
if(Study == INVALID_HANDLE)
|
|
{
|
|
PrintFormat("Error opening file %s: %d", StudyFileName, GetLastError());
|
|
return;
|
|
}
|
|
//--- Open the testing dataset file for writing
|
|
int Test = FileOpen(TestFileName, FILE_WRITE | FILE_CSV | FILE_ANSI, ",", CP_UTF8);
|
|
if(Test == INVALID_HANDLE)
|
|
{
|
|
PrintFormat("Error opening file %s: %d", TestFileName, GetLastError());
|
|
return;
|
|
}
|
|
//--- Write datasets to files
|
|
for(int i = 0; i < total-3; i++)
|
|
{
|
|
|
|
target1 = 0;
|
|
target2 = 0;
|
|
target3 = close[i] - close[i+1];
|
|
|
|
if(target3 > 0)
|
|
{
|
|
target1 = 1;
|
|
target2 = 0;
|
|
}
|
|
if(target3 < 0)
|
|
{
|
|
target1 = 0;
|
|
target2 = 1;
|
|
}
|
|
|
|
if(!WriteData(A[i], B[i], C[i], D[i], target1, target2, target3,(test[i] == 1 ? Test : Study)))
|
|
{
|
|
PrintFormat("Error to write data: %d", GetLastError());
|
|
break;
|
|
}
|
|
}
|
|
//--- Close files
|
|
Comment("");
|
|
FileFlush(Study);
|
|
FileClose(Study);
|
|
FileFlush(Test);
|
|
FileClose(Test);
|
|
PrintFormat("Study data saved to file %s\\MQL5\\Files\\%s",
|
|
TerminalInfoString(TERMINAL_DATA_PATH), StudyFileName);
|
|
PrintFormat("Test data saved to file %s\\MQL5\\Files\\%s",
|
|
TerminalInfoString(TERMINAL_DATA_PATH), TestFileName);
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Function for writing pattern to file |
|
|
//+------------------------------------------------------------------+
|
|
bool WriteData(double &data0, // Buffer 1 of target values
|
|
double &data1, // Buffer 1 of historical data
|
|
double &data2, // Buffer 2 of historical data
|
|
double &data3, // Buffer 3 of historical data
|
|
int &dates0,
|
|
int &dates1,
|
|
double &dates2,
|
|
int handle) // File handle for writing
|
|
{
|
|
//--- Validate file handle
|
|
if(handle == INVALID_HANDLE)
|
|
{
|
|
Print("Invalid Handle");
|
|
return false;
|
|
}
|
|
|
|
string pattern = (string)data0 + ", "+
|
|
(string)data1 + ", "+
|
|
(string)data2 + ", "+
|
|
(string)data3 + ", "+
|
|
(string)dates0 + ", "+
|
|
(string)dates1 + ", "+
|
|
(string)dates2;
|
|
|
|
return (FileWrite(handle, pattern) > 0);
|
|
}
|
|
//+------------------------------------------------------------------+
|