//+------------------------------------------------------------------+ //| 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.csv";// File name to write the training dataset input string TestFileName = "test_pricedelt_data.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[]; if(ArrayResize(test, total) <= 0) return; 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-5; 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; } double A = (open[i]-high[i+1])*1000; double B = (high[i+1]-close[i+1])*1000; double C = (open[i+1]-low[i+1])*1000; double D = (low[i+1]-open[i+3])*1000; if(!WriteData(A, B, C, D, 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); } //+------------------------------------------------------------------+