308 lines
25 KiB
MQL5
308 lines
25 KiB
MQL5
//+------------------------------------------------------------------+
|
|
//| 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"; // Загружать стратегии из файла
|
|
input bool LoadOnlyCurrentSymbol=false; // Грузить только для тек. инст.
|
|
#endif
|
|
#include <Arrays\ArrayObj.mqh>
|
|
#include "Strategy.mqh"
|
|
#include "StrategyParamsBase.mqh"
|
|
#include ".\Panel\Panel.mqh"
|
|
#include "Panel\Events\EventChartListChanged.mqh"
|
|
//+------------------------------------------------------------------+
|
|
//| Класс-контенер для управления стратегиями типа CStrategy |
|
|
//+------------------------------------------------------------------+
|
|
class CStrategyList
|
|
{
|
|
private:
|
|
CLog* Log; // Логирование
|
|
CArrayObj m_strategies; // Стратегии типа 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);
|
|
|
|
};
|
|
//+------------------------------------------------------------------+
|
|
//| Конструктор по умолчанию. |
|
|
//+------------------------------------------------------------------+
|
|
CStrategyList::CStrategyList(void) : StrButton(GetPointer(this))
|
|
{
|
|
Log=CLog::GetLog();
|
|
StrButton.Show();
|
|
m_strategies.Sort(0);
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Внимание, это единственное место, где удаляется CLog. В других |
|
|
//| местах удалять его не нужно. |
|
|
//+------------------------------------------------------------------+
|
|
CStrategyList::~CStrategyList(void)
|
|
{
|
|
CLog::DeleteLog();
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Очищяем список стратегий |
|
|
//+------------------------------------------------------------------+
|
|
void CStrategyList::Clear(void)
|
|
{
|
|
m_strategies.Clear();
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Посылает всем стратегиям списка событие OnTick |
|
|
//+------------------------------------------------------------------+
|
|
void CStrategyList::OnTick(void)
|
|
{
|
|
for(int i=0; i<m_strategies.Total(); i++)
|
|
{
|
|
CStrategy *strategy=m_strategies.At(i);
|
|
strategy.OnTick();
|
|
}
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Посылает всем стратегиям списка событие OnTimer |
|
|
//+------------------------------------------------------------------+
|
|
void CStrategyList::OnTimer(void)
|
|
{
|
|
for(int i=0; i<m_strategies.Total(); i++)
|
|
{
|
|
CStrategy *strategy=m_strategies.At(i);
|
|
strategy.OnTimer();
|
|
}
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Посылает всем стратегиям списка событие OnBookEvent |
|
|
//+------------------------------------------------------------------+
|
|
void CStrategyList::OnBookEvent(string symbol)
|
|
{
|
|
for(int i=0; i<m_strategies.Total(); i++)
|
|
{
|
|
CStrategy *strategy=m_strategies.At(i);
|
|
strategy.OnBookEvent(symbol);
|
|
}
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Посылает всем стратегиям списка событие 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);
|
|
}
|
|
}*/
|
|
//+------------------------------------------------------------------+
|
|
//| Возвращает общее количество стратегий |
|
|
//+------------------------------------------------------------------+
|
|
int CStrategyList::Total(void)
|
|
{
|
|
return m_strategies.Total();
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Возвращает стратегию по индексу index |
|
|
//+------------------------------------------------------------------+
|
|
CStrategy *CStrategyList::At(int index)
|
|
{
|
|
return m_strategies.At(index);
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Добавляет стратегию в список стратегий. Добавляемая стратегия |
|
|
//| обязана содержать magic номер, рабочий сивмол, и название, т.е. |
|
|
//| установить соответствующие значения через методы ExpertMagic, |
|
|
//| ExpertSymbol и 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;
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Загружает стратегии из переданного XML-файла "xml_name" |
|
|
//| Если флаг load_curr_symbol установлен в истину, будут загружены |
|
|
//| только те стратегии, символ которых соответствует текущему |
|
|
//| символу 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);
|
|
}
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Парсит XML стратегии |
|
|
//+------------------------------------------------------------------+
|
|
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;
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Парсит XML стратегии |
|
|
//+------------------------------------------------------------------+
|
|
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;
|
|
//+------------------------------------------------------------------+
|