1371 lines
29 KiB
Plaintext
1371 lines
29 KiB
Plaintext
////////////////////////////////////////////////////////////
|
|
//
|
|
// SaherElm IT Center MQL5 XAU5 Signal Provider Library
|
|
// --------------------------------------------------------
|
|
// Name: XAU5SignalProvider
|
|
// Description: XAU5 based signal provider ...
|
|
//
|
|
//
|
|
// Maintainer:
|
|
// ------------
|
|
// Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com)
|
|
//
|
|
//////////////////////////////////////////////////////
|
|
//
|
|
// Global Properties ...
|
|
#property library
|
|
#property copyright "Copyright 2023, SaherElm IT Center"
|
|
#property link "https://www.saherelm.ir"
|
|
#property version "1.00"
|
|
#property strict
|
|
|
|
#define XAU5ProviderName "XAU5"
|
|
|
|
//
|
|
// START Inputs ...
|
|
//
|
|
|
|
#include "x-saherelm.xau5.provider.inputs.lib.mq5";
|
|
|
|
//
|
|
// END Inputs ...
|
|
//
|
|
|
|
//
|
|
// Include Common Library ...
|
|
#include "../Libraries/x-saherelm.common.lib.mq5"
|
|
|
|
//
|
|
// Include Logger Library ...
|
|
#include "../Libraries/x-saherelm.log.lib.mq5"
|
|
|
|
//
|
|
// Include Alert Library ...
|
|
#include "../Libraries/x-saherelm.alert.lib.mq5"
|
|
|
|
//
|
|
// Include Draw Library ...
|
|
#include "../Libraries/x-saherelm.draw.lib.mq5"
|
|
|
|
//
|
|
// Include Class Libraries ...
|
|
#include "../Classes/x-saherelm.class.lib.mq5"
|
|
|
|
//
|
|
// START Global Definitions: Variables, Properties and etc ...
|
|
//
|
|
|
|
//
|
|
XCGuard *xAU5Guard;
|
|
XCTrade *xAU5Trader;
|
|
XCReporter *xAU5Reporter;
|
|
XCCollector *xAU5Collector;
|
|
XCAccountInfo *xAU5AccountInfo;
|
|
XCVolumeManager *xAU5VolumeManager;
|
|
|
|
//
|
|
#include "x-saherelm.xau5.provider.indicators.lib.mq5"
|
|
|
|
//
|
|
// Controlling Variables ...
|
|
bool xAU5MEnableProvider = false;
|
|
bool xAU5MAllowLongTrades = false;
|
|
bool xAU5MAllowShortTrades = false;
|
|
|
|
//
|
|
double xAU5Deposit = 0;
|
|
double xAU5Balance = 0;
|
|
double xAU5MinReward = 0;
|
|
double xAU5FreeMargin = 0;
|
|
double xAU5BalanceForOpenTrades = 0;
|
|
double xAU5FreeMarginForOpenTrades = 0;
|
|
|
|
//
|
|
// Guard ...
|
|
double xAU5CriticalDrawDown = 0;
|
|
double xAU5MaxAllowedDrawDown = 0;
|
|
|
|
//
|
|
double xAU5MaxDrawUp = 0;
|
|
double xAU5MaxDrawDown = 0;
|
|
|
|
//
|
|
int NOT_ENOUGH_MONEY_ERROR = 4756;
|
|
const string XAU5_SUPPORT_ID = "SP";
|
|
|
|
//
|
|
// END Global Definitions: Variables, Properties and etc ...
|
|
//
|
|
|
|
//
|
|
// START Provided Functions ...
|
|
//
|
|
|
|
//
|
|
// Initial Library if required ...
|
|
bool XAU5InitSignalProviderLibrary()
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
if (!xAU5EnableProvider)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Controlling Variables ...
|
|
xAU5MEnableProvider = xAU5EnableProvider;
|
|
xAU5MAllowLongTrades = xAU5AllowLongTrades;
|
|
xAU5MAllowShortTrades = xAU5AllowShortTrades;
|
|
|
|
//
|
|
// Validate Inputs ...
|
|
result = XAU5ValidateInputs();
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Init Indicators ...
|
|
result = XAU5InitIndicators();
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
xAU5Reporter = new XCReporter();
|
|
xAU5Collector = new XCCollector();
|
|
xAU5AccountInfo = new XCAccountInfo();
|
|
|
|
//
|
|
// Calculate Account Deposit ...
|
|
xAU5Deposit = xAU5AccountInfo.GetInitialBalance();
|
|
|
|
//
|
|
// Prepare Min Reward ...
|
|
if (xAU5MinRewardInPips > 0)
|
|
{
|
|
xAU5MinReward = PipsToPrice(xAU5MinRewardInPips);
|
|
}
|
|
|
|
//
|
|
// Calculate Min Balance for Trades ...
|
|
if (xAU5BalanceFactorForOpenTrades > 0)
|
|
{
|
|
xAU5BalanceForOpenTrades = xAU5BalanceFactorForOpenTrades * xAU5Deposit;
|
|
}
|
|
|
|
//
|
|
// Claculate Min Free Marging for Trades ...
|
|
if (xAU5FreeMarginFactorForOpenTrades > 0)
|
|
{
|
|
xAU5FreeMarginForOpenTrades = xAU5FreeMarginFactorForOpenTrades * xAU5Deposit;
|
|
}
|
|
|
|
//
|
|
// Calculate Init Time Critical DrawDown Value ...
|
|
if (xAU5CriticalDrawDownFactor > 0)
|
|
{
|
|
xAU5CriticalDrawDown = xAU5CriticalDrawDownFactor * xAU5Deposit;
|
|
}
|
|
|
|
//
|
|
// Calculate Init Time Max Allowed DrawDown Value ...
|
|
if (xAU5MaxAllowedDrawDownFactor > 0)
|
|
{
|
|
xAU5MaxAllowedDrawDown = xAU5MaxAllowedDrawDownFactor * xAU5Deposit;
|
|
}
|
|
|
|
//
|
|
// Instant Volume Manager ...
|
|
xAU5VolumeManager = new XCVolumeManager(
|
|
xAU5StaticVolumeFactor,
|
|
xAU5StaticVolumeMethod,
|
|
xAU5VolumeMultiplier,
|
|
xAU5ApplyVolumeMultiplierFactor,
|
|
xAU5MaxSupportedVolumePerTradeFactor,
|
|
xAU5IncreasedMaxSupportedVolumeRate);
|
|
|
|
//
|
|
// Make XCTrader instance ...
|
|
xAU5Trader = new XCTrade(
|
|
XAU5ProviderName,
|
|
_Symbol,
|
|
xAU5Slippage,
|
|
xAU5MagicNumber,
|
|
xAU5UseVirtualTPSl,
|
|
xAU5MaxAllowedTrades,
|
|
xAU5FreeMarginForOpenTrades,
|
|
xAU5BalanceForOpenTrades);
|
|
|
|
//
|
|
// Instantiate Account Guard ...
|
|
xAU5Guard = new XCGuard(
|
|
//
|
|
// Old Trades ...
|
|
xAU5MaxAllowedTradeAge,
|
|
xAU5OldTradesAction,
|
|
//
|
|
// Critical DrawDown ...
|
|
xAU5CriticalDrawDownFactor,
|
|
xAU5CriticalGuardAction,
|
|
//
|
|
// Max Allowed DrawDown ...
|
|
xAU5MaxAllowedDrawDownFactor,
|
|
xAU5MaxAllowedGuardAction,
|
|
xAU5GuardStopoutFiredAfterTouches,
|
|
//
|
|
// xAU5GuardSupportMethod
|
|
X_SUPPORT_DO_NOTHING
|
|
//
|
|
);
|
|
|
|
//
|
|
// Reset Calculations Buffers and Variables ...
|
|
XAU5ResetCollectors();
|
|
|
|
//
|
|
XAU5UpdateReports();
|
|
|
|
//
|
|
result = true;
|
|
|
|
//
|
|
// Logging State ...
|
|
XAU5IssueInitializationSucceedAlert();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// DeInitial Library if required ...
|
|
void XAU5DeinitSignalProviderLibrary(const int reason)
|
|
{
|
|
//
|
|
// Release Handlers ...
|
|
XAU5ReleaseIndicators();
|
|
|
|
//
|
|
// Logging State ...
|
|
XAU5IssueDeinitializationSucceedAlert();
|
|
}
|
|
|
|
//
|
|
// this is a Globally Function which do all of
|
|
// checkings and positions handling ...
|
|
void XAU5SignalProviderHandleTick()
|
|
{
|
|
//
|
|
// Check Provider is Enable or Not ...
|
|
if (!xAU5MEnableProvider)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
XAU5SignalProviderDoControlState();
|
|
|
|
//
|
|
// Reading Indicator Buffers ...
|
|
XAU5HandleReadingBuffers();
|
|
if (xAU5CalculatedBars < xAU5MaxLengthOfInputs)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Handle Open Trades ...
|
|
XAU5HandleOpenTrades();
|
|
|
|
//
|
|
// Handle Close Trades ...
|
|
XAU5HandleCloseTrades();
|
|
|
|
//
|
|
// DrawDown and DrawUp ...
|
|
XAU5HandleCalculateDrawDownUp();
|
|
}
|
|
|
|
//
|
|
// Do Control Checks here ...
|
|
void XAU5SignalProviderDoControlState()
|
|
{
|
|
//
|
|
// Update account Balance ...
|
|
xAU5Balance = xAU5AccountInfo.GetBalance();
|
|
xAU5FreeMargin = xAU5AccountInfo.GetFreeMargin();
|
|
|
|
//
|
|
// Check Provider is Enable or Not ...
|
|
if (!xAU5MEnableProvider)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Protect Account by Guarding Maximum Drawdown ...
|
|
// TODO: add Guard Result here ...
|
|
string guardMessage = "";
|
|
XGuardHandlerResult guardHandlerResult;
|
|
bool isGuarded = xAU5Guard.Guard(
|
|
guardHandlerResult,
|
|
guardMessage,
|
|
xAU5Trader,
|
|
_Period);
|
|
if (isGuarded)
|
|
{
|
|
//
|
|
// Criticals ...
|
|
int criticalsCount = ArraySize(guardHandlerResult.criticals);
|
|
if (criticalsCount > 0)
|
|
{
|
|
//
|
|
for (int i = 0; i < criticalsCount; i++)
|
|
{
|
|
//
|
|
XSignal iSignal = guardHandlerResult.criticals[i];
|
|
|
|
//
|
|
XAU5OnGuard(
|
|
iSignal,
|
|
X_GUARDED_CRITICAL,
|
|
guardMessage);
|
|
}
|
|
}
|
|
|
|
//
|
|
// Maxes ...
|
|
int maxesCount = ArraySize(guardHandlerResult.maxes);
|
|
if (maxesCount > 0)
|
|
{
|
|
//
|
|
for (int i = 0; i < maxesCount; i++)
|
|
{
|
|
//
|
|
XSignal iSignal = guardHandlerResult.maxes[i];
|
|
|
|
//
|
|
XAU5OnGuard(
|
|
iSignal,
|
|
X_GUARDED_MAX,
|
|
guardMessage,
|
|
guardHandlerResult.isStopOut);
|
|
}
|
|
}
|
|
|
|
//
|
|
// Olds ...
|
|
int oldsCount = ArraySize(guardHandlerResult.olds);
|
|
if (oldsCount > 0)
|
|
{
|
|
//
|
|
for (int i = 0; i < oldsCount; i++)
|
|
{
|
|
//
|
|
XSignal iSignal = guardHandlerResult.olds[i];
|
|
|
|
//
|
|
XAU5OnGuard(
|
|
iSignal,
|
|
X_GUARDED_OLD,
|
|
guardMessage);
|
|
}
|
|
}
|
|
}
|
|
|
|
//
|
|
// Signal Protection ...
|
|
XSignal protectSupports[];
|
|
bool isSupported = xAU5Guard.ProtectSignals(
|
|
protectSupports,
|
|
xAU5Trader);
|
|
if (isSupported)
|
|
{
|
|
//
|
|
LogMessage("Guard do Support Trdae ...");
|
|
}
|
|
|
|
//
|
|
// Handling Signals ...
|
|
XTraderHandlerResult signalHandlerResult;
|
|
xAU5Trader.HandleSignals(
|
|
signalHandlerResult,
|
|
false // Ignore Risk Free Trdaes
|
|
);
|
|
|
|
//
|
|
// Handle Sl Reports ...
|
|
int handledSLsCount = ArraySize(signalHandlerResult.sl);
|
|
if (handledSLsCount > 0)
|
|
{
|
|
//
|
|
for (int i = 0; i < handledSLsCount; i++)
|
|
{
|
|
//
|
|
XSignal iSignal = signalHandlerResult.sl[i];
|
|
XAU5OnSL(iSignal);
|
|
}
|
|
}
|
|
|
|
//
|
|
// Handle TP Reports ...
|
|
int handledTPsCount = ArraySize(signalHandlerResult.tp);
|
|
if (handledTPsCount > 0)
|
|
{
|
|
//
|
|
for (int i = 0; i < handledTPsCount; i++)
|
|
{
|
|
//
|
|
XSignal iSignal = signalHandlerResult.tp[i];
|
|
XAU5OnTP(iSignal);
|
|
}
|
|
}
|
|
|
|
//
|
|
// Handle RFs as TP Reports ...
|
|
int handledRFsCount = ArraySize(signalHandlerResult.rf);
|
|
if (handledRFsCount > 0)
|
|
{
|
|
//
|
|
for (int i = 0; i < handledRFsCount; i++)
|
|
{
|
|
//
|
|
XSignal iSignal = signalHandlerResult.rf[i];
|
|
XAU5OnRF(iSignal);
|
|
}
|
|
}
|
|
}
|
|
|
|
//
|
|
// Handle Trades when OnTrade Event Hnadler Calls ...
|
|
void XAU5SignalProviderHandleTrade()
|
|
{
|
|
//
|
|
// Check Provider Enabled ...
|
|
if (!xAU5MEnableProvider)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Handle OnTrade Event ...
|
|
XOnTradeHandlerResult result = {};
|
|
xAU5Trader.HandleOnTradeEvent(
|
|
result);
|
|
|
|
//
|
|
// Detected TP or SL ...
|
|
if (result.hasNewDeal)
|
|
{
|
|
//
|
|
XDeal last = xAU5Trader.GetLastDeal();
|
|
|
|
//
|
|
if (last.reason == DEAL_REASON_SL)
|
|
{
|
|
xAU5Reporter.AddTradeSLInReports();
|
|
}
|
|
else if (last.reason == DEAL_REASON_TP)
|
|
{
|
|
xAU5Reporter.AddTradeTPInReports();
|
|
}
|
|
}
|
|
}
|
|
|
|
//
|
|
// Handle Open Trades ...
|
|
void XAU5HandleOpenTrades()
|
|
{
|
|
//
|
|
// Check Can Trade ...
|
|
bool canTrade = xAU5MAllowLongTrades || xAU5MAllowShortTrades;
|
|
if (!canTrade)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Check Max Open Trades ...
|
|
int openSignals = xAU5Trader.Count();
|
|
bool canOpenPositions = openSignals < xAU5MaxAllowedTrades;
|
|
if (!canOpenPositions)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Check Signals Exists ...
|
|
XProvidedSignal longSignalInfo;
|
|
XProvidedSignal shortSignalInfo;
|
|
bool hasLongSignal = XAU5CanOpenLongTrade(longSignalInfo);
|
|
bool hasShortSignal = XAU5CanOpenShortTrade(shortSignalInfo);
|
|
if (!hasLongSignal && !hasShortSignal)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Update Reports ...
|
|
XAU5UpdateReports();
|
|
|
|
//
|
|
// Open Long Trades ...
|
|
if (
|
|
//
|
|
hasLongSignal &&
|
|
xAU5MAllowLongTrades)
|
|
{
|
|
//
|
|
bool isOpened = XAU5OpenLongPosition(longSignalInfo);
|
|
}
|
|
|
|
//
|
|
// Open Short Trades ...
|
|
if (
|
|
//
|
|
hasShortSignal &&
|
|
xAU5MAllowShortTrades)
|
|
{
|
|
//
|
|
bool isOpened = XAU5OpenShortPosition(shortSignalInfo);
|
|
}
|
|
}
|
|
|
|
//
|
|
// Handle Close Trades ...
|
|
void XAU5HandleCloseTrades()
|
|
{
|
|
//
|
|
// Force Close Long Trades ...
|
|
bool isClosedLongTrades = false;
|
|
bool canCloseLongTrades = XAU5CanCloseLongTrade();
|
|
if (xAU5MAllowLongTrades && canCloseLongTrades)
|
|
{
|
|
//
|
|
ENUM_X_SIGNAL_TYPE type = X_SIGNAL_LONG;
|
|
|
|
//
|
|
XSignal closed[];
|
|
xAU5Trader.CloseSignals(
|
|
closed,
|
|
type);
|
|
|
|
//
|
|
int closedTradesCount = ArraySize(closed);
|
|
isClosedLongTrades = closedTradesCount > 0;
|
|
if (isClosedLongTrades)
|
|
{
|
|
//
|
|
for (int i = 0; i < closedTradesCount; i++)
|
|
{
|
|
//
|
|
XSignal iSignal = closed[i];
|
|
XAU5OnFC(iSignal);
|
|
}
|
|
}
|
|
}
|
|
|
|
//
|
|
// Force Close Short Trades ...
|
|
bool isClosedShortTrades = false;
|
|
bool canCloseShortTrades = XAU5CanCloseShortTrade();
|
|
if (xAU5MAllowShortTrades && canCloseShortTrades)
|
|
{
|
|
//
|
|
ENUM_X_SIGNAL_TYPE type = X_SIGNAL_SHORT;
|
|
|
|
//
|
|
XSignal closed[];
|
|
xAU5Trader.CloseSignals(
|
|
closed,
|
|
type);
|
|
|
|
//
|
|
int closedTradesCount = ArraySize(closed);
|
|
isClosedShortTrades = closedTradesCount > 0;
|
|
if (isClosedShortTrades)
|
|
{
|
|
//
|
|
for (int i = 0; i < closedTradesCount; i++)
|
|
{
|
|
//
|
|
XSignal iSignal = closed[i];
|
|
XAU5OnFC(iSignal);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//
|
|
// Open Long Position ...
|
|
bool XAU5OpenLongPosition(
|
|
XProvidedSignal &info // Retrieve Signal Additional Info on this Object
|
|
)
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
// Calculate Required Data for Position ...
|
|
double entry = GetEntry(_Symbol, X_SIGNAL_LONG);
|
|
double sl = info.sl > 0 ? info.sl : 0;
|
|
// entry - (200 * _Point);
|
|
// info.sl > 0 ? info.sl : 0;
|
|
double tp = info.tp > 0 ? info.tp : entry + xAU5MinReward;
|
|
double riskFreeRate = xAU5RiskFreeRate;
|
|
double riskFreeStep = xAU5RiskFreeStepInPips;
|
|
double volume = XAU5CalculateVolume(ArraySize(info.signallers));
|
|
|
|
//
|
|
XSignal signal = {};
|
|
signal = xAU5Trader.GenerateSignal(
|
|
X_SIGNAL_LONG,
|
|
tp,
|
|
sl,
|
|
volume,
|
|
riskFreeStep,
|
|
riskFreeRate);
|
|
bool isValidSignal = IsValid(
|
|
signal,
|
|
xAU5MagicNumber);
|
|
if (!isValidSignal)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
int signalsCount = xAU5Trader.Count();
|
|
bool isCross = signalsCount >= 1;
|
|
if (isCross)
|
|
{
|
|
//
|
|
// Handle Cross Signals ...
|
|
XAU5HnadleCrossSignal(signal);
|
|
}
|
|
|
|
//
|
|
// Retrieve Can Execute Signal or not ...
|
|
bool canExecuteSignal =
|
|
!isCross
|
|
//
|
|
||
|
|
//
|
|
(
|
|
//
|
|
isCross &&
|
|
xAU5CrossSignalsMethod != X_CROSS_SIGNAL_DO_NOTHING
|
|
//
|
|
);
|
|
if (!canExecuteSignal)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Execute Signal ...
|
|
ENUM_EXECUTION_SIGNAL_RESULT executionResult = xAU5Trader.ExecuteSignal(
|
|
signal,
|
|
false, // Ignore Number of Trades ...
|
|
true // Check Account Conditions ...
|
|
);
|
|
result = executionResult == X_TRADER_SUCCEED_EXECUTION;
|
|
if (result)
|
|
{
|
|
//
|
|
// Update Reports ...
|
|
string providers[];
|
|
ArrayCopy(
|
|
providers,
|
|
info.signallers
|
|
);
|
|
|
|
//
|
|
XSignal tSignal = xAU5Trader.GetSignal(signal.ticket);
|
|
|
|
//
|
|
XAU5OnSignal(
|
|
tSignal,
|
|
providers);
|
|
}
|
|
else
|
|
{
|
|
XAU5IssueSignalExecutionError(executionResult);
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Open Short Position ...
|
|
bool XAU5OpenShortPosition(
|
|
XProvidedSignal &info // Retrieve Signal Additional Info on this Object
|
|
)
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
// Calculate Required Data for Position ...
|
|
double entry = GetEntry(_Symbol, X_SIGNAL_SHORT);
|
|
double sl = info.sl > 0 ? info.sl : 0;
|
|
// entry + (200 * _Point);
|
|
// info.sl > 0 ? info.sl : 0;
|
|
double tp = info.tp > 0 ? info.tp : entry - xAU5MinReward;
|
|
double riskFreeRate = xAU5RiskFreeRate;
|
|
double riskFreeStep = xAU5RiskFreeStepInPips;
|
|
double volume = XAU5CalculateVolume(ArraySize(info.signallers));
|
|
|
|
//
|
|
XSignal signal = {};
|
|
signal = xAU5Trader.GenerateSignal(
|
|
X_SIGNAL_SHORT,
|
|
tp,
|
|
sl,
|
|
volume,
|
|
riskFreeStep,
|
|
riskFreeRate);
|
|
bool isValidSignal = IsValid(
|
|
signal,
|
|
xAU5MagicNumber);
|
|
if (!isValidSignal)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
int signalsCount = xAU5Trader.Count();
|
|
bool isCross = signalsCount >= 1;
|
|
if (isCross)
|
|
{
|
|
//
|
|
// Handle Cross Signals ...
|
|
XAU5HnadleCrossSignal(signal);
|
|
}
|
|
|
|
//
|
|
// Retrieve Can Execute Signal or not ...
|
|
bool canExecuteSignal =
|
|
!isCross
|
|
//
|
|
||
|
|
//
|
|
(
|
|
//
|
|
isCross &&
|
|
xAU5CrossSignalsMethod != X_CROSS_SIGNAL_DO_NOTHING
|
|
//
|
|
);
|
|
if (!canExecuteSignal)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Execute Signal ...
|
|
ENUM_EXECUTION_SIGNAL_RESULT executionResult = xAU5Trader.ExecuteSignal(
|
|
signal,
|
|
false, // Ignore Number of Trades ...
|
|
true // Check Account Conditions ...
|
|
);
|
|
result = executionResult == X_TRADER_SUCCEED_EXECUTION;
|
|
if (result)
|
|
{
|
|
string providers[];
|
|
ArrayCopy(
|
|
providers,
|
|
info.signallers
|
|
);
|
|
|
|
//
|
|
XSignal tSignal = xAU5Trader.GetSignal(signal.ticket);
|
|
|
|
//
|
|
XAU5OnSignal(
|
|
tSignal,
|
|
providers);
|
|
}
|
|
else
|
|
{
|
|
XAU5IssueSignalExecutionError(executionResult);
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Apply Cross Signal Actions ...
|
|
void XAU5HnadleCrossSignal(
|
|
XSignal &signal // Specified Signal ...
|
|
)
|
|
{
|
|
//
|
|
// Check Cross Signal Handling Enabled ...
|
|
if (xAU5CrossSignalsMethod == X_CROSS_SIGNAL_DO_NOTHING)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
// First We have to Change Cross Signals Comment ...
|
|
string comment = xAU5Trader.GenerateSignalComment(
|
|
signal,
|
|
true);
|
|
|
|
//
|
|
signal.comment = comment;
|
|
|
|
//
|
|
// If Every thing is Regular, prevent any changes here ...
|
|
if (xAU5CrossSignalsMethod == X_CROSS_SIGNAL_REGULAR)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Retrieve Max in Drawdown Signal ...
|
|
XSignal maxInDDSignal = xAU5Trader.GetMaxInDrawdownSignal();
|
|
|
|
//
|
|
// Validate Signal ...
|
|
bool isValidSignal =
|
|
maxInDDSignal.ticket > 0 &&
|
|
maxInDDSignal.symbol == signal.symbol &&
|
|
IsValid(maxInDDSignal, xAU5MagicNumber);
|
|
if (!isValidSignal)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
double volume = signal.volume;
|
|
bool isDirectional = maxInDDSignal.type == signal.type;
|
|
|
|
//
|
|
// Prevent any changes on Regular Conditions ...
|
|
bool isRegular =
|
|
isDirectional
|
|
? (
|
|
//
|
|
xAU5CrossSignalsMethod == X_CROSS_SIGNAL_DIRECTION_REGULAR ||
|
|
xAU5CrossSignalsMethod == X_CROSS_SIGNAL_DIRECTION_REGULAR_INDIRECTION_IMPORTANT
|
|
//
|
|
)
|
|
: (
|
|
//
|
|
xAU5CrossSignalsMethod == X_CROSS_SIGNAL_INDIRECTION_REGULAR ||
|
|
xAU5CrossSignalsMethod == X_CROSS_SIGNAL_DIRECTION_IMPORTANT_INDIRECTION_REGULAR
|
|
//
|
|
);
|
|
if (isRegular)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Important Act needs to Multiply volume by 2 ...
|
|
volume *= 2;
|
|
|
|
//
|
|
// Set Volume of signal ...
|
|
signal.volume = volume;
|
|
}
|
|
|
|
//
|
|
// Calculate Usefull data ...
|
|
void XAU5HandleCalculateDrawDownUp()
|
|
{
|
|
//
|
|
XSignal signals[];
|
|
xAU5Trader.GetSignals(signals);
|
|
int signalsCount = ArraySize(signals);
|
|
if (signalsCount <= 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
double profit = 0;
|
|
double volume = 0;
|
|
XCollector collector = {};
|
|
double maxAllowedVolume = xAU5VolumeManager.GetMaxAllowedVolume();
|
|
for (int i = 0; i < signalsCount; i++)
|
|
{
|
|
//
|
|
XSignal iSignal = signals[i];
|
|
|
|
//
|
|
profit += iSignal.profit;
|
|
|
|
//
|
|
volume =
|
|
volume == 0 ||
|
|
volume < iSignal.volume
|
|
? iSignal.volume
|
|
: volume;
|
|
}
|
|
|
|
//
|
|
if (profit < 0)
|
|
{
|
|
//
|
|
xAU5MaxDrawDown =
|
|
xAU5MaxDrawDown == 0 ||
|
|
xAU5MaxDrawDown > MathAbs(profit)
|
|
? MathAbs(profit)
|
|
: xAU5MaxDrawDown;
|
|
|
|
//
|
|
// Update Buffers ...
|
|
datetime time = iTime(_Symbol, _Period, 0);
|
|
|
|
//
|
|
// Calculate Max Allowed Drawdown ...
|
|
//
|
|
collector.time = time;
|
|
collector.volume = volume;
|
|
collector.maxDrawDown = xAU5MaxDrawDown;
|
|
collector.maxAllowedVolume = maxAllowedVolume;
|
|
collector.minBalanceForOpenTrade = xAU5Trader.GetMinBalanceForOpenTrades();
|
|
}
|
|
else
|
|
{
|
|
//
|
|
xAU5MaxDrawUp =
|
|
xAU5MaxDrawUp == 0 ||
|
|
xAU5MaxDrawUp < profit
|
|
? profit
|
|
: xAU5MaxDrawUp;
|
|
}
|
|
|
|
//
|
|
// Update Collector ...
|
|
xAU5Collector.Add(collector);
|
|
|
|
//
|
|
// Update Reports ...
|
|
xAU5Reporter.UpdateMaxDrawUpInReports(xAU5MaxDrawUp);
|
|
xAU5Reporter.UpdateMaxDrawDownInReports(xAU5MaxDrawDown);
|
|
xAU5Reporter.UpdateMaxSameTimeTradesInReports(signalsCount);
|
|
}
|
|
|
|
//
|
|
// START Event Handlers ...
|
|
//
|
|
|
|
//
|
|
// Runs When a Signal Successfully Executed ...
|
|
void XAU5OnSignal(
|
|
XSignal &signal, // Specified Signal
|
|
string &providers[] // Signal Providers
|
|
)
|
|
{
|
|
//
|
|
// Issue Propper Alert ...
|
|
XAU5IssueSignalExecutionAlert(
|
|
signal,
|
|
providers);
|
|
|
|
//
|
|
// Add Signal To Supports on Guard ...
|
|
xAU5Guard.AddSupport(signal);
|
|
|
|
//
|
|
// Update Reports ...
|
|
xAU5Reporter.AddSignalToReports(
|
|
signal,
|
|
providers);
|
|
}
|
|
|
|
//
|
|
// Runs when a Position Got Take Profit ...
|
|
void XAU5OnTP(
|
|
XSignal &signal // Specified Signal
|
|
)
|
|
{
|
|
//
|
|
// Issue Propper Alert ...
|
|
XAU5IssueTPAlert(signal);
|
|
|
|
//
|
|
// Remove Signal From Guard Protector ...
|
|
xAU5Guard.Remove(signal.ticket);
|
|
xAU5Guard.RemoveSupport(signal.ticket);
|
|
|
|
//
|
|
// Update Reports ...
|
|
xAU5Reporter.AddTradeTPInReports();
|
|
XAU5UpdateReports();
|
|
}
|
|
|
|
//
|
|
// Runs when a Position Got Stop Loss ...
|
|
void XAU5OnSL(
|
|
XSignal &signal // Specified Signal
|
|
)
|
|
{
|
|
//
|
|
// Issue Propper Alert ...
|
|
XAU5IssueSLAlert(signal);
|
|
|
|
//
|
|
// Remove Signal From Guard Protector ...
|
|
xAU5Guard.Remove(signal.ticket);
|
|
xAU5Guard.RemoveSupport(signal.ticket);
|
|
|
|
//
|
|
// Update Reports ...
|
|
xAU5Reporter.AddTradeSLInReports();
|
|
XAU5UpdateReports();
|
|
}
|
|
|
|
//
|
|
// Runs when a Position Risk Free ...
|
|
void XAU5OnRF(
|
|
XSignal &signal // Specified Signal
|
|
)
|
|
{
|
|
//
|
|
// Issue Propper Alert ...
|
|
XAU5IssueRFAlert(signal);
|
|
|
|
//
|
|
// Remove Signal From Guard Protector ...
|
|
// since RiskFree Signals has SL ...
|
|
xAU5Guard.Remove(signal.ticket);
|
|
xAU5Guard.RemoveSupport(signal.ticket);
|
|
|
|
//
|
|
// Update Reports ...
|
|
xAU5Reporter.AddTradeSLInReports();
|
|
XAU5UpdateReports();
|
|
}
|
|
|
|
//
|
|
// Runs when a Position Force Closed ...
|
|
void XAU5OnFC(
|
|
XSignal &signal // Specified Signal
|
|
)
|
|
{
|
|
//
|
|
// Issue Propper Alert ...
|
|
XAU5IssueFCAlert(signal);
|
|
|
|
//
|
|
// Remove Signal From Guard Protector ...
|
|
xAU5Guard.Remove(signal.ticket);
|
|
xAU5Guard.RemoveSupport(signal.ticket);
|
|
|
|
//
|
|
// Update Reports ...
|
|
xAU5Reporter.AddTradeSLInReports();
|
|
XAU5UpdateReports();
|
|
}
|
|
|
|
//
|
|
// Runs when a Position Guarded ...
|
|
void XAU5OnGuard(
|
|
XSignal &signal, // Specified Signal
|
|
ENUM_X_GUARD_REASONS reason, // Reason of Guard Applied
|
|
string message, // Guard Returned Message
|
|
bool isStopOut = false // Specified StopOut Fired or not
|
|
)
|
|
{
|
|
//
|
|
// Chekc StopOut ...
|
|
if (isStopOut)
|
|
{
|
|
//
|
|
LogMessage(message);
|
|
|
|
//
|
|
xAU5MEnableProvider = false;
|
|
}
|
|
|
|
//
|
|
// Issue Propper Alert ...
|
|
XAU5IssueGuardAlert(signal, reason, isStopOut);
|
|
|
|
//
|
|
// Update Reports ...
|
|
xAU5Reporter.AddTradeSLInReports();
|
|
XAU5UpdateReports();
|
|
}
|
|
|
|
//
|
|
// END Event Handlers ...
|
|
//
|
|
|
|
//
|
|
// END Provided Functions ...
|
|
//
|
|
|
|
//
|
|
// START Private Functions ...
|
|
//
|
|
|
|
//
|
|
// START Volume Baseed Functions ...
|
|
//
|
|
|
|
//
|
|
// Calculating Volume for Tradings ...
|
|
double XAU5CalculateVolume(
|
|
double forceMultiplier = 0 // some times we get more than one signal at the same time, here we increase volume using this ...
|
|
)
|
|
{
|
|
//
|
|
// Check Open Trades ...
|
|
int openSignals = xAU5Trader.Count();
|
|
|
|
//
|
|
double result = xAU5VolumeManager.CalculateVolume(
|
|
openSignals,
|
|
_Symbol);
|
|
|
|
//
|
|
result *= forceMultiplier;
|
|
|
|
//
|
|
// Normalize Volume ...
|
|
result = NormalizeVolume(result, _Symbol, 2);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// END Volume Baseed Functions ...
|
|
//
|
|
|
|
//
|
|
// START Report/Collect and Guard Functions ...
|
|
//
|
|
|
|
//
|
|
// Update Report Info ...
|
|
void XAU5UpdateReports()
|
|
{
|
|
//
|
|
double staticVolume = xAU5VolumeManager.GetStaticVolume();
|
|
string staticVolumeCalculationMethod = EnumToString(xAU5StaticVolumeMethod);
|
|
double maxAllowedVolume = xAU5VolumeManager.GetMaxAllowedVolume();
|
|
double volumeIncreaseMultiplier = xAU5VolumeManager.GetVolumeMultiplier();
|
|
double balanceGowingToApplyMultiplier = xAU5VolumeManager.GetIncreaseMultiplierBalance();
|
|
|
|
double balanceGrowingToIncreaseMaxAllowedVolume = xAU5VolumeManager.GetIncreaseMaxAllowedVolumeBalance();
|
|
|
|
//
|
|
// Update Reports Volume Info ...
|
|
xAU5Reporter.UpdateReportsVolumeInfo(
|
|
staticVolume,
|
|
staticVolumeCalculationMethod,
|
|
maxAllowedVolume,
|
|
volumeIncreaseMultiplier,
|
|
balanceGowingToApplyMultiplier,
|
|
balanceGrowingToIncreaseMaxAllowedVolume);
|
|
|
|
//
|
|
// Calculate Max Allowed Drawdown ...
|
|
double minBalanceForTradeBasedOnBalance = xAU5Trader.GetMinBalanceForOpenTrades();
|
|
xAU5Reporter.UpdateMinBalanceForOpenTradesInReports(minBalanceForTradeBasedOnBalance);
|
|
}
|
|
|
|
//
|
|
// Reset XAU5 Info Buffers ...
|
|
void XAU5ResetCollectors()
|
|
{
|
|
//
|
|
// Reset Calculations Buffers and Variables ...
|
|
xAU5MaxDrawUp = 0;
|
|
xAU5MaxDrawDown = 0;
|
|
|
|
//
|
|
xAU5Trader.Reset();
|
|
xAU5Reporter.Reset();
|
|
xAU5Collector.Reset();
|
|
xAU5VolumeManager.ResetForceVolumeMultiplier();
|
|
}
|
|
|
|
//
|
|
// END Report/Collect and Guard Functions ...
|
|
//
|
|
|
|
//
|
|
// START Alert Functions ...
|
|
//
|
|
|
|
//
|
|
// Send Alert ...
|
|
void XAU5IssueAlert(string message)
|
|
{
|
|
//
|
|
// Validate Args ...
|
|
if (StringLen(message) <= 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
if (xAU5EnableAlerts)
|
|
{
|
|
SendAlert(message);
|
|
}
|
|
else
|
|
{
|
|
LogMessage(message);
|
|
}
|
|
}
|
|
|
|
//
|
|
// Send Take Profit Alert on Specific Signal ...
|
|
void XAU5IssueTPAlert(
|
|
XSignal &signal // Specified Signal
|
|
)
|
|
{
|
|
//
|
|
string message = "";
|
|
|
|
//
|
|
message = xAU5Trader.GenerateSignalTPComment(signal);
|
|
|
|
//
|
|
XAU5IssueAlert(message);
|
|
}
|
|
|
|
//
|
|
// Send Stop Loss Alert on Specific Signal ...
|
|
void XAU5IssueSLAlert(
|
|
XSignal &signal // Specified Signal
|
|
)
|
|
{
|
|
//
|
|
string message = "";
|
|
|
|
//
|
|
message = xAU5Trader.GenerateSignalSLComment(signal);
|
|
|
|
//
|
|
XAU5IssueAlert(message);
|
|
}
|
|
|
|
//
|
|
// Send Force Close Alert on Specific Signal ...
|
|
void XAU5IssueFCAlert(
|
|
XSignal &signal // Specified Signal
|
|
)
|
|
{
|
|
//
|
|
string message = "";
|
|
|
|
//
|
|
message = xAU5Trader.GenerateSignalForceCloseComment(signal);
|
|
|
|
//
|
|
XAU5IssueAlert(message);
|
|
}
|
|
|
|
//
|
|
// Send Risk Free Alert on Specific Signal ...
|
|
void XAU5IssueRFAlert(
|
|
XSignal &signal // Specified Signal
|
|
)
|
|
{
|
|
//
|
|
string message = "";
|
|
|
|
//
|
|
message = xAU5Trader.GenerateSignalRFComment(signal, 0);
|
|
|
|
//
|
|
XAU5IssueAlert(message);
|
|
}
|
|
|
|
//
|
|
// Send Guard Alert on Specific Signal ...
|
|
void XAU5IssueGuardAlert(
|
|
XSignal &signal, // Specified Signal
|
|
ENUM_X_GUARD_REASONS reason, // Specified Guard Reason
|
|
bool isStopOut = false // Specified StopOut Fired or not
|
|
)
|
|
{
|
|
//
|
|
string message = "";
|
|
|
|
//
|
|
message = xAU5Guard.GenerateGuardedSignalComment(
|
|
signal,
|
|
reason);
|
|
|
|
//
|
|
XAU5IssueAlert(message);
|
|
}
|
|
|
|
//
|
|
// Send Initialization Succeed Alert ...
|
|
void XAU5IssueInitializationSucceedAlert()
|
|
{
|
|
//
|
|
string message = "Initializion of (" + XAU5ProviderName + " Provider" + " _ " + _Symbol + " _ " + EnumToString(_Period) + ") Succeeded ...";
|
|
XAU5IssueAlert(message);
|
|
}
|
|
|
|
//
|
|
// Send De Initialization Succeed Alert ...
|
|
void XAU5IssueDeinitializationSucceedAlert()
|
|
{
|
|
//
|
|
string message = "De Initializion of (" + XAU5ProviderName + " Provider" + ") Succeeded ...";
|
|
XAU5IssueAlert(message);
|
|
}
|
|
|
|
//
|
|
// Send Signal Successfully Execution Alert ...
|
|
void XAU5IssueSignalExecutionAlert(
|
|
XSignal &signal, // Specified Signal
|
|
string &providers[] // Signal Providers
|
|
)
|
|
{
|
|
//
|
|
string message = xAU5Trader.ToString(signal);
|
|
|
|
//
|
|
string providersMsg = ToString(providers);
|
|
if (StringLen(providersMsg) > 0)
|
|
{
|
|
message += ", Providers: " + providersMsg;
|
|
}
|
|
|
|
//
|
|
XAU5IssueAlert(message);
|
|
}
|
|
|
|
//
|
|
// Send Signal Execution Alert ...
|
|
void XAU5IssueSignalExecutionError(int error)
|
|
{
|
|
//
|
|
string message = GetSignalExecutionError(error);
|
|
if (StringLen(message) == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
XAU5IssueAlert(message);
|
|
}
|
|
|
|
//
|
|
// END Alert Functions ...
|
|
//
|
|
|
|
//
|
|
// END Private Functions ...
|
|
//
|