627 lines
12 KiB
Plaintext
627 lines
12 KiB
Plaintext
///////////////////////////////////////////////////////
|
|
//
|
|
// SaherElm IT Center MQL5 Class Library
|
|
// --------------------------------------
|
|
// Name: XSCX121SMCBaseStrategy
|
|
// Description: provides all based classes for use ...
|
|
//
|
|
//
|
|
// 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.x-alert.class.mq5"
|
|
#include "../../Classes/x-saherelm.x-bar.analyser.class.mq5"
|
|
#include "../../Classes/x-saherelm.x-poi.class.mq5"
|
|
#include "../../Classes/x-saherelm.x-trade.class.mq5"
|
|
#include "../Classes/x-121.smc.cobjects.class.mq5"
|
|
#include "../Libraries/x-121.smc.lib.mq5"
|
|
|
|
//
|
|
// Helper(s) ...
|
|
|
|
//
|
|
// XStrategy Class Implementation(s) ...
|
|
class XCX121SMCBaseStrategy : public XCBaseAlert
|
|
{
|
|
//
|
|
// Public ...
|
|
public:
|
|
//
|
|
XCAccount mAccount;
|
|
|
|
//
|
|
// Constructor(s) ...
|
|
XCX121SMCBaseStrategy(
|
|
//
|
|
// Base ...
|
|
string _symbol, // Trading Symbol
|
|
ENUM_TIMEFRAMES _period // Trading TimeFrame
|
|
)
|
|
{
|
|
//
|
|
mSymbol = _symbol;
|
|
mPeriod = _period;
|
|
|
|
//
|
|
Init();
|
|
}
|
|
|
|
//
|
|
// Deconstructor ...
|
|
~XCX121SMCBaseStrategy()
|
|
{
|
|
DeInit();
|
|
}
|
|
|
|
//
|
|
// Getter / Setter (s) ...
|
|
|
|
/**
|
|
* Retrieve Symbol ...
|
|
*
|
|
* @return ( string )
|
|
*/
|
|
string GetSymbol()
|
|
{
|
|
return mSymbol;
|
|
}
|
|
|
|
/**
|
|
* Retrieve TimeFrame ...
|
|
*
|
|
* @return ( ENUM_TIMEFRAMES )
|
|
*/
|
|
ENUM_TIMEFRAMES GetPeriod()
|
|
{
|
|
return mPeriod;
|
|
}
|
|
|
|
/**
|
|
* Check Strategy is Disabled or not ...
|
|
*
|
|
* @return ( bool )
|
|
*/
|
|
bool IsDisabled()
|
|
{
|
|
return mForceDisabled;
|
|
}
|
|
|
|
/**
|
|
* Get Signal Volume ...
|
|
*
|
|
* @return ( double )
|
|
*/
|
|
double Volume()
|
|
{
|
|
return mVolume;
|
|
}
|
|
|
|
/**
|
|
* Set Signal Volume ...
|
|
*
|
|
* @param value: Double ...
|
|
*/
|
|
void Volume(double value)
|
|
{
|
|
//
|
|
if (value < 0.01)
|
|
{
|
|
value = 0.01;
|
|
}
|
|
|
|
//
|
|
mVolume = value;
|
|
}
|
|
|
|
/**
|
|
* Get Risk/Reward Ratio ...
|
|
*
|
|
* @return ( double )
|
|
*/
|
|
double R2R()
|
|
{
|
|
return mR2R;
|
|
}
|
|
|
|
/**
|
|
* Set Risk/Reward Ratio ...
|
|
*
|
|
* @param value: Double ...
|
|
*/
|
|
void R2R(double value)
|
|
{
|
|
//
|
|
if (value < 0)
|
|
{
|
|
value = 1.5;
|
|
}
|
|
|
|
//
|
|
mR2R = value;
|
|
}
|
|
|
|
/**
|
|
* Get Long Signalling State ...
|
|
*
|
|
* @return ( bool )
|
|
*/
|
|
bool AllowLong()
|
|
{
|
|
return mAllowLong;
|
|
}
|
|
|
|
/**
|
|
* Set Long Signalling State ...
|
|
*
|
|
* @param value: Boolean ...
|
|
*/
|
|
void AllowLong(bool value)
|
|
{
|
|
mAllowLong = value;
|
|
}
|
|
|
|
/**
|
|
* Get Short Signalling State ...
|
|
*
|
|
* @return ( bool )
|
|
*/
|
|
bool AllowShort()
|
|
{
|
|
return mAllowShort;
|
|
}
|
|
|
|
/**
|
|
* Set Short Signalling State ...
|
|
*
|
|
* @param value: Argument 1
|
|
*/
|
|
void AllowShort(bool value)
|
|
{
|
|
mAllowShort = value;
|
|
}
|
|
|
|
//
|
|
// Actions ...
|
|
|
|
/**
|
|
* Disable Strategy ...
|
|
*/
|
|
void Disable()
|
|
{
|
|
mForceDisabled = true;
|
|
}
|
|
|
|
/**
|
|
* Enable Strategy ...
|
|
*/
|
|
void Enable()
|
|
{
|
|
mForceDisabled = false;
|
|
}
|
|
|
|
/**
|
|
* Register Signal Event Handler ...
|
|
*
|
|
* @param handler: an Instance of TOnXSignal ...
|
|
*/
|
|
void AddXSignalEventHandler(TOnX121SMCSignal handler)
|
|
{
|
|
//
|
|
Add(
|
|
handler,
|
|
mSignalEventHandlers //
|
|
);
|
|
}
|
|
|
|
//
|
|
// Virtual Actions ...
|
|
|
|
/**
|
|
* Destroy All Class Implementations ...
|
|
*/
|
|
virtual void Destroy()
|
|
{
|
|
}
|
|
|
|
/**
|
|
* Handle Tick Processing ...
|
|
*
|
|
* @param positions: XPosition instance Collection ...
|
|
*/
|
|
virtual void HandleTick()
|
|
{
|
|
//
|
|
DoUnwaitedTicksWork();
|
|
|
|
//
|
|
// Check State ...
|
|
if (IsDisabled())
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Check Signalling State ...
|
|
if (!AllowLong() && !AllowShort())
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Check Bar Tracker State ...
|
|
if (!mBarTracker.CanProcessBar())
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Retrieve Current Tick and Check it's Time
|
|
// by Last Issued Tick ...
|
|
MqlTick cTick;
|
|
bool hasTick = GetTick(
|
|
mSymbol,
|
|
cTick //
|
|
);
|
|
bool isSameAsLast = cTick.time == mLastTick.time;
|
|
if (isSameAsLast)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Update Last Issued Tick ...
|
|
mLastTick = cTick;
|
|
|
|
//
|
|
// Check Current Processing Tick has Signal or not ...
|
|
XSignal signal;
|
|
X121SMCStrategyConditions conditions;
|
|
bool hasSignal = HasSignal(
|
|
signal,
|
|
conditions //
|
|
);
|
|
if (!hasSignal)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Waits Until Next Candle if Current Tick has Signal ...
|
|
mBarTracker.Waits();
|
|
|
|
//
|
|
// Notify Signal Event Handlers ...
|
|
NotifyOnSignalEventHandlers(
|
|
signal,
|
|
conditions //
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Check for any Guards ...
|
|
*
|
|
* @param guards: X121SMCGuard instance Collection ...
|
|
* @param positions: XPosition instance Collection ...
|
|
*
|
|
* @return ( virtual bool )
|
|
*/
|
|
virtual bool HandleGuard(
|
|
X121SMCGuard &guards[],
|
|
XPosition &positions[] //
|
|
)
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
// Check State ...
|
|
if (IsDisabled())
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Check Signalling State ...
|
|
if (!AllowLong() && !AllowShort())
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Check Bar Tracker State ...
|
|
if (!mGuardBarTracker.CanProcessBar())
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Check For Guards ...
|
|
result = CheckGuard(
|
|
guards,
|
|
positions //
|
|
);
|
|
|
|
//
|
|
// Waits Until Next Candle if Current Tick has Guard ...
|
|
mGuardBarTracker.Waits();
|
|
|
|
//
|
|
Clean(positions);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Check For Signal ...
|
|
*
|
|
* @param signal: XSignal instance ...
|
|
* @param conditions: X121SMCStrategyConditions instance ...
|
|
*
|
|
* @return ( virtual bool )
|
|
*/
|
|
virtual bool HasSignal(
|
|
XSignal &signal,
|
|
X121SMCStrategyConditions &conditions //
|
|
)
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
signal.Clean();
|
|
conditions.Clean();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Check for any Guard Actions ...
|
|
*
|
|
* @param guards: X121SMCGuard instance Collection ...
|
|
*
|
|
* @return ( virtual bool )
|
|
*/
|
|
virtual bool CheckGuard(
|
|
X121SMCGuard &guards[],
|
|
const XPosition &positions[] // Positions ...
|
|
)
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Update all Requirements without waiting ...
|
|
*/
|
|
virtual void DoUnwaitedTicksWork()
|
|
{
|
|
}
|
|
|
|
/**
|
|
* Detect SL Candidates based on Strategy and Conditions ...
|
|
*
|
|
* @param result: Double array reference ...
|
|
* @param entry: Double, Position Entry Price ...
|
|
* @param conditions: X121SMCStrategyConditions instance, Provides Configurations ...
|
|
* @param type: ENUM_X_POSITION_TYPES member, Specified Position Type ...
|
|
*/
|
|
virtual void DetectSLCandidates(
|
|
double &result[],
|
|
double entry,
|
|
X121SMCStrategyConditions &conditions,
|
|
ENUM_X_POSITION_TYPES type //
|
|
)
|
|
{
|
|
}
|
|
|
|
/**
|
|
* Customize Strategy Identifier ...
|
|
*
|
|
* @return ( virtual string )
|
|
*/
|
|
virtual string GetTag()
|
|
{
|
|
return X121SMCStrategyToken;
|
|
}
|
|
|
|
//
|
|
// Protected ...
|
|
protected:
|
|
//
|
|
|
|
//
|
|
// Props ...
|
|
|
|
//
|
|
MqlTick mLastTick; // Last Issued Tick ...
|
|
|
|
//
|
|
XBarTracker mBarTracker; // Strategy Bar Tracker ...
|
|
XBarTracker mGuardBarTracker; // Strategy Guard Bar Tracker ...
|
|
XCBarAnalyser *mBarAnalyser; // Bar Analyser Instance
|
|
|
|
//
|
|
// Actions ...
|
|
|
|
/**
|
|
* Prepare and Normalize Signal for Execution in Strategy ...
|
|
*
|
|
* @param signal: XSignal instance ...
|
|
*
|
|
* @return ( bool )
|
|
*/
|
|
bool PrepareSignal(XSignal &signal)
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
// Check Strategy Is Enables ...
|
|
result = !IsDisabled();
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Validate Signal ...
|
|
result = signal.IsValid();
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
bool isLong = IsLong(signal.type);
|
|
|
|
//
|
|
// TODO: Implement Signal Preparation Mechanism here ...
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
|
|
/**
|
|
* Notified Signal Event Handler(s) ...
|
|
*
|
|
* @param signal: XSignal instance ...
|
|
* @param conditions: XStrategyConditions instance ...
|
|
*/
|
|
void NotifyOnSignalEventHandlers(
|
|
XSignal &signal,
|
|
X121SMCStrategyConditions &conditions //
|
|
)
|
|
{
|
|
//
|
|
if (!signal.IsValid() || !conditions.IsValid())
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
int count = ArraySize(mSignalEventHandlers);
|
|
if (!IsValidSize(count))
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
//
|
|
mSignalEventHandlers[i](
|
|
signal,
|
|
conditions //
|
|
);
|
|
}
|
|
}
|
|
|
|
//
|
|
// Private ...
|
|
private:
|
|
//
|
|
// Props ...
|
|
|
|
//
|
|
string mSymbol; // Trading Symbol ...
|
|
ENUM_TIMEFRAMES mPeriod; // Trading TimeFrame ...
|
|
bool mForceDisabled; // Force Disabled ...
|
|
bool mAllowLong; // Allow Long Signals
|
|
bool mAllowShort; // Allow Short Signals
|
|
double mR2R; // Risk/Rewar Ratio ...
|
|
double mVolume; // Static Volume Per Trades ...
|
|
|
|
//
|
|
// On X121 Signal Recieved Event Handler(s) ...
|
|
TOnX121SMCSignal mSignalEventHandlers[];
|
|
|
|
//
|
|
// Actions ...
|
|
|
|
/**
|
|
* Install all Helpers and Prepare all Configurations for required Indicators ...
|
|
*/
|
|
void Init()
|
|
{
|
|
//
|
|
mForceDisabled = false;
|
|
|
|
//
|
|
R2R(1);
|
|
Volume(0.01);
|
|
|
|
//
|
|
string symbol = Symbol();
|
|
ENUM_TIMEFRAMES period = Period();
|
|
|
|
//
|
|
// Initialize Bar Tracker ...
|
|
mBarTracker.Init(
|
|
symbol,
|
|
period //
|
|
);
|
|
|
|
//
|
|
// Initialize Guard Bar Tracker ...
|
|
mGuardBarTracker.Init(
|
|
symbol,
|
|
period //
|
|
);
|
|
|
|
//
|
|
mBarAnalyser = new XCBarAnalyser();
|
|
|
|
//
|
|
}
|
|
|
|
/**
|
|
* De Initialize all Tools ...
|
|
*/
|
|
void DeInit()
|
|
{
|
|
//
|
|
ZeroMemory(mBarAnalyser);
|
|
|
|
//
|
|
delete mBarAnalyser;
|
|
|
|
//
|
|
Clean(mSignalEventHandlers);
|
|
|
|
//
|
|
Destroy();
|
|
}
|
|
|
|
/**
|
|
* Reste Bar Tracker ...
|
|
*/
|
|
void ResetBarTracker()
|
|
{
|
|
mBarTracker.Clean();
|
|
}
|
|
|
|
//
|
|
};
|
|
|
|
// |