Watch
1
0
Fork
You've already forked NeuroBook
0
forked from rosh/NeuroBook
NeuroBook/Scripts/initial_data/create_initial_data.mq5

182 lines
16 KiB
MQL5

2025-05-30 16:12:30 +02:00
<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| 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'2023.01.01 00:00:00'; // Start of the population period
input datetime End = D'2024.12.31 23:59:00'; // End of the population period
input ENUM_TIMEFRAMES TimeFrame = PERIOD_M5; // Timeframe for loading data
input int BarsToLine = 40; // Number of historical bars in one pattern
input string StudyFileName = "study_data.csv";// File name to write the training dataset
input string TestFileName = "test_data.csv"; // File name to write the testing dataset
input bool NormalizeData = true; // Data normalization flag
//+------------------------------------------------------------------+
//| Script program start |
//+------------------------------------------------------------------+
void OnStart(void)
{
//--- Connect indicators to the chart
int h_ZZ = iCustom(_Symbol, TimeFrame, "Examples\\ZigZag.ex5", 48, 1, 47);
int h_RSI = iRSI(_Symbol, TimeFrame, 12, PRICE_TYPICAL);
int h_MACD = iMACD(_Symbol, TimeFrame, 12, 48, 12, PRICE_TYPICAL);
double close[];
if(CopyClose(_Symbol, TimeFrame, Start, End, close) <= 0)
return;
//--- Load indicator data into dynamic arrays
double zz[], macd_main[], macd_signal[], rsi[];
datetime end_zz = End + PeriodSeconds(TimeFrame) * 500;
if(h_ZZ == INVALID_HANDLE || CopyBuffer(h_ZZ, 0, Start, end_zz, zz) <= 0)
{
PrintFormat("Error loading indicator %s data", "ZigZag");
return;
}
if(h_RSI == INVALID_HANDLE || CopyBuffer(h_RSI, 0, Start, End, rsi) <= 0)
{
PrintFormat("Error loading indicator %s data", "RSI");
return;
}
if(h_MACD == INVALID_HANDLE || CopyBuffer(h_MACD, MAIN_LINE, Start, End, macd_main) <= 0 ||
CopyBuffer(h_MACD, SIGNAL_LINE, Start, End, macd_signal) <= 0)
{
PrintFormat("Error loading indicator %s data", "MACD");
return;
}
int total = ArraySize(close);
double target1[], target2[], macd_delta[], test[];
if(ArrayResize(target1, total) <= 0 || ArrayResize(target2, total) <= 0 ||
ArrayResize(test, total) <= 0 || ArrayResize(macd_delta, total) <= 0)
return;
//--- Calculate targets: direction and distance to the nearest extremum
ArrayInitialize(test, 0);
double extremum = -1;
for(int i = ArraySize(zz) - 2; i >= 0; i--)
{
if(zz[i + 1] > 0 && zz[i + 1] != EMPTY_VALUE)
extremum = zz[i + 1];
if(i >= total)
continue;
target2[i] = extremum - close[i];
target1[i] = (target2[i] >= 0 ? 1 : -1);
macd_delta[i] = macd_main[i] - macd_signal[i];
}
//--- Data normalization
if(NormalizeData)
{
double main_norm = MathMax(MathAbs(macd_main[ArrayMinimum(macd_main)]),
macd_main[ArrayMaximum(macd_main)]);
double sign_norm = MathMax(MathAbs(macd_signal[ArrayMinimum(macd_signal)]),
macd_signal[ArrayMaximum(macd_signal)]);
double delt_norm = MathMax(MathAbs(macd_delta[ArrayMinimum(macd_delta)]),
macd_delta[ArrayMaximum(macd_delta)]);
for(int i = 0; i < total; i++)
{
rsi[i] = (rsi[i] - 50.0) / 50.0;
macd_main[i] /= main_norm;
macd_signal[i] /= sign_norm;
macd_delta[i] /= delt_norm;
}
}
//--- Randomly generate data indexes for the test dataset
int for_test = (int)((total - BarsToLine) * 0.2);
for(int i = 0; i < for_test; i++)
{