//+------------------------------------------------------------------+ //| Main.mqh | //| Copyright 2025, Niquel Mendoza. | //| https://www.mql5.com/es/users/nique_372/news | //+------------------------------------------------------------------+ #property copyright "Copyright 2025, Niquel Mendoza." #property link "https://www.mql5.com/es/users/nique_372/news" #property strict //+------------------------------------------------------------------+ //| Include | //+------------------------------------------------------------------+ #define CACCOUNT_STATUS_ACTIVE_ON_POSITION_MODIFIED #define TSNMQLART_POSMGMT_BE_ELOGS //--- Librerias externas // breakeven #include // mem manage #include // std symbol #include // GUI #define private public #define protected public #include #undef private #undef protected //+------------------------------------------------------------------+ //| Resource | //+------------------------------------------------------------------+ #resource "\\Images\\EasyAndFastGUI\\Icons\\bmp16\\arrow_up.bmp" #resource "\\Images\\EasyAndFastGUI\\Icons\\bmp16\\arrow_down.bmp" #resource "\\Images\\EasyAndFastGUI\\Controls\\checkbox_off.bmp" #resource "\\Images\\EasyAndFastGUI\\Controls\\checkbox_on.bmp" //--- const string image_array[2] = { "Images\\EasyAndFastGUI\\Icons\\bmp16\\arrow_up.bmp", "Images\\EasyAndFastGUI\\Icons\\bmp16\\arrow_down.bmp" }; //+------------------------------------------------------------------+ //| Defines | //+------------------------------------------------------------------+ #define YSIZE_CAPTION 40 #define X_OFFSET_PANEL 5 #define Y_OFFSET_PANEL 5 #define TABLE_P_TICKET 0 #define TABLE_P_TYPE 1 #define TABLE_P_VOLUME 2 #define TABLE_P_ENTRY 3 #define TABLE_P_TAKEPROFIT 4 #define TABLE_P_STOPLOSS 5 #define TABLE_P_TRAILINGSTOP_CHECKBOX 6 #define PANEL_TABLE_IMAGE_IDX_UP 0 #define PANEL_TABLE_IMAGE_IDX_DOWN 1 #define BREAKEVEN_PANEL_MAX_POS (256) //+------------------------------------------------------------------+ //| Global variables | //+------------------------------------------------------------------+ TSN::CBreakEvenCreator g_breakeven_creator; //+------------------------------------------------------------------+ //| CBreakEvenPanel class | //+------------------------------------------------------------------+ class CBreakEvenPanel : public CWndCreate { public: struct pack(4) TicketRelations { int idx_in_row_positions; int idx_in_data; }; private: //--- int m_width_panel; TSN::ENUM_BREAKEVEN_TYPE m_mode_breakeven; bool m_enable_to_trailing; //--- TSN::CBreakEvenBase* m_breakevens[BREAKEVEN_PANEL_MAX_POS]; CSymbolInfo* m_symbol_array[BREAKEVEN_PANEL_MAX_POS]; int m_data_s; // numero de bes // el tamño de bes=numero de symbolos usados //--- File->Posicion (ticket) ulong m_row_por_posicion[BREAKEVEN_PANEL_MAX_POS]; int m_row_por_posicion_s; //--- CHashMapFast(string, int) m_hasmap_symbol_por_pos; // delveullve la poscoin en el array en base al simbolo CHashMapFastRP(ulong, TicketRelations, _fibbo) m_hashmap_idxs; // em nase al ticket te da el idx en csymbol y be //--- CWindow m_window; CTabs m_tabs; //--- CTable m_table_principal; //--- CTextLabel m_label_text; CComboBox m_combox_be_type; CTextBox m_text_box1; //--- string m_err; //--- void OnChangeComboxTSL(); void RedrawCompletePanel(); //--- class CAccountGestorInt : public TSN::CAccountGestor { private: CBreakEvenPanel* m_padre; public: //--- Constructor CAccountGestorInt(CBreakEvenPanel* p) : m_padre(p) { TSN::account_status.RegisterEvents(&this, ACCOUNT_STATUS_REG_FLAG_ON_CLOSE_POSITION | ACCOUNT_STATUS_REG_FLAG_ON_OPEN_POSITION | ACCOUNT_STATUS_REG_FLAG_ON_POSITION_MODIFY | ACCOUNT_STATUS_REG_FLAG_ON_INIT_NEW_POS); } //--- Destructor ~CAccountGestorInt(void) { if(TSN::account_status.IsActive()) TSN::account_status.UnregisterEvents(&this, ACCOUNT_STATUS_REG_FLAG_ON_CLOSE_POSITION | ACCOUNT_STATUS_REG_FLAG_ON_OPEN_POSITION | ACCOUNT_STATUS_REG_FLAG_ON_POSITION_MODIFY | ACCOUNT_STATUS_REG_FLAG_ON_INIT_NEW_POS); } //--- void OnOpenPosition(const TSN::ROnOpenPosition &pos) override final { m_padre.OnOpenPosition(pos); } void OnClosePosition(const TSN::ROnClosePosition &pos, const int global_pos_index) override final { m_padre.OnClosePosition(pos, global_pos_index); } void OnPositionModify(const TSN::Position& pos, const uint8_t change_flags) override final { m_padre.OnPositionModify(pos, change_flags); } void OnInitNewPos(const TSN::ROnOpenPosition &position) override final { m_padre.OnInitNewPos(position); } }; CAccountGestorInt* m_gestor; public: CBreakEvenPanel(); ~CBreakEvenPanel() { if(m_gestor != NULL) delete m_gestor; } //--- void OnEvent(const int id, const long &lparam, const double &dparam, const string &sparam) override; void OnDeinitEvent(const int reason); void OnTickEvent(); // void OnOpenPosition(const TSN::ROnOpenPosition &pos); void OnClosePosition(const TSN::ROnClosePosition &pos, const int global_pos_index); void OnPositionModify(const TSN::Position& pos, const uint8_t change_flags); void OnInitNewPos(const TSN::ROnOpenPosition &position); //--- CAccountGestorInt* GetGestorInt() { return m_gestor; } //--- void CreateGUI(int WIDTH_PANEL, int HEIGHT_PANEL, TSN::ENUM_BREAKEVEN_TYPE be_initial_type); }; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ CBreakEvenPanel::CBreakEvenPanel(void) : m_row_por_posicion_s(0), m_data_s(0), m_hasmap_symbol_por_pos(BREAKEVEN_PANEL_MAX_POS), m_hashmap_idxs(BREAKEVEN_PANEL_MAX_POS), m_enable_to_trailing(false) { m_gestor = new CAccountGestorInt(&this); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void CBreakEvenPanel::OnEvent(const int id, const long &lparam, const double &dparam, const string &sparam) { if(id == CHARTEVENT_CUSTOM + ON_CLICK_CHECKBOX) { if(m_enable_to_trailing && lparam == m_table_principal.Id()) { //--- const bool state = (bool)dparam; //--- int i = sparam.Find("_"), row; if(i == -1) { FastLog(FUNCION_ACTUAL, WARNING_TEXT, StringFormat("Se ignoro '%s'", sparam)); return; // nada } i++; // suma TSN::CStringConvert::ToValueNSigned(sparam, i, row, StringLen(sparam)); //uint col = StringToInteger(res[1]); //Print(sparam); //--- TicketRelations rel; const ulong pos = m_row_por_posicion[row]; m_hashmap_idxs.TryGet(pos, rel); //--- if(state ? (!m_breakevens[rel.idx_in_data].Add(pos)) : (!m_breakevens[rel.idx_in_data].Remove(pos))) { // Fallaron StringSetLength(m_err, 0); // reset TSN::CErrorTrace::FormatTrace(m_err); FastLog(FUNCION_ACTUAL, ERROR_TEXT, m_err); } } return; } if(id == CHARTEVENT_CUSTOM + ON_CLICK_COMBOBOX_ITEM) { if(lparam == m_combox_be_type.Id()) OnChangeComboxTSL(); } } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void CBreakEvenPanel::OnChangeComboxTSL(void) { //--- const string slected = m_combox_be_type.GetValue(); const int idx = m_combox_be_type.GetListViewPointer().SelectedItemIndex(); m_mode_breakeven = (TSN::ENUM_BREAKEVEN_TYPE)idx; //--- int idx_remove[]; ArrayResize(idx_remove, m_data_s); int w = 0; //--- TSN::BreakEvenCreatorRes res(NULL, false); for(int k = 0; k < m_data_s; k++) { //--- delete m_breakevens[k]; // limpiamos //--- Creamos uno nuevo const string symbol = m_symbol_array[k].Name(); res = g_breakeven_creator.Create(m_mode_breakeven, symbol, NOT_MAGIC_NUMBER); if(!res.ok) { m_err = "Error in modify breakeven mode with idx = " + string(k) + TSNSTD_ERRTRACE_SEPARATOR; TSN::CErrorTrace::FormatTrace(m_err); FastLog(FUNCION_ACTUAL, ERROR_TEXT, m_err); //--- idx_remove[w++] = k; continue; } //--- m_breakevens[k] = res.ptr; } //--- for(int k = 0; k < (int)m_table_principal.RowsTotal(); k++) m_table_principal.ChangeImage(TABLE_P_TRAILINGSTOP_CHECKBOX, k, 0); //--- m_table_principal.Update(true); FastLog(FUNCION_ACTUAL, INFO_TEXT, StringFormat("Nuevo modo de breakeven = '%s'", TSN::g_breakevens_types_str[m_mode_breakeven])); //--- ArrayResize(idx_remove, w); RemoveMultipleIndexesWithVSize(m_breakevens, m_data_s, idx_remove); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void CBreakEvenPanel::OnDeinitEvent(const int reason) { //--- for(int i = 0; i < m_data_s; i++) { delete m_symbol_array[i]; delete m_breakevens[i]; //cuidado no se elimna rel putnero interno eso lo hace account status } //--- CWndEvents::Destroy(); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void CBreakEvenPanel::OnTickEvent(void) { for(int i = 0; i < m_data_s; i++) m_breakevens[i].BreakEven(); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void CBreakEvenPanel::CreateGUI(int WIDTH_PANEL, int HEIGHT_PANEL, TSN::ENUM_BREAKEVEN_TYPE be_initial_type) { //--- this.m_mode_breakeven = be_initial_type; //--- m_width_panel = fmax(WIDTH_PANEL, 200); //--- m_window.CaptionColor(clrGray); m_window.CaptionColorHover(clrGray); m_window.CaptionColorLocked(clrBlack); m_window.BorderColor(C'212,212,212'); m_window.BackColorPressed(clrBlue); m_window.Font("Arial Black"); CWndCreate::CreateWindow(m_window, "BreakevenManager", 0, 0, m_width_panel, HEIGHT_PANEL, true, false, true, true); //--- Tabs static string texts[2] = { "General", "Configuracion"}; int widts[2] = {100, 90}; color clrs[2] = {clrNONE, clrNONE}; // defa //--- int x = m_window.X() + X_OFFSET_PANEL; int y = m_window.Y() + (YSIZE_CAPTION + Y_OFFSET_PANEL); //--- int xsize_tabs = (m_window.XSize()) - (X_OFFSET_PANEL * 2); int ysize_tabs = (m_window.YSize() - YSIZE_CAPTION) - (Y_OFFSET_PANEL * 2); m_tabs.BackColor(C'191,191,191'); m_tabs.BackColorPressed(C'201,203,205'); CWndCreate::CreateTabs(m_tabs, m_window, 0, x, y, xsize_tabs, ysize_tabs, texts, widts, clrs, clrs); //--- #define X_OFFSET_INTERNO 7 x += X_OFFSET_INTERNO; y += 20; int x_table = x; int y_table = y; Print(m_tabs.YGap()); Print(m_tabs.XGap()); //--- Pestaña 1 #define COLS_TOTAL_TABLE 7 static string headers[COLS_TOTAL_TABLE] = {"Ticket", "Type", "Volume", "Entry", "Takeprofit", "Stoploss", "Breakeven"}; //--- int xsize_table = xsize_tabs - (X_OFFSET_INTERNO * 2); int ysize_table = ysize_tabs - (Y_OFFSET_PANEL * 2) - (20); //tamaño util | tmaaño dellos offeset | tamaño del header tabs #define HEADER_YSIZE_TABLE 30 m_table_principal.HeaderYSize(HEADER_YSIZE_TABLE); CWndCreate::CreateTable(m_table_principal, m_window, 0, m_tabs, 0, COLS_TOTAL_TABLE, 1, headers, x_table, y_table, xsize_table, ysize_table, false, false); ENUM_ALIGN_MODE align[COLS_TOTAL_TABLE]; ::ArrayInitialize(align, ALIGN_LEFT); //align[TABLE_P_TYPE] = ALIGN_RIGHT; //align[TABLE_P_TRAILINGSTOP_CHECKBOX] = ALIGN_LEFT; //Print(xsize_table); //--- int widths[COLS_TOTAL_TABLE]; const double col_proportions[COLS_TOTAL_TABLE] = {0.162, 0.102, 0.122, 0.142, 0.142, 0.142, 0.182}; int sum = 0; for(int i = 0; i < COLS_TOTAL_TABLE; i++) { widths[i] = (int)(xsize_table * col_proportions[i]); sum += widths[i]; } int rest = xsize_table - sum; widths[COLS_TOTAL_TABLE - 1] += rest < 0 ? 0 : rest; //ArrayPrint(widths); //Print(m_table_principal.XSize()); //--- Setemoas offeset imagen //ofeset de los iconos imagenes int imge_x_offset[COLS_TOTAL_TABLE] = {0, 8, 0, 0, 0, 0, 8}; int imge_y_offset[COLS_TOTAL_TABLE] = {0, 7, 0, 0, 0, 0, 7}; m_table_principal.ImageXOffset(imge_x_offset); m_table_principal.ImageYOffset(imge_y_offset); //--- Propiedades generales int ysize_utilizable = ysize_table - HEADER_YSIZE_TABLE; m_table_principal.TextAlign(align); m_table_principal.ColumnsWidth(widths); m_table_principal.HeadersColor(C'0x06,0x9C,0xCE'); m_table_principal.CellColor(clrWhiteSmoke); m_table_principal.CellYSize(30); //--- Array of text offset along the X axis in the columns int text_x_offset[COLS_TOTAL_TABLE]; ::ArrayInitialize(text_x_offset, 5); text_x_offset[TABLE_P_TRAILINGSTOP_CHECKBOX] += 22; text_x_offset[TABLE_P_TYPE] += 20; //--- m_table_principal.TextXOffset(text_x_offset); m_table_principal.SetImages(TABLE_P_TYPE, 0, image_array); //--- m_table_principal.DataType(TABLE_P_TICKET, TYPE_ULONG); m_table_principal.DataType(TABLE_P_TYPE, TYPE_STRING); m_table_principal.DataType(TABLE_P_VOLUME, TYPE_DOUBLE); m_table_principal.DataType(TABLE_P_ENTRY, TYPE_DOUBLE); m_table_principal.DataType(TABLE_P_TAKEPROFIT, TYPE_DOUBLE); m_table_principal.DataType(TABLE_P_STOPLOSS, TYPE_DOUBLE); //--- int x_label = x; int y_label = y; CWndCreate::CreateTextLabel(m_label_text, "Elija el tipo de breakeven", m_window, 0, m_tabs, 1, x_label, y_label, 210, 40); m_label_text.BackColor(C'201,203,205'); m_label_text.FontSize(12); y += 30; //--- static string trailign_stop_type[3] = { "Breakeven by rr", "Breakeven by fixed points", "Breakeven by atr"}; //--- int pos = (int)this.m_mode_breakeven; //--- CWndCreate::CreateCombobox(m_combox_be_type, "Tipo de breakeven: ", m_window, 0, m_tabs, 1, false, x, y, 262, 150, trailign_stop_type, 65, pos); m_combox_be_type.BackColor(C'201,203,205'); m_combox_be_type.FontSize(10); //--- Final CWndEvents::CompletedGUI(); } /* #define TABLE_P_TICKET 0 #define TABLE_P_TYPE 1 #define TABLE_P_VOLUME 2 #define TABLE_P_ENTRY 3 #define TABLE_P_TAKEPROFIT 4 #define TABLE_P_STOPLOSS 5 #define TABLE_P_TRAILINGSTOP_CHECKBOX 6 */ //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void CBreakEvenPanel::OnOpenPosition(const TSN::ROnOpenPosition &pos) { //--- m_enable_to_trailing = true; //--- const ulong ticket = pos.position.ticket; const string symbol_name = TSN::account_status.last_trans_symbol; CSymbolInfo* ptr_symbol; //--- Obtemos info del simbolo int pos_symbol; if(!m_hasmap_symbol_por_pos.ReserveSlot(symbol_name)) //obtenemos datos del simbolo { const uint p = m_hasmap_symbol_por_pos.m_last_pos_tbl; pos_symbol = m_hasmap_symbol_por_pos.m_table[p].value; ptr_symbol = m_symbol_array[pos_symbol]; } else { ptr_symbol = new CSymbolInfo(); ptr_symbol.Name(symbol_name); //--- Creacion del breakeven TSN::BreakEvenCreatorRes res = g_breakeven_creator.Create(m_mode_breakeven, symbol_name, NOT_MAGIC_NUMBER); if(!res.ok) { StringSetLength(m_err, 0); TSN::CErrorTrace::FormatTrace(m_err); FastLog(FUNCION_ACTUAL, ERROR_TEXT, m_err); return; } //--- TSN::CBreakEvenBase* new_be = res.ptr; new_be.Automatic(false); // No sera automatico //--- m_symbol_array[m_data_s] = ptr_symbol; m_breakevens[m_data_s] = new_be; //--- const uint p = m_hasmap_symbol_por_pos.m_last_pos_tbl; m_hasmap_symbol_por_pos.m_table[p].key = symbol_name; m_hasmap_symbol_por_pos.m_table[p].value = m_data_s; //--- pos_symbol = m_data_s++; } //--- Aumentamos el taamaño de tickets por row m_row_por_posicion[m_row_por_posicion_s++] = ticket; //--- obteneos el row, y si no coincide entonces resize int row_index = int(m_table_principal.RowsTotal() - 1); if(row_index != m_row_por_posicion_s - 1) { row_index++; m_table_principal.AddRow(row_index); m_table_principal.SetImages(TABLE_P_TYPE, row_index, image_array); } //--- TicketRelations relations; relations.idx_in_data = pos_symbol; relations.idx_in_row_positions = row_index; //--- m_hashmap_idxs.Add(ticket, relations); //--- //PrintFormat("Row = %d | Ticket = %I64u | Digitos %d", row_index, pos.position.ticket, ptr_symbol.Digits()); //--- ticket m_table_principal.SetValue(TABLE_P_TICKET, row_index, IntegerToString(ticket), 0); //--- Tipo static const string type_entrys[2] = { "Buy", "Sell"}; const uint image_index[2] = { PANEL_TABLE_IMAGE_IDX_UP, PANEL_TABLE_IMAGE_IDX_DOWN}; const color clr_index[2] = { clrGreen, clrRed}; const uint8_t t = (uint8_t)pos.position.type; //--- m_table_principal.SetValue(TABLE_P_TYPE, row_index, type_entrys[t]); m_table_principal.TextColor(TABLE_P_TYPE, row_index, clr_index[t]); m_table_principal.ChangeImage(TABLE_P_TYPE, row_index, t); //--- m_table_principal.SetValue(TABLE_P_VOLUME, row_index, DoubleToString(pos.position.volume), 3); m_table_principal.SetValue(TABLE_P_ENTRY, row_index, DoubleToString(pos.position.open_price), ptr_symbol.Digits()); m_table_principal.SetValue(TABLE_P_TAKEPROFIT, row_index, DoubleToString(pos.position.tp), ptr_symbol.Digits()); m_table_principal.SetValue(TABLE_P_STOPLOSS, row_index, DoubleToString(pos.position.sl), ptr_symbol.Digits()); //--- static const string image_array_chekbox[2] = { "Images\\EasyAndFastGUI\\Controls\\checkbox_off.bmp", "Images\\EasyAndFastGUI\\Controls\\checkbox_on.bmp" }; //--- m_table_principal.SetImages(TABLE_P_TRAILINGSTOP_CHECKBOX, row_index, image_array_chekbox); m_table_principal.ChangeImage(TABLE_P_TRAILINGSTOP_CHECKBOX, row_index, 0); //0 por defecto m_table_principal.SetValue(TABLE_P_TRAILINGSTOP_CHECKBOX, row_index, "Breakeven", 0); m_table_principal.CellType(TABLE_P_TRAILINGSTOP_CHECKBOX, row_index, CELL_CHECKBOX); m_table_principal.Update(true); } //+------------------------------------------------------------------+ void CBreakEvenPanel::OnClosePosition(const TSN::ROnClosePosition &pos, const int global_pos_index) { TicketRelations relations; if(m_hashmap_idxs.TryGet(pos.position.ticket, relations)) //obtnemos el row de la posicion en base al ticket { //--- eliminamos la fila de la tabla if(m_table_principal.RowsTotal() > 1) //mas de 1 fila { m_table_principal.DeleteRow(relations.idx_in_row_positions, true); //eliminamos la fila } else //1 fila { //--- ticket for(int i = 0; i < COLS_TOTAL_TABLE; i++) { m_table_principal.SetValue(i, 0); //reset a null } m_table_principal.Update(true); m_enable_to_trailing = false; } //--- m_hashmap_idxs.RemoveLastFindCall(); //--- eliiminamos el indice del array de posicoines [row -> (posicion = ticket)] int index_to_remove[1]; index_to_remove[0] = relations.idx_in_row_positions; //--- RemoveMultipleIndexesWithVSize(m_row_por_posicion, m_row_por_posicion_s, index_to_remove); } } //+------------------------------------------------------------------+ // Funcion que se ejecuta en OnInit, solo si hay posiciones abiertas void CBreakEvenPanel::OnInitNewPos(const TSN::ROnOpenPosition &position) { OnOpenPosition(position); // Reutilizamos } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void CBreakEvenPanel::OnPositionModify(const TSN::Position &pos, const uchar change_flags) { TicketRelations relations; if(m_hashmap_idxs.TryGet(pos.ticket, relations)) // En base al ticket obteneis los indices { //--- Obtemos info del simbolo CSymbolInfo* ptr_symbol = m_symbol_array[relations.idx_in_data]; //--- if((change_flags & ACCOUNTSTATUS_ON_POS_MODIFY_FLAG_CHANGE_SL) != 0) m_table_principal.SetValue(TABLE_P_STOPLOSS, relations.idx_in_row_positions, DoubleToString(pos.sl), ptr_symbol.Digits()); if((change_flags & ACCOUNTSTATUS_ON_POS_MODIFY_FLAG_CHANGE_TP) != 0) m_table_principal.SetValue(TABLE_P_TAKEPROFIT, relations.idx_in_row_positions, DoubleToString(pos.tp), ptr_symbol.Digits()); //--- m_table_principal.Update(true); } } //+------------------------------------------------------------------+