MQLArticles/Strategy/Drv/Ao/Main.mqh

148 lines
6.4 KiB
MQL5
Raw Permalink Normal View History

2025-12-05 16:33:47 -05:00
//+------------------------------------------------------------------+
//| StrategyAO.mqh |
//| Copyright 2025, Niquel Mendoza. |
//| https://www.mql5.com/es/users/nique_372/news |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, Niquel Mendoza."
#property link "https://www.mql5.com/es/users/nique_372/news"
#property strict
#ifndef MQLARTICLES_STRATEGY_DRV_AO_MAIN_MQH
#define MQLARTICLES_STRATEGY_DRV_AO_MAIN_MQH
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
#include "..\\..\\Core\\Base.mqh"
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
2025-12-24 22:28:26 -05:00
// TPadre = CStrategyBase Or TPadre = CStrategyWithFilters
template <typename TPadre>
class CStrategyAO : public TPadre
2025-12-05 16:33:47 -05:00
{
protected:
//--- Atr para auto-optimizacion (simulacion)
int m_simulation_atr_handle;
double m_simulation_atr_arr[];
bool m_in_simulate_mode;
bool CopyDataAtrSimulate(int start, int count);
//--- Hooks
virtual bool OnSimulationStart() { return true; }
virtual void OnSimulationEnd() { }
public:
2026-02-14 17:00:45 -05:00
CStrategyAO(const ulong magic_number_, const string& symbol_, const ENUM_TIMEFRAMES timeframe_, const long chart_id_,
const int subwindow_, const ulong max_deviation_, const string& name);
2025-12-05 16:33:47 -05:00
~CStrategyAO(void);
//---
using CStrategyBase::GetSL;
using CStrategyBase::GetTP;
2026-02-14 17:00:45 -05:00
double GetSL(double entry_price, ENUM_POSITION_TYPE position_type, const datetime curr_time, int index);
double GetTP(double entry_price, ENUM_POSITION_TYPE position_type, const datetime curr_time, int index);
2025-12-05 16:33:47 -05:00
//--- Funciones de seteo
void SetAtrTP_SLOptimizer(int handle);
//--- Funciones para la simulacion de la estrategia - para la autooptimizacion
virtual bool SimulateBar(int index, double open, double high, double low, double close, datetime time, long tick_volume,
const float &genes[], OptimizationOrderTime &trades[]) { return false; } // En la simulacion de una vela
bool OnInitSimulate(int start, int count); // Al inicio de la simuation (vela start numero de velas)
void OnEndSimulate();// Al finalizar la simulacion
//--- Getters extra
2026-02-14 17:00:45 -05:00
inline bool InSimulation() const { return m_in_simulate_mode; }
2025-12-05 16:33:47 -05:00
inline int SimulationAtrHandle() const { return m_simulation_atr_handle; }
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
2026-02-14 17:00:45 -05:00
template <typename TPadre> CStrategyAO::CStrategyAO(const ulong magic_number_, const string& symbol_, const ENUM_TIMEFRAMES timeframe_, const long chart_id_,
const int subwindow_, const ulong max_deviation_, const string& name)
2026-01-31 19:44:40 -05:00
: TPadre( magic_number_, symbol_, timeframe_, chart_id_, subwindow_, max_deviation_, name),
2025-12-05 16:33:47 -05:00
m_simulation_atr_handle(INVALID_HANDLE), m_in_simulate_mode(false)
{
//---
::ArraySetAsSeries(m_simulation_atr_arr, true);
::ArrayResize(m_simulation_atr_arr, 0);
}
//+------------------------------------------------------------------+
//| Funciones para la simulacion |
//+------------------------------------------------------------------+
2025-12-24 22:28:26 -05:00
template <typename TPadre>
2025-12-05 16:33:47 -05:00
bool CStrategyAO::CopyDataAtrSimulate(int start, int count)
{
::ResetLastError();
if(::CopyBuffer(m_simulation_atr_handle, 0, start, count, m_simulation_atr_arr) < count)
{
LogError(::StringFormat("No se pudieron copiar datos del atr, ultimo error= %d", GetLastError()), FUNCION_ACTUAL);
return false;
}
return true;
}
//+------------------------------------------------------------------+
2025-12-24 22:28:26 -05:00
template <typename TPadre>
2025-12-05 16:33:47 -05:00
bool CStrategyAO::OnInitSimulate(int start, int count)
{
// Antes de la simulacion copiamos el atr para el uso del tp y sl
if(!CopyDataAtrSimulate(start, count))
return false;
if(!OnSimulationStart())
return false;
//---
m_in_simulate_mode = true;
return true;
}
//+------------------------------------------------------------------+
2025-12-24 22:28:26 -05:00
template <typename TPadre>
2025-12-05 16:33:47 -05:00
void CStrategyAO::OnEndSimulate(void)
{
m_in_simulate_mode = false;
OnSimulationEnd();
}
//+------------------------------------------------------------------+
2025-12-24 22:28:26 -05:00
template <typename TPadre>
2025-12-05 16:33:47 -05:00
void CStrategyAO::SetAtrTP_SLOptimizer(int handle)
{
if(handle == INVALID_HANDLE)
{
LogError("El handle del indicador atr para la simulacion es invalido", FUNCION_ACTUAL);
Remover();
return;
}
m_simulation_atr_handle = handle;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
2025-12-24 22:28:26 -05:00
template <typename TPadre>
2026-02-14 17:00:45 -05:00
double CStrategyAO::GetTP(double entry_price, ENUM_POSITION_TYPE position_type, const datetime curr_time, int index)
2025-12-05 16:33:47 -05:00
{
2025-12-21 13:04:17 -05:00
const double distance = (m_tp_sl_mode) ? (m_point_value * m_TP_POINTS) : (m_atr_multiplier_tp * m_simulation_atr_arr[index]);
if(distance < DBL_EPSILON)
return 0.00;
const double to_sum = ::fmax((m_stops_leevel + (m_tick.ask - m_tick.bid)), distance);
2025-12-05 16:33:47 -05:00
return position_type == POSITION_TYPE_BUY ? entry_price + to_sum : entry_price - to_sum;
}
//+------------------------------------------------------------------+
2025-12-24 22:28:26 -05:00
template <typename TPadre>
2026-02-14 17:00:45 -05:00
double CStrategyAO::GetSL(double entry_price, ENUM_POSITION_TYPE position_type, const datetime curr_time, int index)
2025-12-05 16:33:47 -05:00
{
2025-12-21 13:04:17 -05:00
const double distance = (m_tp_sl_mode) ? (m_point_value * m_SL_POINTS) : (m_atr_multiplier_sl * m_simulation_atr_arr[index]);
if(distance < DBL_EPSILON)
return 0.00;
const double to_sum = ::fmax((m_stops_leevel + (m_tick.ask - m_tick.bid)), distance);
2025-12-05 16:33:47 -05:00
return position_type == POSITION_TYPE_BUY ? entry_price - to_sum : entry_price + to_sum;
}
2025-12-21 13:04:17 -05:00
2025-12-05 16:33:47 -05:00
//+------------------------------------------------------------------+
2026-01-29 08:19:14 -05:00
#endif // MQLARTICLES_STRATEGY_DRV_AO_MAIN_MQH