UTE/Strategy/Panel/Panel.mqh

290 lines
21 KiB
MQL5
Raw Permalink Normal View History

2025-05-30 16:34:43 +02:00
<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| Panel.mqh |
//| Copyright 2016, Vasiliy Sokolov, St-Petersburg, Russia |
//| https://www.mql5.com/ru/users/c-4 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, Vasiliy Sokolov."
#property link "https://www.mql5.com/ru/users/c-4"
#include "Node.mqh"
#include "ElChart.mqh"
#include "ElDropDownList.mqh"
#include "AgentForm.mqh"
#include "Events\EventChartListChanged.mqh"
#include "Events\EventChartEndEdit.mqh"
#include "..\Strategy.mqh"
#include "..\Message.mqh"
#include "..\Logs.mqh"
class CStrategyList;
class CStrategy;
//+------------------------------------------------------------------+
//| 87C0;L=0O ?0=5;L :;0AA0 |
//+------------------------------------------------------------------+
class CStrBtn : public CElButton
{
private:
CAgentForm Form; // $>@<0 C?@02;5=8O M:A?5@B0<8
double m_volume; // #AB0=>2;5==K9 >1J5<
CStrategyList* StrategiesList; // >ABC? : E@0=8;8IC AB@0B5389
CStrategy* CurrStrategy; // "5:CI0O 2K45;5==0O AB@0B538O
CLog* Log; // >38@>20=85
void SelectStrategy(string str_name);
void ChangeRegim(string str_regim);
void ChangeRegimAll(string str_regim);
void RefreshRegim(ENUM_TRADE_STATE state);
void ChangeVolume(CEventChartEndEdit* chVol);
void ClickBuySell(CEventChartObjClick* event);
public:
CStrBtn(CStrategyList* slist);
virtual void OnClick(CEventChartObjClick* event);
virtual void OnShow(void);
virtual void Event(CEvent* event);
void AddStrategyName(string name);
};
//+------------------------------------------------------------------+
//| 87C0;L=0O ?0=5;L :;0AA0 |
//+------------------------------------------------------------------+
CStrBtn::CStrBtn(CStrategyList* slist)
{
m_volume = StringToDouble(Form.Volume.Text());
Log = CLog::GetLog();
StrategiesList = slist;
Width(17);
Height(17);
XCoord(110);
YCoord(0);
TextFont("Webdings");
Text(CharToString(0x36));
m_elements.Add(GetPointer(Form));
Form.ListAgents.AddElement("ALL");
}
//+------------------------------------------------------------------+
//| >402;O5< 2K2>4 AC1 D>@<K |
//+------------------------------------------------------------------+
void CStrBtn::OnShow(void)
{
}
//+------------------------------------------------------------------+
//| !2>@0G8205</@072>@0G8205< D>@<C C?@02;5=8O M:A?5@B0<8 |
//+------------------------------------------------------------------+
void CStrBtn::OnClick(CEventChartObjClick *event)
{
if(State() == PUSH_ON && !Form.IsShowed())
Form.Show();
if(State() == PUSH_OFF && Form.IsShowed())
Form.Hide();
}
//+------------------------------------------------------------------+
//| >102;O5B ?>;=>5 =0720=85 AB@0B5388 2 A?8A>: AB@0B5389 |
//+------------------------------------------------------------------+
void CStrBtn::AddStrategyName(string name)
{
Form.ListAgents.AddElement(name);
}
//+------------------------------------------------------------------+
//| 5@5E20BK205< A>1KB8O, B@51CNI85 @5038@>20=8O =0 =8E |
//+------------------------------------------------------------------+
void CStrBtn::Event(CEvent *event)
{
CNode::Event(event);
if(event.EventType() == EVENT_CHART_LIST_CHANGED)
{
CEventChartListChanged* changed = event;
if (changed.ListNameChanged() == Form.ListAgents.Name())
SelectStrategy(Form.ListAgents.Text());
else if (changed.ListNameChanged() == Form.ListRegim.Name())
{
if(Form.ListAgents.Text() != "ALL")
ChangeRegim(Form.ListRegim.Text());
else
ChangeRegimAll(Form.ListRegim.Text());
}
}
if(event.EventType() == EVENT_CHART_END_EDIT)
ChangeVolume(event);
if(event.EventType() == EVENT_CHART_OBJECT_CLICK)
ClickBuySell(event);
}
//+------------------------------------------------------------------+
//| 1=>2;O5B B5:CI89 >1J5< |
//+------------------------------------------------------------------+
void CStrBtn::ChangeVolume(CEventChartEndEdit *endEdit)
{
if(endEdit.ObjectName() != Form.Volume.Name())return;
double vol = StringToDouble(Form.Volume.Text());
if(vol <= 0.0)
{
string text = "Wrong volume '" + Form.Volume.Text() + "'. The volume must be a number greater than zero";
CMessage* msg = new CMessage(MESSAGE_WARNING, __FUNCTION__, text);
Log.AddMessage(msg);
Form.Volume.Text(DoubleToString(m_volume, 1));
return;
}
m_volume = vol;
Form.Volume.Text(DoubleToString(m_volume, 1));
}
//+------------------------------------------------------------------+
//| 1@010BK205B =060B85 :=>?>: :C?8BL 8;8 ?@>40BL |
//+------------------------------------------------------------------+
void CStrBtn::ClickBuySell(CEventChartObjClick *event)
{
ENUM_POSITION_TYPE direction;
CElButton* btn = NULL;
if(Form.ListAgents.Text() == "ALL" &&
(event.ObjectName() == Form.BuyButton.Name() ||
event.ObjectName() == Form.SellButton.Name()))
{
string text = "The group operation for manual buying and selling is not supported:(";
CMessage* msg = new CMessage(MESSAGE_ERROR, __FUNCTION__, text);
Log.AddMessage(msg);
Form.BuyButton.State(PUSH_OFF);
Form.SellButton.State(PUSH_OFF);
return;
}
else if(event.ObjectName() == Form.BuyButton.Name())
{
direction = POSITION_TYPE_BUY;
btn = GetPointer(Form.BuyButton);
}
else if(event.ObjectName() == Form.SellButton.Name())
{
direction = POSITION_TYPE_SELL;
btn = GetPointer(Form.SellButton);
}
else if(event.ObjectName() == Form.UpVol.Name())
{
m_volume += 1.0;
Form.Volume.Text(DoubleToString(m_volume, 1));
Sleep(100);
Form.UpVol.State(PUSH_OFF);
return;
}
else if(event.ObjectName() == Form.DnVol.Name())
{
m_volume -= 1.0;
if(m_volume < 1.0)
m_volume = 1.0;
Form.Volume.Text(DoubleToString(m_volume, 1));
Sleep(100);
Form.DnVol.State(PUSH_OFF);
return;
}
else
return;
if(m_volume == 0.0)
{
string text = "You must set the desired volume";
CMessage* msg = new CMessage(MESSAGE_WARNING, __FUNCTION__, text);
Log.AddMessage(msg);
btn.State(PUSH_OFF);
return;
}
if(CheckPointer(CurrStrategy) == POINTER_INVALID)
{
string text = "Strategy is not selected. Choose a strategy and try again";
CMessage* msg = new CMessage(MESSAGE_WARNING, __FUNCTION__, text);
Log.AddMessage(msg);
btn.State(PUSH_OFF);
return;
}
if(direction == POSITION_TYPE_BUY)
CurrStrategy.Buy(m_volume);
else
CurrStrategy.Sell(m_volume);
Sleep(100);
btn.State(PUSH_OFF);
}
//+------------------------------------------------------------------+
//| K18@05B AB@0B538N 87 A?8A:0 AB@0B5389 ?> 8<5=8 |
//+------------------------------------------------------------------+
void CStrBtn::SelectStrategy(string str_name)
{
CurrStrategy = NULL;
if(str_name == "ALL")
return;
for(int i = 0; i < StrategiesList.Total(); i++)
{
CStrategy* str = StrategiesList.At(i);
string sexp = str.ExpertNameFull();
if(str.ExpertNameFull() != str_name)continue;
CurrStrategy = str;
RefreshRegim(CurrStrategy.TradeState());
return;
}
string text = "Strategy with name not find. Select strategy failed";
CMessage* msg = new CMessage(MESSAGE_WARNING, __FUNCTION__, text);
Log.AddMessage(msg);
}
//+------------------------------------------------------------------+
//| 5=O5B B>@3>2K9 @568< 4;O 2A5E AB@0B5389 2 A?8A:5 |
//+------------------------------------------------------------------+
void CStrBtn::ChangeRegimAll(string str_regim)
{
for(int i = 0; i < StrategiesList.Total(); i++)
{
CurrStrategy = StrategiesList.At(i);
ChangeRegim(str_regim);
}
CurrStrategy = NULL;
}
//+------------------------------------------------------------------+
//| 5=O5B B>@3>2K9 @568< AB@0B5388 |
//+------------------------------------------------------------------+
void CStrBtn::ChangeRegim(string str_regim)
{
if(CurrStrategy == NULL)
{
string text = "The strategy is not selected in strategy list. Select the strategy and try again";
CMessage* msg = new CMessage(MESSAGE_WARNING, __FUNCTION__, text);
Log.AddMessage(msg);
return;
}
if(str_regim == REGIM_BUY_AND_SELL)
CurrStrategy.TradeState(TRADE_BUY_AND_SELL);
else if(str_regim == REGIM_BUY_ONLY)
CurrStrategy.TradeState(TRADE_BUY_ONLY);
else if(str_regim == REGIM_SELL_ONLY)
CurrStrategy.TradeState(TRADE_SELL_ONLY);
else if(str_regim == REGIM_WAIT)
CurrStrategy.TradeState(TRADE_WAIT);
else if(str_regim == REGIM_STOP)
CurrStrategy.TradeState(TRADE_STOP);
else if(str_regim == REGIM_NO_NEW_ENTRY)
CurrStrategy.TradeState(TRADE_NO_NEW_ENTRY);
else
{
string text = "Regim " + str_regim + " does not match any of the supported modes";
CMessage* msg = new CMessage(MESSAGE_WARNING, __FUNCTION__, text);
Log.AddMessage(msg);
}
}
//+------------------------------------------------------------------+
//| >:07K205B B5:CI89 B>@3>2K9 @568< AB@0B5388 2 >:=5 2K1>@0 @568<0 |
//+------------------------------------------------------------------+
void CStrBtn::RefreshRegim(ENUM_TRADE_STATE state)
{
if(CheckPointer(CurrStrategy) == POINTER_INVALID)
return;
switch(state)
{
case TRADE_BUY_AND_SELL:
Form.ListRegim.SelectElementByName(REGIM_BUY_AND_SELL);
break;
case TRADE_BUY_ONLY:
Form.ListRegim.SelectElementByName(REGIM_BUY_ONLY);
break;
case TRADE_SELL_ONLY:
Form.ListRegim.SelectElementByName(REGIM_SELL_ONLY);
break;
case TRADE_WAIT:
Form.ListRegim.SelectElementByName(REGIM_WAIT);
break;
case TRADE_STOP:
Form.ListRegim.SelectElementByName(REGIM_STOP);
break;
case TRADE_NO_NEW_ENTRY:
Form.ListRegim.SelectElementByName(REGIM_NO_NEW_ENTRY);
break;
}
}