Attach Trade Handler to EA Class ...

This commit is contained in:
2024-11-03 17:03:14 +03:30
parent 56b2e11bfc
commit 7f1f0cd4e5
6 changed files with 5048 additions and 597 deletions
+107 -582
View File
@@ -23,13 +23,7 @@
//
// Imports ...
#include "../Classes/x-saherelm.xalert.class.mq5"
#include "../Classes/x-saherelm.xtrade.class.mq5"
#include "../Helpers/x-saherelm.xcc.helper.mq5"
#include "../Helpers/x-saherelm.xct.helper.mq5"
#include "../X121SMCEA/Classes/x-121.smc.ea.panel.class.mq5"
#include "../X121SMCEA/Strategy/x-121.smc.ob.strategy.class.mq5"
#include "../X121SMCEA/Strategy/x-121.smc.supdem.strategy.class.mq5"
#include "../X121SMCEA/Experts/x-121.smc.expert.class.mq5"
//
// Inputs ...
@@ -47,6 +41,14 @@ input group "Symbols";
input bool eaMultiSymbol = false; // Use Multi Symbol
input string eaSymbols = "EURUSDb,GBPUSDb,USDJPYb,USDCHFb,DowJones30,XAUUSDb"; // Trading Symbols
//
// Volume ...
input group "Volume";
input bool eaUseDynamicVolume = false; // Use Dynamic Volume
input double eaDynamicVolumeStep = 0.01; // Increase Volume Step
input double eaDynamicVolumeBalanceFactor = 200; // Balance Factor for Generate Dynamic Volume
input double eaVolume = 0.05; // Static Volume
//
// Signalling ...
input group "Signalling";
@@ -58,11 +60,11 @@ input bool eaAllowShort = true; // Allow Short Signals
// Here we Provider All Requirements for
// Configuring Alert Provider ...
input group "Alert";
input bool eAEnableAlerts = true; // Enable Alerts
input bool eALogAlerts = true; // Log Alerts
input bool eAMailAlerts = false; // Mail Alerts
input bool eAPushAlerts = false; // Push Alerts
input bool eATerminalAlerts = false; // Terminal Alerts
input bool eaEnableAlerts = true; // Enable Alerts
input bool eaLogAlerts = true; // Log Alerts
input bool eaMailAlerts = false; // Mail Alerts
input bool eaPushAlerts = false; // Push Alerts
input bool eaTerminalAlerts = false; // Terminal Alerts
//
// Time Report ...
@@ -75,21 +77,7 @@ input bool eaReportNewHours = false; // Report New Hours
//
// Define Local Variables ...
//
string eaLogTag; // EA Log Prefix ...
XSCAlert *eaAlert; // Alert Provider for Expert Adviser ...
XSCTrade *eaTrader; // Trader of Expert Adviser ...
XTimeTracker eaTimeTracker; // Time Tracker for Providing Reports ...
XSCXCTHelper *eaCTHelper; // Bar Timer Indicator Helper class ...
XSCXCCHelper *eaCCHelper; // Chart Styler Indicator Helper Class ...
X121CAppDialog eaMainWindow; // EA Main Dialog ...
XCX121SMCBaseStrategy *eaStrategies[]; // Registere Strategies in EA ...
//
// Signalling Controller Conditions ...
bool eaDisableSignalling;
bool eaAllowLongSignalling;
bool eaAllowShortSignalling;
XC121SMCExpert eaExpert;
//
// Event Handlers ...
@@ -114,28 +102,55 @@ int OnInit()
}
//
// Initial Controller Conditions ...
InitControllerConditions();
// Configure EA Expert Class ...
//
// Initialize all Requirements ...
if (!InitEA())
eaExpert.OnSignalEventListener = OnSignalRecieved;
eaExpert.OnPositionSLEventListener = OnStopLossTriggered;
eaExpert.OnPositionTPEventListener = OnTakeProfitTriggered;
eaExpert.OnPositionForceCloseEventListener = OnPositionForceClosed;
//
eaExpert.MagicNumber(eaMagicNumber);
eaExpert.Slippage(eaSlippage);
eaExpert.TagPrefix(eaLogSuffix);
//
eaExpert.SetSymbol(_Symbol);
eaExpert.SetPeriod(_Period);
eaExpert.Symbols(eaSymbols);
eaExpert.MultiSymbol(eaMultiSymbol);
//
eaExpert.AllowLong(eaAllowLong);
eaExpert.AllowShort(eaAllowShort);
//
eaExpert.SetAlertEnableAlerts(eaEnableAlerts);
eaExpert.SetAlertLogAlerts(eaLogAlerts);
eaExpert.SetAlertMailAlerts(eaMailAlerts);
eaExpert.SetAlertPushAlerts(eaPushAlerts);
eaExpert.SetAlertTerminalAlerts(eaTerminalAlerts);
//
eaExpert.ReportNewMonths(eaReportNewMonths);
eaExpert.ReportNewWeeks(eaReportNewWeeks);
eaExpert.ReportNewDays(eaReportNewDays);
eaExpert.ReportNewHours(eaReportNewHours);
//
eaExpert.Volume(eaVolume);
eaExpert.UseDynamicVolume(eaUseDynamicVolume);
eaExpert.DynamicVolumeStep(eaDynamicVolumeStep);
eaExpert.DynamicVolumeBalanceFactor(eaDynamicVolumeBalanceFactor);
//
bool isInited = eaExpert.HandleOnInit();
if (!isInited)
{
return INIT_FAILED;
}
//
// Initialize all GUI Objects and Classes ...
if (!InitGUI())
{
return INIT_FAILED;
}
//
// Alert Initialization ...
string msg = "Initialized Successfully ...";
eaAlert.Alert(msg);
//
// Init Succeed ...
return INIT_SUCCEEDED;
@@ -159,46 +174,28 @@ void OnDeinit(const int reason)
//
// Destroy Timer ...
DestroyTimer();
//
// Destroy all GUI Requirements ...
DestroyGUI();
//
// Alert De Initialization Succeeded ...
string msg = "De Initialized Successfully ...";
eaAlert.Alert(msg);
//
// Destroy all EA Requirements ...
DestroyEA();
eaExpert.HandleOnDeInit();
}
//
// On Tick Handler ...
void OnTick()
{
//
UpdateGUI();
HandleReportTime();
UpdatePositionsState();
HandleStrategiesOnTick();
eaExpert.HandleOnTick();
}
//
// Handle On Trade Event ...
void OnTrade()
{
eaTrader.HandleOnTrade();
eaExpert.HandleOnTrade();
}
//
// On Timer ...
void OnTimer()
{
//
// TODO: Implement Time Based Functionalities ...
eaExpert.HandleOnTimer();
}
//
@@ -210,59 +207,11 @@ void OnChartEvent(
const string &sparam //
)
{
//
// Apply Chart Event on Main Window ...
eaMainWindow.ChartEvent(id, lparam, dparam, sparam);
//
// Handle OnClick Event ...
if (id == CHARTEVENT_OBJECT_CLICK)
{
HandleOnClickEvent(sparam);
}
eaExpert.HandleOnChartEvent(id, lparam, dparam, sparam);
}
//
// Custom Event Handlers ...
/**
* Handle StopLoss Triggered Positions ...
*
* @param deal: XDeal ...
*/
void OnStopLossTriggered(const XDeal &deal)
{
//
HandleReportBalance();
}
/**
* Handle Take Profit Triggered Positions ...
*
* @param deal: XDeal ...
*/
void OnTakeProfitTriggered(const XDeal &deal)
{
//
HandleReportBalance();
}
/**
* Handle Force Close a Position ...
*
* @param ticket: Position Ticket ...
* @param position: XPosition ...
* @param comment: Closing Comment ...
*/
void OnPositionForceClosed(
const ulong ticket,
const XPosition &position,
const string comment //
)
{
//
HandleReportBalance();
}
// Event Listeners ...
/**
* Do What we Want by Specific Signal ...
@@ -276,13 +225,50 @@ void OnSignalRecieved(
)
{
//
// TODO: Implement this ...
//
ENUM_X_SIGNAL_EXECUTION_RESULT state;
eaTrader.ExecuteSignal(
eaExpert.HandleOnSignalRecieved(
signal,
state //
conditions //
);
}
/**
* Handle StopLoss Triggered Positions ...
*
* @param deal: XDeal instance ...
*/
void OnStopLossTriggered(const XDeal &deal)
{
eaExpert.HandleOnStopLossTriggered(deal);
}
/**
* Handle Take Profit Triggered Positions ...
*
* @param deal: XDeal instance ...
*/
void OnTakeProfitTriggered(const XDeal &deal)
{
eaExpert.HandleOnTakeProfitTriggered(deal);
}
/**
* Handle Force Close a Position ...
*
* @param ticket: ULONG ...
* @param position: XPosition instance ...
* @param comment: String ...
*/
void OnPositionForceClosed(
const ulong ticket,
const XPosition &position,
const string comment //
)
{
//
eaExpert.HandleOnPositionForceClosed(
ticket,
position,
comment //
);
}
@@ -368,465 +354,4 @@ void DestroyTimer()
// EventKillTimer();
}
/**
* Initialize all Requirements for Working Fine ...
*
* @return ( bool )
*/
bool InitEA()
{
//
bool result = false;
//
// Locally Apply Changes on Prefix ...
eaLogTag = ShortName + ">" + eaLogSuffix;
//
// Bar Timer ...
XCTInputs ctInputs;
ctInputs.Default(); // Default Configurations ...
eaCTHelper = new XSCXCTHelper();
result = eaCTHelper.Init(
_Symbol,
_Period,
ctInputs //
);
if (!result)
{
return result;
}
//
// Bar Styles ...
XCCInputs ccInputs;
ccInputs.Default(); // Default Configurations ...
eaCCHelper = new XSCXCCHelper();
result = eaCCHelper.Init(
_Symbol,
_Period,
ccInputs //
);
if (!result)
{
return result;
}
//
// Create Instance of Alert Class and Configure it ...
eaAlert = new XSCAlert();
eaAlert.SetPrefix(eaLogTag);
eaAlert.SetLogAlerts(eALogAlerts);
eaAlert.SetMailAlerts(eAMailAlerts);
eaAlert.SetPushAlerts(eAPushAlerts);
eaAlert.SetEnableAlerts(eAEnableAlerts);
eaAlert.SetTerminalAlerts(eATerminalAlerts);
//
// Create Trader Instance and Configure it ...
eaTrader = new XSCTrade(
eaSlippage,
eaMagicNumber //
);
eaTrader.AddOnStopLossEventHandler(OnStopLossTriggered);
eaTrader.AddOnTakeProfitEventHandler(OnTakeProfitTriggered);
eaTrader.AddOnForceClosePositionEventHandler(OnPositionForceClosed);
//
// Symbols Parsing ...
//
// Parsers ...
XSymbolParser _symbolParser;
//
// Single Symbol ...
if (!eaMultiSymbol)
{
//
// Register Strategy ...
//
// Create Class Instance ...
XCX121SMCBaseStrategy *iX121SMCStrategy;
iX121SMCStrategy = new XCX121SMCOBStrategy(
_Symbol,
_Period //
);
//
iX121SMCStrategy.AddXSignalEventHandler(OnSignalRecieved);
//
ConfigureStrategy(iX121SMCStrategy);
RegisterStrategy(iX121SMCStrategy);
}
//
// Multi Symbol ...
else
{
//
// Parse Symbols ...
string symbols[];
int symbolsCount = SplitContent(
symbols,
eaSymbols //
);
result = IsValidSize(symbolsCount);
if (!result)
{
return result;
}
//
for (int i = 0; i < symbolsCount; i++)
{
//
// Select Symbol ...
string iSymbol = symbols[i];
//
// Register Strategy Based On Symbol ...
//
// Parse Symbol ...
bool isEURUSD = _symbolParser.IsEURUSD(_Symbol);
bool isGBPUSD = _symbolParser.IsGBPUSD(_Symbol);
bool isEURGBP = _symbolParser.IsEURGBP(_Symbol);
bool isUSDCHF = _symbolParser.IsUSDCHF(_Symbol);
bool isEURCHF = _symbolParser.IsEURCHF(_Symbol);
bool isUSDJPY = _symbolParser.IsUSDJPY(_Symbol);
bool isEURJPY = _symbolParser.IsEURJPY(_Symbol);
bool isUSDCAD = _symbolParser.IsUSDCAD(_Symbol);
bool isEURCAD = _symbolParser.IsEURCAD(_Symbol);
bool isAUDUSD = _symbolParser.IsAUDUSD(_Symbol);
bool isEURAUD = _symbolParser.IsEURAUD(_Symbol);
bool isNZDUSD = _symbolParser.IsNZDUSD(_Symbol);
bool isEURNZD = _symbolParser.IsEURNZD(_Symbol);
//
// Check Symbol Can Register or not ...
bool canRegisterStrategy = true;
//
if (canRegisterStrategy)
{
//
// Create Class Instance ...
XCX121SMCBaseStrategy *iX121SMCStrategy;
iX121SMCStrategy = new XCX121SMCOBStrategy(
iSymbol,
_Period //
);
//
iX121SMCStrategy.AddXSignalEventHandler(OnSignalRecieved);
//
// Register Strategy ...
ConfigureStrategy(iX121SMCStrategy);
RegisterStrategy(iX121SMCStrategy);
}
}
}
//
return result;
}
/**
* Destroy all Initialized EA Requirements ...
*/
void DestroyEA()
{
//
delete eaCTHelper;
delete eaCCHelper;
//
delete eaAlert;
}
/**
* Initialize all GUI Requirements for EA ...
*
* @return ( bool )
*/
bool InitGUI()
{
//
bool result = false;
//
// Initialize Application Main Window ...
result = eaMainWindow.Create(
ChartID(),
ShortName, // Dialog Name ...
0, // SubWindow ...
10, // X1 ...
35, // Y1 ...
300, // X2 ...
200 // Y2 ...
);
//
// Try To Run Main Window ...
if (result)
{
//
eaMainWindow.Configure();
eaMainWindow.Run();
}
//
return result;
}
/**
* Update GUI based on Each Ticks ...
*/
void UpdateGUI()
{
//
// TODO: Implement this ...
}
/**
* Destroy all Initialized GUi Requirements ...
*/
void DestroyGUI()
{
//
eaMainWindow.Destroy();
}
//
// Others ...
/**
* Update Positions States ...
*/
void UpdatePositionsState()
{
//
// TODO: Implement this ...
}
/**
* Handle On Click Event for GUI Components ...
*
* @param name: Object Name ...
*/
void HandleOnClickEvent(string name)
{
//
if (!IsValid(name))
{
return;
}
//
bool isClicked = false;
//
// TODO: Implement Chart Objects On Click ...
//
if (isClicked)
{
UpdateGUI();
}
}
/**
* Report Account Balance ...
*/
void HandleReportBalance()
{
//
if (!IsRunningOnTestMode())
{
return;
}
//
string msg = "Balance: " + ToString(eaTrader.mAccount.GetBalance());
eaAlert.Log(msg);
}
/**
* Time Reporting based on Inputs ...
*/
void HandleReportTime()
{
//
// Monthly Report ....
if (eaReportNewMonths &&
eaTimeTracker.IsNewMonth())
{
//
string msg = "New Month ...";
//
eaAlert.Alert(msg);
}
//
// Weekly Report ....
if (eaReportNewWeeks &&
eaTimeTracker.IsNewWeek())
{
//
string msg = "New Week ...";
//
eaAlert.Alert(msg);
}
//
// Daily Report ....
if (eaReportNewDays &&
eaTimeTracker.IsNewDay())
{
//
string msg = "New Day ...";
//
eaAlert.Alert(msg);
}
//
// Hourly Report ....
if (eaReportNewHours &&
eaTimeTracker.IsNewHour())
{
//
string msg = "New Hour ...";
//
eaAlert.Alert(msg);
}
}
/**
* Initial EA Controller Conditions ...
*/
void InitControllerConditions()
{
//
// Signalling Conditions ...
eaAllowLongSignalling = eaAllowLong;
eaAllowShortSignalling = eaAllowShort;
//
eaDisableSignalling = !eaAllowLongSignalling &&
!eaAllowShortSignalling;
}
//
/**
* Configure Startegy ...
*/
void ConfigureStrategy(XCX121SMCBaseStrategy *&strategy)
{
//
if (strategy == NULL)
{
return;
}
//
// Configure Alerts ...
strategy.SetAlertPrefix(eaLogTag);
strategy.SetAlertLogAlerts(eALogAlerts);
strategy.SetAlertMailAlerts(eAMailAlerts);
strategy.SetAlertPushAlerts(eAPushAlerts);
strategy.SetAlertEnableAlerts(eAEnableAlerts);
strategy.SetAlertTerminalAlerts(eATerminalAlerts);
//
// Confiugre Signalling ...
//
strategy.AllowLong(eaAllowLongSignalling);
strategy.AllowShort(eaAllowShortSignalling);
//
if (eaDisableSignalling)
{
strategy.Disable();
}
else
{
strategy.Enable();
}
//
}
/**
* Register an Strategy in EA ...
*
* @param strategy: XCX121SMCBaseStrategy instance ...
*/
void RegisterStrategy(XCX121SMCBaseStrategy *strategy)
{
//
if (strategy == NULL)
{
return;
}
//
ArrayResize(
eaStrategies,
ArraySize(eaStrategies) + 1 //
);
//
eaStrategies[ArraySize(eaStrategies) - 1] = strategy;
}
/**
* Re Configure All Registered Strategies ...
*/
void ReConfigureAllStrategies()
{
//
int count = ArraySize(eaStrategies);
if (!IsValidSize(count))
{
return;
}
//
for (int i = 0; i < count; i++)
{
//
ConfigureStrategy(eaStrategies[i]);
}
}
/**
* Call all Registered Strategies On Tick ...
*/
void HandleStrategiesOnTick()
{
//
int count = ArraySize(eaStrategies);
if (!IsValidSize(count))
{
return;
}
//
for (int i = 0; i < count; i++)
{
//
// Call Tick Handler Function ...
eaStrategies[i].HandleTick();
}
}
//