forked from nique_372/MQLArticles
153 lines
6.7 KiB
MQL5
153 lines
6.7 KiB
MQL5
//+------------------------------------------------------------------+
| |
//| 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"
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
namespace TSN
| |
{
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
// TPadre = CStrategyBase Or TPadre = CStrategyWithFilters
| |
template <typename TPadre>
| |
class CStrategyAO : public TPadre
| |
{
| |
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:
| |
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);
| |
~CStrategyAO(void);
| |
| |
//---
| |
using CStrategyBase::GetSL;
| |
using CStrategyBase::GetTP;
| |
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);
| |
| |
//--- 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
| |
inline bool InSimulation() const { return m_in_simulate_mode; }
| |
inline int SimulationAtrHandle() const { return m_simulation_atr_handle; }
| |
};
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
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)
| |
: TPadre( magic_number_, symbol_, timeframe_, chart_id_, subwindow_, max_deviation_, name),
| |
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 |
| |
//+------------------------------------------------------------------+
| |
template <typename TPadre>
| |
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;
| |
}
| |
| |
//+------------------------------------------------------------------+
| |
template <typename TPadre>
| |
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;
| |
}
| |
//+------------------------------------------------------------------+
| |
template <typename TPadre>
| |
void CStrategyAO::OnEndSimulate(void)
| |
{
| |
m_in_simulate_mode = false;
| |
OnSimulationEnd();
| |
}
| |
| |
//+------------------------------------------------------------------+
| |
template <typename TPadre>
| |
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;
| |
}
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
template <typename TPadre>
| |
double CStrategyAO::GetTP(double entry_price, ENUM_POSITION_TYPE position_type, const datetime curr_time, int index)
| |
{
| |
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);
| |
return position_type == POSITION_TYPE_BUY ? entry_price + to_sum : entry_price - to_sum;
| |
}
| |
//+------------------------------------------------------------------+
| |
template <typename TPadre>
| |
double CStrategyAO::GetSL(double entry_price, ENUM_POSITION_TYPE position_type, const datetime curr_time, int index)
| |
{
| |
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);
| |
return position_type == POSITION_TYPE_BUY ? entry_price - to_sum : entry_price + to_sum;
| |
}
| |
}
| |
//+------------------------------------------------------------------+
| |
#endif // MQLARTICLES_STRATEGY_DRV_AO_MAIN_MQH
|