1374 lines
29 KiB
Plaintext
1374 lines
29 KiB
Plaintext
////////////////////////////////////////////////////////////
|
|
//
|
|
// SaherElm IT Center MQL5 XGPUS5 Signal Provider Library
|
|
// --------------------------------------------------------
|
|
// Name: XGPUS5SignalProvider
|
|
// Description: XGPUS5 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 XGPUS5ProviderName "XGPUS5"
|
|
|
|
//
|
|
// START Inputs ...
|
|
//
|
|
|
|
#include "x-saherelm.xgpus5.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 *xGPUS5Guard;
|
|
XCTrade *xGPUS5Trader;
|
|
XCReporter *xGPUS5Reporter;
|
|
XCCollector *xGPUS5Collector;
|
|
XCAccountInfo *xGPUS5AccountInfo;
|
|
XCVolumeManager *xGPUS5VolumeManager;
|
|
|
|
//
|
|
#include "x-saherelm.xgpus5.provider.indicators.lib.mq5"
|
|
|
|
//
|
|
// Controlling Variables ...
|
|
bool xGPUS5MEnableProvider = false;
|
|
bool xGPUS5MAllowLongTrades = false;
|
|
bool xGPUS5MAllowShortTrades = false;
|
|
|
|
//
|
|
double xGPUS5Deposit = 0;
|
|
double xGPUS5Balance = 0;
|
|
double xGPUS5MinReward = 0;
|
|
double xGPUS5FreeMargin = 0;
|
|
double xGPUS5BalanceForOpenTrades = 0;
|
|
double xGPUS5FreeMarginForOpenTrades = 0;
|
|
|
|
//
|
|
// Guard ...
|
|
double xGPUS5CriticalDrawDown = 0;
|
|
double xGPUS5MaxAllowedDrawDown = 0;
|
|
|
|
//
|
|
double xGPUS5MaxDrawUp = 0;
|
|
double xGPUS5MaxDrawDown = 0;
|
|
|
|
//
|
|
int NOT_ENOUGH_MONEY_ERROR = 4756;
|
|
const string XGPUS5_SUPPORT_ID = "SP";
|
|
|
|
//
|
|
// END Global Definitions: Variables, Properties and etc ...
|
|
//
|
|
|
|
//
|
|
// START Provided Functions ...
|
|
//
|
|
|
|
//
|
|
// Initial Library if required ...
|
|
bool XGPUS5InitSignalProviderLibrary()
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
if (!xGPUS5EnableProvider)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Controlling Variables ...
|
|
xGPUS5MEnableProvider = xGPUS5EnableProvider;
|
|
xGPUS5MAllowLongTrades = xGPUS5AllowLongTrades;
|
|
xGPUS5MAllowShortTrades = xGPUS5AllowShortTrades;
|
|
|
|
//
|
|
// Validate Inputs ...
|
|
result = XGPUS5ValidateInputs();
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Init Indicators ...
|
|
result = XGPUS5InitIndicators();
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
xGPUS5Reporter = new XCReporter();
|
|
xGPUS5Collector = new XCCollector();
|
|
xGPUS5AccountInfo = new XCAccountInfo();
|
|
|
|
//
|
|
// Calculate Account Deposit ...
|
|
xGPUS5Deposit = xGPUS5AccountInfo.GetInitialBalance();
|
|
|
|
//
|
|
// Prepare Min Reward ...
|
|
if (xGPUS5MinRewardInPips > 0)
|
|
{
|
|
xGPUS5MinReward = PipsToPrice(xGPUS5MinRewardInPips);
|
|
}
|
|
|
|
//
|
|
// Calculate Min Balance for Trades ...
|
|
if (xGPUS5BalanceFactorForOpenTrades > 0)
|
|
{
|
|
xGPUS5BalanceForOpenTrades = xGPUS5BalanceFactorForOpenTrades * xGPUS5Deposit;
|
|
}
|
|
|
|
//
|
|
// Claculate Min Free Marging for Trades ...
|
|
if (xGPUS5FreeMarginFactorForOpenTrades > 0)
|
|
{
|
|
xGPUS5FreeMarginForOpenTrades = xGPUS5FreeMarginFactorForOpenTrades * xGPUS5Deposit;
|
|
}
|
|
|
|
//
|
|
// Calculate Init Time Critical DrawDown Value ...
|
|
if (xGPUS5CriticalDrawDownFactor > 0)
|
|
{
|
|
xGPUS5CriticalDrawDown = xGPUS5CriticalDrawDownFactor * xGPUS5Deposit;
|
|
}
|
|
|
|
//
|
|
// Calculate Init Time Max Allowed DrawDown Value ...
|
|
if (xGPUS5MaxAllowedDrawDownFactor > 0)
|
|
{
|
|
xGPUS5MaxAllowedDrawDown = xGPUS5MaxAllowedDrawDownFactor * xGPUS5Deposit;
|
|
}
|
|
|
|
//
|
|
// Instant Volume Manager ...
|
|
xGPUS5VolumeManager = new XCVolumeManager(
|
|
xGPUS5StaticVolumeFactor,
|
|
xGPUS5StaticVolumeMethod,
|
|
xGPUS5VolumeMultiplier,
|
|
xGPUS5ApplyVolumeMultiplierFactor,
|
|
xGPUS5MaxSupportedVolumePerTradeFactor,
|
|
xGPUS5IncreasedMaxSupportedVolumeRate);
|
|
|
|
//
|
|
// Make XCTrader instance ...
|
|
xGPUS5Trader = new XCTrade(
|
|
XGPUS5ProviderName,
|
|
_Symbol,
|
|
xGPUS5Slippage,
|
|
xGPUS5MagicNumber,
|
|
xGPUS5UseVirtualTPSl,
|
|
xGPUS5MaxAllowedTrades,
|
|
xGPUS5FreeMarginForOpenTrades,
|
|
xGPUS5BalanceForOpenTrades);
|
|
|
|
//
|
|
// Instantiate Account Guard ...
|
|
xGPUS5Guard = new XCGuard(
|
|
//
|
|
// Old Trades ...
|
|
xGPUS5MaxAllowedTradeAge,
|
|
xGPUS5OldTradesAction,
|
|
//
|
|
// Critical DrawDown ...
|
|
xGPUS5CriticalDrawDownFactor,
|
|
xGPUS5CriticalGuardAction,
|
|
//
|
|
// Max Allowed DrawDown ...
|
|
xGPUS5MaxAllowedDrawDownFactor,
|
|
xGPUS5MaxAllowedGuardAction,
|
|
xGPUS5GuardStopoutFiredAfterTouches,
|
|
//
|
|
// xGPUS5GuardSupportMethod
|
|
X_SUPPORT_DO_NOTHING
|
|
//
|
|
);
|
|
|
|
//
|
|
// Reset Calculations Buffers and Variables ...
|
|
XGPUS5ResetCollectors();
|
|
|
|
//
|
|
XGPUS5UpdateReports();
|
|
|
|
//
|
|
result = true;
|
|
|
|
//
|
|
// Logging State ...
|
|
XGPUS5IssueInitializationSucceedAlert();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// DeInitial Library if required ...
|
|
void XGPUS5DeinitSignalProviderLibrary(const int reason)
|
|
{
|
|
//
|
|
// Release Handlers ...
|
|
XGPUS5ReleaseIndicators();
|
|
|
|
//
|
|
// Logging State ...
|
|
XGPUS5IssueDeinitializationSucceedAlert();
|
|
}
|
|
|
|
//
|
|
// this is a Globally Function which do all of
|
|
// checkings and positions handling ...
|
|
void XGPUS5SignalProviderHandleTick()
|
|
{
|
|
//
|
|
// Check Provider is Enable or Not ...
|
|
if (!xGPUS5MEnableProvider)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
XGPUS5SignalProviderDoControlState();
|
|
|
|
//
|
|
// Reading Indicator Buffers ...
|
|
XGPUS5HandleReadingBuffers();
|
|
if (xGPUS5CalculatedBars < xGPUS5MaxLengthOfInputs)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Handle Open Trades ...
|
|
XGPUS5HandleOpenTrades();
|
|
|
|
//
|
|
// Handle Close Trades ...
|
|
XGPUS5HandleCloseTrades();
|
|
|
|
//
|
|
// DrawDown and DrawUp ...
|
|
XGPUS5HandleCalculateDrawDownUp();
|
|
}
|
|
|
|
//
|
|
// Do Control Checks here ...
|
|
void XGPUS5SignalProviderDoControlState()
|
|
{
|
|
//
|
|
// Update account Balance ...
|
|
xGPUS5Balance = xGPUS5AccountInfo.GetBalance();
|
|
xGPUS5FreeMargin = xGPUS5AccountInfo.GetFreeMargin();
|
|
|
|
//
|
|
// Check Provider is Enable or Not ...
|
|
if (!xGPUS5MEnableProvider)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Protect Account by Guarding Maximum Drawdown ...
|
|
// TODO: add Guard Result here ...
|
|
string guardMessage = "";
|
|
XGuardHandlerResult guardHandlerResult;
|
|
bool isGuarded = xGPUS5Guard.Guard(
|
|
guardHandlerResult,
|
|
guardMessage,
|
|
xGPUS5Trader,
|
|
_Period);
|
|
if (isGuarded)
|
|
{
|
|
//
|
|
// Criticals ...
|
|
int criticalsCount = ArraySize(guardHandlerResult.criticals);
|
|
if (criticalsCount > 0)
|
|
{
|
|
//
|
|
for (int i = 0; i < criticalsCount; i++)
|
|
{
|
|
//
|
|
XSignal iSignal = guardHandlerResult.criticals[i];
|
|
|
|
//
|
|
XGPUS5OnGuard(
|
|
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];
|
|
|
|
//
|
|
XGPUS5OnGuard(
|
|
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];
|
|
|
|
//
|
|
XGPUS5OnGuard(
|
|
iSignal,
|
|
X_GUARDED_OLD,
|
|
guardMessage);
|
|
}
|
|
}
|
|
}
|
|
|
|
//
|
|
// Signal Protection ...
|
|
XSignal protectSupports[];
|
|
bool isSupported = xGPUS5Guard.ProtectSignals(
|
|
protectSupports,
|
|
xGPUS5Trader);
|
|
if (isSupported)
|
|
{
|
|
//
|
|
LogMessage("Guard do Support Trdae ...");
|
|
}
|
|
|
|
//
|
|
// Handling Signals ...
|
|
XTraderHandlerResult signalHandlerResult;
|
|
xGPUS5Trader.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];
|
|
XGPUS5OnSL(iSignal);
|
|
}
|
|
}
|
|
|
|
//
|
|
// Handle TP Reports ...
|
|
int handledTPsCount = ArraySize(signalHandlerResult.tp);
|
|
if (handledTPsCount > 0)
|
|
{
|
|
//
|
|
for (int i = 0; i < handledTPsCount; i++)
|
|
{
|
|
//
|
|
XSignal iSignal = signalHandlerResult.tp[i];
|
|
XGPUS5OnTP(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];
|
|
XGPUS5OnRF(iSignal);
|
|
}
|
|
}
|
|
}
|
|
|
|
//
|
|
// Handle Trades when OnTrade Event Hnadler Calls ...
|
|
void XGPUS5SignalProviderHandleTrade()
|
|
{
|
|
//
|
|
// Check Provider Enabled ...
|
|
if (!xGPUS5MEnableProvider)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Handle OnTrade Event ...
|
|
XOnTradeHandlerResult result = {};
|
|
xGPUS5Trader.HandleOnTradeEvent(
|
|
result);
|
|
|
|
//
|
|
// Detected TP or SL ...
|
|
if (result.hasNewDeal)
|
|
{
|
|
//
|
|
XDeal last = xGPUS5Trader.GetLastDeal();
|
|
|
|
//
|
|
if (last.reason == DEAL_REASON_SL)
|
|
{
|
|
xGPUS5Reporter.AddTradeSLInReports();
|
|
}
|
|
else if (last.reason == DEAL_REASON_TP)
|
|
{
|
|
xGPUS5Reporter.AddTradeTPInReports();
|
|
}
|
|
}
|
|
}
|
|
|
|
//
|
|
// Handle Open Trades ...
|
|
void XGPUS5HandleOpenTrades()
|
|
{
|
|
//
|
|
// Check Can Trade ...
|
|
bool canTrade = xGPUS5MAllowLongTrades || xGPUS5MAllowShortTrades;
|
|
if (!canTrade)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Check Max Open Trades ...
|
|
int openSignals = xGPUS5Trader.Count();
|
|
bool canOpenPositions = openSignals < xGPUS5MaxAllowedTrades;
|
|
if (!canOpenPositions)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Check Signals Exists ...
|
|
XProvidedSignal longSignalInfo;
|
|
XProvidedSignal shortSignalInfo;
|
|
bool hasLongSignal = XGPUS5CanOpenLongTrade(longSignalInfo);
|
|
bool hasShortSignal = XGPUS5CanOpenShortTrade(shortSignalInfo);
|
|
if (!hasLongSignal && !hasShortSignal)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Update Reports ...
|
|
XGPUS5UpdateReports();
|
|
|
|
//
|
|
xGPUS5WaitUntilNextCandle = true;
|
|
|
|
//
|
|
// Open Long Trades ...
|
|
if (
|
|
//
|
|
hasLongSignal &&
|
|
xGPUS5MAllowLongTrades)
|
|
{
|
|
//
|
|
bool isOpened = XGPUS5OpenLongPosition(longSignalInfo);
|
|
}
|
|
|
|
//
|
|
// Open Short Trades ...
|
|
if (
|
|
//
|
|
hasShortSignal &&
|
|
xGPUS5MAllowShortTrades)
|
|
{
|
|
//
|
|
bool isOpened = XGPUS5OpenShortPosition(shortSignalInfo);
|
|
}
|
|
}
|
|
|
|
//
|
|
// Handle Close Trades ...
|
|
void XGPUS5HandleCloseTrades()
|
|
{
|
|
//
|
|
// Force Close Long Trades ...
|
|
bool isClosedLongTrades = false;
|
|
bool canCloseLongTrades = XGPUS5CanCloseLongTrade();
|
|
if (xGPUS5MAllowLongTrades && canCloseLongTrades)
|
|
{
|
|
//
|
|
ENUM_X_SIGNAL_TYPE type = X_SIGNAL_LONG;
|
|
|
|
//
|
|
XSignal closed[];
|
|
xGPUS5Trader.CloseSignals(
|
|
closed,
|
|
type);
|
|
|
|
//
|
|
int closedTradesCount = ArraySize(closed);
|
|
isClosedLongTrades = closedTradesCount > 0;
|
|
if (isClosedLongTrades)
|
|
{
|
|
//
|
|
for (int i = 0; i < closedTradesCount; i++)
|
|
{
|
|
//
|
|
XSignal iSignal = closed[i];
|
|
XGPUS5OnFC(iSignal);
|
|
}
|
|
}
|
|
}
|
|
|
|
//
|
|
// Force Close Short Trades ...
|
|
bool isClosedShortTrades = false;
|
|
bool canCloseShortTrades = XGPUS5CanCloseShortTrade();
|
|
if (xGPUS5MAllowShortTrades && canCloseShortTrades)
|
|
{
|
|
//
|
|
ENUM_X_SIGNAL_TYPE type = X_SIGNAL_SHORT;
|
|
|
|
//
|
|
XSignal closed[];
|
|
xGPUS5Trader.CloseSignals(
|
|
closed,
|
|
type);
|
|
|
|
//
|
|
int closedTradesCount = ArraySize(closed);
|
|
isClosedShortTrades = closedTradesCount > 0;
|
|
if (isClosedShortTrades)
|
|
{
|
|
//
|
|
for (int i = 0; i < closedTradesCount; i++)
|
|
{
|
|
//
|
|
XSignal iSignal = closed[i];
|
|
XGPUS5OnFC(iSignal);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//
|
|
// Open Long Position ...
|
|
bool XGPUS5OpenLongPosition(
|
|
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 + xGPUS5MinReward;
|
|
double riskFreeRate = xGPUS5RiskFreeRate;
|
|
double riskFreeStep = xGPUS5RiskFreeStepInPips;
|
|
double volume = XGPUS5CalculateVolume(ArraySize(info.signallers));
|
|
|
|
//
|
|
XSignal signal = {};
|
|
signal = xGPUS5Trader.GenerateSignal(
|
|
X_SIGNAL_LONG,
|
|
tp,
|
|
sl,
|
|
volume,
|
|
riskFreeStep,
|
|
riskFreeRate);
|
|
bool isValidSignal = IsValid(
|
|
signal,
|
|
xGPUS5MagicNumber);
|
|
if (!isValidSignal)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
int signalsCount = xGPUS5Trader.Count();
|
|
bool isCross = signalsCount >= 1;
|
|
if (isCross)
|
|
{
|
|
//
|
|
// Handle Cross Signals ...
|
|
XGPUS5HnadleCrossSignal(signal);
|
|
}
|
|
|
|
//
|
|
// Retrieve Can Execute Signal or not ...
|
|
bool canExecuteSignal =
|
|
!isCross
|
|
//
|
|
||
|
|
//
|
|
(
|
|
//
|
|
isCross &&
|
|
xGPUS5CrossSignalsMethod != X_CROSS_SIGNAL_DO_NOTHING
|
|
//
|
|
);
|
|
if (!canExecuteSignal)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Execute Signal ...
|
|
ENUM_EXECUTION_SIGNAL_RESULT executionResult = xGPUS5Trader.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 = xGPUS5Trader.GetSignal(signal.ticket);
|
|
|
|
//
|
|
XGPUS5OnSignal(
|
|
tSignal,
|
|
providers);
|
|
}
|
|
else
|
|
{
|
|
XGPUS5IssueSignalExecutionError(executionResult);
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Open Short Position ...
|
|
bool XGPUS5OpenShortPosition(
|
|
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 - xGPUS5MinReward;
|
|
double riskFreeRate = xGPUS5RiskFreeRate;
|
|
double riskFreeStep = xGPUS5RiskFreeStepInPips;
|
|
double volume = XGPUS5CalculateVolume(ArraySize(info.signallers));
|
|
|
|
//
|
|
XSignal signal = {};
|
|
signal = xGPUS5Trader.GenerateSignal(
|
|
X_SIGNAL_SHORT,
|
|
tp,
|
|
sl,
|
|
volume,
|
|
riskFreeStep,
|
|
riskFreeRate);
|
|
bool isValidSignal = IsValid(
|
|
signal,
|
|
xGPUS5MagicNumber);
|
|
if (!isValidSignal)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
int signalsCount = xGPUS5Trader.Count();
|
|
bool isCross = signalsCount >= 1;
|
|
if (isCross)
|
|
{
|
|
//
|
|
// Handle Cross Signals ...
|
|
XGPUS5HnadleCrossSignal(signal);
|
|
}
|
|
|
|
//
|
|
// Retrieve Can Execute Signal or not ...
|
|
bool canExecuteSignal =
|
|
!isCross
|
|
//
|
|
||
|
|
//
|
|
(
|
|
//
|
|
isCross &&
|
|
xGPUS5CrossSignalsMethod != X_CROSS_SIGNAL_DO_NOTHING
|
|
//
|
|
);
|
|
if (!canExecuteSignal)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Execute Signal ...
|
|
ENUM_EXECUTION_SIGNAL_RESULT executionResult = xGPUS5Trader.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 = xGPUS5Trader.GetSignal(signal.ticket);
|
|
|
|
//
|
|
XGPUS5OnSignal(
|
|
tSignal,
|
|
providers);
|
|
}
|
|
else
|
|
{
|
|
XGPUS5IssueSignalExecutionError(executionResult);
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Apply Cross Signal Actions ...
|
|
void XGPUS5HnadleCrossSignal(
|
|
XSignal &signal // Specified Signal ...
|
|
)
|
|
{
|
|
//
|
|
// Check Cross Signal Handling Enabled ...
|
|
if (xGPUS5CrossSignalsMethod == X_CROSS_SIGNAL_DO_NOTHING)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
// First We have to Change Cross Signals Comment ...
|
|
string comment = xGPUS5Trader.GenerateSignalComment(
|
|
signal,
|
|
true);
|
|
|
|
//
|
|
signal.comment = comment;
|
|
|
|
//
|
|
// If Every thing is Regular, prevent any changes here ...
|
|
if (xGPUS5CrossSignalsMethod == X_CROSS_SIGNAL_REGULAR)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Retrieve Max in Drawdown Signal ...
|
|
XSignal maxInDDSignal = xGPUS5Trader.GetMaxInDrawdownSignal();
|
|
|
|
//
|
|
// Validate Signal ...
|
|
bool isValidSignal =
|
|
maxInDDSignal.ticket > 0 &&
|
|
maxInDDSignal.symbol == signal.symbol &&
|
|
IsValid(maxInDDSignal, xGPUS5MagicNumber);
|
|
if (!isValidSignal)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
double volume = signal.volume;
|
|
bool isDirectional = maxInDDSignal.type == signal.type;
|
|
|
|
//
|
|
// Prevent any changes on Regular Conditions ...
|
|
bool isRegular =
|
|
isDirectional
|
|
? (
|
|
//
|
|
xGPUS5CrossSignalsMethod == X_CROSS_SIGNAL_DIRECTION_REGULAR ||
|
|
xGPUS5CrossSignalsMethod == X_CROSS_SIGNAL_DIRECTION_REGULAR_INDIRECTION_IMPORTANT
|
|
//
|
|
)
|
|
: (
|
|
//
|
|
xGPUS5CrossSignalsMethod == X_CROSS_SIGNAL_INDIRECTION_REGULAR ||
|
|
xGPUS5CrossSignalsMethod == 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 XGPUS5HandleCalculateDrawDownUp()
|
|
{
|
|
//
|
|
XSignal signals[];
|
|
xGPUS5Trader.GetSignals(signals);
|
|
int signalsCount = ArraySize(signals);
|
|
if (signalsCount <= 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
double profit = 0;
|
|
double volume = 0;
|
|
XCollector collector = {};
|
|
double maxAllowedVolume = xGPUS5VolumeManager.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)
|
|
{
|
|
//
|
|
xGPUS5MaxDrawDown =
|
|
xGPUS5MaxDrawDown == 0 ||
|
|
xGPUS5MaxDrawDown > MathAbs(profit)
|
|
? MathAbs(profit)
|
|
: xGPUS5MaxDrawDown;
|
|
|
|
//
|
|
// Update Buffers ...
|
|
datetime time = iTime(_Symbol, _Period, 0);
|
|
|
|
//
|
|
// Calculate Max Allowed Drawdown ...
|
|
//
|
|
collector.time = time;
|
|
collector.volume = volume;
|
|
collector.maxDrawDown = xGPUS5MaxDrawDown;
|
|
collector.maxAllowedVolume = maxAllowedVolume;
|
|
collector.minBalanceForOpenTrade = xGPUS5Trader.GetMinBalanceForOpenTrades();
|
|
}
|
|
else
|
|
{
|
|
//
|
|
xGPUS5MaxDrawUp =
|
|
xGPUS5MaxDrawUp == 0 ||
|
|
xGPUS5MaxDrawUp < profit
|
|
? profit
|
|
: xGPUS5MaxDrawUp;
|
|
}
|
|
|
|
//
|
|
// Update Collector ...
|
|
xGPUS5Collector.Add(collector);
|
|
|
|
//
|
|
// Update Reports ...
|
|
xGPUS5Reporter.UpdateMaxDrawUpInReports(xGPUS5MaxDrawUp);
|
|
xGPUS5Reporter.UpdateMaxDrawDownInReports(xGPUS5MaxDrawDown);
|
|
xGPUS5Reporter.UpdateMaxSameTimeTradesInReports(signalsCount);
|
|
}
|
|
|
|
//
|
|
// START Event Handlers ...
|
|
//
|
|
|
|
//
|
|
// Runs When a Signal Successfully Executed ...
|
|
void XGPUS5OnSignal(
|
|
XSignal &signal, // Specified Signal
|
|
string &providers[] // Signal Providers
|
|
)
|
|
{
|
|
//
|
|
// Issue Propper Alert ...
|
|
XGPUS5IssueSignalExecutionAlert(
|
|
signal,
|
|
providers);
|
|
|
|
//
|
|
// Add Signal To Supports on Guard ...
|
|
xGPUS5Guard.AddSupport(signal);
|
|
|
|
//
|
|
// Update Reports ...
|
|
xGPUS5Reporter.AddSignalToReports(
|
|
signal,
|
|
providers);
|
|
}
|
|
|
|
//
|
|
// Runs when a Position Got Take Profit ...
|
|
void XGPUS5OnTP(
|
|
XSignal &signal // Specified Signal
|
|
)
|
|
{
|
|
//
|
|
// Issue Propper Alert ...
|
|
XGPUS5IssueTPAlert(signal);
|
|
|
|
//
|
|
// Remove Signal From Guard Protector ...
|
|
xGPUS5Guard.Remove(signal.ticket);
|
|
xGPUS5Guard.RemoveSupport(signal.ticket);
|
|
|
|
//
|
|
// Update Reports ...
|
|
xGPUS5Reporter.AddTradeTPInReports();
|
|
XGPUS5UpdateReports();
|
|
}
|
|
|
|
//
|
|
// Runs when a Position Got Stop Loss ...
|
|
void XGPUS5OnSL(
|
|
XSignal &signal // Specified Signal
|
|
)
|
|
{
|
|
//
|
|
// Issue Propper Alert ...
|
|
XGPUS5IssueSLAlert(signal);
|
|
|
|
//
|
|
// Remove Signal From Guard Protector ...
|
|
xGPUS5Guard.Remove(signal.ticket);
|
|
xGPUS5Guard.RemoveSupport(signal.ticket);
|
|
|
|
//
|
|
// Update Reports ...
|
|
xGPUS5Reporter.AddTradeSLInReports();
|
|
XGPUS5UpdateReports();
|
|
}
|
|
|
|
//
|
|
// Runs when a Position Risk Free ...
|
|
void XGPUS5OnRF(
|
|
XSignal &signal // Specified Signal
|
|
)
|
|
{
|
|
//
|
|
// Issue Propper Alert ...
|
|
XGPUS5IssueRFAlert(signal);
|
|
|
|
//
|
|
// Remove Signal From Guard Protector ...
|
|
// since RiskFree Signals has SL ...
|
|
xGPUS5Guard.Remove(signal.ticket);
|
|
xGPUS5Guard.RemoveSupport(signal.ticket);
|
|
|
|
//
|
|
// Update Reports ...
|
|
xGPUS5Reporter.AddTradeSLInReports();
|
|
XGPUS5UpdateReports();
|
|
}
|
|
|
|
//
|
|
// Runs when a Position Force Closed ...
|
|
void XGPUS5OnFC(
|
|
XSignal &signal // Specified Signal
|
|
)
|
|
{
|
|
//
|
|
// Issue Propper Alert ...
|
|
XGPUS5IssueFCAlert(signal);
|
|
|
|
//
|
|
// Remove Signal From Guard Protector ...
|
|
xGPUS5Guard.Remove(signal.ticket);
|
|
xGPUS5Guard.RemoveSupport(signal.ticket);
|
|
|
|
//
|
|
// Update Reports ...
|
|
xGPUS5Reporter.AddTradeSLInReports();
|
|
XGPUS5UpdateReports();
|
|
}
|
|
|
|
//
|
|
// Runs when a Position Guarded ...
|
|
void XGPUS5OnGuard(
|
|
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);
|
|
|
|
//
|
|
xGPUS5MEnableProvider = false;
|
|
}
|
|
|
|
//
|
|
// Issue Propper Alert ...
|
|
XGPUS5IssueGuardAlert(signal, reason, isStopOut);
|
|
|
|
//
|
|
// Update Reports ...
|
|
xGPUS5Reporter.AddTradeSLInReports();
|
|
XGPUS5UpdateReports();
|
|
}
|
|
|
|
//
|
|
// END Event Handlers ...
|
|
//
|
|
|
|
//
|
|
// END Provided Functions ...
|
|
//
|
|
|
|
//
|
|
// START Private Functions ...
|
|
//
|
|
|
|
//
|
|
// START Volume Baseed Functions ...
|
|
//
|
|
|
|
//
|
|
// Calculating Volume for Tradings ...
|
|
double XGPUS5CalculateVolume(
|
|
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 = xGPUS5Trader.Count();
|
|
|
|
//
|
|
double result = xGPUS5VolumeManager.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 XGPUS5UpdateReports()
|
|
{
|
|
//
|
|
double staticVolume = xGPUS5VolumeManager.GetStaticVolume();
|
|
string staticVolumeCalculationMethod = EnumToString(xGPUS5StaticVolumeMethod);
|
|
double maxAllowedVolume = xGPUS5VolumeManager.GetMaxAllowedVolume();
|
|
double volumeIncreaseMultiplier = xGPUS5VolumeManager.GetVolumeMultiplier();
|
|
double balanceGowingToApplyMultiplier = xGPUS5VolumeManager.GetIncreaseMultiplierBalance();
|
|
|
|
double balanceGrowingToIncreaseMaxAllowedVolume = xGPUS5VolumeManager.GetIncreaseMaxAllowedVolumeBalance();
|
|
|
|
//
|
|
// Update Reports Volume Info ...
|
|
xGPUS5Reporter.UpdateReportsVolumeInfo(
|
|
staticVolume,
|
|
staticVolumeCalculationMethod,
|
|
maxAllowedVolume,
|
|
volumeIncreaseMultiplier,
|
|
balanceGowingToApplyMultiplier,
|
|
balanceGrowingToIncreaseMaxAllowedVolume);
|
|
|
|
//
|
|
// Calculate Max Allowed Drawdown ...
|
|
double minBalanceForTradeBasedOnBalance = xGPUS5Trader.GetMinBalanceForOpenTrades();
|
|
xGPUS5Reporter.UpdateMinBalanceForOpenTradesInReports(minBalanceForTradeBasedOnBalance);
|
|
}
|
|
|
|
//
|
|
// Reset XGPUS5 Info Buffers ...
|
|
void XGPUS5ResetCollectors()
|
|
{
|
|
//
|
|
// Reset Calculations Buffers and Variables ...
|
|
xGPUS5MaxDrawUp = 0;
|
|
xGPUS5MaxDrawDown = 0;
|
|
|
|
//
|
|
xGPUS5Trader.Reset();
|
|
xGPUS5Reporter.Reset();
|
|
xGPUS5Collector.Reset();
|
|
xGPUS5VolumeManager.ResetForceVolumeMultiplier();
|
|
}
|
|
|
|
//
|
|
// END Report/Collect and Guard Functions ...
|
|
//
|
|
|
|
//
|
|
// START Alert Functions ...
|
|
//
|
|
|
|
//
|
|
// Send Alert ...
|
|
void XGPUS5IssueAlert(string message)
|
|
{
|
|
//
|
|
// Validate Args ...
|
|
if (StringLen(message) <= 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
if (xGPUS5EnableAlerts)
|
|
{
|
|
SendAlert(message);
|
|
}
|
|
else
|
|
{
|
|
LogMessage(message);
|
|
}
|
|
}
|
|
|
|
//
|
|
// Send Take Profit Alert on Specific Signal ...
|
|
void XGPUS5IssueTPAlert(
|
|
XSignal &signal // Specified Signal
|
|
)
|
|
{
|
|
//
|
|
string message = "";
|
|
|
|
//
|
|
message = xGPUS5Trader.GenerateSignalTPComment(signal);
|
|
|
|
//
|
|
XGPUS5IssueAlert(message);
|
|
}
|
|
|
|
//
|
|
// Send Stop Loss Alert on Specific Signal ...
|
|
void XGPUS5IssueSLAlert(
|
|
XSignal &signal // Specified Signal
|
|
)
|
|
{
|
|
//
|
|
string message = "";
|
|
|
|
//
|
|
message = xGPUS5Trader.GenerateSignalSLComment(signal);
|
|
|
|
//
|
|
XGPUS5IssueAlert(message);
|
|
}
|
|
|
|
//
|
|
// Send Force Close Alert on Specific Signal ...
|
|
void XGPUS5IssueFCAlert(
|
|
XSignal &signal // Specified Signal
|
|
)
|
|
{
|
|
//
|
|
string message = "";
|
|
|
|
//
|
|
message = xGPUS5Trader.GenerateSignalForceCloseComment(signal);
|
|
|
|
//
|
|
XGPUS5IssueAlert(message);
|
|
}
|
|
|
|
//
|
|
// Send Risk Free Alert on Specific Signal ...
|
|
void XGPUS5IssueRFAlert(
|
|
XSignal &signal // Specified Signal
|
|
)
|
|
{
|
|
//
|
|
string message = "";
|
|
|
|
//
|
|
message = xGPUS5Trader.GenerateSignalRFComment(signal, 0);
|
|
|
|
//
|
|
XGPUS5IssueAlert(message);
|
|
}
|
|
|
|
//
|
|
// Send Guard Alert on Specific Signal ...
|
|
void XGPUS5IssueGuardAlert(
|
|
XSignal &signal, // Specified Signal
|
|
ENUM_X_GUARD_REASONS reason, // Specified Guard Reason
|
|
bool isStopOut = false // Specified StopOut Fired or not
|
|
)
|
|
{
|
|
//
|
|
string message = "";
|
|
|
|
//
|
|
message = xGPUS5Guard.GenerateGuardedSignalComment(
|
|
signal,
|
|
reason);
|
|
|
|
//
|
|
XGPUS5IssueAlert(message);
|
|
}
|
|
|
|
//
|
|
// Send Initialization Succeed Alert ...
|
|
void XGPUS5IssueInitializationSucceedAlert()
|
|
{
|
|
//
|
|
string message = "Initializion of (" + XGPUS5ProviderName + " Provider" + " _ " + _Symbol + " _ " + EnumToString(_Period) + ") Succeeded ...";
|
|
XGPUS5IssueAlert(message);
|
|
}
|
|
|
|
//
|
|
// Send De Initialization Succeed Alert ...
|
|
void XGPUS5IssueDeinitializationSucceedAlert()
|
|
{
|
|
//
|
|
string message = "De Initializion of (" + XGPUS5ProviderName + " Provider" + ") Succeeded ...";
|
|
XGPUS5IssueAlert(message);
|
|
}
|
|
|
|
//
|
|
// Send Signal Successfully Execution Alert ...
|
|
void XGPUS5IssueSignalExecutionAlert(
|
|
XSignal &signal, // Specified Signal
|
|
string &providers[] // Signal Providers
|
|
)
|
|
{
|
|
//
|
|
string message = xGPUS5Trader.ToString(signal);
|
|
|
|
//
|
|
string providersMsg = ToString(providers);
|
|
if (StringLen(providersMsg) > 0)
|
|
{
|
|
message += ", Providers: " + providersMsg;
|
|
}
|
|
|
|
//
|
|
XGPUS5IssueAlert(message);
|
|
}
|
|
|
|
//
|
|
// Send Signal Execution Alert ...
|
|
void XGPUS5IssueSignalExecutionError(int error)
|
|
{
|
|
//
|
|
string message = GetSignalExecutionError(error);
|
|
if (StringLen(message) == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
XGPUS5IssueAlert(message);
|
|
}
|
|
|
|
//
|
|
// END Alert Functions ...
|
|
//
|
|
|
|
//
|
|
// END Private Functions ...
|
|
//
|