315 行
9.6 KiB
Python
#+------------------------------------------------------------------+
| |||
#| Templatates.py |
| |||
#| Copyright 2026, Niquel Mendoza. |
| |||
#| https://www.mql5.com |
| |||
#+------------------------------------------------------------------+
| |||
| |||
from Base.Constants import *
| |||
| |||
#+------------------------------------------------------------------+
| |||
#| Templates (comentarios) |
| |||
#+------------------------------------------------------------------+
| |||
# Linea simple //+----...----+
| |||
TPL_LINE = "//+" + "-" * (SCRIPT_LINE_WIDTH - 4) + "+"
| |||
| |||
# Linea con texto //| texto | (alineado a la derecha)
| |||
TPL_LINEW = "//|{text:>" + str(SCRIPT_LINEW_PURESIZE) + "} |"
| |||
| |||
# Bloque //+---+ //| | //+---+
| |||
TPL_BLOCK = TPL_LINE + SCRIPT_EOL + TPL_LINEW.format(text="") + SCRIPT_EOL + TPL_LINE + SCRIPT_EOL
| |||
| |||
#+------------------------------------------------------------------+
| |||
#| Templates (archivo) |
| |||
#+------------------------------------------------------------------+
| |||
TPL_HEADER = """{line}
| |||
{file}
| |||
{autor}
| |||
{link}
| |||
{line}
| |||
#property copyright "{autor_raw}"
| |||
#property link "{link_raw}"
| |||
#property strict
| |||
| |||
"""
| |||
| |||
TPL_GUARD_INIT = """#ifndef {guard}
| |||
#define {guard}
| |||
| |||
"""
| |||
| |||
TPL_GUARD_END = "#endif // {guard}\n"
| |||
| |||
# {open}{path}{close} => "path" o <path>
| |||
TPL_INCLUDE = "#include {open}{path}{close}\n"
| |||
| |||
| |||
#+------------------------------------------------------------------+
| |||
#| Template (clase indicador) |
| |||
#+------------------------------------------------------------------+
| |||
# {{ }} => llaves literales (format)
| |||
TPL_CLASS_IND = """class {class_name} : public CiIndicatorSimple<MQLA_B({num_buffers})>
| |||
{{
| |||
public:
| |||
{class_name}() : CiIndicatorSimple<MQLA_B({num_buffers})>({type}) {{}}
| |||
~{class_name}() {{}}
| |||
bool Create(ENUM_TIMEFRAMES timeframe, string symbol, bool series, bool hide{firma})
| |||
{{
| |||
if(hide)
| |||
::TesterHideIndicators(true);
| |||
const int h = ::{creator}(symbol, timeframe{resource}{valores});
| |||
if(hide)
| |||
::TesterHideIndicators(false);
| |||
m_timeframe = timeframe;
| |||
m_symbol = symbol;
| |||
return CiIndicatorSimple<MQLA_B({num_buffers})>::Create(h, series);
| |||
}}
| |||
}};
| |||
"""
| |||
| |||
#+------------------------------------------------------------------+
| |||
#| Pool (cache de indicadores) |
| |||
#+------------------------------------------------------------------+
| |||
POOL_CLASS_NAME = "CIndicatorCache"
| |||
| |||
# Tipo del parametro => escritura en s_bit (la key del pool). {v} = nombre del parametro
| |||
# Regla: tipos nativos => POOL_WRITERS | ENUM_* => POOL_WRITE_ENUM | cualquier otro => error
| |||
POOL_WRITE_ENUM = "WriteInteger({v})"
| |||
POOL_WRITERS = {
| |||
"bool" : "WriteBool({v})",
| |||
"string" : "WriteStringWPadd({v})",
| |||
"double" : "WriteDouble(NormalizeDouble({v}, INDICATOR_CACHE_DBL_DIG))",
| |||
"float" : "WriteFloat({v})",
| |||
**{t : "WriteInteger({v})" for t in ("char", "uchar", "short", "ushort", "int", "uint", "long", "ulong", "color", "datetime")},
| |||
}
| |||
| |||
# {m} = miembro (prefix en snake_case), {prefix} = prefix tal cual (nombre de Get/Remove y key)
| |||
TPL_POOL_MEMBER = """ //--- {prefix}
| |||
static {class_name}* s_{m}[];
| |||
static IndicatorCacheExtraData s_{m}_data[];
| |||
static int s_{m}_size;
| |||
static int s_{m}_r;
| |||
| |||
"""
| |||
| |||
TPL_POOL_DECL = """ //--- {prefix}
| |||
static {class_name}* Get{prefix}(ENUM_TIMEFRAMES timeframe, string symbol, bool series, bool hide{firma});
| |||
static __forceinline bool Remove{prefix}({class_name}* element) {{ return RemoveFromArray(element, s_{m}, s_{m}_data, s_{m}_size); }}
| |||
| |||
"""
| |||
| |||
TPL_POOL_CLASS_HEAD = """class {pool}
| |||
{{
| |||
private:
| |||
//---
| |||
static TSN::CHashMapFastNor<TSN::CBitBuffer, int, TSN::CGenericHash_CBitBuffer> s_hash_str_to_int;
| |||
static bool s_is_active;
| |||
static CBitBuffer s_bit;
| |||
| |||
"""
| |||
| |||
TPL_POOL_CLASS_MID = """ //--- Funcion template para remover
| |||
template <typename T>
| |||
static bool RemoveFromArray(T* &element, T* &array[], IndicatorCacheExtraData &data[], int &size);
| |||
| |||
public:
| |||
{pool}(void) {{}}
| |||
~{pool}(void) {{}}
| |||
| |||
//--- Init - Deinit
| |||
static void Init();
| |||
static void Deinit(const int reason);
| |||
| |||
//--- IsActive
| |||
static __forceinline bool IsActive() {{ return s_is_active; }}
| |||
| |||
"""
| |||
| |||
TPL_POOL_CLASS_END = " };\n"
| |||
| |||
# Init + RemoveFromArray (no dependen de los indicadores). {pool} = nombre de la clase
| |||
TPL_POOL_COMMON = """//+------------------------------------------------------------------+
| |||
//| Init |
| |||
//+------------------------------------------------------------------+
| |||
static void {pool}::Init(void)
| |||
{{
| |||
if(s_is_active)
| |||
return;
| |||
| |||
//---
| |||
s_is_active = true;
| |||
}}
| |||
| |||
//+------------------------------------------------------------------+
| |||
//| |
| |||
//+------------------------------------------------------------------+
| |||
template <typename T>
| |||
static bool {pool}::RemoveFromArray(T* &element, T* &array[], IndicatorCacheExtraData &data[], int &size)
| |||
{{
| |||
//---
| |||
const ENUM_POINTER_TYPE ptr_type = CheckPointer(element);
| |||
if(ptr_type == POINTER_INVALID)
| |||
{{
| |||
FastLog(FUNCION_ACTUAL, ERROR_TEXT, StringFormat("se esta intentando eliminar un elemento INVALIDO, type = %s", typename(T)));
| |||
return false;
| |||
}}
| |||
| |||
//--- Find
| |||
int idx = -1;
| |||
for(int i = 0; i < size; i++)
| |||
{{
| |||
if(array[i] == element)
| |||
{{
| |||
idx = i;
| |||
break;
| |||
}}
| |||
}}
| |||
| |||
//--- Verificamos si existe
| |||
if(idx == -1)
| |||
{{
| |||
FastLog(FUNCION_ACTUAL, ERROR_TEXT, "El puntero que se busca eliminar ya no existe");
| |||
return false;
| |||
}}
| |||
| |||
//--- Reducimos y comparamos, en caso ser menor o igual a 0 se termino para esta "key" lo eliminamos
| |||
if(--data[idx].ref_count <= 0)
| |||
{{
| |||
//--- Eliminar objeto, solo si es DINAMICO
| |||
if(ptr_type == POINTER_DYNAMIC)
| |||
delete array[idx];
| |||
| |||
//--- Removemos del hashmap
| |||
if(!s_hash_str_to_int.Remove(data[idx]))
| |||
{{
| |||
FastLog(FUNCION_ACTUAL, CRITICAL_ERROR_TEXT,
| |||
StringFormat("No se pudo eliminar el key con hash = '%I64u'", CGenericHash_CBitBuffer::Hash(data[idx])));
| |||
return false;
| |||
}}
| |||
| |||
//--- Swap con el ultimo elemento
| |||
const int last = --size;
| |||
bool res = true;
| |||
if(last != idx)
| |||
{{
| |||
array[idx] = array[last]; // puntero
| |||
data[idx].RobarDe(data[last]); // data
| |||
// Nota aqui el objeto que estaba en LAST ahora estara en la posicion IDX por loq ue key es de LAST
| |||
res = s_hash_str_to_int.TrySet(data[idx], idx);
| |||
}}
| |||
| |||
//---
| |||
return res;
| |||
}}
| |||
| |||
//--- exito en eliminar (Aqunue como tal no se elimino la ref)
| |||
return true;
| |||
}}
| |||
| |||
"""
| |||
| |||
# El "allocador" (crecer el array de punteros y el de datos juntos) es el define MQLA_INDPOOL_CRESERVE2 de PoolDef.mqh,
| |||
# envuelto en #ifndef: se puede reemplazar definiendolo (misma firma: arr, data, s, r) antes de incluir el pool.
| |||
| |||
# {writes} = una linea " s_bit.Write*(...);\n" por parametro
| |||
TPL_POOL_GET = """static {class_name}* {pool}::Get{prefix}(ENUM_TIMEFRAMES timeframe, string symbol, bool series, bool hide{firma})
| |||
{{
| |||
//--- Normalizamos timeframe
| |||
if(timeframe == PERIOD_CURRENT)
| |||
timeframe = _Period;
| |||
| |||
//--- Key
| |||
s_bit.Clear();
| |||
s_bit.WriteStringWPadd("{prefix}");
| |||
s_bit.WriteStringWPadd(symbol);
| |||
s_bit.WriteInteger(timeframe);
| |||
{writes} s_bit.WriteBool(series);
| |||
| |||
//--- Verificamos si existe en caso no existe lo creamos
| |||
int idx;
| |||
if(s_hash_str_to_int.ReserveSlot(s_bit))
| |||
{{
| |||
//---
| |||
idx = s_{m}_size++;
| |||
| |||
//--- Arrays (el de punteros y el de datos crecen juntos)
| |||
MQLA_INDPOOL_CRESERVE2(s_{m}, s_{m}_data, s_{m}_size, s_{m}_r)
| |||
| |||
//---
| |||
s_{m}[idx] = new {class_name}();
| |||
if(!s_{m}[idx].Create(timeframe, symbol, series, hide{valores}))
| |||
{{
| |||
delete s_{m}[idx];
| |||
s_{m}_size--;
| |||
s_hash_str_to_int.RemoveLastFindCall(); // ultimo call
| |||
return NULL;
| |||
}}
| |||
| |||
//--- copia por =
| |||
(CBitBuffer&)s_{m}_data[idx] = s_bit; // #1 Copia
| |||
s_{m}_data[idx].ref_count = 1;
| |||
| |||
//--- Hashmap
| |||
const uint p = s_hash_str_to_int.m_last_pos_tbl;
| |||
s_hash_str_to_int.m_table[p].key.RobarDe(s_bit);
| |||
s_hash_str_to_int.m_table[p].value = idx;
| |||
s_bit.SyncArrayRSize(); // esto por el swap tenemos que acutlizar r
| |||
}}
| |||
else
| |||
{{
| |||
const uint p = s_hash_str_to_int.m_last_pos_tbl;
| |||
idx = s_hash_str_to_int.m_table[p].value;
| |||
s_{m}_data[idx].ref_count++;
| |||
}}
| |||
| |||
//--- Retornamos puntero
| |||
return s_{m}[idx];
| |||
}}
| |||
| |||
"""
| |||
| |||
TPL_POOL_DEINIT_HEAD = """//+------------------------------------------------------------------+
| |||
//| Deinit |
| |||
//+------------------------------------------------------------------+
| |||
static void {pool}::Deinit(const int reason)
| |||
{{
| |||
//---
| |||
if(!s_is_active)
| |||
return;
| |||
| |||
"""
| |||
| |||
TPL_POOL_DEINIT_ITEM = """//--- {prefix}
| |||
if(s_{m}_size)
| |||
{{
| |||
for(int i = 0; i < s_{m}_size; i++)
| |||
if(CheckPointer(s_{m}[i]) == POINTER_DYNAMIC)
| |||
delete s_{m}[i];
| |||
s_{m}_size = 0;
| |||
}}
| |||
| |||
"""
| |||
| |||
TPL_POOL_DEINIT_END = """//---
| |||
s_is_active = false;
| |||
}}
| |||
| |||
"""
| |||
| |||
TPL_POOL_STATIC_HEAD = """//+------------------------------------------------------------------+
| |||
//| Inicializacion de variables estaticas |
| |||
//+------------------------------------------------------------------+
| |||
TSN::CHashMapFastNor<TSN::CBitBuffer, int, TSN::CGenericHash_CBitBuffer> {pool}::s_hash_str_to_int;
| |||
bool {pool}::s_is_active = false;
| |||
CBitBuffer {pool}::s_bit(32);
| |||
| |||
"""
| |||
| |||
TPL_POOL_STATIC_ITEM = """//--- {prefix}
| |||
{class_name}* {pool}::s_{m}[] = {{}};
| |||
IndicatorCacheExtraData {pool}::s_{m}_data[];
| |||
int {pool}::s_{m}_size = 0;
| |||
int {pool}::s_{m}_r = 0;
| |||
| |||
"""
| |||
| |||
|