MQLArticles/Utils/FA/ClasesBases.mqh

422 lines
28 KiB
MQL5

2025-09-22 09:08:13 -05:00
<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| ClasesBases.mqh |
//| Copyright 2025, Niquel Mendoza. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
2025-11-10 09:33:53 -05:00
#property copyright "Copyright 2025, Niquel Mendoza."
2025-09-22 09:08:13 -05:00
#property link "https://www.mql5.com"
2025-09-22 09:08:13 -05:00
#property strict
2025-09-22 09:08:13 -05:00
#ifndef MQLARTICLES_UTILS_FA_CLASESBASES_MQH
2025-09-22 09:08:13 -05:00
#define MQLARTICLES_UTILS_FA_CLASESBASES_MQH
2025-09-22 09:08:13 -05:00
//+------------------------------------------------------------------+
//| Includes |
//+------------------------------------------------------------------+
2025-12-21 12:41:35 -05:00
//--- (CManagerBase | CSpecializedManager)
#include "Managers.mqh"
2025-09-22 09:08:13 -05:00
2025-09-22 09:08:13 -05:00
//--- (CBarControler | CBarControlerFast | CAtr | CAtrOptimized | CAtrOptimizedZero | CAtrOptimizedNonZero | CAtrUltraOptimized)
#include "Atr.mqh"
//+------------------------------------------------------------------+
//| Clase CDiff - Defines |
//+------------------------------------------------------------------+
enum ENUM_MODE_DIFF
2025-09-22 09:08:13 -05:00
{
MODE_DIFF_BY_FIXED_POITNS, // By Points
MODE_DIFF_BY_ATR // By Atr
};
//+-------------------------------------------------------------------------+
2026-01-13 14:49:49 -05:00
//| Class CDiff - Difference calculation based on ATR or fixed points |
//+-------------------------------------------------------------------------+
2025-09-22 09:08:13 -05:00
/*
ESP:
2026-01-13 14:49:49 -05:00
Para usar la clase, solo se puede setear una vez..
2025-09-22 09:08:13 -05:00
1. llame y configure el modo SetMode
2025-09-22 09:08:13 -05:00
2. Luego condigura SetAtr y SetMode
2026-01-06 06:47:46 -05:00
Nota: El atr, ya debera de copiar los datos que luego seran accedios por esta clase, la clase unicamente calcula la
2025-09-22 09:08:13 -05:00
diferencia.
2026-01-13 14:49:49 -05:00
2025-09-22 09:08:13 -05:00
ENG:
2026-01-13 14:49:49 -05:00
2025-09-22 09:08:13 -05:00
To use the class, you can only set it once.
2026-01-13 14:49:49 -05:00
2025-09-22 09:08:13 -05:00
1. Call and set the SetMode mode.
2. Then set SetAtr and SetMode.
2025-09-22 09:08:13 -05:00
2026-01-06 06:47:46 -05:00
Note: The atr should already copy the data that will later be accessed by this class, the class only calculates the difference.
2025-09-22 09:08:13 -05:00
*/
2025-09-22 09:08:13 -05:00
//---
2025-09-22 09:08:13 -05:00
class CDiff : public CLoggerBase
{
private:
//---
int idx; // Index for ATR
string symbol; // Symbol for point calculation
double diff; // Calculated difference value
ENUM_MODE_DIFF mode_diff; // Difference calculation mode
2025-09-22 09:08:13 -05:00
double mul; // Multiplier for ATR
CAtr* atr; // Pointer to ATR object
2025-09-22 09:08:13 -05:00
bool is_atr; // Flag to indicate if ATR is used
//---
2025-09-22 09:08:13 -05:00
bool set_atr;
bool set_point;
2025-09-22 09:08:13 -05:00
public:
CDiff();
CDiff(const CDiff & diff_);
2025-09-22 09:08:13 -05:00
~CDiff() {}
2025-09-22 09:08:13 -05:00
//--- Configuration methods
void SetMode(ENUM_MODE_DIFF mode_diff_);
void SetAtr(CAtr * atr_, int idx_, double mul_);
void SetPoint(string symbol_, int min_diff_points_);
2025-09-22 09:08:13 -05:00
//--- Method to update on new candle
__forceinline void OnNewBar() { if(is_atr) diff = mul * atr[idx]; }
//---
inline bool AtrIsActivate() const { return is_atr; }
2025-09-22 09:08:13 -05:00
inline ENUM_MODE_DIFF GetTypeDiff() const { return mode_diff; }
inline double AtrMul() const { return mul; }
void AtrMul(double new_value);
inline CAtr* GetAtrPointer() const { return atr; }
2025-09-22 09:08:13 -05:00
inline void SetNewDiffAtrValue(const int new_idx);
2025-09-22 09:08:13 -05:00
inline void DiffPoints(int new_diff_points) { this.diff = new_diff_points * SymbolInfoDouble(this.symbol, SYMBOL_POINT); }
inline int DiffPoints() const { return (int)(this.diff / SymbolInfoDouble(this.symbol, SYMBOL_POINT)); }
inline double Diff() const { return diff; }
};
2025-12-21 12:41:35 -05:00
2025-09-22 09:08:13 -05:00
//+------------------------------------------------------------------+
2025-09-22 09:08:13 -05:00
//| Default constructor |
//+------------------------------------------------------------------+
2025-12-21 12:41:35 -05:00
CDiff::CDiff(void)
2025-09-22 09:08:13 -05:00
: idx(0), set_atr(false), set_point(false), diff(0.00), mul(1.00), atr(NULL), is_atr(false)
{
2025-09-22 09:08:13 -05:00
// Initialize with default values
}
2025-09-22 09:08:13 -05:00
//+------------------------------------------------------------------+
2025-09-22 09:08:13 -05:00
//| Copy constructor |
2025-09-22 09:08:13 -05:00
//+------------------------------------------------------------------+
CDiff::CDiff(const CDiff &diff_)
: idx(diff_.idx),
symbol(diff_.symbol),
diff(diff_.diff),
2025-09-22 09:08:13 -05:00
mode_diff(diff_.mode_diff),
mul(diff_.mul),
atr(diff_.atr),
is_atr(diff_.is_atr),
2025-09-22 09:08:13 -05:00
set_atr(diff_.set_atr),
set_point(diff_.set_point)
{
}
2025-09-22 09:08:13 -05:00
//+------------------------------------------------------------------+
//| Updates difference value based on ATR |
//+------------------------------------------------------------------+
2025-09-22 09:08:13 -05:00
void CDiff::SetNewDiffAtrValue(const int new_idx)
{
2025-09-22 09:08:13 -05:00
if(!is_atr) //Only set diff if it's atr and valid
return;
this.diff = atr[new_idx] * mul;
2025-09-22 09:08:13 -05:00
}
//+------------------------------------------------------------------+
//| Sets the calculation mode |
//+------------------------------------------------------------------+
void CDiff::SetMode(ENUM_MODE_DIFF mode_diff_)
{
//-- Don't allow changing mode if already configured
2025-09-22 09:08:13 -05:00
if(set_atr || set_point) //If a mode has already been set then don't allow
return;
this.mode_diff = mode_diff_;
this.is_atr = (mode_diff_ == MODE_DIFF_BY_ATR);
}
//+------------------------------------------------------------------+
//| Changes the ATR multiplier |
2025-09-22 09:08:13 -05:00
//+------------------------------------------------------------------+
void CDiff::AtrMul(double new_value)
{
2025-09-22 09:08:13 -05:00
if(!set_atr) //If atr hasn't been set, return
return;
2025-09-22 09:08:13 -05:00
//--
2025-09-22 09:08:13 -05:00
this.mul = new_value;
2026-01-06 06:47:46 -05:00
//--
2025-09-22 09:08:13 -05:00
if(mul <= 0.00)
{
2026-01-06 06:47:46 -05:00
LogWarning(StringFormat("Invalid ATR multiplier ( %f ), switching to points mode", mul), __FUNCTION__);
2025-09-22 09:08:13 -05:00
is_atr = false;
2026-01-06 06:47:46 -05:00
}
2025-09-22 09:08:13 -05:00
else
{
is_atr = true;
}
}
//+------------------------------------------------------------------+
//| Configures calculation by points |
//+------------------------------------------------------------------+
2025-09-22 09:08:13 -05:00
void CDiff::SetPoint(string symbol_, int min_diff_points_)
2025-09-22 09:08:13 -05:00
{
if(mode_diff != MODE_DIFF_BY_FIXED_POITNS)
return;
//--
2025-09-22 09:08:13 -05:00
if(set_point) //Points have already been set
{
LogError("Points mode already configured. Use DiffPoints to change", __FUNCTION__);
2025-09-22 09:08:13 -05:00
return;
}
2025-09-22 09:08:13 -05:00
//--
this.symbol = symbol_;
set_point = true;
2025-09-22 09:08:13 -05:00
//--
bool c;
2025-09-22 09:08:13 -05:00
if(symbol_ == "" || !SymbolExist(symbol_, c))
2025-09-22 09:08:13 -05:00
{
LogFatalError(StringFormat("Invalid symbol in SetPoint: %s", symbol_), __FUNCTION__);
this.diff = 0;
Remover();
return;
}
2025-09-22 09:08:13 -05:00
//---
this.diff = SymbolInfoDouble(this.symbol, SYMBOL_POINT) * min_diff_points_;
}
2025-09-22 09:08:13 -05:00
//+------------------------------------------------------------------+
//| Configures calculation by ATR |
//+------------------------------------------------------------------+
void CDiff::SetAtr(CAtr* atr_, int idx_, double mul_)
{
2025-09-22 09:08:13 -05:00
if(mode_diff != MODE_DIFF_BY_ATR)
return; //If it's not atr then we exit
2025-09-22 09:08:13 -05:00
//--
if(set_atr) //ATR has already been set
{
2025-09-22 09:08:13 -05:00
LogError("ATR mode already configured. Use AtrMul to change", __FUNCTION__);
2025-11-10 09:33:53 -05:00
2025-09-22 09:08:13 -05:00
return;