0
1
Çatalla 0
şundan çatallanmış nique_372/AiDataTaskRuner
AiDataTaskRuner/Backend/Main.mqh

500 satır
18 KiB
MQL5
Ham Kalıcı Bağlantı Normal Görünüm Geçmiş

2026-03-13 19:46:53 -05:00
//+------------------------------------------------------------------+
//| Main.mqh |
//| Copyright 2025, Niquel Mendoza. |
//| https://www.mql5.com/es/users/nique_372 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, Niquel Mendoza."
#property link "https://www.mql5.com/es/users/nique_372"
#property strict
#ifndef AIDATATASKRUNNER_BACKEND_MAIN_MQH
#define AIDATATASKRUNNER_BACKEND_MAIN_MQH
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
#include "Bases\\Saver.mqh"
#include "..\\UI\\Main.mqh"
#include <Generic\Queue.mqh>
2026-03-13 22:11:10 -05:00
#include "..\\..\\ExtraCodes\\MultiTester\\MTTester.mqh"
2026-03-13 19:46:53 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
class CExecutionTester : public ITaskRunerAi
{
private:
//---
long m_chart_id_ea_2; // Chart id del ea 2
TaskTester m_current_task; // Tarea actual en configuracion
TaskTester m_tasks[]; // Array de tareas
int m_tasks_size; // Cantidad de tareas
CTaskSaver m_saver; // Guardado y carga de tareas
const long m_current_chart_id;
//---
string m_expert_path; // Ruta al experto que se desea ejeuctar (para ejecutarlo)
string m_main_folder; // Folder base
int m_files_common_to_move_size; // Tamaaño de archivos a mover
string m_files_common_to_move[]; // Archivos a mover
string m_progres_task_file; // Archivo de progreso
//---
int m_file_common_flag_out; // Flag comon para los archivos quie el backend mueve
int m_file_common_flag_in;// Flag comon para los archivos que da el ea
string m_base_path;
2026-03-13 21:49:37 -05:00
//---
int m_current_task_procces_idx;
2026-03-13 19:46:53 -05:00
// temporales (se modican en ejecucion)
string m_temp_exp_path; // path temproal
string m_temp_progres_csv_file;
string m_temp_main_folder;
//---
bool m_in_execution; // Ahora mismo se estan ejeuctando tareas?
CTaskRunerAiTabDGenMain* m_sec_generation;
CQueue<int> m_cola_indices;
//---
void SentEvent(); // Envia evento
void RecibirEvento(const bool result, const string& msg);
public:
CExecutionTester(void);
2026-03-13 22:14:54 -05:00
~CExecutionTester(void) {}
2026-03-13 19:46:53 -05:00
//---
2026-03-13 21:49:37 -05:00
void Set(CTaskRunerAiTabDGenMain *sec_gen, bool comon_in, bool comon_out, long chart_id_ea);
2026-03-13 19:46:53 -05:00
//---
void InitCurrentTask(const TaskTester& t) override final { m_current_task = t; }
//--- Configuracion de la tarea actual
void OnCurrentTask_ModifyConfigMoveFile(const string str) override final;
void OnCurrentTask_ModifyConfigExpertPath(const string str) override final;
void OnCurrentTask_ModifyConfigProgresCsvFile(const string str) override final;
void OnCurrentTask_ModifyConfigMainFolder(const string str) override final;
//--- Modificacion de la tarea actual
void OnCurrentTask_ModifyDateStart(const datetime new_start) override final;
void OnCurrentTask_ModifyDateEnd(const datetime new_end) override final;
void OnCurrentTask_ModifySimbolo(const string new_symbol) override final;
void OnCurrentTask_ModifySimboloFolder(const string new_symbol_f) override final;
void OnCurrentTask_ModifyTimeframe(const ENUM_TIMEFRAMES new_tf) override final;
void OnCurrentTask_ModifyLabel(const string label) override final;
void OnCurrentTask_ModifyLabelId(const int label_id) override final;
void OnCurrentTask_ModifySetFile(const string set_file) override final;
//---
bool OnCurrentTask_Add();
//---
int RunAllTask(int& out[]) override final;
int CleanAllTask(int& out[]) override final;
//---
// Load
bool SaveAllTaskInFile(const string& file_name) override final;
// El archivo debera de estar en comon
// task: aqui se pondran todas las tareas
// retonar el numero de tareas
bool SetTastByFile(const string& file_name) override final;
TaskTester GetTaskByIndex(const int index) const override final { return m_tasks[index]; }
inline TaskTester CurrentTask() const override final { return m_current_task; }
inline int TaskSize() const override final { return m_tasks_size; }
//---
void ChartEvent(const int32_t id, const long& lparam, const double& dparam, const string& sparam);
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
CExecutionTester::CExecutionTester(void)
: m_progres_task_file(NULL), m_file_common_flag_out(NULL), m_temp_main_folder(NULL), m_current_chart_id(ChartID()),
2026-03-13 21:49:37 -05:00
m_in_execution(false), m_expert_path(NULL), m_main_folder(NULL), m_current_task_procces_idx(-1)
2026-03-13 19:46:53 -05:00
{
2026-03-13 21:49:37 -05:00
m_files_common_to_move_size = ArrayResize(m_files_common_to_move, 0);
2026-03-13 19:46:53 -05:00
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CExecutionTester::Set(CTaskRunerAiTabDGenMain *sec_gen, bool comon_in, bool comon_out, long chart_id_ea)
{
//---
2026-03-13 21:49:37 -05:00
m_sec_generation = sec_gen;
2026-03-13 19:46:53 -05:00
m_file_common_flag_in = comon_in ? FILE_COMMON : 0;
m_file_common_flag_out = comon_out ? FILE_COMMON : 0;
m_base_path = comon_out ? TERMINAL_MT5_COMMON_PATH : TERMINAL_MT5_DATA_PAH;
//--- Abrimos el segundo grafico
// Sin
if(::ChartSymbol(chart_id_ea) == "")
{
LogError(StringFormat("Chart id = %d, invalido", chart_id_ea), FUNCION_ACTUAL);
Remover();
return;
}
m_chart_id_ea_2 = chart_id_ea;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CExecutionTester::OnCurrentTask_ModifyConfigMoveFile(const string str)
{
const int t = StringSplit(str, '|', g_str_arr_dyn_arr);
if(t < 1)
{
LogError(StringFormat("No se ha encontrado el separador | dentro de:\n%s", str), FUNCION_ACTUAL);
return;
}
m_files_common_to_move_size = ArrayCopy(m_files_common_to_move, g_str_arr_dyn_arr);
}
//+------------------------------------------------------------------+
void CExecutionTester::OnCurrentTask_ModifyConfigExpertPath(const string str)
{
if(m_in_execution)
{
m_temp_exp_path = str;
}
else
{
m_expert_path = str;
}
}
//+------------------------------------------------------------------+
// NOTA:
// EL ARCHIVO DE PROGRESO VA RELAITIVO AL MAIN_FOLDER
// por jeemplo Progreess\\temp.csv
void CExecutionTester::OnCurrentTask_ModifyConfigProgresCsvFile(const string str)
{
if(m_in_execution)
{
m_temp_progres_csv_file = m_temp_main_folder + str;
}
else
{
m_progres_task_file = m_main_folder + str;
}
}
//+------------------------------------------------------------------+
void CExecutionTester::OnCurrentTask_ModifyConfigMainFolder(const string str)
{
if(m_in_execution)
{
m_temp_main_folder = str;
}
else
{
m_main_folder = str;
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CExecutionTester::OnCurrentTask_ModifyDateStart(const datetime new_start)
{
m_current_task.start_date = new_start;
}
//+------------------------------------------------------------------+
void CExecutionTester::OnCurrentTask_ModifyDateEnd(const datetime new_end)
{
m_current_task.end_date = new_end;
}
//+------------------------------------------------------------------+
void CExecutionTester::OnCurrentTask_ModifySimbolo(const string new_symbol)
{
m_current_task.symbol = new_symbol;
}
//+------------------------------------------------------------------+
void CExecutionTester::OnCurrentTask_ModifySimboloFolder(const string new_symbol_f)
{
m_current_task.symbol_folder = new_symbol_f;
}
//+------------------------------------------------------------------+
void CExecutionTester::OnCurrentTask_ModifyTimeframe(const ENUM_TIMEFRAMES new_tf)
{
m_current_task.timeframe = new_tf;
}
//+------------------------------------------------------------------+
void CExecutionTester::OnCurrentTask_ModifyLabel(const string label)
{
m_current_task.label = label;
}
//+------------------------------------------------------------------+
void CExecutionTester::OnCurrentTask_ModifyLabelId(const int label_id)
{
m_current_task.label_id = label_id;
}
//+------------------------------------------------------------------+
void CExecutionTester::OnCurrentTask_ModifySetFile(const string set_file)
{
m_current_task.set_file = set_file;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CExecutionTester::OnCurrentTask_Add(void)
{
//---
bool res = true;
bool is_cts;
//---
if(m_main_folder == NULL)
{
m_last_error_msg += "Main folder invalido|";
res = false;
}
2026-03-13 21:49:37 -05:00
if(m_expert_path == NULL || StringFindAtras(m_expert_path, ".ex5") == -1)
2026-03-13 19:46:53 -05:00
{
m_last_error_msg += "Path invalido o extension no es ex5|";
res = false;
}
//--- Como tal de la tarea
if(!::SymbolExist(m_current_task.symbol, is_cts))
{
m_last_error_msg += "Simbolo no existe|";
res = false;
}
if(m_current_task.timeframe == WRONG_VALUE)
{
m_last_error_msg += "Timeframe no configurado|";
res = false;
}
if(m_current_task.start_date >= m_current_task.end_date)
{
m_last_error_msg += "Fecha inicio debe ser menor a fecha fin|";
res = false;
}
if(StringFindAtras(m_current_task.set_file, ".set") == -1)
{
m_last_error_msg += "El archivo setfile debe de tener la extension .set|";
res = false;
}
//---
if(!res)
LogError(m_last_error_msg, FUNCION_ACTUAL);
2026-03-13 21:49:37 -05:00
else
{
::ArrayResize(m_tasks, m_tasks_size + 1);
m_tasks[m_tasks_size] = m_current_task;
m_tasks_size++;
}
2026-03-13 19:46:53 -05:00
//---
return res;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CExecutionTester::SaveAllTaskInFile(const string &file_name)
{
// usamos el valor de (is common flag) del global config que tine epuesto el fronted.. para almcenar los archivos
return m_saver.Save(file_name, m_tasks, m_tasks_size, AIDATATASK_RUNNER_COMON_FLAG, false);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
// Aquilimpiamos todas las tareas que se puedan limpiar
// Osea que esa tarea NO se este procesando o este en cola a ser ejecutada
int CExecutionTester::CleanAllTask(int &out[])
{
for(int i = 0; i < m_tasks_size; i++)
{
if(m_tasks[i].state == AIEXECUTOR_TASK_STATE_IN_PROCESS || m_tasks[i].state == AIEXECUTOR_TASK_STATE_IN_QUEQE)
continue; // No
out.Push(i);
}
m_tasks_size = RemoveMultipleIndexes(m_tasks, out, 0);
return ArraySize(out);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int CExecutionTester::RunAllTask(int &out[])
{
//--- Sin tareas
if(m_tasks_size <= 0)
{
m_last_error_msg = "No hay tareas para ejecutar";
return 0;
}
//---
int t = 0;
//---
for(int i = 0; i < m_tasks_size; i++)
{
if(m_tasks[i].state == AIEXECUTOR_TASK_STATE_PENDIENTE)
{
// Actuliazamos el estado
m_tasks[i].state = AIEXECUTOR_TASK_STATE_IN_QUEQE;
// Añadimos a la cola interna
m_cola_indices.Enqueue(i); // Agregamos este elemento a la cola
// Añadimos al array out
out[::ArrayResize(out, ++t) - 1] = i;
}
}
//--- Sin tareas pendientes
if(t <= 0)
{
m_last_error_msg = "No hay tareas pendientes para ejecutar";
}
else
{
//--- Valor incial de los temp
m_temp_main_folder = m_main_folder;
m_temp_exp_path = m_expert_path;
m_temp_progres_csv_file = m_progres_task_file;
// Mandamos el primer evento
SentEvent();
}
//---
return t;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CExecutionTester::SetTastByFile(const string &file_name)
{
return m_saver.Load(file_name, m_tasks, m_tasks_size, AIDATATASK_RUNNER_COMON_FLAG);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CExecutionTester::SentEvent()
{
if(m_cola_indices.Count() < 1)
{
LogCaution("Todas las tareas han acabado", FUNCION_ACTUAL);
m_in_execution = false; // Termino
//--- asing de los temp
m_main_folder = m_temp_main_folder;
m_expert_path = m_temp_exp_path;
m_progres_task_file = m_temp_progres_csv_file;
return;
}
2026-03-13 21:49:37 -05:00
//---
2026-03-13 19:46:53 -05:00
const int idx = m_cola_indices.Dequeue();
2026-03-13 21:49:37 -05:00
::EventChartCustom(m_chart_id_ea_2, DEFMTTESTER_E_ON_TASK, m_current_chart_id, 0.00, m_tasks[idx].ToString() + "," + m_expert_path);
2026-03-13 19:46:53 -05:00
m_tasks[idx].state = AIEXECUTOR_TASK_STATE_IN_PROCESS;
m_sec_generation.UpdateStateInTable(idx, m_tasks[idx].state);
2026-03-13 21:49:37 -05:00
m_current_task_procces_idx = idx;
2026-03-13 19:46:53 -05:00
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CExecutionTester::RecibirEvento(const bool result, const string & msg)
{
//---
if(result)
{
//---
2026-03-13 21:49:37 -05:00
m_tasks[m_current_task_procces_idx].state = AIEXECUTOR_TASK_STATE_FINISHED;
//---
string tf = ::EnumToString(m_tasks[m_current_task_procces_idx].timeframe);
::StringReplace(tf, "PERIOD_", "");
const string final_ubicaction_folder = ::StringFormat("%s\\%s\\%s\\%s_%d\\", m_main_folder, m_tasks[m_current_task_procces_idx].symbol_folder, tf,
m_tasks[m_current_task_procces_idx].label, m_tasks[m_current_task_procces_idx].label_id);
2026-03-13 19:46:53 -05:00
//---
if(m_files_common_to_move_size > 0)
{
// Aqui movemos...
for(int i = 0; i < m_files_common_to_move_size; i++)
{
//--- Movemos
::ResetLastError();
if(!::FileMove(m_files_common_to_move[i], m_file_common_flag_in, final_ubicaction_folder, FILE_REWRITE | m_file_common_flag_out))
{
LogError(::StringFormat("No se pudo mover el archivo =%s, a = %s, ultimo error = %d"
, m_files_common_to_move[i], final_ubicaction_folder, ::GetLastError()), FUNCION_ACTUAL);
}
}
}
2026-03-13 21:49:37 -05:00
//--- Copiamos el setfile
const string out = m_base_path + "\\" + final_ubicaction_folder; // donde se pondra
const string in = TERMINAL_MT5_ROOT + "Profiles\\Tester\\" + m_tasks[m_current_task_procces_idx].set_file;
if(!MTTESTER::FileMove(in, out, true))
{
LogError(::StringFormat("Fallo al mover:\n%s\nA\n%s\nUltimo error en kernel32 = %d", out, in, kernel32::GetLastError()), FUNCION_ACTUAL);
}
2026-03-13 19:46:53 -05:00
//--- Guardamos progreso
// El progreso se guarda junto con los archivos out (data que pertenece al backend)
// solo se guardan las tareas (proceseando|pendientes|encola)
if(m_progres_task_file != NULL)
{
if(!m_saver.Save(m_progres_task_file, m_tasks, m_tasks_size, m_file_common_flag_out, true)) // filtrasmos solo guramos lo necesario
{
LogError("Fallo al guardar progreso en el archivo csv", FUNCION_ACTUAL);
}
}
}
else
{
2026-03-13 21:49:37 -05:00
m_tasks[m_current_task_procces_idx].state = AIEXECUTOR_TASK_STATE_FAILED;
2026-03-13 19:46:53 -05:00
LogError(StringFormat("Fallo al procesar la tarea #%d, msg:\n", msg), FUNCION_ACTUAL);
}
//---
2026-03-13 21:49:37 -05:00
m_sec_generation.UpdateStateInTable(m_current_task_procces_idx, m_tasks[m_current_task_procces_idx].state); // Actulizar el estado
2026-03-13 19:46:53 -05:00
//---
SentEvent(); // Lanzamos el siguiente evento (intemante se verifica si es que hay tareas ono)
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CExecutionTester::ChartEvent(const int id, const long & lparam, const double & dparam, const string & sparam)
{
2026-03-13 21:49:37 -05:00
// Si se finaliza
if(id == CHARTEVENT_CUSTOM + DEFMTTESTER_E_FINISH_TASK && m_current_chart_id == long(dparam))
2026-03-13 19:46:53 -05:00
{
2026-03-13 21:49:37 -05:00
RecibirEvento((lparam == DEFMTTESTER_TESTER_R_EXITO), sparam);
2026-03-13 19:46:53 -05:00
}
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
2026-03-13 21:49:37 -05:00
#endif // AIDATATASKRUNNER_BACKEND_MAIN_MQH