1673 lines
34 KiB
Plaintext
1673 lines
34 KiB
Plaintext
///////////////////////////////////////////////////////
|
|
//
|
|
// SaherElm IT Center MQL5 Class Library
|
|
// --------------------------------------
|
|
// Name: XCRestrictions
|
|
// Description: provides all Requirements for Apply Restrictions ...
|
|
//
|
|
//
|
|
// 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
|
|
|
|
//
|
|
// Imports ...
|
|
#include "../Classes/x-saherelm.base.class.mq5"
|
|
#include "../Libraries/x-saherelm.x-trade.lib.mq5"
|
|
|
|
// #region Definitions ...
|
|
//
|
|
// Awailable Restrictions Period ...
|
|
enum ENUM_X_TRADE_RESTRICTIONS_PERIOD
|
|
{
|
|
X_TRADE_RESTRICATION_NONE, // None
|
|
X_TRADE_RESTRICATION_HOURLY, // Per Hour
|
|
X_TRADE_RESTRICATION_DAILY, // Per Day
|
|
X_TRADE_RESTRICATION_WEEKLY, // Per Week
|
|
X_TRADE_RESTRICATION_MONTHLY, // Per Month
|
|
};
|
|
|
|
// #region Extensions ...
|
|
//
|
|
// Converts to String ...
|
|
string ToXString(ENUM_X_TRADE_RESTRICTIONS_PERIOD value)
|
|
{
|
|
//
|
|
string result = NULL;
|
|
|
|
//
|
|
result = EnumToString(value);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Check Has Restrictions or not ...
|
|
bool HasXRestrictions(ENUM_X_TRADE_RESTRICTIONS_PERIOD value)
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
result = value != X_TRADE_RESTRICATION_NONE;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Get Restrictions Period Delay in Seconds ...
|
|
*
|
|
* @param value: ENUM_X_TRADE_RESTRICTIONS_PERIOD ...
|
|
*
|
|
* @return ( int )
|
|
*/
|
|
int RestrictionsPeriodSeconds(ENUM_X_TRADE_RESTRICTIONS_PERIOD value)
|
|
{
|
|
//
|
|
int result = 0;
|
|
|
|
//
|
|
// Check Restrictions is Valid or not ...
|
|
if (!HasXRestrictions(value))
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
switch (value)
|
|
{
|
|
//
|
|
default:
|
|
case X_TRADE_RESTRICATION_NONE:
|
|
result = 0;
|
|
break;
|
|
|
|
//
|
|
case X_TRADE_RESTRICATION_HOURLY:
|
|
result = PeriodSeconds(PERIOD_H1);
|
|
break;
|
|
|
|
//
|
|
case X_TRADE_RESTRICATION_DAILY:
|
|
result = PeriodSeconds(PERIOD_D1);
|
|
break;
|
|
|
|
//
|
|
case X_TRADE_RESTRICATION_WEEKLY:
|
|
result = PeriodSeconds(PERIOD_W1);
|
|
break;
|
|
|
|
//
|
|
case X_TRADE_RESTRICATION_MONTHLY:
|
|
result = PeriodSeconds(PERIOD_MN1);
|
|
break;
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Calculate Restrictions End Time ...
|
|
*
|
|
* @param value: ENUM_X_TRADE_RESTRICTIONS_PERIOD ...
|
|
* @param multiplier: int, Restrictions Period Multiplier ...
|
|
* @param fromTime: Restrictions Starts Time ...
|
|
*
|
|
* @return ( datetime )
|
|
*/
|
|
datetime RestrictionsEndTime(
|
|
ENUM_X_TRADE_RESTRICTIONS_PERIOD value,
|
|
int multiplier = 1,
|
|
datetime fromTime = NULL //
|
|
)
|
|
{
|
|
//
|
|
datetime result = NULL;
|
|
|
|
//
|
|
// Normalize Multiplier ...
|
|
if (multiplier < 1)
|
|
{
|
|
multiplier = 1;
|
|
}
|
|
if (!IsXValid(fromTime))
|
|
{
|
|
fromTime = TimeCurrent();
|
|
}
|
|
|
|
//
|
|
// Calculate Requirements ...
|
|
int periodSeconds = RestrictionsPeriodSeconds(value);
|
|
int timeInSeconds = (int)fromTime + (periodSeconds * multiplier);
|
|
MqlDateTime timeStruct;
|
|
bool hasStruct = TimeToStruct(
|
|
(datetime)timeInSeconds,
|
|
timeStruct //
|
|
);
|
|
if (hasStruct)
|
|
{
|
|
//
|
|
timeStruct.min = 0;
|
|
timeStruct.sec = 0;
|
|
|
|
//
|
|
result = StructToTime(timeStruct);
|
|
}
|
|
else
|
|
{
|
|
result = (datetime)timeInSeconds;
|
|
}
|
|
|
|
//
|
|
ZeroMemory(timeStruct);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
// #endregion
|
|
|
|
// #region Event Handler Types ...
|
|
typedef void (*TRestrictionEvent)();
|
|
// #endregion
|
|
// #endregion
|
|
|
|
//
|
|
// Implementation ...
|
|
class XCRestrictions : public XCBase
|
|
{
|
|
//
|
|
// Public ...
|
|
public:
|
|
//
|
|
// Props ...
|
|
|
|
//
|
|
// Constructor ...
|
|
XCRestrictions()
|
|
{
|
|
//
|
|
Clean();
|
|
Default();
|
|
}
|
|
|
|
//
|
|
// Deconstructor ...
|
|
~XCRestrictions()
|
|
{
|
|
//
|
|
Clean();
|
|
}
|
|
|
|
// #region Properties ...
|
|
// #region RestrictionsPeriod ...
|
|
/**
|
|
* Get Restrictions Period ...
|
|
*
|
|
* @return ( ENUM_X_TRADE_RESTRICTIONS_PERIOD )
|
|
*/
|
|
ENUM_X_TRADE_RESTRICTIONS_PERIOD GetRestrictionsPeriod()
|
|
{
|
|
return mRestrictionsPeriod;
|
|
}
|
|
|
|
/**
|
|
* Get Restrictions Period ...
|
|
*
|
|
* @param value: ENUM_X_TRADE_RESTRICTIONS_PERIOD
|
|
*/
|
|
void SetRestrictionsPeriod(ENUM_X_TRADE_RESTRICTIONS_PERIOD value)
|
|
{
|
|
mRestrictionsPeriod = value;
|
|
}
|
|
// #endregion
|
|
|
|
// #region Restrictions Period Multiplier ...
|
|
/**
|
|
* Get Restrictions Period Multiplier ...
|
|
*
|
|
* @return ( int )
|
|
*/
|
|
int GetRestrictionsPeriodMultiplier()
|
|
{
|
|
return mRestrictionsPeriodMultiplier;
|
|
}
|
|
|
|
/**
|
|
* Set Restrictions Period Multiplier ...
|
|
*
|
|
* @param value: int ...
|
|
*/
|
|
void SetRestrictionsPeriodMultiplier(int value)
|
|
{
|
|
//
|
|
if (value < 1)
|
|
{
|
|
value = 1;
|
|
}
|
|
|
|
//
|
|
mRestrictionsPeriodMultiplier = value;
|
|
}
|
|
// #endregion
|
|
|
|
// #region Signals ...
|
|
/**
|
|
* Get Issued Signals ...
|
|
*
|
|
* @return ( int )
|
|
*/
|
|
int GetSignals()
|
|
{
|
|
return mSignals;
|
|
}
|
|
|
|
/**
|
|
* Get Issued Long Signals ...
|
|
*
|
|
* @return ( int )
|
|
*/
|
|
int GetLongSignals()
|
|
{
|
|
return mLongSignals;
|
|
}
|
|
|
|
/**
|
|
* Issued Short Signals ...
|
|
*
|
|
* @return ( int )
|
|
*/
|
|
int GetShortSignals()
|
|
{
|
|
return mShortSignals;
|
|
}
|
|
|
|
/**
|
|
* Get Issued Signals in Restrictions Period ...
|
|
*
|
|
* @return ( int )
|
|
*/
|
|
int GetRestrictedSignals()
|
|
{
|
|
return mRestrictedSignals;
|
|
}
|
|
|
|
/**
|
|
* Get Issued Long Signals in Restrictions Period ...
|
|
*
|
|
* @return ( int )
|
|
*/
|
|
int GetRestrictedLongSignals()
|
|
{
|
|
return mRestrictedLongSignals;
|
|
}
|
|
|
|
/**
|
|
* Get Issued Short Signals in Restrictions Period ...
|
|
*
|
|
* @return ( int )
|
|
*/
|
|
int GetRestrictedShortSignals()
|
|
{
|
|
return mRestrictedShortSignals;
|
|
}
|
|
|
|
/**
|
|
* Get Allowed Signals in Restrictions Period ...
|
|
*
|
|
* @return ( int )
|
|
*/
|
|
int GetAllowedSignalsInRestrictions()
|
|
{
|
|
return mAllowedSignalsInRestrictions;
|
|
}
|
|
|
|
/**
|
|
* Set Allowed Signals in Restrictions Period ...
|
|
*
|
|
* @param value: int ...
|
|
*/
|
|
void SetAllowedSignalsInRestrictions(int value)
|
|
{
|
|
//
|
|
if (value < 0)
|
|
{
|
|
value = 0;
|
|
}
|
|
|
|
//
|
|
mAllowedSignalsInRestrictions = value;
|
|
}
|
|
|
|
/**
|
|
* Get Allowed Long Signals in Restrictions Period ...
|
|
*
|
|
* @return ( int )
|
|
*/
|
|
int GetAllowedLongSignalsInRestrictions()
|
|
{
|
|
return mAllowedLongSignalsInRestrictions;
|
|
}
|
|
|
|
/**
|
|
* Set Allowed Long Signals in Restrictions Period ...
|
|
*
|
|
* @param value: int ...
|
|
*/
|
|
void SetAllowedLongSignalsInRestrictions(int value)
|
|
{
|
|
//
|
|
if (value < 0)
|
|
{
|
|
value = 0;
|
|
}
|
|
|
|
//
|
|
mAllowedLongSignalsInRestrictions = value;
|
|
}
|
|
|
|
/**
|
|
* Get Allowed Short Signals in Restrictions Period ...
|
|
*
|
|
* @return ( int )
|
|
*/
|
|
int GetAllowedShortSignalsInRestrictions()
|
|
{
|
|
return mAllowedShortSignalsInRestrictions;
|
|
}
|
|
|
|
/**
|
|
* Set Allowed Short Signals in Restrictions Period ...
|
|
*
|
|
* @param value: int ...
|
|
*/
|
|
void SetAllowedShortSignalsInRestrictions(int value)
|
|
{
|
|
//
|
|
if (value < 0)
|
|
{
|
|
value = 0;
|
|
}
|
|
|
|
//
|
|
mAllowedShortSignalsInRestrictions = value;
|
|
}
|
|
// #endregion
|
|
|
|
// #region Trades ...
|
|
/**
|
|
* Executed Trades ...
|
|
*
|
|
* @return ( int )
|
|
*/
|
|
int GetTrades()
|
|
{
|
|
return mTrades;
|
|
}
|
|
|
|
/**
|
|
* Executed Long Trades ...
|
|
*
|
|
* @return ( int )
|
|
*/
|
|
int GetLongTrades()
|
|
{
|
|
return mLongTrades;
|
|
}
|
|
|
|
/**
|
|
* Executed Short Trades ...
|
|
*
|
|
* @return ( int )
|
|
*/
|
|
int GetShortTrades()
|
|
{
|
|
return mShortTrades;
|
|
}
|
|
|
|
/**
|
|
* Executed Trades in Restrictions Period ...
|
|
*
|
|
* @return ( int )
|
|
*/
|
|
int GetRestrictedTrades()
|
|
{
|
|
return mRestrictedTrades;
|
|
}
|
|
|
|
/**
|
|
* Executed Long Trades in Restrictions Period ...
|
|
*
|
|
* @return ( int )
|
|
*/
|
|
int GetRestrictedLongTrades()
|
|
{
|
|
return mRestrictedLongTrades;
|
|
}
|
|
|
|
/**
|
|
* Executed Short Trades in Restrictions Period ...
|
|
*
|
|
* @return ( int )
|
|
*/
|
|
int GetRestrictedShortTrades()
|
|
{
|
|
return mRestrictedShortTrades;
|
|
}
|
|
|
|
/**
|
|
* Get Allowed Trades in Restrictions Period ...
|
|
*
|
|
* @return ( int )
|
|
*/
|
|
int GetAllowedTradesInRestrictions()
|
|
{
|
|
return mAllowedTradesInRestrictions;
|
|
}
|
|
|
|
/**
|
|
* Set Allowed Trades in Restrictions Period ...
|
|
*
|
|
* @param value: int ...
|
|
*/
|
|
void SetAllowedTradesInRestrictions(int value)
|
|
{
|
|
//
|
|
if (value < 0)
|
|
{
|
|
value = 0;
|
|
}
|
|
|
|
//
|
|
mAllowedTradesInRestrictions = value;
|
|
}
|
|
|
|
/**
|
|
* Get Allowed Long Trades in Restrictions Period ...
|
|
*
|
|
* @return ( int )
|
|
*/
|
|
int GetAllowedLongTradesInRestrictions()
|
|
{
|
|
return mAllowedLongTradesInRestrictions;
|
|
}
|
|
|
|
/**
|
|
* Set Allowed Long Trades in Restrictions Period ...
|
|
*
|
|
* @param value: int ...
|
|
*/
|
|
void SetAllowedLongTradesInRestrictions(int value)
|
|
{
|
|
//
|
|
if (value < 0)
|
|
{
|
|
value = 0;
|
|
}
|
|
|
|
//
|
|
mAllowedLongTradesInRestrictions = value;
|
|
}
|
|
|
|
/**
|
|
* Get Allowed Short Trades in Restrictions Period ...
|
|
*
|
|
* @return ( int )
|
|
*/
|
|
int GetAllowedShortTradesInRestrictions()
|
|
{
|
|
return mAllowedShortTradesInRestrictions;
|
|
}
|
|
|
|
/**
|
|
* Set Allowed Short Trades in Restrictions Period ...
|
|
*
|
|
* @param value: int ...
|
|
*/
|
|
void SetAllowedShortTradesInRestrictions(int value)
|
|
{
|
|
//
|
|
if (value < 0)
|
|
{
|
|
value = 0;
|
|
}
|
|
|
|
//
|
|
mAllowedShortTradesInRestrictions = value;
|
|
}
|
|
// #endregion
|
|
|
|
// #region Wins ...
|
|
/**
|
|
* Touched Wins ...
|
|
*
|
|
* @return ( int )
|
|
*/
|
|
int GetWins()
|
|
{
|
|
return mWins;
|
|
}
|
|
|
|
/**
|
|
* Touched Long Wins ...
|
|
*
|
|
* @return ( int )
|
|
*/
|
|
int GetLongWins()
|
|
{
|
|
return mLongWins;
|
|
}
|
|
|
|
/**
|
|
* Touched Short Wins ...
|
|
*
|
|
* @return ( int )
|
|
*/
|
|
int GetShortWins()
|
|
{
|
|
return mShortWins;
|
|
}
|
|
|
|
/**
|
|
* Touched Wins in Restrictions Period ...
|
|
*
|
|
* @return ( int )
|
|
*/
|
|
int GetRestrictedWins()
|
|
{
|
|
return mRestrictedWins;
|
|
}
|
|
|
|
/**
|
|
* Touched Long Wins in Restrictions Period ...
|
|
*
|
|
* @return ( int )
|
|
*/
|
|
int GetRestrictedLongWins()
|
|
{
|
|
return mRestrictedLongWins;
|
|
}
|
|
|
|
/**
|
|
* Touched Short Wins in Restrictions Period ...
|
|
*
|
|
* @return ( int )
|
|
*/
|
|
int GetRestrictedShortWins()
|
|
{
|
|
return mRestrictedShortWins;
|
|
}
|
|
|
|
/**
|
|
* Get Allowed Wins in Restrictions Period ...
|
|
*
|
|
* @return ( int )
|
|
*/
|
|
int GetAllowedWinsInRestrictions()
|
|
{
|
|
return mAllowedWinsInRestrictions;
|
|
}
|
|
|
|
/**
|
|
* Set Allowed Wins in Restrictions Period ...
|
|
*
|
|
* @param value: int ...
|
|
*/
|
|
void SetAllowedWinsInRestrictions(int value)
|
|
{
|
|
//
|
|
if (value < 0)
|
|
{
|
|
value = 0;
|
|
}
|
|
|
|
//
|
|
mAllowedWinsInRestrictions = value;
|
|
}
|
|
|
|
/**
|
|
* Get Allowed Long Wins in Restrictions Period ...
|
|
*
|
|
* @return ( int )
|
|
*/
|
|
int GetAllowedLongWinsInRestrictions()
|
|
{
|
|
return mAllowedLongWinsInRestrictions;
|
|
}
|
|
|
|
/**
|
|
* Set Allowed Long Wins in Restrictions Period ...
|
|
*
|
|
* @param value: int ...
|
|
*/
|
|
void SetAllowedLongWinsInRestrictions(int value)
|
|
{
|
|
//
|
|
if (value < 0)
|
|
{
|
|
value = 0;
|
|
}
|
|
|
|
//
|
|
mAllowedLongWinsInRestrictions = value;
|
|
}
|
|
|
|
/**
|
|
* Get Allowed Short Wins in Restrictions Period ...
|
|
*
|
|
* @return ( int )
|
|
*/
|
|
int GetAllowedShortWinsInRestrictions()
|
|
{
|
|
return mAllowedShortWinsInRestrictions;
|
|
}
|
|
|
|
/**
|
|
* Set Allowed Short Wins in Restrictions Period ...
|
|
*
|
|
* @param value: int ...
|
|
*/
|
|
void SetAllowedShortWinsInRestrictions(int value)
|
|
{
|
|
//
|
|
if (value < 0)
|
|
{
|
|
value = 0;
|
|
}
|
|
|
|
//
|
|
mAllowedShortWinsInRestrictions = value;
|
|
}
|
|
// #endregion
|
|
|
|
// #region Losess ...
|
|
/**
|
|
* Touched Loses ...
|
|
*
|
|
* @return ( int )
|
|
*/
|
|
int GetLoses()
|
|
{
|
|
return mLoses;
|
|
}
|
|
|
|
/**
|
|
* Touched Long Loses ...
|
|
*
|
|
* @return ( int )
|
|
*/
|
|
int GetLongLoses()
|
|
{
|
|
return mLongLoses;
|
|
}
|
|
|
|
/**
|
|
* Touched Short Loses ...
|
|
*
|
|
* @return ( int )
|
|
*/
|
|
int GetShortLoses()
|
|
{
|
|
return mShortLoses;
|
|
}
|
|
|
|
/**
|
|
* Touched Loses in Restrictions Period ...
|
|
*
|
|
* @return ( int )
|
|
*/
|
|
int GetRestrictedLoses()
|
|
{
|
|
return mRestrictedLoses;
|
|
}
|
|
|
|
/**
|
|
* Touched Long Loses in Restrictions Period ...
|
|
*
|
|
* @return ( int )
|
|
*/
|
|
int GetRestrictedLongLoses()
|
|
{
|
|
return mRestrictedLongLoses;
|
|
}
|
|
|
|
/**
|
|
* Touched Short Loses in Restrictions Period ...
|
|
*
|
|
* @return ( int )
|
|
*/
|
|
int GetRestrictedShortLoses()
|
|
{
|
|
return mRestrictedShortLoses;
|
|
}
|
|
|
|
/**
|
|
* Get Allowed Loses in Restrictions Period ...
|
|
*
|
|
* @return ( int )
|
|
*/
|
|
int GetAllowedLosesInRestrictions()
|
|
{
|
|
return mAllowedLosesInRestrictions;
|
|
}
|
|
|
|
/**
|
|
* Set Allowed Loses in Restrictions Period ...
|
|
*
|
|
* @param value: int ...
|
|
*/
|
|
void SetAllowedLosesInRestrictions(int value)
|
|
{
|
|
//
|
|
if (value < 0)
|
|
{
|
|
value = 0;
|
|
}
|
|
|
|
//
|
|
mAllowedLosesInRestrictions = value;
|
|
}
|
|
|
|
/**
|
|
* Get Allowed Long Loses in Restrictions Period ...
|
|
*
|
|
* @return ( int )
|
|
*/
|
|
int GetAllowedLongLosesInRestrictions()
|
|
{
|
|
return mAllowedLongLosesInRestrictions;
|
|
}
|
|
|
|
/**
|
|
* Set Allowed Long Loses in Restrictions Period ...
|
|
*
|
|
* @param value: int ...
|
|
*/
|
|
void SetAllowedLongLosesInRestrictions(int value)
|
|
{
|
|
//
|
|
if (value < 0)
|
|
{
|
|
value = 0;
|
|
}
|
|
|
|
//
|
|
mAllowedLongLosesInRestrictions = value;
|
|
}
|
|
|
|
/**
|
|
* Get Allowed Short Loses in Restrictions Period ...
|
|
*
|
|
* @return ( int )
|
|
*/
|
|
int GetAllowedShortLosesInRestrictions()
|
|
{
|
|
return mAllowedShortLosesInRestrictions;
|
|
}
|
|
|
|
/**
|
|
* Set Allowed Short Loses in Restrictions Period ...
|
|
*
|
|
* @param value: int ...
|
|
*/
|
|
void SetAllowedShortLosesInRestrictions(int value)
|
|
{
|
|
//
|
|
if (value < 0)
|
|
{
|
|
value = 0;
|
|
}
|
|
|
|
//
|
|
mAllowedShortLosesInRestrictions = value;
|
|
}
|
|
// #endregion
|
|
|
|
// #region Current Restrictions ...
|
|
/**
|
|
* Get Current Restrictions End Time ...
|
|
*
|
|
* @return ( datetime )
|
|
*/
|
|
datetime GetCurrentRestrictionsEnd()
|
|
{
|
|
return mRestrictionsEnd;
|
|
}
|
|
|
|
/**
|
|
* Get Current Restrictions Start Time ...
|
|
*
|
|
* @return ( datetime )
|
|
*/
|
|
datetime GetCurrentRestrictionsStart()
|
|
{
|
|
return mRestrictionsStart;
|
|
}
|
|
// #endregion
|
|
// #endregion
|
|
|
|
// #region Tools ...
|
|
/**
|
|
* Check Restrictions Exists or not ...
|
|
*
|
|
* @return ( bool )
|
|
*/
|
|
bool HasRestrictions()
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
result =
|
|
HasXRestrictions(mRestrictionsPeriod);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Handle Signal Issued ...
|
|
*
|
|
* @param signal: XSignal ...
|
|
*/
|
|
void HandleSignal(XSignal &signal)
|
|
{
|
|
//
|
|
if (!signal.IsValid())
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
bool isLong = IsLong(signal.type);
|
|
|
|
//
|
|
mSignals++;
|
|
|
|
//
|
|
if (isLong)
|
|
{
|
|
mLongSignals++;
|
|
}
|
|
else
|
|
{
|
|
mShortSignals++;
|
|
}
|
|
|
|
//
|
|
if (HasRestrictions())
|
|
{
|
|
//
|
|
mRestrictedSignals++;
|
|
|
|
//
|
|
if (isLong)
|
|
{
|
|
mRestrictedLongSignals++;
|
|
}
|
|
else
|
|
{
|
|
mRestrictedShortSignals++;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Handle Signal Execution ...
|
|
*
|
|
* @param signal: XSignal ...
|
|
*/
|
|
void HandleTrade(XSignal &signal)
|
|
{
|
|
//
|
|
if (!signal.IsValid())
|
|
{
|
|
return;
|
|
}
|
|
bool isLong = IsLong(signal.type);
|
|
|
|
//
|
|
mTrades++;
|
|
|
|
//
|
|
if (isLong)
|
|
{
|
|
mLongTrades++;
|
|
}
|
|
else
|
|
{
|
|
mShortTrades++;
|
|
}
|
|
|
|
//
|
|
if (HasRestrictions())
|
|
{
|
|
//
|
|
mRestrictedTrades++;
|
|
|
|
//
|
|
if (isLong)
|
|
{
|
|
mRestrictedLongTrades++;
|
|
}
|
|
else
|
|
{
|
|
mRestrictedShortTrades++;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Handle Trade Win ...
|
|
*
|
|
* @param type: ENUM_X_POSITION_TYPES ...
|
|
*/
|
|
void HandleTradeWin(ENUM_X_POSITION_TYPES type)
|
|
{
|
|
//
|
|
if (!IsXValid(type))
|
|
{
|
|
return;
|
|
}
|
|
bool isLong = IsLong(type);
|
|
|
|
//
|
|
mWins++;
|
|
|
|
//
|
|
if (isLong)
|
|
{
|
|
mLongWins++;
|
|
}
|
|
else
|
|
{
|
|
mShortWins++;
|
|
}
|
|
|
|
//
|
|
if (HasRestrictions())
|
|
{
|
|
//
|
|
mRestrictedWins++;
|
|
|
|
//
|
|
if (isLong)
|
|
{
|
|
mRestrictedLongWins++;
|
|
}
|
|
else
|
|
{
|
|
mRestrictedShortWins++;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Handle Trade Lose ...
|
|
*
|
|
* @param type: ENUM_X_POSITION_TYPES ...
|
|
*/
|
|
void HandleTradeLose(ENUM_X_POSITION_TYPES type)
|
|
{
|
|
//
|
|
if (!IsXValid(type))
|
|
{
|
|
return;
|
|
}
|
|
bool isLong = IsLong(type);
|
|
|
|
//
|
|
mLoses++;
|
|
|
|
//
|
|
if (isLong)
|
|
{
|
|
mLongLoses++;
|
|
}
|
|
else
|
|
{
|
|
mShortLoses++;
|
|
}
|
|
|
|
//
|
|
if (HasRestrictions())
|
|
{
|
|
//
|
|
mRestrictedLoses++;
|
|
|
|
//
|
|
if (isLong)
|
|
{
|
|
mRestrictedLongLoses++;
|
|
}
|
|
else
|
|
{
|
|
mRestrictedShortLoses++;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Check Allow Issue Signal or not ...
|
|
*
|
|
* @param signal: XSignal ...
|
|
*
|
|
* @return ( bool )
|
|
*/
|
|
bool CheckAllowIssueSignal(XSignal &signal)
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
// Validate Signal ...
|
|
result = signal.IsValid();
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
bool isLong = IsLong(signal.type);
|
|
|
|
//
|
|
// Check if Restrictions Exists ...
|
|
if (!HasRestrictions())
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Check Signal Restrictions ...
|
|
if (result)
|
|
{
|
|
//
|
|
if (IsValidSize(mAllowedSignalsInRestrictions))
|
|
{
|
|
//
|
|
result =
|
|
result &&
|
|
mRestrictedLongSignals < mAllowedSignalsInRestrictions;
|
|
}
|
|
}
|
|
|
|
//
|
|
// Check Long Signal Restrictions ...
|
|
if (result)
|
|
{
|
|
//
|
|
if (isLong &&
|
|
IsValidSize(mAllowedLongSignalsInRestrictions))
|
|
{
|
|
//
|
|
result =
|
|
result &&
|
|
mRestrictedLongSignals < mAllowedLongSignalsInRestrictions;
|
|
}
|
|
}
|
|
|
|
//
|
|
// Check Short Signal Restrictions ...
|
|
if (result)
|
|
{
|
|
//
|
|
if (!isLong &&
|
|
IsValidSize(mAllowedShortSignalsInRestrictions))
|
|
{
|
|
//
|
|
result =
|
|
result &&
|
|
mRestrictedShortSignals < mAllowedShortSignalsInRestrictions;
|
|
}
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Check Allow Issue Signal or not ...
|
|
*
|
|
* @param signal: XSignal ...
|
|
*
|
|
* @return ( bool )
|
|
*/
|
|
int CheckAllowExecuteSignal(XSignal &signal)
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
// Validate Signal ...
|
|
result = signal.IsValid();
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
bool isLong = IsLong(signal.type);
|
|
|
|
//
|
|
// Check if Restrictions Exists ...
|
|
if (!HasRestrictions())
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Check Trade Restrictions ...
|
|
if (result)
|
|
{
|
|
//
|
|
if (IsValidSize(mAllowedTradesInRestrictions))
|
|
{
|
|
//
|
|
result =
|
|
result &&
|
|
mRestrictedTrades < mAllowedTradesInRestrictions;
|
|
}
|
|
}
|
|
|
|
//
|
|
// Check Long Trade Restrictions ...
|
|
if (result)
|
|
{
|
|
//
|
|
if (IsValidSize(mAllowedLongTradesInRestrictions))
|
|
{
|
|
//
|
|
result =
|
|
result &&
|
|
mRestrictedLongTrades < mAllowedLongTradesInRestrictions;
|
|
}
|
|
}
|
|
|
|
//
|
|
// Check Short Trade Restrictions ...
|
|
if (result)
|
|
{
|
|
//
|
|
if (IsValidSize(mAllowedShortTradesInRestrictions))
|
|
{
|
|
//
|
|
result =
|
|
result &&
|
|
mRestrictedShortTrades < mAllowedShortTradesInRestrictions;
|
|
}
|
|
}
|
|
|
|
//
|
|
// Check Wins Restrictions ...
|
|
if (result)
|
|
{
|
|
//
|
|
if (IsValidSize(mAllowedWinsInRestrictions))
|
|
{
|
|
//
|
|
result =
|
|
result &&
|
|
mRestrictedWins < mAllowedWinsInRestrictions;
|
|
}
|
|
}
|
|
|
|
//
|
|
// Check Long Wins Restriction ...
|
|
if (result)
|
|
{
|
|
//
|
|
if (IsValidSize(mAllowedLongWinsInRestrictions))
|
|
{
|
|
//
|
|
result =
|
|
result &&
|
|
mRestrictedLongWins < mAllowedLongWinsInRestrictions;
|
|
}
|
|
}
|
|
|
|
//
|
|
// Check Short Wins Restriction ...
|
|
if (result)
|
|
{
|
|
//
|
|
if (IsValidSize(mAllowedShortWinsInRestrictions))
|
|
{
|
|
//
|
|
result =
|
|
result &&
|
|
mRestrictedShortWins < mAllowedShortWinsInRestrictions;
|
|
}
|
|
}
|
|
|
|
//
|
|
// Check Lose Restrictions ...
|
|
if (result)
|
|
{
|
|
//
|
|
if (IsValidSize(mAllowedLosesInRestrictions))
|
|
{
|
|
//
|
|
result =
|
|
result &&
|
|
mRestrictedLoses < mAllowedLosesInRestrictions;
|
|
}
|
|
}
|
|
|
|
//
|
|
// Check Long Lose Restrictions ...
|
|
if (result)
|
|
{
|
|
//
|
|
if (IsValidSize(mAllowedLongLosesInRestrictions))
|
|
{
|
|
//
|
|
result =
|
|
result &&
|
|
mRestrictedLongLoses < mAllowedLongLosesInRestrictions;
|
|
}
|
|
}
|
|
|
|
//
|
|
// Check Short Lose Restrictions ...
|
|
if (result)
|
|
{
|
|
//
|
|
if (IsValidSize(mAllowedShortLosesInRestrictions))
|
|
{
|
|
//
|
|
result =
|
|
result &&
|
|
mRestrictedShortLoses < mAllowedShortLosesInRestrictions;
|
|
}
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
// #endregion
|
|
|
|
// #region Event Handlers Registrations ...
|
|
/**
|
|
* Register Restrictions Start Event Handler ...
|
|
*
|
|
* @param handler: TRestrictionEvent ...
|
|
*/
|
|
void AddRestrictionsStartEventHandler(TRestrictionEvent handler)
|
|
{
|
|
//
|
|
if (handler == NULL)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
Add(handler, mOnRestrictionsStartEventHandlers);
|
|
}
|
|
|
|
/**
|
|
* Register Restrictions End Event Handler ...
|
|
*
|
|
* @param handler: TRestrictionEvent ...
|
|
*/
|
|
void AddRestrictionsEndEventHandler(TRestrictionEvent handler)
|
|
{
|
|
//
|
|
if (handler == NULL)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
Add(handler, mOnRestrictionsEndEventHandlers);
|
|
}
|
|
// #endregion
|
|
|
|
/**
|
|
* Process Ticks for Restrictions ...
|
|
* Applied Eac Secconds ...
|
|
*/
|
|
void Process()
|
|
{
|
|
//
|
|
// Check Restrictions Exists or not ...
|
|
if (!HasRestrictions())
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Calculate Requirements ...
|
|
datetime currentTime = TimeCurrent();
|
|
|
|
//
|
|
// Check Current Restrictions Exists or not ...
|
|
// Initial Restrictions Period if not Exists ...
|
|
if (!IsXValid(mRestrictionsStart))
|
|
{
|
|
//
|
|
// Initial Restrictions ...
|
|
mRestrictionsStart = currentTime;
|
|
mRestrictionsEnd = RestrictionsEndTime(
|
|
mRestrictionsPeriod,
|
|
mRestrictionsPeriodMultiplier,
|
|
mRestrictionsStart //
|
|
);
|
|
|
|
//
|
|
// Loging Restrictions Start ...
|
|
LogRestrictionsStart();
|
|
|
|
//
|
|
// Notfy Restrictions Start Event Handlers ...
|
|
NotifyRestrictionsStartEventHandlers();
|
|
|
|
//
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Validate Current Restrictions ...
|
|
|
|
//
|
|
// Check Process Exists or not ...
|
|
if (IsXValid(mLastCheck))
|
|
{
|
|
//
|
|
// Validate New Check Must Happens after 1 Minute ...
|
|
bool canCheck = currentTime >= mLastCheck + 60;
|
|
if (!canCheck)
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
|
|
//
|
|
// Update Last Check ...
|
|
mLastCheck = currentTime;
|
|
|
|
//
|
|
// Check Restrictions End ...
|
|
bool isEnded = currentTime >= mRestrictionsEnd;
|
|
if (isEnded)
|
|
{
|
|
//
|
|
// Fire Restrictions End Event Handlers ...
|
|
NotifyRestrictionsEndEventHandlers();
|
|
|
|
//
|
|
// Logging Resrtrictions Summary if Required ...
|
|
LogRestrictionsEnd();
|
|
|
|
//
|
|
// Reset Restrictions ...
|
|
ResetRestrictions();
|
|
|
|
//
|
|
return;
|
|
}
|
|
|
|
//
|
|
// TODO: Implement What we want if necessary ...
|
|
}
|
|
|
|
//
|
|
// Protected ...
|
|
protected:
|
|
//
|
|
// Props ...
|
|
|
|
/**
|
|
* Cleanup ...
|
|
*/
|
|
void Clean()
|
|
{
|
|
//
|
|
mAllowedWinsInRestrictions = 0;
|
|
mAllowedLongWinsInRestrictions = 0;
|
|
mAllowedShortWinsInRestrictions = 0;
|
|
|
|
//
|
|
mAllowedLosesInRestrictions = 0;
|
|
mAllowedLongLosesInRestrictions = 0;
|
|
mAllowedShortLosesInRestrictions = 0;
|
|
|
|
//
|
|
mAllowedSignalsInRestrictions = 0;
|
|
mAllowedLongSignalsInRestrictions = 0;
|
|
mAllowedShortSignalsInRestrictions = 0;
|
|
|
|
//
|
|
mAllowedTradesInRestrictions = 0;
|
|
mAllowedLongTradesInRestrictions = 0;
|
|
mAllowedShortTradesInRestrictions = 0;
|
|
|
|
//
|
|
ResetRestrictions();
|
|
XClean(mOnRestrictionsEndEventHandlers);
|
|
XClean(mOnRestrictionsStartEventHandlers);
|
|
}
|
|
|
|
/**
|
|
* Set Default Values ...
|
|
*/
|
|
void Default()
|
|
{
|
|
//
|
|
mRestrictionsPeriodMultiplier = 1;
|
|
mRestrictionsPeriod = X_TRADE_RESTRICATION_NONE;
|
|
}
|
|
|
|
/**
|
|
* Logging Restrictions Start Summary ...
|
|
*/
|
|
void LogRestrictionsStart()
|
|
{
|
|
}
|
|
|
|
/**
|
|
* Logging Restrictions End Summary ...
|
|
*/
|
|
void LogRestrictionsEnd()
|
|
{
|
|
}
|
|
|
|
// #region Event Handlers Notifier ...
|
|
/**
|
|
* Notify Registered Restrictions Start Event Handlers ...
|
|
*/
|
|
void NotifyRestrictionsStartEventHandlers()
|
|
{
|
|
//
|
|
int count = ArraySize(mOnRestrictionsStartEventHandlers);
|
|
if (!IsValidSize(count))
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
mOnRestrictionsStartEventHandlers[i]();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Notify Registered Restrictions End Event Handlers ...
|
|
*/
|
|
void NotifyRestrictionsEndEventHandlers()
|
|
{
|
|
//
|
|
int count = ArraySize(mOnRestrictionsEndEventHandlers);
|
|
if (!IsValidSize(count))
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
mOnRestrictionsEndEventHandlers[i]();
|
|
}
|
|
}
|
|
// #endregion
|
|
|
|
// #region Restrictions Summary Cleaners ...
|
|
/**
|
|
* Reset all Restricted Counters ...
|
|
*/
|
|
void ResetRestrictions()
|
|
{
|
|
//
|
|
mRestrictionsEnd = NULL;
|
|
mRestrictionsStart = NULL;
|
|
|
|
//
|
|
ResetTradeRestrictions();
|
|
ResetSignalRestrictions();
|
|
ResetTradeWinRestrictions();
|
|
ResetTradeLoseRestrictions();
|
|
}
|
|
|
|
/**
|
|
* Resetting Signal Restrictions ...
|
|
*/
|
|
void ResetSignalRestrictions()
|
|
{
|
|
//
|
|
mRestrictedSignals = 0;
|
|
mRestrictedLongSignals = 0;
|
|
mRestrictedShortSignals = 0;
|
|
}
|
|
|
|
/**
|
|
* Resetting Trade Restrictions ...
|
|
*/
|
|
void ResetTradeRestrictions()
|
|
{
|
|
//
|
|
mRestrictedTrades = 0;
|
|
mRestrictedLongTrades = 0;
|
|
mRestrictedShortTrades = 0;
|
|
}
|
|
|
|
/**
|
|
* Resetting Trade Win Restrictions ...
|
|
*/
|
|
void ResetTradeWinRestrictions()
|
|
{
|
|
mWins = 0;
|
|
mLongWins = 0;
|
|
mShortWins = 0;
|
|
}
|
|
|
|
/**
|
|
* Resetting Trade Lose Restrictions ...
|
|
*/
|
|
void ResetTradeLoseRestrictions()
|
|
{
|
|
//
|
|
mLoses = 0;
|
|
mLongLoses = 0;
|
|
mShortLoses = 0;
|
|
}
|
|
// #endregion ...
|
|
|
|
//
|
|
// Private ...
|
|
private:
|
|
//
|
|
// Props ...
|
|
|
|
//
|
|
// Last Restrictions Time ...
|
|
datetime mLastCheck; // Last Restrictions Check ...
|
|
datetime mRestrictionsEnd; // Last Restrictions End Time ...
|
|
datetime mRestrictionsStart; // Last Restrictions Start Time ...
|
|
|
|
//
|
|
// Restrictions Period ...
|
|
int mRestrictionsPeriodMultiplier; // Restrictions Period Multiplier ...
|
|
ENUM_X_TRADE_RESTRICTIONS_PERIOD mRestrictionsPeriod; // Restrictions Period ...
|
|
|
|
// #region Signals ...
|
|
//
|
|
int mSignals; // Issued Signals ...
|
|
int mLongSignals; // Issued Long Signals ...
|
|
int mShortSignals; // Issued Short Signals ...
|
|
|
|
//
|
|
int mRestrictedSignals; // Issued Signals in Restrictions Period ...
|
|
int mRestrictedLongSignals; // Issued Long Signals in Restrictions Period ...
|
|
int mRestrictedShortSignals; // Issued Short Signals in Restrictions Period ...
|
|
|
|
//
|
|
int mAllowedSignalsInRestrictions; // Allowed Signals in Restrictions Period ...
|
|
int mAllowedLongSignalsInRestrictions; // Allowed Long Signals in Restrictions Period ...
|
|
int mAllowedShortSignalsInRestrictions; // Allowed Short Signals in Restrictions Period ...
|
|
// #endregion
|
|
|
|
// #region Trades ...
|
|
//
|
|
int mTrades; // Executed Trades ...
|
|
int mLongTrades; // Executed Long Trades ...
|
|
int mShortTrades; // Executed Short Trades ...
|
|
|
|
//
|
|
int mRestrictedTrades; // Executed Trades in Restrictions Period ...
|
|
int mRestrictedLongTrades; // Executed Long Trades in Restrictions Period ...
|
|
int mRestrictedShortTrades; // Executed Short Trades in Restrictions Period ...
|
|
|
|
//
|
|
int mAllowedTradesInRestrictions; // Allowed Trades in Restrictions Period ...
|
|
int mAllowedLongTradesInRestrictions; // Allowed Long Trades in Restrictions Period ...
|
|
int mAllowedShortTradesInRestrictions; // Allowed Short Trades in Restrictions Period ...
|
|
// #endregion
|
|
|
|
// #region Wins ...
|
|
//
|
|
int mWins; // Touched Wins ...
|
|
int mLongWins; // Touched Long Wins ...
|
|
int mShortWins; // Touched Short Wins ...
|
|
|
|
//
|
|
int mRestrictedWins; // Touched Wins in Restrictions Period ...
|
|
int mRestrictedLongWins; // Touched Long Wins in Restrictions Period ...
|
|
int mRestrictedShortWins; // Touched Short Wins in Restrictions Period ...
|
|
|
|
//
|
|
int mAllowedWinsInRestrictions; // Allowed Wins in Restrictions Period ...
|
|
int mAllowedLongWinsInRestrictions; // Allowed Long Wins in Restrictions Period ...
|
|
int mAllowedShortWinsInRestrictions; // Allowed Short Wins in Restrictions Period ...
|
|
// #endregion
|
|
|
|
// #region Losess ...
|
|
//
|
|
int mLoses; // Touched Loses ...
|
|
int mLongLoses; // Touched Long Loses ...
|
|
int mShortLoses; // Touched Short Loses ...
|
|
|
|
//
|
|
int mRestrictedLoses; // Touched Loses in Restrictions Period ...
|
|
int mRestrictedLongLoses; // Touched Long Loses in Restrictions Period ...
|
|
int mRestrictedShortLoses; // Touched Short Loses in Restrictions Period ...
|
|
|
|
//
|
|
int mAllowedLosesInRestrictions; // Allowed Loses in Restrictions Period ...
|
|
int mAllowedLongLosesInRestrictions; // Allowed Long Loses in Restrictions Period ...
|
|
int mAllowedShortLosesInRestrictions; // Allowed Short Loses in Restrictions Period ...
|
|
// #endregion
|
|
|
|
// #region Event Handlers ...
|
|
TRestrictionEvent mOnRestrictionsEndEventHandlers[]; // Restrictions End Event Handlers ...
|
|
TRestrictionEvent mOnRestrictionsStartEventHandlers[]; // Restrictions Start Event Hnadlers ...
|
|
// #endregion
|
|
};
|
|
|
|
// |