1374 lines
29 KiB
Plaintext
1374 lines
29 KiB
Plaintext
////////////////////////////////////////////////////////////
|
|
//
|
|
// SaherElm IT Center MQL5 XBTUS5 Signal Provider Library
|
|
// --------------------------------------------------------
|
|
// Name: XBTUS5SignalProvider
|
|
// Description: XBTUS5 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 XBTUS5ProviderName "XBTUS5"
|
|
|
|
//
|
|
// START Inputs ...
|
|
//
|
|
|
|
#include "x-saherelm.xbtus5.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 *xBTUS5Guard;
|
|
XCTrade *xBTUS5Trader;
|
|
XCReporter *xBTUS5Reporter;
|
|
XCCollector *xBTUS5Collector;
|
|
XCAccountInfo *xBTUS5AccountInfo;
|
|
XCVolumeManager *xBTUS5VolumeManager;
|
|
|
|
//
|
|
#include "x-saherelm.xbtus5.provider.indicators.lib.mq5"
|
|
|
|
//
|
|
// Controlling Variables ...
|
|
bool xBTUS5MEnableProvider = false;
|
|
bool xBTUS5MAllowLongTrades = false;
|
|
bool xBTUS5MAllowShortTrades = false;
|
|
|
|
//
|
|
double xBTUS5Deposit = 0;
|
|
double xBTUS5Balance = 0;
|
|
double xBTUS5MinReward = 0;
|
|
double xBTUS5FreeMargin = 0;
|
|
double xBTUS5BalanceForOpenTrades = 0;
|
|
double xBTUS5FreeMarginForOpenTrades = 0;
|
|
|
|
//
|
|
// Guard ...
|
|
double xBTUS5CriticalDrawDown = 0;
|
|
double xBTUS5MaxAllowedDrawDown = 0;
|
|
|
|
//
|
|
double xBTUS5MaxDrawUp = 0;
|
|
double xBTUS5MaxDrawDown = 0;
|
|
|
|
//
|
|
int NOT_ENOUGH_MONEY_ERROR = 4756;
|
|
const string XBTUS5_SUPPORT_ID = "SP";
|
|
|
|
//
|
|
// END Global Definitions: Variables, Properties and etc ...
|
|
//
|
|
|
|
//
|
|
// START Provided Functions ...
|
|
//
|
|
|
|
//
|
|
// Initial Library if required ...
|
|
bool XBTUS5InitSignalProviderLibrary()
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
if (!xBTUS5EnableProvider)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Controlling Variables ...
|
|
xBTUS5MEnableProvider = xBTUS5EnableProvider;
|
|
xBTUS5MAllowLongTrades = xBTUS5AllowLongTrades;
|
|
xBTUS5MAllowShortTrades = xBTUS5AllowShortTrades;
|
|
|
|
//
|
|
// Validate Inputs ...
|
|
result = XBTUS5ValidateInputs();
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Init Indicators ...
|
|
result = XBTUS5InitIndicators();
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
xBTUS5Reporter = new XCReporter();
|
|
xBTUS5Collector = new XCCollector();
|
|
xBTUS5AccountInfo = new XCAccountInfo();
|
|
|
|
//
|
|
// Calculate Account Deposit ...
|
|
xBTUS5Deposit = xBTUS5AccountInfo.GetInitialBalance();
|
|
|
|
//
|
|
// Prepare Min Reward ...
|
|
if (xBTUS5MinRewardInPips > 0)
|
|
{
|
|
xBTUS5MinReward = PipsToPrice(xBTUS5MinRewardInPips);
|
|
}
|
|
|
|
//
|
|
// Calculate Min Balance for Trades ...
|
|
if (xBTUS5BalanceFactorForOpenTrades > 0)
|
|
{
|
|
xBTUS5BalanceForOpenTrades = xBTUS5BalanceFactorForOpenTrades * xBTUS5Deposit;
|
|
}
|
|
|
|
//
|
|
// Claculate Min Free Marging for Trades ...
|
|
if (xBTUS5FreeMarginFactorForOpenTrades > 0)
|
|
{
|
|
xBTUS5FreeMarginForOpenTrades = xBTUS5FreeMarginFactorForOpenTrades * xBTUS5Deposit;
|
|
}
|
|
|
|
//
|
|
// Calculate Init Time Critical DrawDown Value ...
|
|
if (xBTUS5CriticalDrawDownFactor > 0)
|
|
{
|
|
xBTUS5CriticalDrawDown = xBTUS5CriticalDrawDownFactor * xBTUS5Deposit;
|
|
}
|
|
|
|
//
|
|
// Calculate Init Time Max Allowed DrawDown Value ...
|
|
if (xBTUS5MaxAllowedDrawDownFactor > 0)
|
|
{
|
|
xBTUS5MaxAllowedDrawDown = xBTUS5MaxAllowedDrawDownFactor * xBTUS5Deposit;
|
|
}
|
|
|
|
//
|
|
// Instant Volume Manager ...
|
|
xBTUS5VolumeManager = new XCVolumeManager(
|
|
xBTUS5StaticVolumeFactor,
|
|
xBTUS5StaticVolumeMethod,
|
|
xBTUS5VolumeMultiplier,
|
|
xBTUS5ApplyVolumeMultiplierFactor,
|
|
xBTUS5MaxSupportedVolumePerTradeFactor,
|
|
xBTUS5IncreasedMaxSupportedVolumeRate);
|
|
|
|
//
|
|
// Make XCTrader instance ...
|
|
xBTUS5Trader = new XCTrade(
|
|
XBTUS5ProviderName,
|
|
_Symbol,
|
|
xBTUS5Slippage,
|
|
xBTUS5MagicNumber,
|
|
xBTUS5UseVirtualTPSl,
|
|
xBTUS5MaxAllowedTrades,
|
|
xBTUS5FreeMarginForOpenTrades,
|
|
xBTUS5BalanceForOpenTrades);
|
|
|
|
//
|
|
// Instantiate Account Guard ...
|
|
xBTUS5Guard = new XCGuard(
|
|
//
|
|
// Old Trades ...
|
|
xBTUS5MaxAllowedTradeAge,
|
|
xBTUS5OldTradesAction,
|
|
//
|
|
// Critical DrawDown ...
|
|
xBTUS5CriticalDrawDownFactor,
|
|
xBTUS5CriticalGuardAction,
|
|
//
|
|
// Max Allowed DrawDown ...
|
|
xBTUS5MaxAllowedDrawDownFactor,
|
|
xBTUS5MaxAllowedGuardAction,
|
|
xBTUS5GuardStopoutFiredAfterTouches,
|
|
//
|
|
// xBTUS5GuardSupportMethod
|
|
X_SUPPORT_DO_NOTHING
|
|
//
|
|
);
|
|
|
|
//
|
|
// Reset Calculations Buffers and Variables ...
|
|
XBTUS5ResetCollectors();
|
|
|
|
//
|
|
XBTUS5UpdateReports();
|
|
|
|
//
|
|
result = true;
|
|
|
|
//
|
|
// Logging State ...
|
|
XBTUS5IssueInitializationSucceedAlert();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// DeInitial Library if required ...
|
|
void XBTUS5DeinitSignalProviderLibrary(const int reason)
|
|
{
|
|
//
|
|
// Release Handlers ...
|
|
XBTUS5ReleaseIndicators();
|
|
|
|
//
|
|
// Logging State ...
|
|
XBTUS5IssueDeinitializationSucceedAlert();
|
|
}
|
|
|
|
//
|
|
// this is a Globally Function which do all of
|
|
// checkings and positions handling ...
|
|
void XBTUS5SignalProviderHandleTick()
|
|
{
|
|
//
|
|
// Check Provider is Enable or Not ...
|
|
if (!xBTUS5MEnableProvider)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
XBTUS5SignalProviderDoControlState();
|
|
|
|
//
|
|
// Reading Indicator Buffers ...
|
|
XBTUS5HandleReadingBuffers();
|
|
if (xBTUS5CalculatedBars < xBTUS5MaxLengthOfInputs)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Handle Open Trades ...
|
|
XBTUS5HandleOpenTrades();
|
|
|
|
//
|
|
// Handle Close Trades ...
|
|
XBTUS5HandleCloseTrades();
|
|
|
|
//
|
|
// DrawDown and DrawUp ...
|
|
XBTUS5HandleCalculateDrawDownUp();
|
|
}
|
|
|
|
//
|
|
// Do Control Checks here ...
|
|
void XBTUS5SignalProviderDoControlState()
|
|
{
|
|
//
|
|
// Update account Balance ...
|
|
xBTUS5Balance = xBTUS5AccountInfo.GetBalance();
|
|
xBTUS5FreeMargin = xBTUS5AccountInfo.GetFreeMargin();
|
|
|
|
//
|
|
// Check Provider is Enable or Not ...
|
|
if (!xBTUS5MEnableProvider)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Protect Account by Guarding Maximum Drawdown ...
|
|
// TODO: add Guard Result here ...
|
|
string guardMessage = "";
|
|
XGuardHandlerResult guardHandlerResult;
|
|
bool isGuarded = xBTUS5Guard.Guard(
|
|
guardHandlerResult,
|
|
guardMessage,
|
|
xBTUS5Trader,
|
|
_Period);
|
|
if (isGuarded)
|
|
{
|
|
//
|
|
// Criticals ...
|
|
int criticalsCount = ArraySize(guardHandlerResult.criticals);
|
|
if (criticalsCount > 0)
|
|
{
|
|
//
|
|
for (int i = 0; i < criticalsCount; i++)
|
|
{
|
|
//
|
|
XSignal iSignal = guardHandlerResult.criticals[i];
|
|
|
|
//
|
|
XBTUS5OnGuard(
|
|
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];
|
|
|
|
//
|
|
XBTUS5OnGuard(
|
|
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];
|
|
|
|
//
|
|
XBTUS5OnGuard(
|
|
iSignal,
|
|
X_GUARDED_OLD,
|
|
guardMessage);
|
|
}
|
|
}
|
|
}
|
|
|
|
//
|
|
// Signal Protection ...
|
|
XSignal protectSupports[];
|
|
bool isSupported = xBTUS5Guard.ProtectSignals(
|
|
protectSupports,
|
|
xBTUS5Trader);
|
|
if (isSupported)
|
|
{
|
|
//
|
|
LogMessage("Guard do Support Trdae ...");
|
|
}
|
|
|
|
//
|
|
// Handling Signals ...
|
|
XTraderHandlerResult signalHandlerResult;
|
|
xBTUS5Trader.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];
|
|
XBTUS5OnSL(iSignal);
|
|
}
|
|
}
|
|
|
|
//
|
|
// Handle TP Reports ...
|
|
int handledTPsCount = ArraySize(signalHandlerResult.tp);
|
|
if (handledTPsCount > 0)
|
|
{
|
|
//
|
|
for (int i = 0; i < handledTPsCount; i++)
|
|
{
|
|
//
|
|
XSignal iSignal = signalHandlerResult.tp[i];
|
|
XBTUS5OnTP(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];
|
|
XBTUS5OnRF(iSignal);
|
|
}
|
|
}
|
|
}
|
|
|
|
//
|
|
// Handle Trades when OnTrade Event Hnadler Calls ...
|
|
void XBTUS5SignalProviderHandleTrade()
|
|
{
|
|
//
|
|
// Check Provider Enabled ...
|
|
if (!xBTUS5MEnableProvider)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Handle OnTrade Event ...
|
|
XOnTradeHandlerResult result = {};
|
|
xBTUS5Trader.HandleOnTradeEvent(
|
|
result);
|
|
|
|
//
|
|
// Detected TP or SL ...
|
|
if (result.hasNewDeal)
|
|
{
|
|
//
|
|
XDeal last = xBTUS5Trader.GetLastDeal();
|
|
|
|
//
|
|
if (last.reason == DEAL_REASON_SL)
|
|
{
|
|
xBTUS5Reporter.AddTradeSLInReports();
|
|
}
|
|
else if (last.reason == DEAL_REASON_TP)
|
|
{
|
|
xBTUS5Reporter.AddTradeTPInReports();
|
|
}
|
|
}
|
|
}
|
|
|
|
//
|
|
// Handle Open Trades ...
|
|
void XBTUS5HandleOpenTrades()
|
|
{
|
|
//
|
|
// Check Can Trade ...
|
|
bool canTrade = xBTUS5MAllowLongTrades || xBTUS5MAllowShortTrades;
|
|
if (!canTrade)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Check Max Open Trades ...
|
|
int openSignals = xBTUS5Trader.Count();
|
|
bool canOpenPositions = openSignals < xBTUS5MaxAllowedTrades;
|
|
if (!canOpenPositions)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Check Signals Exists ...
|
|
XProvidedSignal longSignalInfo;
|
|
XProvidedSignal shortSignalInfo;
|
|
bool hasLongSignal = XBTUS5CanOpenLongTrade(longSignalInfo);
|
|
bool hasShortSignal = XBTUS5CanOpenShortTrade(shortSignalInfo);
|
|
if (!hasLongSignal && !hasShortSignal)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Update Reports ...
|
|
XBTUS5UpdateReports();
|
|
|
|
//
|
|
xBTUS5WaitUntilNextCandle = true;
|
|
|
|
//
|
|
// Open Long Trades ...
|
|
if (
|
|
//
|
|
hasLongSignal &&
|
|
xBTUS5MAllowLongTrades)
|
|
{
|
|
//
|
|
bool isOpened = XBTUS5OpenLongPosition(longSignalInfo);
|
|
}
|
|
|
|
//
|
|
// Open Short Trades ...
|
|
if (
|
|
//
|
|
hasShortSignal &&
|
|
xBTUS5MAllowShortTrades)
|
|
{
|
|
//
|
|
bool isOpened = XBTUS5OpenShortPosition(shortSignalInfo);
|
|
}
|
|
}
|
|
|
|
//
|
|
// Handle Close Trades ...
|
|
void XBTUS5HandleCloseTrades()
|
|
{
|
|
//
|
|
// Force Close Long Trades ...
|
|
bool isClosedLongTrades = false;
|
|
bool canCloseLongTrades = XBTUS5CanCloseLongTrade();
|
|
if (xBTUS5MAllowLongTrades && canCloseLongTrades)
|
|
{
|
|
//
|
|
ENUM_X_SIGNAL_TYPE type = X_SIGNAL_LONG;
|
|
|
|
//
|
|
XSignal closed[];
|
|
xBTUS5Trader.CloseSignals(
|
|
closed,
|
|
type);
|
|
|
|
//
|
|
int closedTradesCount = ArraySize(closed);
|
|
isClosedLongTrades = closedTradesCount > 0;
|
|
if (isClosedLongTrades)
|
|
{
|
|
//
|
|
for (int i = 0; i < closedTradesCount; i++)
|
|
{
|
|
//
|
|
XSignal iSignal = closed[i];
|
|
XBTUS5OnFC(iSignal);
|
|
}
|
|
}
|
|
}
|
|
|
|
//
|
|
// Force Close Short Trades ...
|
|
bool isClosedShortTrades = false;
|
|
bool canCloseShortTrades = XBTUS5CanCloseShortTrade();
|
|
if (xBTUS5MAllowShortTrades && canCloseShortTrades)
|
|
{
|
|
//
|
|
ENUM_X_SIGNAL_TYPE type = X_SIGNAL_SHORT;
|
|
|
|
//
|
|
XSignal closed[];
|
|
xBTUS5Trader.CloseSignals(
|
|
closed,
|
|
type);
|
|
|
|
//
|
|
int closedTradesCount = ArraySize(closed);
|
|
isClosedShortTrades = closedTradesCount > 0;
|
|
if (isClosedShortTrades)
|
|
{
|
|
//
|
|
for (int i = 0; i < closedTradesCount; i++)
|
|
{
|
|
//
|
|
XSignal iSignal = closed[i];
|
|
XBTUS5OnFC(iSignal);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//
|
|
// Open Long Position ...
|
|
bool XBTUS5OpenLongPosition(
|
|
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 + xBTUS5MinReward;
|
|
double riskFreeRate = xBTUS5RiskFreeRate;
|
|
double riskFreeStep = xBTUS5RiskFreeStepInPips;
|
|
double volume = XBTUS5CalculateVolume(ArraySize(info.signallers));
|
|
|
|
//
|
|
XSignal signal = {};
|
|
signal = xBTUS5Trader.GenerateSignal(
|
|
X_SIGNAL_LONG,
|
|
tp,
|
|
sl,
|
|
volume,
|
|
riskFreeStep,
|
|
riskFreeRate);
|
|
bool isValidSignal = IsValid(
|
|
signal,
|
|
xBTUS5MagicNumber);
|
|
if (!isValidSignal)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
int signalsCount = xBTUS5Trader.Count();
|
|
bool isCross = signalsCount >= 1;
|
|
if (isCross)
|
|
{
|
|
//
|
|
// Handle Cross Signals ...
|
|
XBTUS5HnadleCrossSignal(signal);
|
|
}
|
|
|
|
//
|
|
// Retrieve Can Execute Signal or not ...
|
|
bool canExecuteSignal =
|
|
!isCross
|
|
//
|
|
||
|
|
//
|
|
(
|
|
//
|
|
isCross &&
|
|
xBTUS5CrossSignalsMethod != X_CROSS_SIGNAL_DO_NOTHING
|
|
//
|
|
);
|
|
if (!canExecuteSignal)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Execute Signal ...
|
|
ENUM_EXECUTION_SIGNAL_RESULT executionResult = xBTUS5Trader.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 = xBTUS5Trader.GetSignal(signal.ticket);
|
|
|
|
//
|
|
XBTUS5OnSignal(
|
|
tSignal,
|
|
providers);
|
|
}
|
|
else
|
|
{
|
|
XBTUS5IssueSignalExecutionError(executionResult);
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Open Short Position ...
|
|
bool XBTUS5OpenShortPosition(
|
|
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 - xBTUS5MinReward;
|
|
double riskFreeRate = xBTUS5RiskFreeRate;
|
|
double riskFreeStep = xBTUS5RiskFreeStepInPips;
|
|
double volume = XBTUS5CalculateVolume(ArraySize(info.signallers));
|
|
|
|
//
|
|
XSignal signal = {};
|
|
signal = xBTUS5Trader.GenerateSignal(
|
|
X_SIGNAL_SHORT,
|
|
tp,
|
|
sl,
|
|
volume,
|
|
riskFreeStep,
|
|
riskFreeRate);
|
|
bool isValidSignal = IsValid(
|
|
signal,
|
|
xBTUS5MagicNumber);
|
|
if (!isValidSignal)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
int signalsCount = xBTUS5Trader.Count();
|
|
bool isCross = signalsCount >= 1;
|
|
if (isCross)
|
|
{
|
|
//
|
|
// Handle Cross Signals ...
|
|
XBTUS5HnadleCrossSignal(signal);
|
|
}
|
|
|
|
//
|
|
// Retrieve Can Execute Signal or not ...
|
|
bool canExecuteSignal =
|
|
!isCross
|
|
//
|
|
||
|
|
//
|
|
(
|
|
//
|
|
isCross &&
|
|
xBTUS5CrossSignalsMethod != X_CROSS_SIGNAL_DO_NOTHING
|
|
//
|
|
);
|
|
if (!canExecuteSignal)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Execute Signal ...
|
|
ENUM_EXECUTION_SIGNAL_RESULT executionResult = xBTUS5Trader.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 = xBTUS5Trader.GetSignal(signal.ticket);
|
|
|
|
//
|
|
XBTUS5OnSignal(
|
|
tSignal,
|
|
providers);
|
|
}
|
|
else
|
|
{
|
|
XBTUS5IssueSignalExecutionError(executionResult);
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Apply Cross Signal Actions ...
|
|
void XBTUS5HnadleCrossSignal(
|
|
XSignal &signal // Specified Signal ...
|
|
)
|
|
{
|
|
//
|
|
// Check Cross Signal Handling Enabled ...
|
|
if (xBTUS5CrossSignalsMethod == X_CROSS_SIGNAL_DO_NOTHING)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
// First We have to Change Cross Signals Comment ...
|
|
string comment = xBTUS5Trader.GenerateSignalComment(
|
|
signal,
|
|
true);
|
|
|
|
//
|
|
signal.comment = comment;
|
|
|
|
//
|
|
// If Every thing is Regular, prevent any changes here ...
|
|
if (xBTUS5CrossSignalsMethod == X_CROSS_SIGNAL_REGULAR)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Retrieve Max in Drawdown Signal ...
|
|
XSignal maxInDDSignal = xBTUS5Trader.GetMaxInDrawdownSignal();
|
|
|
|
//
|
|
// Validate Signal ...
|
|
bool isValidSignal =
|
|
maxInDDSignal.ticket > 0 &&
|
|
maxInDDSignal.symbol == signal.symbol &&
|
|
IsValid(maxInDDSignal, xBTUS5MagicNumber);
|
|
if (!isValidSignal)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
double volume = signal.volume;
|
|
bool isDirectional = maxInDDSignal.type == signal.type;
|
|
|
|
//
|
|
// Prevent any changes on Regular Conditions ...
|
|
bool isRegular =
|
|
isDirectional
|
|
? (
|
|
//
|
|
xBTUS5CrossSignalsMethod == X_CROSS_SIGNAL_DIRECTION_REGULAR ||
|
|
xBTUS5CrossSignalsMethod == X_CROSS_SIGNAL_DIRECTION_REGULAR_INDIRECTION_IMPORTANT
|
|
//
|
|
)
|
|
: (
|
|
//
|
|
xBTUS5CrossSignalsMethod == X_CROSS_SIGNAL_INDIRECTION_REGULAR ||
|
|
xBTUS5CrossSignalsMethod == 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 XBTUS5HandleCalculateDrawDownUp()
|
|
{
|
|
//
|
|
XSignal signals[];
|
|
xBTUS5Trader.GetSignals(signals);
|
|
int signalsCount = ArraySize(signals);
|
|
if (signalsCount <= 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
double profit = 0;
|
|
double volume = 0;
|
|
XCollector collector = {};
|
|
double maxAllowedVolume = xBTUS5VolumeManager.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)
|
|
{
|
|
//
|
|
xBTUS5MaxDrawDown =
|
|
xBTUS5MaxDrawDown == 0 ||
|
|
xBTUS5MaxDrawDown > MathAbs(profit)
|
|
? MathAbs(profit)
|
|
: xBTUS5MaxDrawDown;
|
|
|
|
//
|
|
// Update Buffers ...
|
|
datetime time = iTime(_Symbol, _Period, 0);
|
|
|
|
//
|
|
// Calculate Max Allowed Drawdown ...
|
|
//
|
|
collector.time = time;
|
|
collector.volume = volume;
|
|
collector.maxDrawDown = xBTUS5MaxDrawDown;
|
|
collector.maxAllowedVolume = maxAllowedVolume;
|
|
collector.minBalanceForOpenTrade = xBTUS5Trader.GetMinBalanceForOpenTrades();
|
|
}
|
|
else
|
|
{
|
|
//
|
|
xBTUS5MaxDrawUp =
|
|
xBTUS5MaxDrawUp == 0 ||
|
|
xBTUS5MaxDrawUp < profit
|
|
? profit
|
|
: xBTUS5MaxDrawUp;
|
|
}
|
|
|
|
//
|
|
// Update Collector ...
|
|
xBTUS5Collector.Add(collector);
|
|
|
|
//
|
|
// Update Reports ...
|
|
xBTUS5Reporter.UpdateMaxDrawUpInReports(xBTUS5MaxDrawUp);
|
|
xBTUS5Reporter.UpdateMaxDrawDownInReports(xBTUS5MaxDrawDown);
|
|
xBTUS5Reporter.UpdateMaxSameTimeTradesInReports(signalsCount);
|
|
}
|
|
|
|
//
|
|
// START Event Handlers ...
|
|
//
|
|
|
|
//
|
|
// Runs When a Signal Successfully Executed ...
|
|
void XBTUS5OnSignal(
|
|
XSignal &signal, // Specified Signal
|
|
string &providers[] // Signal Providers
|
|
)
|
|
{
|
|
//
|
|
// Issue Propper Alert ...
|
|
XBTUS5IssueSignalExecutionAlert(
|
|
signal,
|
|
providers);
|
|
|
|
//
|
|
// Add Signal To Supports on Guard ...
|
|
xBTUS5Guard.AddSupport(signal);
|
|
|
|
//
|
|
// Update Reports ...
|
|
xBTUS5Reporter.AddSignalToReports(
|
|
signal,
|
|
providers);
|
|
}
|
|
|
|
//
|
|
// Runs when a Position Got Take Profit ...
|
|
void XBTUS5OnTP(
|
|
XSignal &signal // Specified Signal
|
|
)
|
|
{
|
|
//
|
|
// Issue Propper Alert ...
|
|
XBTUS5IssueTPAlert(signal);
|
|
|
|
//
|
|
// Remove Signal From Guard Protector ...
|
|
xBTUS5Guard.Remove(signal.ticket);
|
|
xBTUS5Guard.RemoveSupport(signal.ticket);
|
|
|
|
//
|
|
// Update Reports ...
|
|
xBTUS5Reporter.AddTradeTPInReports();
|
|
XBTUS5UpdateReports();
|
|
}
|
|
|
|
//
|
|
// Runs when a Position Got Stop Loss ...
|
|
void XBTUS5OnSL(
|
|
XSignal &signal // Specified Signal
|
|
)
|
|
{
|
|
//
|
|
// Issue Propper Alert ...
|
|
XBTUS5IssueSLAlert(signal);
|
|
|
|
//
|
|
// Remove Signal From Guard Protector ...
|
|
xBTUS5Guard.Remove(signal.ticket);
|
|
xBTUS5Guard.RemoveSupport(signal.ticket);
|
|
|
|
//
|
|
// Update Reports ...
|
|
xBTUS5Reporter.AddTradeSLInReports();
|
|
XBTUS5UpdateReports();
|
|
}
|
|
|
|
//
|
|
// Runs when a Position Risk Free ...
|
|
void XBTUS5OnRF(
|
|
XSignal &signal // Specified Signal
|
|
)
|
|
{
|
|
//
|
|
// Issue Propper Alert ...
|
|
XBTUS5IssueRFAlert(signal);
|
|
|
|
//
|
|
// Remove Signal From Guard Protector ...
|
|
// since RiskFree Signals has SL ...
|
|
xBTUS5Guard.Remove(signal.ticket);
|
|
xBTUS5Guard.RemoveSupport(signal.ticket);
|
|
|
|
//
|
|
// Update Reports ...
|
|
xBTUS5Reporter.AddTradeSLInReports();
|
|
XBTUS5UpdateReports();
|
|
}
|
|
|
|
//
|
|
// Runs when a Position Force Closed ...
|
|
void XBTUS5OnFC(
|
|
XSignal &signal // Specified Signal
|
|
)
|
|
{
|
|
//
|
|
// Issue Propper Alert ...
|
|
XBTUS5IssueFCAlert(signal);
|
|
|
|
//
|
|
// Remove Signal From Guard Protector ...
|
|
xBTUS5Guard.Remove(signal.ticket);
|
|
xBTUS5Guard.RemoveSupport(signal.ticket);
|
|
|
|
//
|
|
// Update Reports ...
|
|
xBTUS5Reporter.AddTradeSLInReports();
|
|
XBTUS5UpdateReports();
|
|
}
|
|
|
|
//
|
|
// Runs when a Position Guarded ...
|
|
void XBTUS5OnGuard(
|
|
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);
|
|
|
|
//
|
|
xBTUS5MEnableProvider = false;
|
|
}
|
|
|
|
//
|
|
// Issue Propper Alert ...
|
|
XBTUS5IssueGuardAlert(signal, reason, isStopOut);
|
|
|
|
//
|
|
// Update Reports ...
|
|
xBTUS5Reporter.AddTradeSLInReports();
|
|
XBTUS5UpdateReports();
|
|
}
|
|
|
|
//
|
|
// END Event Handlers ...
|
|
//
|
|
|
|
//
|
|
// END Provided Functions ...
|
|
//
|
|
|
|
//
|
|
// START Private Functions ...
|
|
//
|
|
|
|
//
|
|
// START Volume Baseed Functions ...
|
|
//
|
|
|
|
//
|
|
// Calculating Volume for Tradings ...
|
|
double XBTUS5CalculateVolume(
|
|
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 = xBTUS5Trader.Count();
|
|
|
|
//
|
|
double result = xBTUS5VolumeManager.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 XBTUS5UpdateReports()
|
|
{
|
|
//
|
|
double staticVolume = xBTUS5VolumeManager.GetStaticVolume();
|
|
string staticVolumeCalculationMethod = EnumToString(xBTUS5StaticVolumeMethod);
|
|
double maxAllowedVolume = xBTUS5VolumeManager.GetMaxAllowedVolume();
|
|
double volumeIncreaseMultiplier = xBTUS5VolumeManager.GetVolumeMultiplier();
|
|
double balanceGowingToApplyMultiplier = xBTUS5VolumeManager.GetIncreaseMultiplierBalance();
|
|
|
|
double balanceGrowingToIncreaseMaxAllowedVolume = xBTUS5VolumeManager.GetIncreaseMaxAllowedVolumeBalance();
|
|
|
|
//
|
|
// Update Reports Volume Info ...
|
|
xBTUS5Reporter.UpdateReportsVolumeInfo(
|
|
staticVolume,
|
|
staticVolumeCalculationMethod,
|
|
maxAllowedVolume,
|
|
volumeIncreaseMultiplier,
|
|
balanceGowingToApplyMultiplier,
|
|
balanceGrowingToIncreaseMaxAllowedVolume);
|
|
|
|
//
|
|
// Calculate Max Allowed Drawdown ...
|
|
double minBalanceForTradeBasedOnBalance = xBTUS5Trader.GetMinBalanceForOpenTrades();
|
|
xBTUS5Reporter.UpdateMinBalanceForOpenTradesInReports(minBalanceForTradeBasedOnBalance);
|
|
}
|
|
|
|
//
|
|
// Reset XBTUS5 Info Buffers ...
|
|
void XBTUS5ResetCollectors()
|
|
{
|
|
//
|
|
// Reset Calculations Buffers and Variables ...
|
|
xBTUS5MaxDrawUp = 0;
|
|
xBTUS5MaxDrawDown = 0;
|
|
|
|
//
|
|
xBTUS5Trader.Reset();
|
|
xBTUS5Reporter.Reset();
|
|
xBTUS5Collector.Reset();
|
|
xBTUS5VolumeManager.ResetForceVolumeMultiplier();
|
|
}
|
|
|
|
//
|
|
// END Report/Collect and Guard Functions ...
|
|
//
|
|
|
|
//
|
|
// START Alert Functions ...
|
|
//
|
|
|
|
//
|
|
// Send Alert ...
|
|
void XBTUS5IssueAlert(string message)
|
|
{
|
|
//
|
|
// Validate Args ...
|
|
if (StringLen(message) <= 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
if (xBTUS5EnableAlerts)
|
|
{
|
|
SendAlert(message);
|
|
}
|
|
else
|
|
{
|
|
LogMessage(message);
|
|
}
|
|
}
|
|
|
|
//
|
|
// Send Take Profit Alert on Specific Signal ...
|
|
void XBTUS5IssueTPAlert(
|
|
XSignal &signal // Specified Signal
|
|
)
|
|
{
|
|
//
|
|
string message = "";
|
|
|
|
//
|
|
message = xBTUS5Trader.GenerateSignalTPComment(signal);
|
|
|
|
//
|
|
XBTUS5IssueAlert(message);
|
|
}
|
|
|
|
//
|
|
// Send Stop Loss Alert on Specific Signal ...
|
|
void XBTUS5IssueSLAlert(
|
|
XSignal &signal // Specified Signal
|
|
)
|
|
{
|
|
//
|
|
string message = "";
|
|
|
|
//
|
|
message = xBTUS5Trader.GenerateSignalSLComment(signal);
|
|
|
|
//
|
|
XBTUS5IssueAlert(message);
|
|
}
|
|
|
|
//
|
|
// Send Force Close Alert on Specific Signal ...
|
|
void XBTUS5IssueFCAlert(
|
|
XSignal &signal // Specified Signal
|
|
)
|
|
{
|
|
//
|
|
string message = "";
|
|
|
|
//
|
|
message = xBTUS5Trader.GenerateSignalForceCloseComment(signal);
|
|
|
|
//
|
|
XBTUS5IssueAlert(message);
|
|
}
|
|
|
|
//
|
|
// Send Risk Free Alert on Specific Signal ...
|
|
void XBTUS5IssueRFAlert(
|
|
XSignal &signal // Specified Signal
|
|
)
|
|
{
|
|
//
|
|
string message = "";
|
|
|
|
//
|
|
message = xBTUS5Trader.GenerateSignalRFComment(signal, 0);
|
|
|
|
//
|
|
XBTUS5IssueAlert(message);
|
|
}
|
|
|
|
//
|
|
// Send Guard Alert on Specific Signal ...
|
|
void XBTUS5IssueGuardAlert(
|
|
XSignal &signal, // Specified Signal
|
|
ENUM_X_GUARD_REASONS reason, // Specified Guard Reason
|
|
bool isStopOut = false // Specified StopOut Fired or not
|
|
)
|
|
{
|
|
//
|
|
string message = "";
|
|
|
|
//
|
|
message = xBTUS5Guard.GenerateGuardedSignalComment(
|
|
signal,
|
|
reason);
|
|
|
|
//
|
|
XBTUS5IssueAlert(message);
|
|
}
|
|
|
|
//
|
|
// Send Initialization Succeed Alert ...
|
|
void XBTUS5IssueInitializationSucceedAlert()
|
|
{
|
|
//
|
|
string message = "Initializion of (" + XBTUS5ProviderName + " Provider" + " _ " + _Symbol + " _ " + EnumToString(_Period) + ") Succeeded ...";
|
|
XBTUS5IssueAlert(message);
|
|
}
|
|
|
|
//
|
|
// Send De Initialization Succeed Alert ...
|
|
void XBTUS5IssueDeinitializationSucceedAlert()
|
|
{
|
|
//
|
|
string message = "De Initializion of (" + XBTUS5ProviderName + " Provider" + ") Succeeded ...";
|
|
XBTUS5IssueAlert(message);
|
|
}
|
|
|
|
//
|
|
// Send Signal Successfully Execution Alert ...
|
|
void XBTUS5IssueSignalExecutionAlert(
|
|
XSignal &signal, // Specified Signal
|
|
string &providers[] // Signal Providers
|
|
)
|
|
{
|
|
//
|
|
string message = xBTUS5Trader.ToString(signal);
|
|
|
|
//
|
|
string providersMsg = ToString(providers);
|
|
if (StringLen(providersMsg) > 0)
|
|
{
|
|
message += ", Providers: " + providersMsg;
|
|
}
|
|
|
|
//
|
|
XBTUS5IssueAlert(message);
|
|
}
|
|
|
|
//
|
|
// Send Signal Execution Alert ...
|
|
void XBTUS5IssueSignalExecutionError(int error)
|
|
{
|
|
//
|
|
string message = GetSignalExecutionError(error);
|
|
if (StringLen(message) == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
XBTUS5IssueAlert(message);
|
|
}
|
|
|
|
//
|
|
// END Alert Functions ...
|
|
//
|
|
|
|
//
|
|
// END Private Functions ...
|
|
//
|