Files
xMQL5/BKPS/14030505/Documents/BKP/Old/Strategies/x-saherelm.xsrbr.strategy.mq5
T

723 lines
18 KiB
Plaintext

///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 Class Library
// --------------------------------------
// Name: XSCXSRBRStrategy
// 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.xstrategy.class.mq5"
//
string XSRBRStartegyToken = "XSRBR";
//
class XSCXSRBRStrategy : public XSCBaseStrategy
{
//
// Public ...
public:
//
// Constructor(s) ...
void XSCXSRBRStrategy(
//
// Base ...
string _symbol, // Trading Symbol
ENUM_TIMEFRAMES _period, // Trading TimeFrame
ENUM_TIMEFRAMES _analyzingPeriod, // Analyzing Timeframe
double _volume, // Voluem
double _r2r, // Risk/Reward Ratio
int _slippage, // Trader Slippage
long _magicNumber, // Trader Magic Number
bool _useTPSLAsPoint = false, // Converts TP and SL by Provided Points
bool _forceMaxTPSLAsPoint = false, // Force Max TP and SL by Provided Points
double _tpPoint = 0, // TP As Point
double _slPoint = 0, // SL As Point
bool _allowLong = true,
bool _allowShort = true,
int _maxAllowedLongs = 0,
int _maxAllowedShorts = 0,
ENUM_X_SIGNALLING_DIRECTION _signallingDirection = X_SIGNALLING_TREND_DIRECTION // Signalling in Which Direction
) : XSCBaseStrategy(_symbol,
_period,
_volume,
_r2r,
_slippage,
_magicNumber,
_useTPSLAsPoint,
_forceMaxTPSLAsPoint,
_tpPoint,
_slPoint,
_allowLong,
_allowShort,
_maxAllowedLongs,
_maxAllowedShorts //
)
{
//
mAnalyzingPeriod = _analyzingPeriod;
mSignallingDirection = _signallingDirection;
//
SetAlertPrefix(GetTag());
}
//
// Getter / Setter (s) ...
//
ENUM_X_SIGNALLING_DIRECTION SignallingDirection()
{
return mSignallingDirection;
}
//
void SignallingDirection(ENUM_X_SIGNALLING_DIRECTION value)
{
mSignallingDirection = value;
}
//
// Overrides ...
//
// Customize Strategy Identifier ...
string GetTag() override
{
return XSRBRStartegyToken;
}
//
// Check Conditions For Signal ...
bool HasSignal(
XSignal &signal //
) override
{
//
bool result = false;
//
double _threshold = 5;
double threshold = GetPoints(mSymbol) * _threshold;
//
signal.Clean();
//
double sl = 0;
double tp = 0;
double risk = 0;
double entry = 0;
double reward = 0;
double r2r = R2R();
ENUM_POSITION_TYPE type = POSITION_TYPE_BUY;
ENUM_X_ORDER_MODES mode = X_ORDER_MODE_MARKET;
//
// Check Signalling Direction ...
result = mSignallingDirection != X_SIGNALLING_NONE;
if (!result)
{
return result;
}
//
// Check Conditions Exists ...
result = HasRequirements();
if (!result)
{
//
// Prepare Conditions ...
result = PrepareRequirements();
if (!result)
{
return result;
}
}
//
// Implement Signalling Conditions ...
XOHCL cBar;
result = cBar.Init(
mSymbol,
mPeriod,
1 //
);
//
// Check Analyzing Period Contains a Trend or not ...
//
// Detecting Trend ...
bool isInBullishTrend = false;
bool isInBearishTrend = false;
//
// Verify Pressure for Signals ...
bool verifyPressure = true;
bool checkTrensByMethod2 = false;
//
bool checkHammer = false;
bool checkMorningStar = false;
bool checkBullishEngulfing = true;
//
bool checkShootingStar = false;
bool checkEveningStar = false;
bool checkBearishEngulfing = true;
//
isInBullishTrend = bar.HasBullishTrend(
highSwingBars,
lowSwingBars,
checkTrensByMethod2 //
);
if (!isInBullishTrend)
{
//
isInBearishTrend = bar.HasBearishTrend(
highSwingBars,
lowSwingBars,
checkTrensByMethod2 //
);
}
//
// Only Looks For Signals if aTrend Detected ...
result = isInBullishTrend || isInBearishTrend;
if (!result)
{
//
ResetRequirements();
return result;
}
//
if (isInBullishTrend)
{
//
DrawBullishTrend(
cBar,
highSwingBars,
lowSwingBars,
ChartID(),
true,
true //
);
}
else
{
//
DrawBearishTrend(
cBar,
highSwingBars,
lowSwingBars,
ChartID(),
true,
true //
);
}
//
// Here We have to Decide Trend or Reversal Act ...
//
// Check Trend Direction Signal ...
if (mSignallingDirection == X_SIGNALLING_BOTH_DIRECTIONS ||
mSignallingDirection == X_SIGNALLING_TREND_DIRECTION)
{
//
if (isInBullishTrend)
{
//
// Find Long Signals ...
// since we are in Bullish Trend and
// we must do Trend Trading ...
//
// In Trend Trading we must Waits for :
// - Support Rejection;
//
// Support Rejecting Hppens when Price Go Down of Support area and
// Go Up or
// Price Recive a Candlestic Pattern ...
//
// this Moving Up Make us Ready for Reciving a
// Candlestick Pattern which Prove us Buyers Pressure ...
// - Hammer;
// - Morning Star;
// - Bullish Engulfing;
//
XOHCL tmpSWH[];
XOHCL tmpSWL[];
//
bool isSupportRejected = IsSupportRejected(support, verifyPressure, checkHammer, checkMorningStar, checkBullishEngulfing);
bool isResistanceBreaked = IsResistanceBreaked(resistance, verifyPressure, checkHammer, checkMorningStar, checkBullishEngulfing);
//
bool isBullishTrend = cBar.HasBullishTrend(
tmpSWH,
tmpSWL,
true //
);
//
result =
//
isBullishTrend
//
&&
//
(
//
isSupportRejected
//
||
//
isResistanceBreaked
//
)
//
;
//
if (result)
{
//
type = POSITION_TYPE_BUY;
//
if (isSupportRejected)
{
//
sl = support.low - threshold;
RemoveSupport(support);
support.Clean();
}
//
if (isResistanceBreaked)
{
//
sl = resistance.low - threshold;
RemoveResistance(resistance);
resistance.Clean();
}
//
RemoveDraws(XTLSBullishTrendToken);
RemoveDraws(XTLSBearishTrendToken);
}
}
else if (isInBearishTrend)
{
//
// Find Short Signals ...
// since we are in Bearish Trend and
// we must do Trend Trading ...
//
XOHCL tmpSWH[];
XOHCL tmpSWL[];
//
bool isSupportBreaked = IsSupportBreaked(support, verifyPressure, checkShootingStar, checkEveningStar, checkBearishEngulfing);
bool isResistanceRejected = IsResistanceRejected(resistance, verifyPressure, checkShootingStar, checkEveningStar, checkBearishEngulfing);
//
bool isBearishTrend = cBar.HasBearishTrend(
tmpSWH,
tmpSWL,
true //
);
//
result =
//
isBearishTrend
//
&&
//
(
//
isSupportBreaked
//
||
//
isResistanceRejected
//
)
//
;
//
if (result)
{
//
type = POSITION_TYPE_SELL;
//
if (isSupportBreaked)
{
//
sl = support.high + threshold;
RemoveSupport(support);
support.Clean();
}
//
if (isResistanceRejected)
{
//
sl = resistance.high + threshold;
RemoveResistance(resistance);
resistance.Clean();
}
//
RemoveDraws(XTLSBullishTrendToken);
RemoveDraws(XTLSBearishTrendToken);
}
}
}
else
{
result = false;
}
//
// Check Trend Reversal Direction Signal ...
if (!result &&
(mSignallingDirection == X_SIGNALLING_BOTH_DIRECTIONS ||
mSignallingDirection == X_SIGNALIING_TREND_REVERSAL_DIRECTION))
{
//
if (isInBullishTrend)
{
//
// Find Short Signals ...
// since we are in Bullish Trend and
// we must do Trend Reversal Trading ...
//
XOHCL tmpSWH[];
XOHCL tmpSWL[];
//
bool isSupportBreaked = IsSupportBreaked(support, verifyPressure, checkShootingStar, checkEveningStar, checkBearishEngulfing);
bool isResistanceRejected = IsResistanceRejected(resistance, verifyPressure, checkShootingStar, checkEveningStar, checkBearishEngulfing);
//
bool isBearishTrend = cBar.HasBearishTrend(
tmpSWH,
tmpSWL,
true //
);
//
result =
//
true // isBearishTrend
//
&&
//
(
//
isSupportBreaked
//
||
//
isResistanceRejected
//
)
//
;
//
if (result)
{
//
type = POSITION_TYPE_SELL;
//
if (isSupportBreaked)
{
//
sl = support.high + threshold;
RemoveSupport(support);
support.Clean();
}
//
if (isResistanceRejected)
{
//
sl = resistance.high + threshold;
RemoveResistance(resistance);
resistance.Clean();
}
//
RemoveDraws(XTLSBullishTrendToken);
RemoveDraws(XTLSBearishTrendToken);
}
}
else if (isInBearishTrend)
{
//
// Find Long Signals ...
// since we are in Bearish Trend and
// we must do Trend Reversal Trading ...
//
XOHCL tmpSWH[];
XOHCL tmpSWL[];
//
bool isSupportRejected = IsSupportRejected(support, verifyPressure, checkHammer, checkMorningStar, checkBullishEngulfing);
bool isResistanceBreaked = IsResistanceBreaked(resistance, verifyPressure, checkHammer, checkMorningStar, checkBullishEngulfing);
//
bool isBullishTrend = cBar.HasBullishTrend(
tmpSWH,
tmpSWL,
true //
);
//
result =
//
true // isBullishTrend
//
&&
//
(
//
isSupportRejected
//
||
//
isResistanceBreaked
//
)
//
;
//
if (result)
{
//
type = POSITION_TYPE_BUY;
//
if (isSupportRejected)
{
//
sl = support.low - threshold;
RemoveSupport(support);
support.Clean();
}
//
if (isResistanceBreaked)
{
//
sl = resistance.low - threshold;
RemoveResistance(resistance);
resistance.Clean();
}
//
RemoveDraws(XTLSBullishTrendToken);
RemoveDraws(XTLSBearishTrendToken);
}
}
}
//
// Make Sure Signal Exists ...
if (!result)
{
//
RemoveDraws(XTLSBullishTrendToken);
RemoveDraws(XTLSBearishTrendToken);
//
ResetRequirements();
return result;
}
//
entry = GetEntry(mSymbol, type);
CalculateTPSLByPrice(
sl,
tp,
type,
entry,
r2r //
);
//
result = signal.Prepare(
mSymbol,
GetTag(),
mPeriod,
type,
mode,
entry,
mVolume,
sl,
tp //
);
//
return result;
}
//
// Protected ...
protected:
//
// Props ...
//
ENUM_TIMEFRAMES mAnalyzingPeriod; // Analyzing Period
ENUM_X_SIGNALLING_DIRECTION mSignallingDirection; // Signalling Direction
//
XOHCL bar;
//
// Pivots ...
XOHCL support;
XOHCL resistance;
//
XOHCL lowSwingBars[];
XOHCL highSwingBars[];
//
// Private ...
private:
//
//
// Check Requirements Exists or not ...
bool HasRequirements()
{
//
bool result = false;
//
result =
//
bar.IsValid() &&
support.IsValid() &&
resistance.IsValid()
//
;
//
return result;
}
//
// Prepare Requirements ...
bool PrepareRequirements()
{
//
bool result = false;
//
if (support.IsValid())
{
RemoveSupport(support);
}
//
// Finding Analyzing Bar ...
result = bar.Init(
mSymbol,
mAnalyzingPeriod,
1 //
);
if (!result)
{
return result;
}
//
// Find Support Bar based on Analyzing Bar ...
result = bar.HasSupport(
support //
);
if (!result)
{
return result;
}
//
DrawSupport(support, ChartID(), 5);
//
if (resistance.IsValid())
{
RemoveResistance(resistance);
}
//
// Find Resistance Bar based on Analyzing Bar ...
result = bar.HasResistance(
resistance //
);
//
DrawResistance(resistance, ChartID(), 5);
//
return result;
}
//
// Reset Reqirements ...
void ResetRequirements()
{
//
if (IsSupportBreaked(support, false))
{
//
RemoveSupport(support);
support.Clean();
}
//
if (IsResistanceBreaked(resistance, false))
{
//
RemoveResistance(resistance);
resistance.Clean();
}
}
};
//