MQLArticles/PosMgmt/BE/Imp/Atr.mqh
98 lines
4.3 KiB
MQL5
Nique_372
2026-10-29 11:36:56 -05:00
//+------------------------------------------------------------------+
//| Atr.mqh |
//| Copyright 2026, Niquel Mendoza. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, Niquel Mendoza."
#property link "https://www.mql5.com"
#property strict
#ifndef MQLARTICLES_POSMGMT_BE_IMP_ATR_MQH
#define MQLARTICLES_POSMGMT_BE_IMP_ATR_MQH
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
#include "..\\Base\\BEBase.mqh"
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
namespace TSN
{
//+------------------------------------------------------------------+
//| Class to apply break even based on atr |
//+------------------------------------------------------------------+
Nique_372
2026-10-29 12:18:18 -05:00
#define TSNMQLART_POSMGMT_BE_ATR_NPARAMS (3)
#define TSNMQLART_POSMGMT_BE_ATR_I_ATRPTR (0)
#define TSNMQLART_POSMGMT_BE_ATR_I_ATRMUL (1)
#define TSNMQLART_POSMGMT_BE_ATR_I_ATR_EMUL (2)
Nique_372
2026-10-29 11:36:56 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
class CBreakEvenAtr : public CBreakEvenBase
{
private:
CAtrOptimized* m_atr_ultra;
double m_atr_multiplier_be;
double m_atr_multiplier_extra_be;
public:
CBreakEvenAtr(string symbol_, ulong magic_)
: CBreakEvenBase(symbol_, magic_, TSNMQLART_POSMGMT_BE_ATR_NPARAMS),
m_atr_ultra(NULL), m_atr_multiplier_be(1.0), m_atr_multiplier_extra_be(1.0) {}
//---
bool Add(ulong post_ticket, double open_price, double sl_price, ENUM_POSITION_TYPE position_type,
Nique_372
2026-10-29 17:44:49 -05:00
datetime open_time, const string& sym) override final;
Nique_372
2026-10-29 11:36:56 -05:00
bool Set(const BreakEvenParams &params[]) override final;
};
//+------------------------------------------------------------------+
//| Function to set CBreakEvenAtr variables with MqlParams array |
//+------------------------------------------------------------------+
bool CBreakEvenAtr::Set(const BreakEvenParams &params[])
{
//---
m_atr_ultra = params[TSNMQLART_POSMGMT_BE_ATR_I_ATRPTR].atr_pointer_value;
m_atr_multiplier_be = params[TSNMQLART_POSMGMT_BE_ATR_I_ATRMUL].double_value;
Nique_372
2026-10-29 12:18:18 -05:00
m_atr_multiplier_extra_be = params[TSNMQLART_POSMGMT_BE_ATR_I_ATR_EMUL].double_value;
Nique_372
2026-10-29 11:36:56 -05:00
//---
if(m_atr_multiplier_extra_be >= m_atr_multiplier_be)
Nique_372
2026-11-06 10:40:44 -05:00
return CErrorTrace::FailInit<CBasicBEFormater>(TSNMQLART_BE_TYPES_ERR_ATR_INVALID_EXTRABE_VAL, CBitsReinterprete::ToBasic<ulong, double>(m_atr_multiplier_extra_be));
Nique_372
2026-10-29 11:36:56 -05:00
if(CheckPointer(m_atr_ultra) == POINTER_INVALID)
Nique_372
2026-11-06 10:40:44 -05:00
return CErrorTrace::FailInit<CBasicBEFormater>(TSNMQLART_BE_TYPES_ERR_ATR_PTR_INVALID, 0);
Nique_372
2026-10-29 11:36:56 -05:00
return true;
}
//+------------------------------------------------------------------+
//| Function to add an element to the positions array |
//+------------------------------------------------------------------+
bool CBreakEvenAtr::Add(ulong post_ticket, double open_price, double sl_price, ENUM_POSITION_TYPE position_type, datetime open_time, const string & sym)
{
if(sym != m_symbol)
Nique_372
2026-11-06 10:40:44 -05:00
return CErrorTrace::FailInit<CBasicBEFormater>(TSNMQLART_BE_TYPES_ERR_INV_SYMBOL, post_ticket);
Nique_372
2026-10-29 11:36:56 -05:00
//---
Nique_372
2026-11-01 17:28:51 -05:00
MQLARTICLES_BREAKEVEN_PROLOGO(position_type, post_ticket)
//---
Nique_372
2026-10-29 11:36:56 -05:00
const double val = m_atr_ultra.GetAtrValue(open_time);
m_pos_be[m_pos_be_s].breakeven_price = position_type == POSITION_TYPE_BUY ? open_price + (val * m_atr_multiplier_extra_be) : open_price - (val * m_atr_multiplier_extra_be);
m_pos_be[m_pos_be_s].price_to_beat = position_type == POSITION_TYPE_BUY ? open_price + (val * m_atr_multiplier_be) : open_price - (val * m_atr_multiplier_be);
Nique_372
2026-11-01 17:28:51 -05:00
//---
m_pos_be_s++;
Nique_372
2026-10-29 17:44:49 -05:00
//---
Nique_372
2026-10-29 11:36:56 -05:00
return true;
}
}
#endif // MQLARTICLES_POSMGMT_BE_IMP_ATR_MQH