forked from nique_372/MQLArticles
//+------------------------------------------------------------------+
| |
//| Fibonacci.mqh |
| |
//| Copyright 2025, Niquel Mendoza. |
| |
//| https://www.mql5.com |
| |
//+------------------------------------------------------------------+
| |
#property copyright "Copyright 2025, Niquel Mendoza."
| |
#property link "https://www.mql5.com"
| |
#property strict
| |
| |
#ifndef MQLARTICLES_UTILS_OBJECTS_FIBBO_FIBONACCI_MQH
| |
#define MQLARTICLES_UTILS_OBJECTS_FIBBO_FIBONACCI_MQH
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
#include "Def.mqh"
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
namespace TSN
| |
{
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
class CFibbo
| |
{
| |
private:
| |
//--- Caracteristicas principales
| |
uint m_flags; // Banderas de configuracion
| |
| |
//--- Precios
| |
double m_price_2;
| |
double m_price_1;
| |
| |
//--- Dibujado
| |
string m_obj_name;
| |
datetime m_init_fibbo;
| |
long m_chart_id;
| |
int m_subwin;
| |
//---
| |
CHashMapFastP(double, int, _fibbo) m_hash_level_to_index;
| |
| |
//--- Fibbo levels
| |
FibboLevel m_fibbo_levels[];
| |
int m_total_fibbo_s;
| |
| |
//---
| |
bool CalculatePriceLevelsFibbo();
| |
| |
//---
| |
ENUM_TSNQMQLART_FIBBO_ERR m_last_err;
| |
| |
| |
public:
| |
CFibbo(const bool draw);
| |
~CFibbo(void);
| |
| |
//--- Creacion
| |
bool Create(const long chart_ID,
| |
const string& name,
| |
const int sub_window,
| |
const color clr,
| |
const ENUM_LINE_STYLE style,
| |
const int width,
| |
const string& tooltip,
| |
const bool back = false,
| |
const bool selection = false,
| |
const long z_order = 0);
| |
| |
| |
//--- Trabajo con nivels e inicializacion
| |
bool InitFibbo(const string& vals, const string& clrs, ENUM_LINE_STYLE styles, int widths, ushort separator_str);
| |
__forceinline uint8_t ModifyLevel(double level, color new_color = clrNONE, ENUM_LINE_STYLE new_style = WRONG_VALUE, int new_widt = -1);
| |
uint8_t ModifyLevelByIdx(int level_index, color new_color = clrNONE, ENUM_LINE_STYLE new_style = WRONG_VALUE, int new_widt = -1);
| |
| |
//--- Trabajar con el fibbo (move, hide, show, update time2 del objeto)
| |
bool Move(datetime new_init, datetime new_end, double price1, double price2);
| |
__forceinline void UpdateTime2(const datetime new_end) const; // Solo actulizar el fibbo a un nuevo time2
| |
__forceinline bool Hide() const { return HIDE_OBJECT(m_obj_name, m_chart_id); }
| |
__forceinline bool Show() const { return SHOW_OBJECT(m_obj_name, m_chart_id); }
| |
| |
//--- Resolucion de valores (aqui usa el hashmap y en base a un double (level_val) te da el indice que corresponde)
| |
__forceinline bool GetLevelIndex(const double level_val, int& idx);
| |
| |
//--- Levels
| |
// Obtiene los niveles del fibbo
| |
int GetArrayLevel(double &levels[], bool contains_lebel_0_100 = false) const;
| |
bool GetNearestLevel(double target_price, ENUM_MODE_NEAREST_LEVEL_FIBBO mode, double& level);
| |
| |
//--- Props del fibbo
| |
__forceinline int TotalFibboLevels() const { return m_total_fibbo_s; }
| |
__forceinline double Price1() const { return m_price_1; }
| |
__forceinline double Price2() const { return m_price_2; }
| |
__forceinline datetime InitTime() const { return m_init_fibbo; }
| |
__forceinline long ChartId() const { return m_chart_id; }
| |
__forceinline int Subwin() const { return m_subwin; }
| |
__forceinline int FibboLevelLastIndex() const { return m_total_fibbo_s - 1; }
| |
__forceinline bool IsCreated() const { return (m_flags & TSNMQLART_GRAPH_FIBBO_F_CREATED) != 0; }
| |
__forceinline bool CanDraw() const { return (m_flags & TSNMQLART_GRAPH_FIBBO_F_DRAW_OBJ) != 0; }
| |
| |
//--- Error
| |
__forceinline ENUM_TSNQMQLART_FIBBO_ERR LastErr() const { return m_last_err; }
| |
| |
| |
//--- Niveles
| |
// obtencion de nivel (solo da en base a idx) si quieres via valor primero resuelvelo usando GetLevelIndex
| |
__forceinline FibboLevel GetLevel(const int i) const { return m_fibbo_levels[i]; }
| |
__forceinline double GetLevelPrice(const int i) const { return m_fibbo_levels[i].level_price; }
| |
| |
//--- Info
| |
void Summary();
| |
| |
};
| |
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
CFibbo::CFibbo(const bool draw)
| |
: m_init_fibbo(wrong_datetime), m_price_2(0.00), m_price_1(0.00), m_subwin(0), m_chart_id(0), m_total_fibbo_s(0),
| |
m_flags(draw ? TSNMQLART_GRAPH_FIBBO_F_DRAW_OBJ : 0), m_last_err(TSNQMQLART_FIBBO_ERR_NOT)
| |
{
| |
}
| |
| |
//+------------------------------------------------------------------+
| |
CFibbo::~CFibbo()
| |
{
| |
if((m_flags & TSNMQLART_GRAPH_FIBBO_F_DRAW_OBJ) != 0)
| |
::ObjectDelete(m_chart_id, m_obj_name);
| |
}
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
bool CFibbo::Create(const long chart_ID,
| |
const string& name,
| |
const int sub_window,
| |
const color clr,
| |
const ENUM_LINE_STYLE style,
| |
const int width,
| |
const string& tooltip,
| |
const bool back = false,
| |
const bool selection = false,
| |
const long z_order = 0)
| |
{
| |
| |
//---
| |
#ifdef TSNMQLART_GRAPOBJ_FIBBO_ELOGS
| |
if((m_flags & TSNMQLART_GRAPH_FIBBO_F_CREATED) != 0)
| |
{
| |
FastLog(FUNCION_ACTUAL, WARNING_TEXT, "Se esta volviendo a redibujar el objeto fibbo..");
| |
}
| |
#endif // TSNMQLART_GRAPOBJ_FIBBO_ELOGS
| |
| |
//---
| |
if((m_flags & TSNMQLART_GRAPH_FIBBO_F_DRAW_OBJ) != 0)
| |
{
| |
if(!CNativeoGObjs::FiboLevelsCreate(chart_ID, name, sub_window, 0, 0, 0, 0, clr, style, width, back, selection, false, false, z_order))
| |
{
| |
#ifdef TSNMQLART_GRAPOBJ_FIBBO_ELOGS
| |
FastLog(FUNCION_ACTUAL, ERROR_TEXT, StringFormat("No se pudo crear el fibbo[ %s ], ultimo error = %d", name, GetLastError()));
| |
#endif //TSNMQLART_GRAPOBJ_FIBBO_ELOGS
| |
return CErrorTrace::FailInit<CFibboFormater>(TSNQMQLART_FIBBO_ERR_FAILED_CREATE_OBJ, 0, ::GetLastError());
| |
}
| |
//---
| |
::ObjectSetString(chart_ID, name, OBJPROP_TOOLTIP, tooltip);
| |
}
| |
| |
//---
| |
m_chart_id = chart_ID;
| |
m_subwin = sub_window;
| |
m_obj_name = name;
| |
| |
//---
| |
m_flags |= TSNMQLART_GRAPH_FIBBO_F_CREATED;
| |
| |
//---
| |
return true;
| |
}
| |
| |
//+------------------------------------------------------------------+
| |
__forceinline void CFibbo::UpdateTime2(const datetime new_end) const
| |
{
| |
if((m_flags & TSNMQLART_GRAPH_FIBBO_F_DRAW_OBJ) != 0)
| |
::ObjectSetInteger(m_chart_id, m_obj_name, OBJPROP_TIME, 1, new_end);
| |
}
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
__forceinline bool CFibbo::GetLevelIndex(const double level_val, int& idx)
| |
{
| |
return m_hash_level_to_index.TryGet(level_val, idx);
| |
}
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
bool CFibbo::CalculatePriceLevelsFibbo(void)
| |
{
| |
if(m_price_1 > m_price_2)
| |
{
| |
/*
| |
p1 100
| |
| |
| |
p2 p2 0
| |
*/
| |
const double diff = (m_price_1 - m_price_2);
| |
for(int i = 0; i < m_total_fibbo_s; i++)
| |
m_fibbo_levels[i].level_price = ((m_price_2 + ((m_fibbo_levels[i].level_value) * diff)));
| |
}
| |
else
| |
if(m_price_2 > m_price_1)
| |
{
| |
/*
| |
p2 p2 0
| |
| |
p1 100
| |
*/
| |
| |
const double diff = (m_price_2 - m_price_1);
| |
for(int i = 0; i < m_total_fibbo_s; i++)
| |
m_fibbo_levels[i].level_price = ((m_price_2 - ((m_fibbo_levels[i].level_value) * diff)));
| |
}
| |
else
| |
{
| |
m_last_err = TSNQMQLART_FIBBO_ERR_FAILED_CALC_PRICE_INV_ARE_EQ;
| |
return false;
| |
}
| |
return true;
| |
}
| |
| |
//+------------------------------------------------------------------+
| |
bool CFibbo::Move(datetime new_init, datetime new_end, double price1, double price2)
| |
{
| |
//---
| |
m_price_1 = price1;
| |
m_price_2 = price2;
| |
m_init_fibbo = new_init;
| |
if(!CalculatePriceLevelsFibbo())
| |
return false; // ya pone el error
| |
| |
//---
| |
if((m_flags & TSNMQLART_GRAPH_FIBBO_F_DRAW_OBJ) == 0)
| |
return true; // aqui noma no hace falta ir mas abajo
| |
| |
//---
| |
::ResetLastError();
| |
| |
//---
| |
if(!::ObjectMove(m_chart_id, m_obj_name, 0, new_init, price1))
| |
{
| |
#ifdef TSNMQLART_GRAPOBJ_FIBBO_ELOGS
| |
FastLog(FUNCION_ACTUAL, ERROR_TEXT, ::StringFormat("No se pudo movel el anclaje 0, new_init = %s >> price1 = %.5f, ultimo error = %d",
| |
::TimeToString(new_init), price1, ::GetLastError()));
| |
#endif // TSNMQLART_GRAPOBJ_FIBBO_ELOGS
| |
m_last_err = TSNQMQLART_FIBBO_ERR_FAILED_MOVE_OBJECT;
| |
return false;
| |
}
| |
if(!::ObjectMove(m_chart_id, m_obj_name, 1, new_end, price2))
| |
{
| |
#ifdef TSNMQLART_GRAPOBJ_FIBBO_ELOGS
| |
FastLog(FUNCION_ACTUAL, ERROR_TEXT, ::StringFormat("No se pudo movel el anclaje 1, time2 = %s >> price2 = %.5f, ultimo error = %d",
| |
::TimeToString(new_end), price2, ::GetLastError()));
| |
#endif // TSNMQLART_GRAPOBJ_FIBBO_ELOGS
| |
m_last_err = TSNQMQLART_FIBBO_ERR_FAILED_MOVE_OBJECT;
| |
return false;
| |
}
| |
| |
//---
| |
return true; // exito
| |
}
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
bool CFibbo::InitFibbo(const string& vals, const string& clrs, ENUM_LINE_STYLE styles, int widths, ushort separator_str)
| |
{
| |
//---
| |
color clr_arr[];
| |
int n_clr_arr = 0;
| |
double level_arr[];
| |
int n_level_arr = 0;
| |
| |
//---
| |
n_clr_arr = CStringFunc::ToArr<color, CFibboStrConvClr>(clrs, clr_arr, separator_str);
| |
if(n_clr_arr == 0) // string vacio: nadie puso frame (hoja)
| |
return CErrorTrace::FailInit<CFibboFormater>(TSNQMQLART_FIBBO_ERR_EMPTY_COLOR, 0);
| |
if(n_clr_arr < 0) // fallo el converter (CFibboStrConvClr::ToValue ya puso su frame)
| |
return CErrorTrace::FailWrap<CFibboFormater>(TSNQMQLART_FIBBO_ERR_IN_PARSE_COLORS_STR, 0);
| |
| |
//---
| |
if((n_level_arr = CStringFunc::ToArr<double, CStringConvert>(vals, level_arr, separator_str)) < 1)
| |
return CErrorTrace::FailInit<CFibboFormater>(TSNQMQLART_FIBBO_ERR_FIBBO_VALUE_EMPTY, 0);
| |
| |
//--- Deben ser iguales (en info van los dos tamaños)
| |
if(n_level_arr != n_clr_arr)
| |
return CErrorTrace::FailInit<CFibboFormater>(TSNQMQLART_FIBBO_ERR_ARRAYS_MUST_BE_EQUAL, ulong(uint(n_level_arr)) | ulong(uint(n_clr_arr)) << 32);
| |
| |
//---
| |
m_hash_level_to_index.Clear();
| |
m_total_fibbo_s = ::ArrayResize(m_fibbo_levels, n_level_arr);
| |
| |
//---
| |
for(int i = 0; i < n_clr_arr; i++)
| |
{
| |
//---
| |
// Color
| |
m_fibbo_levels[i].level_color = clr_arr[i];
| |
// Asignamos valor, la idea es ver si esta repetido, si lo esta err
| |
const double v = level_arr[i];
| |
for(int x = 0; x < i; x++)
| |
if(::fabs(m_fibbo_levels[x].level_value - v) <= CFIBBO_MIN_VALUE)
| |
return CErrorTrace::FailInit<CFibboFormater>(TSNQMQLART_FIBBO_ERR_LEVELVALUE_IS_REPETIDO, CBitsReinterprete::ToBasic<ulong, double>(v)); // en info el valor repetido
| |
| |
// terminamos
| |
m_fibbo_levels[i].level_value = v;
| |
}
| |
| |
//--- Ordenamos de menor a mayor
| |
TsnSort(m_fibbo_levels, 0, m_total_fibbo_s - 1, FibboLevel);
| |
| |
//---
| |
if((m_flags & TSNMQLART_GRAPH_FIBBO_F_DRAW_OBJ) != 0)
| |
{
| |
//--- Agregamos al hashmap
| |
for(int i = 0; i < m_total_fibbo_s; i++)
| |
{
| |
//--- añadimos al hashmap
| |
m_hash_level_to_index.Add(m_fibbo_levels[i].level_value, i);
| |
| |
//--- props
| |
::ObjectSetDouble(m_chart_id, m_obj_name, OBJPROP_LEVELVALUE, i, m_fibbo_levels[i].level_value);
| |
::ObjectSetInteger(m_chart_id, m_obj_name, OBJPROP_LEVELCOLOR, i, m_fibbo_levels[i].level_color);
| |
::ObjectSetInteger(m_chart_id, m_obj_name, OBJPROP_LEVELSTYLE, i, styles);
| |
::ObjectSetInteger(m_chart_id, m_obj_name, OBJPROP_LEVELWIDTH, i, widths);
| |
::ObjectSetString(m_chart_id, m_obj_name, OBJPROP_LEVELTEXT, i, ::DoubleToString(100 * m_fibbo_levels[i].level_value, 1));
| |
}
| |
//--- Establecemos el número de los niveles
| |
::ObjectSetInteger(m_chart_id, m_obj_name, OBJPROP_LEVELS, m_total_fibbo_s);
| |
}
| |
else
| |
{
| |
for(int i = 0; i < m_total_fibbo_s; i++)
| |
m_hash_level_to_index.Add(m_fibbo_levels[i].level_value, i);
| |
}
| |
| |
//--- Info
| |
#ifdef TSNMQLART_GRAPOBJ_FIBBO_ELOGS
| |
FastLog(FUNCION_ACTUAL, INFO_TEXT, "Niveles de fibbos listos: ");
| |
ArrayPrint(m_fibbo_levels, 3, " | ");
| |
#endif // TSNMQLART_GRAPOBJ_FIBBO_ELOGS
| |
| |
//--- ejecución con éxito
| |
return(true);
| |
}
| |
| |
//+------------------------------------------------------------------+
| |
__forceinline uint8_t CFibbo::ModifyLevel(double level, color new_color = clrNONE, ENUM_LINE_STYLE new_style = -1, int new_widt = -1)
| |
{
| |
int level_index;
| |
return m_hash_level_to_index.TryGet(level, level_index) ? ModifyLevel(level_index, new_color, new_style, new_widt) : 0;
| |
}
| |
| |
//+------------------------------------------------------------------+
| |
uint8_t CFibbo::ModifyLevelByIdx(int level_index, color new_color = clrNONE, ENUM_LINE_STYLE new_style = -1, int new_widt = -1)
| |
{
| |
//---
| |
uint8_t f = 0;
| |
const bool nd = (m_flags & TSNMQLART_GRAPH_FIBBO_F_DRAW_OBJ) == 0;
| |
| |
//---
| |
if(new_color != clrNONE && (nd || ::ObjectSetInteger(m_chart_id, m_obj_name, OBJPROP_LEVELCOLOR, level_index, new_color)))
| |
f |= FIBBO_FLAG_LEVEL_MODIFY_COLOR_SUCCESS;
| |
| |
//---
| |
if(new_style != WRONG_VALUE && (nd || ::ObjectSetInteger(m_chart_id, m_obj_name, OBJPROP_LEVELSTYLE, level_index, new_style)))
| |
f |= FIBBO_FLAG_LEVEL_MODIFY_STYLE_SUCCESS;
| |
| |
//---
| |
if(new_widt >= 1 && (nd || ::ObjectSetInteger(m_chart_id, m_obj_name, OBJPROP_LEVELWIDTH, level_index, new_widt)))
| |
f |= FIBBO_FLAG_LEVEL_MODIFY_WIDTH_SUCCESS;
| |
| |
//---
| |
return f;
| |
}
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
bool CFibbo::GetNearestLevel(double target_price, ENUM_MODE_NEAREST_LEVEL_FIBBO mode, double& level)
| |
{
| |
//---
| |
if(m_total_fibbo_s == 0)
| |
{
| |
m_last_err = TSNQMQLART_FIBBO_ERR_FAILED_EMPTY_FIBBO;
| |
return false;
| |
}
| |
| |
//---
| |
const bool precio_ascendente = (m_fibbo_levels[0].level_price < m_fibbo_levels[m_total_fibbo_s - 1].level_price);
| |
| |
//---
| |
if(mode == FIBBO_LEVEL_MAYOR_A_TARGE_PRICE)
| |
{
| |
if(precio_ascendente)
| |
{
| |
// Buscar de menor a mayor
| |
for(int i = 0; i < m_total_fibbo_s; i++)
| |
if(m_fibbo_levels[i].level_price > target_price)
| |
{
| |
level = m_fibbo_levels[i].level_price;
| |
return true;
| |
}
| |
}
| |
else
| |
{
| |
// Buscar de mayor a menor (invertido)
| |
for(int i = m_total_fibbo_s - 1; i >= 0; i--)
| |
if(m_fibbo_levels[i].level_price > target_price)
| |
{
| |
level = m_fibbo_levels[i].level_price;
| |
return true;
| |
}
| |
}
| |
}
| |
else
| |
{
| |
if(precio_ascendente)
| |
{
| |
// Buscar el último menor
| |
for(int i = m_total_fibbo_s - 1; i >= 0; i--)
| |
if(m_fibbo_levels[i].level_price < target_price)
| |
{
| |
level = m_fibbo_levels[i].level_price;
| |
return true;
| |
}
| |
}
| |
else
| |
{
| |
// Buscar el primero menor (en orden descendente)
| |
for(int i = 0; i < m_total_fibbo_s; i++)
| |
if(m_fibbo_levels[i].level_price < target_price)
| |
{
| |
level = m_fibbo_levels[i].level_price;
| |
return true;
| |
}
| |
}
| |
}
| |
| |
//---
| |
#ifdef TSNMQLART_GRAPOBJ_FIBBO_ELOGS
| |
FastLog(FUNCION_ACTUAL, WARNING_TEXT, ::StringFormat("No se ha encontrado nivel cercano al target price = %.*f", 5, target_price));
| |
#endif // TSNMQLART_GRAPOBJ_FIBBO_ELOGS
| |
| |
| |
//---
| |
m_last_err = TSNQMQLART_FIBBO_ERR_FAILED_NOT_FOUND_NEAREST_LEVEL;
| |
| |
//---
| |
return false;
| |
}
| |
| |
//+------------------------------------------------------------------+
| |
int CFibbo::GetArrayLevel(double &levels[], bool contains_lebel_0_100 = false) const
| |
{
| |
::ArrayResize(levels, m_total_fibbo_s);
| |
int w = 0;
| |
for(int i = 0; i < m_total_fibbo_s; i++)
| |
{
| |
const double v = m_fibbo_levels[i].level_value;
| |
if(!contains_lebel_0_100 && (v == 0.00 || v == 1.00))
| |
continue;
| |
levels[w++] = v;
| |
}
| |
return ::ArrayResize(levels, w);
| |
}
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
void CFibbo::Summary(void)
| |
{
| |
::PrintFormat("------| Fibbo %s |------", m_obj_name);
| |
::Print("Levels: ");
| |
::ArrayPrint(m_fibbo_levels, 3, " | ", 0, m_total_fibbo_s);
| |
}
| |
| |
//---
| |
}
| |
//+------------------------------------------------------------------+
| |
#endif // MQLARTICLES_UTILS_OBJECTS_FIBBO_FIBONACCI_MQH
| |
//+------------------------------------------------------------------+
|