UTE/Strategy/StrategiesList.mqh

309 lines
25 KiB
MQL5
Raw Permalink Normal View History

2025-05-30 16:34:43 +02:00
<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| StrategiesList.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"
#ifdef SHOW_BASE_SETTINGS
input string StrategiesXMLFile="Strategies.xml"; // 03@C60BL AB@0B5388 87 D09;0
input bool LoadOnlyCurrentSymbol=false; // @C78BL B>;L:> 4;O B5:. 8=AB.
#endif
#include <Arrays\ArrayObj.mqh>
#include "Strategy.mqh"
#include "StrategyParamsBase.mqh"
#include ".\Panel\Panel.mqh"
#include "Panel\Events\EventChartListChanged.mqh"
//+------------------------------------------------------------------+
//| ;0AA-:>=B5=5@ 4;O C?@02;5=8O AB@0B538O<8 B8?0 CStrategy |
//+------------------------------------------------------------------+
class CStrategyList
{
private:
CLog* Log; // >38@>20=85
CArrayObj m_strategies; // !B@0B5388 B8?0 CStrategy
void ParseStrategies(CXmlElement *xmlStrategies,bool load_curr_symbol);
void ParseLimits(CXmlElement *xmlLimits);
CStrBtn StrButton;
public:
CStrategyList(void);
~CStrategyList(void);
void LoadStrategiesFromXML(string xml_name,bool load_curr_symbol);
bool AddStrategy(CStrategy *strategy);
int Total();
void Clear();
CStrategy *At(int index);
void OnTick();
void OnTimer();
void OnBookEvent(string symbol);
void OnDeinit(const int reason);
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam);
};
//+------------------------------------------------------------------+
//| >=AB@C:B>@ ?> C<>;G0=8N. |
//+------------------------------------------------------------------+
CStrategyList::CStrategyList(void) : StrButton(GetPointer(this))
{
Log=CLog::GetLog();
StrButton.Show();
m_strategies.Sort(0);
}
//+------------------------------------------------------------------+
//| =8<0=85, MB> 548=AB25==>5 <5AB>, 345 C40;O5BAO CLog.  4@C38E |
//| <5AB0E C40;OBL 53> =5 =C6=>. |
//+------------------------------------------------------------------+
CStrategyList::~CStrategyList(void)
{
CLog::DeleteLog();
}
//+------------------------------------------------------------------+
//| G8IO5< A?8A>: AB@0B5389 |
//+------------------------------------------------------------------+
void CStrategyList::Clear(void)
{
m_strategies.Clear();
}
//+------------------------------------------------------------------+
//| >AK;05B 2A5< AB@0B538O< A?8A:0 A>1KB85 OnTick |
//+------------------------------------------------------------------+
void CStrategyList::OnTick(void)
{
for(int i=0; i<m_strategies.Total(); i++)
{
CStrategy *strategy=m_strategies.At(i);
strategy.OnTick();
}
}
//+------------------------------------------------------------------+
//| >AK;05B 2A5< AB@0B538O< A?8A:0 A>1KB85 OnTimer |
//+------------------------------------------------------------------+
void CStrategyList::OnTimer(void)
{
for(int i=0; i<m_strategies.Total(); i++)
{
CStrategy *strategy=m_strategies.At(i);
strategy.OnTimer();
}
}
//+------------------------------------------------------------------+
//| >AK;05B 2A5< AB@0B538O< A?8A:0 A>1KB85 OnBookEvent |
//+------------------------------------------------------------------+
void CStrategyList::OnBookEvent(string symbol)
{
for(int i=0; i<m_strategies.Total(); i++)
{
CStrategy *strategy=m_strategies.At(i);
strategy.OnBookEvent(symbol);
}
}
//+------------------------------------------------------------------+
//| >AK;05B 2A5< AB@0B538O< A?8A:0 A>1KB85 OnTimer |
//+------------------------------------------------------------------+
/*void CStrategyList::OnDeinit(const int reason)
{
for(int i = 0; i < m_strategies.Total(); i++)
{
CStrategy* strategy = m_strategies.At(i);
strategy.OnDeinit(reason);
}
}*/
//+------------------------------------------------------------------+
//| >72@0I05B >1I55 :>;8G5AB2> AB@0B5389 |
//+------------------------------------------------------------------+
int CStrategyList::Total(void)
{
return m_strategies.Total();
}
//+------------------------------------------------------------------+
//| >72@0I05B AB@0B538N ?> 8=45:AC index |
//+------------------------------------------------------------------+
CStrategy *CStrategyList::At(int index)
{
return m_strategies.At(index);
}
//+------------------------------------------------------------------+
//| >102;O5B AB@0B538N 2 A?8A>: AB@0B5389. >102;O5<0O AB@0B538O |
//| >1O70=0 A>45@60BL magic =><5@, @01>G89 A82<>;, 8 =0720=85, B.5. |
//| CAB0=>28BL A>>B25BAB2CNI85 7=0G5=8O G5@57 <5B>4K ExpertMagic, |
//| ExpertSymbol 8 ExpertName. |
//+------------------------------------------------------------------+
bool CStrategyList::AddStrategy(CStrategy *strategy)
{
bool res=true;
if(strategy.ExpertMagic()==0)
{
string text="The strategy should have a magic number. Adding strategy "+strategy.ExpertName()+" is impossible";
CMessage *msg=new CMessage(MESSAGE_WARNING,__FUNCTION__,text);
Log.AddMessage(msg);
res=false;
}
if(strategy.ExpertName()==NULL || strategy.ExpertName()=="")
{
string text="The strategy should have an expert name. Adding strategy with magic "+(string)strategy.ExpertMagic()+" is impossible";
CMessage *msg=new CMessage(MESSAGE_WARNING,__FUNCTION__,text);
Log.AddMessage(msg);
res=false;
}
if(strategy.ExpertSymbol()==NULL || strategy.ExpertSymbol()=="")
{
string text="The strategy should have a work symbol. Adding strategy "+strategy.ExpertName()+" is impossible";
CMessage *msg=new CMessage(MESSAGE_WARNING,__FUNCTION__,text);
Log.AddMessage(msg);
res=false;
}
if(strategy.Timeframe()==PERIOD_CURRENT)
{
string text="The strategy should have a work timeframe. Adding strategy "+strategy.ExpertName()+" is impossible";
CMessage *msg=new CMessage(MESSAGE_WARNING,__FUNCTION__,text);
Log.AddMessage(msg);
res=false;
}
int index = m_strategies.Search(strategy);
if(index != -1)
{
string text="Strategy with Magic "+(string)strategy.ExpertMagic()+
" has already been added to the list of strategies. Change the Magic to be added to the strategy";
CMessage *msg=new CMessage(MESSAGE_WARNING,__FUNCTION__,text);
Log.AddMessage(msg);
res=false;
}
if(!strategy.OnInit())
{
string text="Failed OnInit. '"+strategy.ExpertName()+"' strategy will not be added to the list";
CMessage *msg=new CMessage(MESSAGE_WARNING,__FUNCTION__,text);
Log.AddMessage(msg);
res=false;
}
if(res == false)return res;
res=m_strategies.InsertSort(strategy);
if(res)
{
StrButton.AddStrategyName(strategy.ExpertNameFull());
string text="Strategy "+strategy.ExpertNameFull()+" successfully loaded";
CMessage *msg=new CMessage(MESSAGE_INFO,__FUNCTION__,text);
Log.AddMessage(msg);
}
return res;
}
//+------------------------------------------------------------------+
//| 03@C605B AB@0B5388 87 ?5@540==>3> XML-D09;0 "xml_name" |
//| A;8 D;03 load_curr_symbol CAB0=>2;5= 2 8AB8=C, 1C4CB 703@C65=K |
//| B>;L:> B5 AB@0B5388, A8<2>; :>B>@KE A>>B25BAB2C5B B5:CI5<C |
//| A8<2>;C CurrentSymbol() |
//+------------------------------------------------------------------+
void CStrategyList::LoadStrategiesFromXML(string xml_name,bool load_curr_symbol)
{
CXmlDocument doc;
string err;
bool res=doc.CreateFromFile(xml_name,err);
if(!res)
printf(err);
CXmlElement *global=GetPointer(doc.FDocumentElement);
for(int i=0; i<global.GetChildCount(); i++)
{
CXmlElement* child = global.GetChild(i);
if(child.GetName() == "Strategies")
ParseStrategies(child,load_curr_symbol);
}
}
//+------------------------------------------------------------------+
//| 0@A8B XML AB@0B5388 |
//+------------------------------------------------------------------+
void CStrategyList::ParseStrategies(CXmlElement *xmlStrategies,bool load_curr_symbol)
{
CParamsBase *params=NULL;
for(int i=0; i<xmlStrategies.GetChildCount(); i++)
{
CXmlElement *xStrategy=xmlStrategies.GetChild(i);
if(CheckPointer(params)!=POINTER_INVALID)
delete params;
params=new CParamsBase(xStrategy);
if(!params.IsValid() || (params.Symbol()!=Symbol() && load_curr_symbol))
continue;
CStrategy *str=CStrategy::GetStrategy(params.Name());
if(str==NULL)
continue;
str.ExpertMagic(params.Magic());
str.ExpertSymbol(params.Symbol());
str.Timeframe(params.Timeframe());
str.ExpertName(params.Name());
string name=str.ExpertName();
CXmlElement *xml_params=xStrategy.GetChild("Params");
if(xml_params!=NULL)
str.ParseXmlParams(xml_params);
CXmlElement *xml_mm=xStrategy.GetChild("MoneyManagment");
if(xml_mm!=NULL)
{
if(!str.MM.ParseByXml(xml_mm))
{
string text="Strategy "+str.ExpertName()+" (Magic: "+(string)str.ExpertMagic()+") load MM from XML failed";
CMessage *msg=new CMessage(MESSAGE_WARNING,__FUNCTION__,text);
Log.AddMessage(msg);
}
}
CXmlElement *xml_regim=xStrategy.GetChild("TradeStateStart");
if(xml_regim!=NULL)
{
string regim=xml_regim.GetText();
if(regim=="BuyAndSell")
str.TradeState(TRADE_BUY_AND_SELL);
else if(regim=="BuyOnly")
str.TradeState(TRADE_BUY_ONLY);
else if(regim=="SellOnly")
str.TradeState(TRADE_SELL_ONLY);
else if(regim=="Stop")
str.TradeState(TRADE_STOP);
else if(regim=="Wait")
str.TradeState(TRADE_WAIT);
else if(regim=="NoNewEntry")
str.TradeState(TRADE_NO_NEW_ENTRY);
else
{
string text="For strategy "+str.ExpertName()+" (Magic: "+(string)str.ExpertMagic()+
") set not correctly trade state: "+regim;
CMessage *msg=new CMessage(MESSAGE_WARNING,__FUNCTION__,text);
Log.AddMessage(msg);
}
}
AddStrategy(str);
}
if(CheckPointer(params)!=POINTER_INVALID)
delete params;
}
//+------------------------------------------------------------------+
//| 0@A8B XML AB@0B5388 |
//+------------------------------------------------------------------+
void CStrategyList::OnChartEvent(const int id,const long &lparam,const double &dparam,const string &sparam)
{
CEvent *event=NULL;
switch(id)
{
case CHARTEVENT_OBJECT_CLICK:
event=new CEventChartObjClick(sparam);
break;
case CHARTEVENT_MOUSE_MOVE:
event=new CEventChartMouseMove(lparam,(long)dparam,(int)sparam);
break;
case CHARTEVENT_OBJECT_ENDEDIT:
event=new CEventChartEndEdit(sparam);
break;
case CHARTEVENT_CUSTOM+EVENT_CHART_LIST_CHANGED:
event=new CEventChartListChanged(sparam);
break;
}
if(event!=NULL)
{
StrButton.Event(event);
delete event;
}
ChartRedraw();
}
CStrategyList Manager;
//+------------------------------------------------------------------+