Article-15026-MQL5-Trade-Hi.../PositionsControl.mqh

495 lines
44 KiB
MQL5

2026-03-23 12:53:11 +07:00
<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| PositionsControl.mqh |
//| Copyright 2024, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
#include "Position.mqh"
//+------------------------------------------------------------------+
//| ;0AA 8AB>@8G5A:8E ?>78F89 |
//+------------------------------------------------------------------+
class CPositionsControl : public CObject
{
private:
string m_symbol; // =AB@C<5=B, ?> :>B>@><C >B:@KB0 ?>78F8O
long m_current_id; // 45=B8D8:0B>@ B5:CI59 >B>1@06Q==>9 =0 3@0D8:5 ?>78F88
bool m_key_ctrl; // $;03 @07@5H5=85 =0 C?@02;5=85 3@0D8:>< A ?><>ILN :;0280BC@K
//--- >72@0I05B B8? ?>78F88 ?> B8?C A45;:8
ENUM_POSITION_TYPE PositionTypeByDeal(const CDeal *deal);
protected:
CPosition m_temp_pos; // @5<5==K9 >1J5:B ?>78F88 4;O ?>8A:0
CArrayObj m_list_pos; // !?8A>: ?>78F89
long m_chart_id; // 45=B8D8:0B>@ 3@0D8:0
//--- >72@0I05B >1J5:B-?>78F8N 87 A?8A:0 ?> 845=B8D8:0B>@C
CPosition *GetPositionObjByID(const long id);
//--- >72@0I05B D;03 B>3>, GB> ?>78F8O @K=>G=0O
bool IsMarketPosition(const long id);
//--- >72@0I05B C:070B5;L =0 (1) ?5@2CN, (2) ?>A;54=NN 70:@KBCN ?>78F8N 2 A?8A:5
CPosition *GetFirstClosedPosition(void);
CPosition *GetLastClosedPosition(void);
//--- >72@0I05B C:070B5;L =0 (1) ?@54K4CICN, (2) A;54CNICN 70:@KBCN ?>78F8N 2 A?8A:5
CPosition *GetPrevClosedPosition(CPosition *current);
CPosition *GetNextClosedPosition(CPosition *current);
//--- B>1@0605B =0 3@0D8:5 3@0D8G5A:>5 ?@54AB02;5=85 C:070==>9 ?>78F88
void Show(CPosition *pos, const bool chart_redraw=false);
//--- &5=B@8@C5B 3@0D8: =0 B5:CI59 2K1@0==>9 ?>78F88
void CentersChartByCurrentSelected(void);
//--- >72@0I05B 845=B8D8:0B>@ B5:CI59 2K1@0==>9 ?>78F88
long CurrentSelectedID(void) const { return this.m_current_id; }
//--- >72@0I05B 2@5<O (1) >B:@KB8O, (2) 70:@KB8O B5:CI59 2K1@0==>9 ?>78F88
datetime TimeOpenCurrentSelected(void);
datetime TimeCloseCurrentSelected(void);
//--- !:@K205B =0 3@0D8:5 3@0D8G5A:>5 ?@54AB02;5=85 2A5E ?>78F89, :@><5 C:070==>9
void HideAllExceptOne(const long pos_id, const bool chart_redraw=false);
public:
//--- >72@0I05B (1) A8<2>;, (2) 845=B8D8:0B>@ 3@0D8:0
string Symbol(void) const { return this.m_symbol; }
long ChartID(void) const { return this.m_chart_id; }
//--- !>740QB 8 >1=>2;O5B A?8A>: ?>78F89. >65B 1KBL ?5@5>?@545;Q= 2 =0A;54C5<KE :;0AA0E
virtual bool Refresh(void);
//--- >72@0I05B :>;8G5AB2> ?>78F89 2 A?8A:5
int Total(void) const { return this.m_list_pos.Total(); }
//--- (1) !:@K205B, (2) >B>1@0605B =0 3@0D8:5 3@0D8G5A:>5 ?@54AB02;5=85 ?5@2>9 ?>78F88
void HideFirst(const bool chart_redraw=false);
void ShowFirst(const bool chart_redraw=false);
//--- (1) !:@K205B, (2) >B>1@0605B =0 3@0D8:5 3@0D8G5A:>5 ?@54AB02;5=85 ?>A;54=59 ?>78F88
void HideLast(const bool chart_redraw=false);
void ShowLast(const bool chart_redraw=false);
//--- B>1@0605B =0 3@0D8:5 3@0D8G5A:>5 ?@54AB02;5=85 (1) B5:CI59, (2) ?@54K4CI59, (3) A;54CNI59 ?>78F88
void ShowCurrent(const bool chart_redraw=false);
void ShowPrev(const bool chart_redraw=false);
void ShowNext(const bool chart_redraw=false);
//--- >72@0I05B >?8A0=85 B5:CI59 2K1@0==>9 ?>78F88
string CurrentSelectedDescription(void);
//--- 0A?5G0BK205B 2 6C@=0;5 A2>9AB20 2A5E ?>78F89 2 A?8A:5 8 8E A45;>:
void Print(void);
//--- >=AB@C:B>@/45AB@C:B>@
CPositionsControl(const string symbol=NULL);
~CPositionsControl();
};
//+------------------------------------------------------------------+
//| >=AB@C:B>@ |
//+------------------------------------------------------------------+
CPositionsControl::CPositionsControl(const string symbol=NULL)
{
this.m_list_pos.Sort(SORT_MODE_POSITION_TIME_CLOSE_MSC);
this.m_symbol = (symbol==NULL ? ::Symbol() : symbol);
this.m_chart_id = ::ChartID();
this.m_current_id = 0;
this.m_key_ctrl = ::ChartGetInteger(this.m_chart_id, CHART_KEYBOARD_CONTROL);
}
//+------------------------------------------------------------------+
//| 5AB@C:B>@ |
//+------------------------------------------------------------------+
CPositionsControl::~CPositionsControl()
{
this.m_list_pos.Shutdown();
::ChartSetInteger(this.m_chart_id, CHART_KEYBOARD_CONTROL, this.m_key_ctrl);
}
//+------------------------------------------------------------------+
//| >72@0I05B >1J5:B-?>78F8N 87 A?8A:0 ?> 845=B8D8:0B>@C |
//+------------------------------------------------------------------+
CPosition *CPositionsControl::GetPositionObjByID(const long id)
{
//--- #AB0=02;8205< 2@5<5==><C >1J5:BC 845=B8D8:0B>@ ?>78F88, 0 A?8A:C - D;03 A>@B8@>2:8 ?> 845=B8D8:0B>@C ?>78F88
this.m_temp_pos.SetID(id);
this.m_list_pos.Sort(SORT_MODE_POSITION_IDENTIFIER);
//--- >;CG05< 87 A?8A:0 8=45:A >1J5:B0-?>78F88 A B0:8< 845=B8D8:0B>@>< (;81> -1 ?@8 53> >BACBAB288)
//--- > ?>;CG5==><C 8=45:AC ?>;CG05< C:070B5;L =0 >1J5:B ?>78F8N 87 A?8A:0 (;81> NULL ?@8 7=0G5=88 8=45:A0 -1)
int index=this.m_list_pos.Search(&this.m_temp_pos);
CPosition *pos=this.m_list_pos.At(index);
//--- #AB0=02;8205< A?8A:C D;03 A>@B8@>2:8 ?> 2@5<5=8 70:@KB8O ?>78F88 2 <8;;8A5:C=40E 8
//--- 2>72@0I05< C:070B5;L =0 >1J5:B-?>78F8N (;81> NULL ?@8 53> >BACBAB288)
this.m_list_pos.Sort(SORT_MODE_POSITION_TIME_CLOSE_MSC);
return pos;
}
//+------------------------------------------------------------------+
//| >72@0I05B D;03 B>3>, GB> ?>78F8O @K=>G=0O |
//+------------------------------------------------------------------+
bool CPositionsControl::IsMarketPosition(const long id)
{
//---  F8:;5 ?> A?8A:C 459AB2CNI8E ?>78F89 2 B5@<8=0;5
for(int i=::PositionsTotal()-1; i>=0; i--)
{
//--- ?>;CG05< B8:5B ?>78F88 ?> 8=45:AC F8:;0
ulong ticket=::PositionGetTicket(i);
//--- A;8 B8:5B ?>;CG5= 8 ?>78F8N <>6=> 2K1@0BL 8 5Q 845=B8D8:0B>@ @025= ?5@540==><C 2 <5B>4 -
//--- MB> 8A:><0O @K=>G=0O ?>78F8O, 2>72@0I05< true
if(ticket!=0 && ::PositionSelectByTicket(ticket) && ::PositionGetInteger(POSITION_IDENTIFIER)==id)
return true;
}
//--- 5B B0:>9 @K=>G=>9 ?>78F88 - 2>72@0I05< false
return false;
}
//+------------------------------------------------------------------+
//| >72@0I05B C:070B5;L =0 ?5@2CN 70:@KBCN ?>78F8N 2 A?8A:5 |
//+------------------------------------------------------------------+
CPosition *CPositionsControl::GetFirstClosedPosition(void)
{
this.m_list_pos.Sort(SORT_MODE_POSITION_TIME_CLOSE_MSC);
return this.m_list_pos.At(0);
}
//+------------------------------------------------------------------+
//| >72@0I05B C:070B5;L =0 ?>A;54=NN 70:@KBCN ?>78F8N 2 A?8A:5 |
//+------------------------------------------------------------------+
CPosition *CPositionsControl::GetLastClosedPosition(void)
{
this.m_list_pos.Sort(SORT_MODE_POSITION_TIME_CLOSE_MSC);
return this.m_list_pos.At(this.m_list_pos.Total()-1);
}
//+------------------------------------------------------------------+
//| >72@0I05B C:070B5;L =0 ?@54K4CICN 70:@KBCN ?>78F8N 2 A?8A:5 |
//+------------------------------------------------------------------+
CPosition *CPositionsControl::GetPrevClosedPosition(CPosition *current)
{
this.m_list_pos.Sort(SORT_MODE_POSITION_TIME_CLOSE_MSC);
int prev=this.m_list_pos.SearchLess(current);
return this.m_list_pos.At(prev);
}
//+------------------------------------------------------------------+
//| >72@0I05B C:070B5;L =0 A;54CNICN 70:@KBCN ?>78F8N 2 A?8A:5 |
//+------------------------------------------------------------------+
CPosition *CPositionsControl::GetNextClosedPosition(CPosition *current)
{
this.m_list_pos.Sort(SORT_MODE_POSITION_TIME_CLOSE_MSC);
int next=this.m_list_pos.SearchGreat(current);
return this.m_list_pos.At(next);
}
//+------------------------------------------------------------------+
//| K2>48B =0 3@0D8: 3@0D8G5A:>5 ?@54AB02;5=85 C:070==>9 ?>78F88 |
//+------------------------------------------------------------------+
void CPositionsControl::Show(CPosition *pos,const bool chart_redraw=false)
{
if(pos!=NULL)
{
pos.Show(chart_redraw);
this.m_current_id=pos.ID();
}
}
//+------------------------------------------------------------------+
//| !:@K205B A 3@0D8:0 3@0D8G5A:>5 ?@54AB02;5=85 ?5@2>9 ?>78F88 |
//+------------------------------------------------------------------+
void CPositionsControl::HideFirst(const bool chart_redraw=false)
{
CPosition *pos=this.GetFirstClosedPosition();
if(pos!=NULL)
pos.Hide(chart_redraw);
}
//+------------------------------------------------------------------+
//| B>1@0605B =0 3@0D8:5 3@0D8G5A:>5 ?@54AB02;5=85 ?5@2>9 ?>78F88 |
//+------------------------------------------------------------------+
void CPositionsControl::ShowFirst(const bool chart_redraw=false)
{
//--- >;CG05< C:070B5;L =0 ?5@2CN 70:@KBCN ?>78F8N
CPosition *pos=this.GetFirstClosedPosition();
if(pos==NULL)
return;
//--- !:@K205< 7=0G:8 2A5E ?>78F89, :@><5 ?5@2>9 ?> 5Q 845=B8D8:0B>@C
this.HideAllExceptOne(pos.ID());
//--- B>1@0605< 7=0G:8 ?5@2>9 ?>78F88 =0 3@0D8:5 8
//--- F5=B@8@C5< 3@0D8: ?> 7=0G:0< B5:CI59 2K1@0==>9 ?>78F88
this.Show(pos,chart_redraw);
this.CentersChartByCurrentSelected();
}
//+------------------------------------------------------------------+
//| !:@K205B A 3@0D8:0 3@0D8G5A:>5 ?@54AB02;5=85 ?>A;54=59 ?>78F88 |
//+------------------------------------------------------------------+
void CPositionsControl::HideLast(const bool chart_redraw=false)
{
CPosition *pos=this.GetLastClosedPosition();
if(pos!=NULL)
pos.Hide(chart_redraw);
}
//+------------------------------------------------------------------+
//| B>1@0605B =0 3@0D8:5 3@0D8G5A:>5 ?@54AB02;5=85 ?>A;54=59 ?>78F88|
//+------------------------------------------------------------------+
void CPositionsControl::ShowLast(const bool chart_redraw=false)
{
//--- >;CG05< C:070B5;L =0 ?>A;54=NN 70:@KBCN ?>78F8N
CPosition *pos=this.GetLastClosedPosition();
if(pos==NULL)
return;
//--- !:@K205< 7=0G:8 2A5E ?>78F89, :@><5 ?>A;54=59 ?> 5Q 845=B8D8:0B>@C
this.HideAllExceptOne(pos.ID(), false);
//--- B>1@0605< 7=0G:8 ?>A;54=59 ?>78F88 =0 3@0D8:5 8
//--- F5=B@8@C5< 3@0D8: ?> 7=0G:0< B5:CI59 2K1@0==>9 ?>78F88
this.Show(pos,chart_redraw);
this.CentersChartByCurrentSelected();
}
//+------------------------------------------------------------------+
//| B>1@0605B =0 3@0D8:5 3@0D8G5A:>5 ?@54AB02;5=85 B5:CI59 ?>78F88 |
//+------------------------------------------------------------------+
void CPositionsControl::ShowCurrent(const bool chart_redraw=false)