749 lines
13 KiB
Plaintext
749 lines
13 KiB
Plaintext
///////////////////////////////////////////////////////
|
|
//
|
|
// SaherElm IT Center MQL5 Common Library
|
|
// --------------------------------------
|
|
// Name: X121SMCLib
|
|
// Description: provide all commonly used functions ...
|
|
//
|
|
//
|
|
// 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 "../../Classes/x-saherelm.x-poi.drawer.class.mq5"
|
|
#include "../../Helpers/x-saherelm.x121.helper.mq5"
|
|
#include "../../Libraries/x-saherelm.common.lib.mq5"
|
|
#include "../../Libraries/x-saherelm.x-poi.extensions.lib.mq5"
|
|
#include "../../Libraries/x-saherelm.x-poi.lib.mq5"
|
|
#include "../../Libraries/x-saherelm.x-trade.lib.mq5"
|
|
|
|
//
|
|
// Definitions ...
|
|
|
|
//
|
|
const string X121SMCStrategyToken = "X121SMC";
|
|
|
|
//
|
|
const string X121SMCXWZToken = "X121XWZ";
|
|
const string X121SMCXICHToken = "X121XICH";
|
|
|
|
//
|
|
// X121 SMC Providers ...
|
|
enum ENUM_X_121_SMC_PROVIDERS
|
|
{
|
|
//
|
|
X_121_SMC_PROVIDER_NONE,
|
|
X_121_SMC_PROVIDER_XWZ,
|
|
X_121_SMC_PROVIDER_XICH,
|
|
};
|
|
|
|
//
|
|
// Guard Actions ...
|
|
enum ENUM_X_121_SMC_GUARD_ACTIONS
|
|
{
|
|
X_121_SMC_GUARD_ACTION_NONE, // Nothing to Do
|
|
X_121_SMC_GUARD_ACTION_CLOSE, // Close Specified Position
|
|
X_121_SMC_GUARD_ACTION_CLOSE_ALL, // Close All Positions
|
|
X_121_SMC_GUARD_ACTION_CLOSE_LONGS, // Close All Long Positions
|
|
X_121_SMC_GUARD_ACTION_CLOSE_SHORTS, // Close All Short Position
|
|
X_121_SMC_GUARD_ACTION_PARTIAL_CLOSE, // Partial Close Specified Position
|
|
X_121_SMC_GUARD_ACTION_TRAIL_STOP, // Trail Stop of Specified Position
|
|
X_121_SMC_GUARD_ACTION_TRAIL_TARGET, // Trail Target of Specified Position
|
|
X_121_SMC_GUARD_ACTION_HEDGE, // Hedge Specified Positions
|
|
};
|
|
|
|
//
|
|
// Each Market Cycle (Time Frame) Conditions
|
|
// Model as this Structure ...
|
|
struct X121SMCCycleConditions
|
|
{
|
|
//
|
|
string symbol;
|
|
ENUM_TIMEFRAMES period;
|
|
datetime time;
|
|
|
|
//
|
|
XPOIState state;
|
|
X121Conditions x121Conditions;
|
|
|
|
//
|
|
void X121SMCStrategyConditions()
|
|
{
|
|
Clean();
|
|
}
|
|
|
|
//
|
|
// Tools ...
|
|
|
|
/**
|
|
* Cleanup ...
|
|
*/
|
|
void Clean()
|
|
{
|
|
//
|
|
symbol = NULL;
|
|
period = NULL;
|
|
time = NULL;
|
|
|
|
//
|
|
state.Clean();
|
|
x121Conditions.Clean();
|
|
|
|
//
|
|
ZeroMemory(this);
|
|
}
|
|
|
|
/**
|
|
* Validate ...
|
|
*
|
|
* @return ( bool )
|
|
*/
|
|
bool IsValid()
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
result = IsSpecifiedValid(symbol) &&
|
|
IsSpecifiedValid(period) &&
|
|
IsSpecifiedValid(time);
|
|
//
|
|
;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Reporter Functions ...
|
|
|
|
/**
|
|
* Generate Provided Scores ...
|
|
*
|
|
* @param bullishScore: Integer, reference ...
|
|
* @param bearishScore: Integer, referenceF ...
|
|
*/
|
|
void GenerateScore(
|
|
double &bullishScore,
|
|
double &bearishScore //
|
|
)
|
|
{
|
|
//
|
|
bullishScore = 0;
|
|
bearishScore = 0;
|
|
|
|
//
|
|
if (!IsValid())
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
double bullScore = 0;
|
|
double bearScore = 0;
|
|
}
|
|
|
|
/**
|
|
* Generate Summary ...
|
|
*
|
|
* @param onlyCommons: Boolean ...
|
|
* @param onlyConditions: Boolean ...
|
|
* @param includeScores: Boolean ...
|
|
* @param ignoreFalseConditions: Boolean ...
|
|
* @param separator: String ...
|
|
*
|
|
* @return ( string )
|
|
*/
|
|
string GenerateSummary(
|
|
bool onlyCommons = false,
|
|
bool onlyConditions = false,
|
|
bool includeScores = true,
|
|
bool ignoreFalseConditions = true,
|
|
string separator = "\n" //
|
|
)
|
|
{
|
|
//
|
|
string result = NULL;
|
|
|
|
//
|
|
double bullishScore = 0;
|
|
double bearishScore = 0;
|
|
GenerateScore(
|
|
bullishScore,
|
|
bearishScore //
|
|
);
|
|
|
|
//
|
|
string commonStr = GenerateSpecifiedCommonSummary(
|
|
this,
|
|
separator,
|
|
includeScores,
|
|
true //
|
|
);
|
|
|
|
//
|
|
string conditionsStr =
|
|
//
|
|
""
|
|
//
|
|
;
|
|
|
|
//
|
|
result =
|
|
//
|
|
"[" + GetTag() + "]" + separator +
|
|
"----------" + separator +
|
|
(onlyConditions
|
|
? ""
|
|
: commonStr) +
|
|
" " + separator +
|
|
(onlyCommons
|
|
? ""
|
|
: conditionsStr) +
|
|
""
|
|
//
|
|
;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Get Unique Identifier ...
|
|
*
|
|
* @param forObject: Boolean ...
|
|
*
|
|
* @return ( string )
|
|
*/
|
|
string GetTag(
|
|
bool forObject = false //
|
|
)
|
|
{
|
|
//
|
|
string result = NULL;
|
|
|
|
//
|
|
string typeName = GetTypeName(this);
|
|
|
|
//
|
|
if (!forObject)
|
|
{
|
|
result = typeName;
|
|
}
|
|
else
|
|
{
|
|
//
|
|
result = typeName + "_" +
|
|
symbol + "_" + ToString(period) + "_" +
|
|
ToMD5(time);
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
};
|
|
|
|
//
|
|
// Model Signalling Conditions ...
|
|
struct X121SMCStrategyConditions
|
|
{
|
|
//
|
|
string symbol;
|
|
ENUM_TIMEFRAMES period;
|
|
datetime time;
|
|
|
|
//
|
|
double sl;
|
|
double tp;
|
|
string provider;
|
|
double targets[];
|
|
ENUM_X_DIRECTION signalDir;
|
|
|
|
//
|
|
void X121SMCStrategyConditions()
|
|
{
|
|
Clean();
|
|
}
|
|
|
|
//
|
|
// Tools ...
|
|
|
|
/**
|
|
* Cleanup ...
|
|
*/
|
|
void Clean()
|
|
{
|
|
//
|
|
time = NULL;
|
|
symbol = NULL;
|
|
period = NULL;
|
|
|
|
//
|
|
sl = 0;
|
|
tp = 0;
|
|
provider = NULL;
|
|
signalDir = X_DIRECTION_NONE;
|
|
|
|
//
|
|
Clean(targets);
|
|
|
|
//
|
|
ZeroMemory(this);
|
|
}
|
|
|
|
/**
|
|
* Validate ...
|
|
*
|
|
* @return ( bool )
|
|
*/
|
|
bool IsValid()
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
result = IsSpecifiedValid(symbol) &&
|
|
IsSpecifiedValid(period) &&
|
|
IsSpecifiedValid(time);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Check Conditions Has Valid Bullish Signal ...
|
|
*
|
|
* @return ( bool )
|
|
*/
|
|
bool HasBullishSignal()
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
result =
|
|
(sl > 0 ||
|
|
tp > 0) &&
|
|
IsValid() &&
|
|
HasDirection(signalDir);
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
result =
|
|
IsBullish(signalDir);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Check Conditions Has Valid Bearish Signal ...
|
|
*
|
|
* @return ( bool )
|
|
*/
|
|
bool HasBearishSignal()
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
result =
|
|
(sl > 0 ||
|
|
tp > 0) &&
|
|
IsValid() &&
|
|
HasDirection(signalDir);
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
result =
|
|
IsBearish(signalDir);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Reporter Functions ...
|
|
|
|
/**
|
|
* Generate Provided Scores ...
|
|
*
|
|
* @param bullishScore: Integer, reference ...
|
|
* @param bearishScore: Integer, referenceF ...
|
|
*/
|
|
void GenerateScore(
|
|
double &bullishScore,
|
|
double &bearishScore //
|
|
)
|
|
{
|
|
//
|
|
bullishScore = 0;
|
|
bearishScore = 0;
|
|
|
|
//
|
|
if (!IsValid())
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
double bullScore = 0;
|
|
double bearScore = 0;
|
|
}
|
|
|
|
/**
|
|
* Generate Summary ...
|
|
*
|
|
* @param onlyCommons: Boolean ...
|
|
* @param onlyConditions: Boolean ...
|
|
* @param includeScores: Boolean ...
|
|
* @param ignoreFalseConditions: Boolean ...
|
|
* @param separator: String ...
|
|
*
|
|
* @return ( string )
|
|
*/
|
|
string GenerateSummary(
|
|
bool onlyCommons = false,
|
|
bool onlyConditions = false,
|
|
bool includeScores = true,
|
|
bool ignoreFalseConditions = true,
|
|
string separator = "\n" //
|
|
)
|
|
{
|
|
//
|
|
string result = NULL;
|
|
|
|
//
|
|
double bullishScore = 0;
|
|
double bearishScore = 0;
|
|
GenerateScore(
|
|
bullishScore,
|
|
bearishScore //
|
|
);
|
|
|
|
//
|
|
string commonStr = GenerateSpecifiedCommonSummary(
|
|
this,
|
|
separator,
|
|
includeScores,
|
|
true //
|
|
);
|
|
|
|
//
|
|
string conditionsStr =
|
|
//
|
|
""
|
|
//
|
|
;
|
|
|
|
//
|
|
result =
|
|
//
|
|
"[" + GetTag() + "]" + separator +
|
|
"----------" + separator +
|
|
(onlyConditions
|
|
? ""
|
|
: commonStr) +
|
|
" " + separator +
|
|
(onlyCommons
|
|
? ""
|
|
: conditionsStr) +
|
|
""
|
|
//
|
|
;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Get Unique Identifier ...
|
|
*
|
|
* @param forObject: Boolean ...
|
|
*
|
|
* @return ( string )
|
|
*/
|
|
string GetTag(
|
|
bool forObject = false //
|
|
)
|
|
{
|
|
//
|
|
string result = NULL;
|
|
|
|
//
|
|
string typeName = GetTypeName(this);
|
|
|
|
//
|
|
if (!forObject)
|
|
{
|
|
result = typeName;
|
|
}
|
|
else
|
|
{
|
|
//
|
|
result = typeName + "_" +
|
|
symbol + "_" + ToString(period) + "_" +
|
|
ToMD5(time);
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
};
|
|
|
|
//
|
|
// Model a Guard Action ...
|
|
struct X121SMCGuard
|
|
{
|
|
//
|
|
// Props ...
|
|
ENUM_X_121_SMC_GUARD_ACTIONS action;
|
|
datetime time;
|
|
|
|
//
|
|
string symbol;
|
|
string provider;
|
|
|
|
//
|
|
// Specified Position ...
|
|
ulong ticket;
|
|
|
|
//
|
|
// Partial Close ...
|
|
double volumeMultiplier;
|
|
|
|
//
|
|
// SL Trial ...
|
|
double sl;
|
|
|
|
//
|
|
// TP Trial ...
|
|
double tp;
|
|
|
|
//
|
|
// Constructor ...
|
|
X121SMCGuard()
|
|
{
|
|
Clean();
|
|
}
|
|
|
|
//
|
|
// Tools ...
|
|
|
|
/**
|
|
* Cleanup ...
|
|
*/
|
|
void Clean()
|
|
{
|
|
//
|
|
time = NULL;
|
|
action = X_121_SMC_GUARD_ACTION_NONE;
|
|
|
|
//
|
|
symbol = NULL;
|
|
provider = NULL;
|
|
|
|
//
|
|
ticket = 0;
|
|
|
|
//
|
|
volumeMultiplier = 0;
|
|
|
|
//
|
|
sl = 0;
|
|
tp = 0;
|
|
|
|
//
|
|
ZeroMemory(this);
|
|
}
|
|
|
|
/**
|
|
* Validate ...
|
|
*
|
|
* @return ( bool )
|
|
*/
|
|
bool IsValid()
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
result =
|
|
//
|
|
IsValid(time) &&
|
|
IsValid(symbol) &&
|
|
IsValid(provider) &&
|
|
action != X_121_SMC_GUARD_ACTION_NONE
|
|
//
|
|
;
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Validate Model Based On Specified Guard Actions ...
|
|
|
|
//
|
|
return result;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Add Guard to Collection ...
|
|
*
|
|
* @param guard: X121SMCGuard instance ...
|
|
* @param guards: X121SMCGuard instance Collection ...
|
|
*
|
|
* @return ( int )
|
|
*/
|
|
int AddGuard(
|
|
X121SMCGuard &guard,
|
|
X121SMCGuard &guards[] //
|
|
)
|
|
{
|
|
//
|
|
int result = 0;
|
|
|
|
//
|
|
if (!guard.IsValid())
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
AddRef(
|
|
guard,
|
|
guards //
|
|
);
|
|
|
|
//
|
|
result = ArraySize(guards);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
//
|
|
//
|
|
|
|
typedef void (*TOnX121SMCSignal)(
|
|
XSignal &signal,
|
|
X121SMCStrategyConditions &conditions //
|
|
);
|
|
|
|
//
|
|
// Extentions ...
|
|
|
|
/**
|
|
* Validate a Signal Provider ...
|
|
*
|
|
* @param value: ENUM_X_121_SMC_PROVIDERS member ...
|
|
*
|
|
* @return ( bool )
|
|
*/
|
|
bool IsValid(ENUM_X_121_SMC_PROVIDERS value)
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
result = value != X_121_SMC_PROVIDER_NONE;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Converts a Signal Provider to String ...
|
|
*
|
|
* @param value: ENUM_X_121_SMC_PROVIDERS member ...
|
|
*
|
|
* @return ( string )
|
|
*/
|
|
string ToString(ENUM_X_121_SMC_PROVIDERS value)
|
|
{
|
|
//
|
|
string result = NULL;
|
|
|
|
//
|
|
if (!IsValid(value))
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
switch (value)
|
|
{
|
|
//
|
|
case X_121_SMC_PROVIDER_XWZ:
|
|
result = X121SMCXWZToken;
|
|
break;
|
|
|
|
//
|
|
case X_121_SMC_PROVIDER_XICH:
|
|
result = X121SMCXICHToken;
|
|
break;
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Parse Specified String as a Signal Provider ...
|
|
*
|
|
* @param value: String ...
|
|
*
|
|
* @return ( ENUM_X_121_SMC_PROVIDERS )
|
|
*/
|
|
ENUM_X_121_SMC_PROVIDERS ParseProvider(string value)
|
|
{
|
|
//
|
|
ENUM_X_121_SMC_PROVIDERS result = X_121_SMC_PROVIDER_NONE;
|
|
|
|
//
|
|
if (!IsValid(value))
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
if (Contains(X121SMCXWZToken, value))
|
|
{
|
|
result = X_121_SMC_PROVIDER_XWZ;
|
|
}
|
|
else if (Contains(X121SMCXICHToken, value))
|
|
{
|
|
result = X_121_SMC_PROVIDER_XICH;
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|