forked from nique_372/MQLArticles
250 lines
8.9 KiB
MQL5
250 lines
8.9 KiB
MQL5
//+------------------------------------------------------------------+
| |
//| TicketsTable.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_RM_TICKETS_TABLE_MQH
| |
#define MQLARTICLES_RM_TICKETS_TABLE_MQH
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
#include "RM_Functions.mqh"
| |
#include <TSN\\GCol\\HashMap.mqh>
| |
#include <TSN\\GCol\\GenericHashes.mqh>
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
namespace TSN
| |
{
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
Position EMPTY_POSITION;
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
class CTicketsTable : public CHashMapFastRP(ulong, Position, _fibbo)
| |
{
| |
public:
| |
CTicketsTable(void) : CHashMapFastRef<ulong, Position, TSN::CGenericHash_ulong_fibbo>(512) {}
| |
~CTicketsTable(void) {}
| |
| |
//---
| |
bool GetByTicket(const ulong ticket, Position & pos, int& out_idx) const;
| |
bool GetByTicket(const ulong ticket, Position & pos) const;
| |
inline Position GetByTicket(const ulong ticket) const;
| |
__forceinline Position GetPositionByIndex(const int index) const { return m_table[index].value; }
| |
__forceinline ulong GetPositionMagicByIndex(const int index) const { return m_table[index].value.magic; }
| |
| |
//---
| |
void DeleteByIndex(const int pos_idx);
| |
bool DeleteByTicket(const ulong ticket);
| |
bool DeleteByTicket(const ulong ticket, Position & position);
| |
| |
| |
//---
| |
inline void UpdatePositionPartial(const double net, const double close_volume, const int pos_idx);
| |
inline void UpdateTp(const double new_tp, const ulong ticket);
| |
inline void UpdateSl(const double new_sl, const ulong ticket);
| |
| |
//---
| |
void CloseAllPositionMask(const int& index_arr[], const int size_arr, CTrade & trade, const uint flags) const;
| |
int GetPositionsMask(const int& index_arr[], const int size_arr, const uint flags) const;
| |
double GetProfitMask(const int &index_arr[], const int size_arr) const;
| |
void GetPositionsFromMagic(const ulong magig, int& arr[], int& size_arr) const;
| |
};
| |
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
bool CTicketsTable::GetByTicket(const ulong ticket, Position &pos, int &out_idx) const
| |
{
| |
out_idx = Find(ticket);
| |
if(out_idx >= 0)
| |
{
| |
pos = m_table[out_idx].value;
| |
return true;
| |
}
| |
return false;
| |
}
| |
| |
//+------------------------------------------------------------------+
| |
bool CTicketsTable::GetByTicket(const ulong ticket, Position &pos) const
| |
{
| |
const int pos_idx = Find(ticket);
| |
if(pos_idx >= 0)
| |
{
| |
pos = m_table[pos_idx].value;
| |
return true;
| |
}
| |
return false;
| |
}
| |
| |
| |
//+------------------------------------------------------------------+
| |
inline Position CTicketsTable::GetByTicket(const ulong ticket) const
| |
{
| |
const int pos_idx = Find(ticket);
| |
return pos_idx >= 0 ? m_table[pos_idx].value : EMPTY_POSITION;
| |
}
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
void CTicketsTable::DeleteByIndex(const int pos_idx)
| |
{
| |
//---
| |
const uint i = uint(pos_idx >> 3);
| |
// & 7 = % 8
| |
// + 7 por el -7 que se hace uego
| |
const int bit_pos = ((pos_idx & 7) << 3) + 7;
| |
Remove(i, bit_pos);
| |
}
| |
// a = i * 8 + k / 8
| |
/// 8 + 2 = 10 = a
| |
// 1
| |
| |
//+------------------------------------------------------------------+
| |
bool CTicketsTable::DeleteByTicket(const ulong ticket, Position &position)
| |
{
| |
//---
| |
uint i;
| |
int bit_pos;
| |
if(!Find(ticket, i, bit_pos))
| |
{
| |
FastLog(FUNCION_ACTUAL, ERROR_TEXT, StringFormat("No se pudo eliminar el ticket %I64u, ya que no existe", ticket));
| |
return false;
| |
}
| |
| |
//---
| |
position = m_table[TSN_FASTHASHMAP_INDEX(i, bit_pos)].value;
| |
Remove(i, bit_pos);
| |
| |
//---
| |
return true;
| |
}
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
inline void CTicketsTable::UpdateSl(const double new_sl, const ulong ticket)
| |
{
| |
const int pos_idx = Find(ticket);
| |
if(pos_idx >= 0)
| |
m_table[pos_idx].value.sl = new_sl;
| |
}
| |
| |
//+------------------------------------------------------------------+
| |
inline void CTicketsTable::UpdateTp(const double new_tp, const ulong ticket)
| |
{
| |
const int pos_idx = Find(ticket);
| |
if(pos_idx >= 0)
| |
m_table[pos_idx].value.tp = new_tp;
| |
}
| |
| |
//+------------------------------------------------------------------+
| |
inline void CTicketsTable::UpdatePositionPartial(const double net, const double close_volume, const int pos_idx)
| |
{
| |
m_table[pos_idx].value.volume -= close_volume;
| |
m_table[pos_idx].value.profit += net;
| |
#ifdef MQLARTICLES_TICKETSINFO_DEBUG
| |
FastLog(FUNCION_ACTUAL, INFO_TEXT, StringFormat("Volumen cerrado = %.4f (new= %.4f) y profit obtenido = %.2f, al cierra parcial de la posicion = %I64u",
| |
close_volume,
| |
m_table[pos_idx].value.volume,
| |
net,
| |
m_table[pos_idx].value.ticket));
| |
#endif // MQLARTICLES_TICKETSINFO_DEBUG
| |
}
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
void CTicketsTable::CloseAllPositionMask(const int &index_arr[], const int size_arr, CTrade& trade, const uint flags) const
| |
{
| |
for(int i = 0; i < size_arr; i++)
| |
{
| |
const int ri = index_arr[i];
| |
if(!PositionSelectByTicket(m_table[ri].value.ticket))
| |
continue;
| |
| |
//---
| |
const double profit = PositionGetDouble(POSITION_PROFIT);
| |
const ENUM_POSITION_TYPE type = m_table[ri].value.type;
| |
| |
//---
| |
if((type == POSITION_TYPE_BUY && (flags & FLAG_POSITION_TYPE_BUY) == 0)
| |
|| (type == POSITION_TYPE_SELL && (flags & FLAG_POSITION_TYPE_SELL) == 0))
| |
continue;
| |
| |
if((flags & FLAG_CLOSE_ALL_PROFIT) != 0 && profit > 0)
| |
{
| |
trade.PositionClose(m_table[ri].value.ticket);
| |
}
| |
else
| |
if((flags & FLAG_CLOSE_ALL_LOSS) != 0 && profit < 0)
| |
{
| |
trade.PositionClose(m_table[ri].value.ticket);
| |
}
| |
}
| |
}
| |
| |
| |
| |
//+------------------------------------------------------------------+
| |
int CTicketsTable::GetPositionsMask(const int &index_arr[], const int size_arr, const uint flags) const
| |
{
| |
int count = 0;
| |
for(int i = 0; i < size_arr; i++)
| |
{
| |
const ENUM_POSITION_TYPE type = m_table[index_arr[i]].value.type;
| |
if(type == POSITION_TYPE_BUY && (flags & FLAG_POSITION_TYPE_BUY) != 0)
| |
count++;
| |
if(type == POSITION_TYPE_SELL && (flags & FLAG_POSITION_TYPE_SELL) != 0)
| |
count++;
| |
}
| |
return count;
| |
}
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
double CTicketsTable::GetProfitMask(const int &index_arr[], const int size_arr) const
| |
{
| |
double v = 0.0;
| |
for(int i = 0; i < size_arr; i++)
| |
{
| |
if(!PositionSelectByTicket(m_table[index_arr[i]].value.ticket))
| |
continue;
| |
v += PositionGetDouble(POSITION_PROFIT) + PositionGetDouble(POSITION_SWAP);
| |
}
| |
return v;
| |
}
| |
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
// arr tendra que haber sido resizeado previa..
| |
void CTicketsTable::GetPositionsFromMagic(const ulong magic, int& arr[], int &size_arr) const
| |
{
| |
for(int i = 0; i < m_count; i++)
| |
{
| |
if(m_table[i].value.magic != magic && magic != NOT_MAGIC_NUMBER)
| |
continue;
| |
arr[size_arr++] = i;
| |
}
| |
}
| |
| |
//---
| |
}
| |
| |
#endif // MQLARTICLES_RM_TICKETS_TABLE_MQH
| |
//+------------------------------------------------------------------+
|