Files
MQL5Data/Experts/x-test-strategies.ea.mq5
T
2024-08-02 18:50:12 +03:30

697 lines
16 KiB
Plaintext

///////////////////////////////////////////////////////
//
// SaherElm IT Center XTest MQL5 Expert Advisor
// -------------------------------------------------
// Name: XSTestEA
// Description: an Exper Advisor which used XTestSetup
// to Analyse Market ...
//
// Maintainer:
// ------------
// Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com)
//
//////////////////////////////////////////////////////x
//
// Global Properties ...
#property copyright "Copyright 2023, SaherElm IT Center"
#property link "https://saherelm.ir"
#property version "1.00"
#property description "SaherElm XSTestEA"
#property strict
//
#define ShortName "XSTestEA"
//
// Imports ...
#include "../Helpers/x-saherelm.xct.helper.mq5"
#include "../Helpers/x-saherelm.xcc.helper.mq5"
//
#include "../Parsers/x-saherelm.xch.parser.mq5"
#include "../Strategies/x-saherelm.xch.strategy.mq5"
//
// Inputs ...
//
// Common ...
long eaMagicNumber = 78692110; // Magic Number
int eaSlippage = 10; // Slippgae
string eaLogSuffix = ""; // Log Suffix
//
// Symbols ...
bool eaMultiSymbol = false; // Use Multi Symbol
bool eaUseMaxAllowedTradesPerSymbol = true; // Use Max Allowed Trades Per Symbol
string eaSymbols = "EURUSDb,USDJPYb"; // Trading Symbols
// string eaSymbols = "EURUSDb,USDJPYb,USDCHFb,GBPUSDb,XAUUSDb,XAGUSDb"; // Trading Symbols
//
// Trades ...
double eaVolume = 0.01; // Static Volume
double eaR2R = 1.5; // Risk to Reward Ratio for Dynamic TP SL
bool eaUseFixedTPSLPoints = true; // Use Fixed TP SL in Points
bool eaIgnoreTP = false; // Ignore TP
bool eaIgnoreSL = false; // Ignore SL
double eaTPPoint = 150; // Static TP Point
double eaSLPoint = 500; // Static SL Point
//
// Risk Management ...
//
// Risk Management > Trade Management ...
double eaAllowLong = true; // Allow Long Positions
double eaAllowShort = true; // Allow Short Positions
int eaMaxAllowedLongs = 1; // Max Allowed Long Positions
int eaMaxAllowedShorts = 1; // Max Allowed Short Positions
double eaMaxDrawdownPercentForOpenTrades = 3; // Max Drawdown Percent for Open Trades
//
// Risk Management > Hedging ...
bool eaAllowHedging = true; // Allow Hedging Trades
int eaMinTradesForHedge = 2; // Minimum Open Trades for Hedging
double eaMinimumProfitPerVolumeForHedging = 0.3; // Minimum Required Profit per Volume for Hedging
//
// Risk Management > Trailling Stop ...
bool eaAllowTrailStop = false; // Allow Trail Stop
double eaStartTrailInPoint = 100; // Trail Start After Profit (Point)
double eaTrailStepInPoint = 30; // Trail Size in each Step (Point)
bool eaIgnoreTPAfterTrail = true; // Ignore TP after Trail
//
// Risk Management > Trades Recovery ...
bool eaAllowRecovery = false; // Allow Recover Trades
double eaRecoveryTPPoint = 150; // Recovery TP (Point)
double eaRecoverySLPoint = 200; // Recovery SL (Point)
int eaMaxAllowedRecoveryTrades = 0; // Max Allowed Recovery Trades
double eaRecoveryVolumeMultiplier = 2; // Recovery Volume Multiplier
double eaMaxAllowedRecoveryTradesVolume = 0.1; // Max Allowed Recovery Volume
//
// Alert ...
bool eAEnableAlerts = true; // Enable Alerts
bool eALogAlerts = true; // Log Alerts
bool eAMailAlerts = false; // Mail Alerts
bool eAPushAlerts = false; // Push Alerts
bool eATerminalAlerts = false; // Terminal Alerts
//
// Time Report ...
bool eaAlertNewMonths = false; // Alert New Month
bool eaAlertNewWeeks = false; // Alert New Weeks
bool eaAlertNewDays = false; // Alert New Days
bool eaAlertNewHours = false; // Alert New Hours
//
// Variables ...
//
// int mNumberOfSLs = 0;
//
// Bar Style nad Time Instances ...
XSCXCTHelper *mCTHelper;
XSCXCCHelper *mCCHelper;
//
// Registred Strategies ...
XSCBaseStrategy *strategies[];
//
// Trader Instance Class ...
XSCTrade *eaTrader;
//
// Alert Class ...
XSCAlert *eaAlert;
//
// Time Tracker ...
XTimeTracker eaTimeTracker;
//
// Trade Handler Instance Class ...
XSCXTradeHandler *eaTradeHandler;
//
string mTag =
!IsValid(eaLogSuffix)
? ShortName
: ShortName + "_" + eaLogSuffix;
//
// Event Handlers ...
//
// Initialization ...
int OnInit()
{
//
if (!ValidateInputs())
{
return INIT_PARAMETERS_INCORRECT;
}
//
if (!InitialEA())
{
return INIT_FAILED;
}
//
drawPrefix = mTag;
//
// EventSetTimer(1);
// EventSetMillisecondTimer(100);
//
// Init Succeed ...
return INIT_SUCCEEDED;
}
//
// DeInitialization ...
void OnDeinit(const int reason)
{
//
// REASON_PROGRAM 0 The EA has stopped working calling the ExpertRemove() function
// REASON_REMOVE 1 Program removed from a chart
// REASON_RECOMPILE 2 Program recompiled
// REASON_CHARTCHANGE 3 A symbol or a chart period is changed
// REASON_CHARTCLOSE 4 Chart closed
// REASON_PARAMETERS 5 Inputs changed by a user
// REASON_ACCOUNT 6 Another account has been activated or reconnection to the trade server has occurred due to changes in the account settings
// REASON_TEMPLATE 7 Another chart template applied
// REASON_INITFAILED 8 The OnInit() handler returned a non-zero value
// REASON_CLOSE 9 Terminal closed
//
string msg = "MaxSameTimeTrades: " + ToString(eaTradeHandler.MaxSameTimeTrades());
eaAlert.Alert(msg);
//
// De Initialize XSampleEA Providers ...
delete mCTHelper;
delete mCCHelper;
//
delete eaAlert;
delete eaTrader;
delete eaTradeHandler;
//
// EventKillTimer();
//
Clean(strategies);
}
//
// On Tick Handler ...
void OnTick()
{
//
HandleTradeState();
HandleTimeReport();
HandleStrategiesOnTick();
}
//
// Handle On Trade Event ...
void OnTrade()
{
eaTrader.HandleOnTrade();
}
//
// On Timer ...
void OnTimer()
{
//
// HandleTradeState();
// HandleTimeReport();
// HandleStrategiesOnTick();
// //
// if (eaTimeTracker.IsNewDay())
// {
// mNumberOfSLs = 0;
// }
}
//
// Custom Functions ...
//
// Validate Inputs ...
bool ValidateInputs()
{
//
bool result = false;
//
result = true;
//
return result;
}
//
// Initialize all Requirements ...
bool InitialEA()
{
//
bool result = false;
//
// Bar Timer ...
XCTInputs ctInputs;
ctInputs.Default();
mCTHelper = new XSCXCTHelper();
result = mCTHelper.Init(
_Symbol,
_Period,
ctInputs //
);
if (!result)
{
return result;
}
//
// Bar Styles ...
XCCInputs ccInputs;
ccInputs.Default();
mCCHelper = new XSCXCCHelper();
result = mCCHelper.Init(
_Symbol,
_Period,
ccInputs //
);
if (!result)
{
return result;
}
//
// Create Instance of Alert Class ...
eaAlert = new XSCAlert();
eaAlert.SetPrefix(mTag);
eaAlert.SetLogAlerts(eALogAlerts);
eaAlert.SetMailAlerts(eAMailAlerts);
eaAlert.SetPushAlerts(eAPushAlerts);
eaAlert.SetEnableAlerts(eAEnableAlerts);
eaAlert.SetTerminalAlerts(eATerminalAlerts);
//
// Create Trader Instance ...
eaTrader = new XSCTrade(
eaSlippage,
eaMagicNumber //
);
eaTrader.AddOnStopLossEventHandler(OnStopLossTriggered);
eaTrader.AddOnTakeProfitEventHandler(OnTakeProfitTriggered);
eaTrader.AddOnForceClosePositionEventHandler(OnPositionForceClosed);
//
// Initialize Trade Handler ...
eaTradeHandler = new XSCXTradeHandler(eaTrader);
eaTradeHandler.SaveTrades(true);
eaTradeHandler.SaveSignals(true);
//
// Configure Alerts ...
eaTradeHandler.SetAlertPrefix(mTag);
eaTradeHandler.SetAlertEnableAlerts(eAEnableAlerts);
eaTradeHandler.SetAlertLogAlerts(eALogAlerts);
eaTradeHandler.SetAlertMailAlerts(eAMailAlerts);
eaTradeHandler.SetAlertPushAlerts(eAPushAlerts);
eaTradeHandler.SetAlertTerminalAlerts(eATerminalAlerts);
//
// Configure Trade Management ...
eaTradeHandler.AllowLong(eaAllowLong);
eaTradeHandler.AllowShort(eaAllowShort);
eaTradeHandler.MaxAllowedLongs(eaMaxAllowedLongs);
eaTradeHandler.MaxAllowedShorts(eaMaxAllowedShorts);
eaTradeHandler.UseMaxAllowedTradesPerSymbol(eaUseMaxAllowedTradesPerSymbol);
eaTradeHandler.MaxDrawdownPercentForOpenTrades(eaMaxDrawdownPercentForOpenTrades);
//
// Configure Hedging ...
eaTradeHandler.AllowHedging(eaAllowHedging);
eaTradeHandler.HedgeVolumeFactor(eaVolume);
eaTradeHandler.MinTradesForHedge(eaMinTradesForHedge);
eaTradeHandler.HedgeMinProfitPerVolumeFactor(eaMinimumProfitPerVolumeForHedging);
//
// Configure Trailing ...
eaTradeHandler.AllowTrailStops(eaAllowTrailStop);
eaTradeHandler.TrailBasedOnProfit(true);
eaTradeHandler.TrailStartInPoint(eaStartTrailInPoint);
eaTradeHandler.TrailStepInPoint(eaTrailStepInPoint);
eaTradeHandler.IgnoreTPOnTrail(eaIgnoreTPAfterTrail);
//
// Configure Recovery ...
eaTradeHandler.AllowRecover(eaAllowRecovery);
eaTradeHandler.ForcePressuresInRecovery(false);
eaTradeHandler.RecoveryTPPoint(eaRecoveryTPPoint);
eaTradeHandler.RecoverySLPoint(eaRecoverySLPoint);
eaTradeHandler.MaxAllowedRecover(eaMaxAllowedRecoveryTrades);
eaTradeHandler.RecoveryVolumeMultiplier(eaRecoveryVolumeMultiplier);
eaTradeHandler.MaxAllowedRecoveryVolume(eaMaxAllowedRecoveryTradesVolume);
//
// Single Symbol ...
if (!eaMultiSymbol)
{
//
// Create Class Instance ...
XSCXCHStrategy *xchStrategy;
xchStrategy = new XSCXCHStrategy(
_Symbol,
_Period,
PERIOD_H1, // Analyzing Period
eaVolume,
eaR2R, // R2R ...
eaSlippage,
eaMagicNumber,
true, // Ignore Signal Execution
eaUseFixedTPSLPoints, // Use TP SL Point
false, // Use Max
eaTPPoint, // TP
eaSLPoint, // SL
eaIgnoreTP,
eaIgnoreSL,
eaAllowLong,
eaAllowShort,
eaMaxAllowedLongs, // Max Longs
eaMaxAllowedShorts // Max Shorts
);
//
// Configuere Strategy ...
xchStrategy.EnableXCHM(true);
xchStrategy.EnableXCHHK(true);
xchStrategy.EnableXCHSTR(true);
xchStrategy.EnableXCHCHE(true);
xchStrategy.DrawImportantAreas(false);
xchStrategy.AddXCHSignalEventHandler(OnSignalRecieved);
//
// Configure Alerts ...
xchStrategy.SetAlertPrefix(mTag);
xchStrategy.SetAlertEnableAlerts(eAEnableAlerts);
xchStrategy.SetAlertLogAlerts(eALogAlerts);
xchStrategy.SetAlertMailAlerts(eAMailAlerts);
xchStrategy.SetAlertPushAlerts(eAPushAlerts);
xchStrategy.SetAlertTerminalAlerts(eATerminalAlerts);
//
// Register Strategy ...
RegisterStrategy(xchStrategy);
}
//
// 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];
//
// Create Class Instance ...
XSCXCHStrategy *iStrategy;
iStrategy = new XSCXCHStrategy(
iSymbol,
_Period,
PERIOD_H1, // Analyzing Period
eaVolume,
eaR2R, // R2R ...
eaSlippage,
eaMagicNumber,
true, // Ignore Signal Execution
true, // Use TP SL Point
false, // Use Max
eaTPPoint, // TP
eaSLPoint, // SL
eaIgnoreTP,
eaIgnoreSL,
eaAllowLong,
eaAllowShort,
eaMaxAllowedLongs, // Max Longs
eaMaxAllowedShorts // Max Shorts
);
//
// Configuere Strategy ...
iStrategy.EnableXCHM(true);
iStrategy.EnableXCHHK(true);
iStrategy.EnableXCHSTR(true);
iStrategy.EnableXCHCHE(true);
iStrategy.DrawImportantAreas(false);
iStrategy.AddXCHSignalEventHandler(OnSignalRecieved);
//
// Configure Alerts ...
iStrategy.SetAlertPrefix(mTag);
iStrategy.SetAlertEnableAlerts(eAEnableAlerts);
iStrategy.SetAlertLogAlerts(eALogAlerts);
iStrategy.SetAlertMailAlerts(eAMailAlerts);
iStrategy.SetAlertPushAlerts(eAPushAlerts);
iStrategy.SetAlertTerminalAlerts(eATerminalAlerts);
//
// Register Strategy ...
RegisterStrategy(iStrategy);
}
}
//
return result;
}
//
// Register Strategy ...
void RegisterStrategy(XSCBaseStrategy *strategy)
{
//
if (strategy == NULL)
{
return;
}
//
ArrayResize(
strategies,
ArraySize(strategies) + 1 //
);
//
strategies[ArraySize(strategies) - 1] = strategy;
}
//
// Call All Registered Strategis Tick Handler Functions ...
void HandleStrategiesOnTick()
{
//
int count = ArraySize(strategies);
if (!IsValidSize(count))
{
return;
}
//
for (int i = 0; i < count; i++)
{
//
// Call Tick Handler Function ...
strategies[i].HandleTick();
}
}
//
// Manage Trading States ...
void HandleTradeState()
{
//
// Implement All Trade States here ...
eaTradeHandler.UpdateData();
//
// Do Protection Senarios ...
eaTradeHandler.HandleProtection();
}
//
// Time Reporting ...
void HandleTimeReport()
{
//
// Monthly Report ....
if (eaAlertNewMonths &&
eaTimeTracker.IsNewMonth())
{
//
string msg = "New Month ...";
//
eaAlert.Alert(msg);
}
//
// Weekly Report ....
if (eaAlertNewWeeks &&
eaTimeTracker.IsNewWeek())
{
//
string msg = "New Week ...";
//
eaAlert.Alert(msg);
}
//
// Daily Report ....
if (eaAlertNewDays &&
eaTimeTracker.IsNewDay())
{
//
string msg = "New Day ...";
//
eaAlert.Alert(msg);
}
//
// Hourly Report ....
if (eaAlertNewHours &&
eaTimeTracker.IsNewHour())
{
//
string msg = "New Hour ...";
//
eaAlert.Alert(msg);
}
}
//
// All Signals from Strategies Comming Here for Execution or
// Analyzing ...
void OnSignalRecieved(
XSignal &signal,
XCHStrategyConditions &conditions //
)
{
//
// Check Signal and Conditions Validations ...
//
if (!signal.IsValid())
{
return;
}
//
if (!conditions.IsValid())
{
return;
}
//
XCHStartegyConditionsParser parser;
bool isFiltered = parser.IsFiltered(
signal,
conditions //
);
if (isFiltered)
{
return;
}
//
// Attach Conditions Summary to Signal ...
string conditionsSummary = conditions.GenerateSummary(
false,
false,
true,
false //
);
//
signal.conditions = parser.GenerateSignalConditions(conditions); // conditionsSummary;
//
// Execute Signal Using Trade Handler ...
ENUM_X_SIGNAL_EXECUTION_RESULT state;
bool isExecuted = eaTradeHandler.ExecuteSignal(
signal,
state //
);
}
//
// Trader Event Handlers for Trade Handling ...
//
void OnStopLossTriggered(const XDeal &deal)
{
//
// Finish ...
eaTradeHandler.Finish(deal);
// //
// if (deal.profit < 0)
// {
// mNumberOfSLs++;
// }
}
//
void OnTakeProfitTriggered(const XDeal &deal)
{
//
// Finish ...
eaTradeHandler.Finish(deal);
}
//
void OnPositionForceClosed(
const ulong ticket,
const XPosition &position,
const string comment //
)
{
//
// Finish ...
eaTradeHandler.Finish(
ticket,
position,
comment //
);
}
//