diff --git a/Documents/BKP/Old/x-saherelm.xobvg.strategy.mq5 b/Documents/BKP/Old/x-saherelm.xobvg.strategy.mq5 new file mode 100644 index 00000000..bb624e38 --- /dev/null +++ b/Documents/BKP/Old/x-saherelm.xobvg.strategy.mq5 @@ -0,0 +1,766 @@ +/////////////////////////////////////////////////////// +// +// SaherElm IT Center MQL5 Class Library +// -------------------------------------- +// Name: XSCXOBVGStrategy +// 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 "../Helpers/x-saherelm.xtm.helper.mq5" +#include "../Helpers/x-saherelm.xstr.helper.mq5" +#include "../Classes/x-saherelm.xstrategy.class.mq5" + +// +string XOBVGStartegyToken = "XOBVG"; + +// +class XSCXOBVGStrategy : public XSCBaseStrategy +{ + // + // Public ... +public: + // + // Constructor(s) ... + void XSCXOBVGStrategy( + // + // Base ... + string _symbol, // Trading Symbol + ENUM_TIMEFRAMES _period, // Trading TimeFrame + ENUM_TIMEFRAMES _hindPeriod, // Hind Period + ENUM_TIMEFRAMES _mediestPeriod, // Mediest Period + 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 // + ) : XSCBaseStrategy(_symbol, + _period, + _volume, + _r2r, + _slippage, + _magicNumber, + _useTPSLAsPoint, + _forceMaxTPSLAsPoint, + _tpPoint, + _slPoint, + _allowLong, + _allowShort, + _maxAllowedLongs, + _maxAllowedShorts // + ) + { + // + mHindPeriod = _hindPeriod; + mMediestPeriod = _mediestPeriod; + + // + SetAlertPrefix(GetTag()); + + // + hindBar.Clean(); + hindBullishOrderBlock.Clean(); + hindBearishOrderBlock.Clean(); + + // + medBar.Clean(); + medBullishOrderBlock.Clean(); + medBearishOrderBlock.Clean(); + } + + // + // Deconstructor ... + void ~XSCXOBVGStrategy() + { + } + + // + // Getter / Setter (s) ... + + // + ENUM_TIMEFRAMES HindPeriod() + { + return mHindPeriod; + } + + // + void HindPeriod(ENUM_TIMEFRAMES value) + { + mHindPeriod = value; + } + + // + ENUM_TIMEFRAMES MediestPeriod() + { + return mMediestPeriod; + } + + // + void MediestPeriod(ENUM_TIMEFRAMES value) + { + mMediestPeriod = value; + } + + // + // Overrides ... + + // + // Customize Strategy Identifier ... + string GetTag() override + { + return XOBVGStartegyToken; + } + + // + // Check Conditions For Signal ... + bool HasSignal( + XSignal &signal // + ) override + { + // + bool result = false; + + // + 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; + + // + bool hasLong = false; + bool hasShort = false; + + // + XOHCL tmpHSW[]; + XOHCL tmpLSW[]; + + // + bool useHammer = true; + bool useMorningStar = false; + bool useBullishEngulfing = true; + bool useBullishMaurubozu = true; + + // + bool useShootingStar = true; + bool useEveningStar = false; + bool useBearishEngulfing = true; + bool useBearishMaurubozu = true; + + // + XOHCL cBar; + result = cBar.Init( + mSymbol, + mPeriod, + 1 // + ); + if (!result) + { + return result; + } + + // + double ll3 = cBar.FindLowest( + 3, + MODE_LOW // + ); + double hh3 = cBar.FindHighest( + 3, + MODE_HIGH // + ); + + // + result = hindBar.IsValid(); + if (!result) + { + // + result = hindBar.Init( + mSymbol, + mHindPeriod, + 1 // + ); + if (!result) + { + return result; + } + } + + // + bool hasBullishOrderBlock = hindBullishOrderBlock.IsValid(); + bool hasBearishOrderBlock = hindBearishOrderBlock.IsValid(); + result = + // + hasBullishOrderBlock + // + || + // + hasBearishOrderBlock + // + ; + if (!result) + { + // + hasBullishOrderBlock = hindBar.HasBullishOrderBlock(hindBullishOrderBlock); + hasBearishOrderBlock = hindBar.HasBearishOrderBlock(hindBearishOrderBlock); + + // + result = + // + hasBullishOrderBlock + // + || + // + hasBearishOrderBlock + // + ; + if (!result) + { + // + hindBar.Clean(); + return result; + } + } + + // + if (hasBullishOrderBlock) + { + // + DrawBullishOrderBlock( + hindBullishOrderBlock, + ChartID() // + ); + + // + // Try to Detect and Order Block Inside this ... + // in Mediest ... + result = medBar.IsValid(); + if (!result) + { + // + result = medBar.Init( + mSymbol, + mMediestPeriod, + 1 // + ); + if (!result) + { + // + medBar.Clean(); + return result; + } + } + + // + result = medBar.HasBullishOrderBlock(medBullishOrderBlock); + if (!result) + { + // + medBar.Clean(); + return result; + } + + // + DrawBullishOrderBlock( + medBullishOrderBlock, + ChartID() // + ); + + // + // Check Order Block Is Inside HINDOB ... + result = + // + (medBullishOrderBlock.low >= hindBullishOrderBlock.low && + medBullishOrderBlock.low <= hindBullishOrderBlock.high) + // + || + // + (medBullishOrderBlock.high <= hindBullishOrderBlock.high && + medBullishOrderBlock.high >= hindBullishOrderBlock.low) + // + ; + + // + if (result) + { + // + // Check Price ... + result = + // + ll3 < medBullishOrderBlock.high && + ll3 > hindBullishOrderBlock.low + // + ; + if (!result) + { + // + hindBar.Clean(); + hindBullishOrderBlock.Clean(); + hindBearishOrderBlock.Clean(); + + // + medBar.Clean(); + medBullishOrderBlock.Clean(); + medBearishOrderBlock.Clean(); + + // + fvgUpper = 0; + fvgLower = 0; + + // + RemoveDraws(); + + // + return result; + } + + // + hasLong = result; + } + else + { + // + // Detect and FVG Inside ... + result = medBar.HasBullishFVG( + fvgUpper, + fvgLower // + ); + if (!result) + { + // + hindBar.Clean(); + hindBullishOrderBlock.Clean(); + hindBearishOrderBlock.Clean(); + + // + medBar.Clean(); + medBullishOrderBlock.Clean(); + medBearishOrderBlock.Clean(); + + // + fvgUpper = 0; + fvgLower = 0; + + // + RemoveDraws(); + + // + return result; + } + + // + DrawBullishFVG( + medBar, + fvgUpper, + fvgLower, + ChartID() // + ); + + // + result = + // + (fvgLower >= hindBullishOrderBlock.low && + fvgLower <= hindBullishOrderBlock.high) + // + || + // + (fvgUpper <= hindBullishOrderBlock.high && + fvgUpper >= hindBullishOrderBlock.low) + // + ; + // + // Check Price ... + result = + // + ll3 < fvgUpper && + ll3 > hindBullishOrderBlock.low + // + ; + if (!result) + { + // + hindBar.Clean(); + hindBullishOrderBlock.Clean(); + hindBearishOrderBlock.Clean(); + + // + medBar.Clean(); + medBullishOrderBlock.Clean(); + medBearishOrderBlock.Clean(); + + // + fvgUpper = 0; + fvgLower = 0; + + // + RemoveDraws(); + + // + return result; + } + + // + hasLong = result; + } + } + + // + if (hasBearishOrderBlock) + { + // + DrawBearishOrderBlock( + hindBearishOrderBlock, + ChartID() // + ); + + // + // Try to Detect and Order Block Inside this ... + // in Mediest ... + result = medBar.IsValid(); + if (!result) + { + // + result = medBar.Init( + mSymbol, + mMediestPeriod, + 1 // + ); + if (!result) + { + // + medBar.Clean(); + return result; + } + } + + // + result = medBar.HasBearishOrderBlock(medBearishOrderBlock); + if (!result) + { + // + medBar.Clean(); + return result; + } + + // + DrawBearishOrderBlock( + medBearishOrderBlock, + ChartID() // + ); + + // + // Check Order Block Is Inside HINDOB ... + result = + // + (medBullishOrderBlock.low >= hindBearishOrderBlock.low && + medBearishOrderBlock.low <= hindBearishOrderBlock.high) + // + || + // + (medBearishOrderBlock.high <= hindBearishOrderBlock.high && + medBearishOrderBlock.high >= hindBearishOrderBlock.low) + // + ; + if (!result) + { + // + // Detect and FVG Inside ... + result = medBar.HasBearishFVG( + fvgUpper, + fvgLower // + ); + if (!result) + { + // + hindBar.Clean(); + hindBullishOrderBlock.Clean(); + hindBearishOrderBlock.Clean(); + + // + medBar.Clean(); + medBullishOrderBlock.Clean(); + medBearishOrderBlock.Clean(); + + // + fvgUpper = 0; + fvgLower = 0; + + // + RemoveDraws(); + + // + return result; + } + + // + DrawBearishFVG( + medBar, + fvgUpper, + fvgLower, + ChartID() // + ); + + // + result = + // + (fvgLower >= hindBearishOrderBlock.low && + fvgLower <= hindBearishOrderBlock.high) + // + || + // + (fvgUpper <= hindBearishOrderBlock.high && + fvgUpper >= hindBearishOrderBlock.low) + // + ; + if (!result) + { + // + hindBar.Clean(); + hindBullishOrderBlock.Clean(); + hindBearishOrderBlock.Clean(); + + // + medBar.Clean(); + medBullishOrderBlock.Clean(); + medBearishOrderBlock.Clean(); + + // + fvgUpper = 0; + fvgLower = 0; + + // + RemoveDraws(); + + // + return result; + } + } + } + + // + result = hasLong || hasShort; + if (!result) + { + return result; + } + + // + if (hasLong) + { + // + // Long ... + + // + bool isHammer = !useHammer + ? false + : cBar.IsHammer(); + bool isMorningStar = !useMorningStar + ? false + : cBar.IsMorningStar(); + bool isBullishMarubozu = !useBullishEngulfing + ? false + : cBar.IsBullishMarubozu(); + bool isBullishEngulfing = !useBullishMaurubozu + ? false + : cBar.IsBullishEngulfing(); + + // + bool isBullishTrend = cBar.HasBullishTrend( + tmpHSW, + tmpLSW, + true // + ); + + // + // Find Swing Low Above of EMA ... + // Find Swing High Above of EMA ... + // Fins Swing Low Below of EMA ... + // Detect Bullish Pattern ... + hasLong = + // + true + // + && + // + ( + // + isHammer + // + || + // + isMorningStar + // + || + // + isBullishMarubozu + // + || + // + isBullishEngulfing + // + ) + // + ; + if (hasLong) + { + // + type = POSITION_TYPE_BUY; + + // + // sl = tmpLSW[ArraySize(tmpLSW) - 1].low; + } + } + + // + if (hasShort) + { + // + // Short ... + bool isShootingStar = !useShootingStar + ? false + : cBar.IsShootingStar(); + bool isEveningStar = !useEveningStar + ? false + : cBar.IsEveningStar(); + bool isBearishEngulfing = !useBearishEngulfing + ? false + : cBar.IsBearishEngulfing(); + bool isBearishMaurubozu = !useBearishMaurubozu + ? false + : cBar.IsBearishMarubozu(); + + // + bool isBearishTrend = cBar.HasBearishTrend( + tmpHSW, + tmpLSW, + true // + ); + + // + hasShort = + // + false + // + && + // + ( + // + isShootingStar + // + || + // + isEveningStar + // + || + // + isBearishEngulfing + // + || + // + isBearishMaurubozu + // + ) + // + ; + if (hasShort) + { + // + type = POSITION_TYPE_SELL; + + // + // sl = tmpHSW[ArraySize(tmpHSW) - 1].high; + } + } + + // + result = hasLong || hasShort; + if (!result) + { + 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 mHindPeriod; // Hind Period + ENUM_TIMEFRAMES mMediestPeriod; // Mediest Period + + // + // Private ... +private: + // + // Props ... + + // + XOHCL hindBar; + XOHCL hindBullishOrderBlock; + XOHCL hindBearishOrderBlock; + + // + XOHCL medBar; + XOHCL medBullishOrderBlock; + XOHCL medBearishOrderBlock; + + // + double fvgUpper; + double fvgLower; +}; + +// \ No newline at end of file diff --git a/Documents/BKP/usefull.mq5 b/Documents/BKP/usefull.mq5 index b8693821..71719c82 100644 --- a/Documents/BKP/usefull.mq5 +++ b/Documents/BKP/usefull.mq5 @@ -5409,3 +5409,440 @@ int pivotsCount = ArraySize(pivots); // ; +///////////////////////////////////////////////////////////////////// + + // + // Try to Detect and Order Block Inside this ... + // in Mediest ... + result = medBar.IsValid(); + if (!result) + { + // + result = medBar.Init( + mSymbol, + mMediestPeriod, + 1 // + ); + if (!result) + { + // + medBar.Clean(); + return result; + } + } + + // + result = medBar.HasBullishOrderBlock(medBullishOrderBlock); + if (!result) + { + // + medBar.Clean(); + return result; + } + + // + DrawBullishOrderBlock( + medBullishOrderBlock, + ChartID() // + ); + + // + // Check Order Block Is Inside HINDOB ... + result = + // + (medBullishOrderBlock.low >= hindBullishOrderBlock.low && + medBullishOrderBlock.low <= hindBullishOrderBlock.high) + // + || + // + (medBullishOrderBlock.high <= hindBullishOrderBlock.high && + medBullishOrderBlock.high >= hindBullishOrderBlock.low) + // + ; + + // + if (result) + { + // + // Check Price ... + result = + // + ll3 < medBullishOrderBlock.high && + ll3 > hindBullishOrderBlock.low + // + ; + if (!result) + { + // + hindBar.Clean(); + hindBullishOrderBlock.Clean(); + hindBearishOrderBlock.Clean(); + + // + medBar.Clean(); + medBullishOrderBlock.Clean(); + medBearishOrderBlock.Clean(); + + // + fvgUpper = 0; + fvgLower = 0; + + // + RemoveDraws(); + + // + return result; + } + + // + hasLong = result; + } + else + { + // + // Detect and FVG Inside ... + result = medBar.HasBullishFVG( + fvgUpper, + fvgLower // + ); + if (!result) + { + // + hindBar.Clean(); + hindBullishOrderBlock.Clean(); + hindBearishOrderBlock.Clean(); + + // + medBar.Clean(); + medBullishOrderBlock.Clean(); + medBearishOrderBlock.Clean(); + + // + fvgUpper = 0; + fvgLower = 0; + + // + RemoveDraws(); + + // + return result; + } + + // + DrawBullishFVG( + medBar, + fvgUpper, + fvgLower, + ChartID() // + ); + + // + result = + // + (fvgLower >= hindBullishOrderBlock.low && + fvgLower <= hindBullishOrderBlock.high) + // + || + // + (fvgUpper <= hindBullishOrderBlock.high && + fvgUpper >= hindBullishOrderBlock.low) + // + ; + // + // Check Price ... + result = + // + ll3 < fvgUpper && + ll3 > hindBullishOrderBlock.low + // + ; + if (!result) + { + // + hindBar.Clean(); + hindBullishOrderBlock.Clean(); + hindBearishOrderBlock.Clean(); + + // + medBar.Clean(); + medBullishOrderBlock.Clean(); + medBearishOrderBlock.Clean(); + + // + fvgUpper = 0; + fvgLower = 0; + + // + RemoveDraws(); + + // + return result; + } + + // + hasLong = result; + } + + +//////////////////////////////////////////////////////////////////////////// + + + // + // Try to Detect and Order Block Inside this ... + // in Mediest ... + result = medBar.IsValid(); + if (!result) + { + // + result = medBar.Init( + mSymbol, + mMediestPeriod, + 1 // + ); + if (!result) + { + // + medBar.Clean(); + return result; + } + } + + // + result = medBar.HasBearishOrderBlock(medBearishOrderBlock); + if (!result) + { + // + medBar.Clean(); + return result; + } + + // + DrawBearishOrderBlock( + medBearishOrderBlock, + ChartID() // + ); + + // + // Check Order Block Is Inside HINDOB ... + result = + // + (medBullishOrderBlock.low >= hindBearishOrderBlock.low && + medBearishOrderBlock.low <= hindBearishOrderBlock.high) + // + || + // + (medBearishOrderBlock.high <= hindBearishOrderBlock.high && + medBearishOrderBlock.high >= hindBearishOrderBlock.low) + // + ; + if (!result) + { + // + // Detect and FVG Inside ... + result = medBar.HasBearishFVG( + fvgUpper, + fvgLower // + ); + if (!result) + { + // + hindBar.Clean(); + hindBullishOrderBlock.Clean(); + hindBearishOrderBlock.Clean(); + + // + medBar.Clean(); + medBullishOrderBlock.Clean(); + medBearishOrderBlock.Clean(); + + // + fvgUpper = 0; + fvgLower = 0; + + // + RemoveDraws(); + + // + return result; + } + + // + DrawBearishFVG( + medBar, + fvgUpper, + fvgLower, + ChartID() // + ); + + // + result = + // + (fvgLower >= hindBearishOrderBlock.low && + fvgLower <= hindBearishOrderBlock.high) + // + || + // + (fvgUpper <= hindBearishOrderBlock.high && + fvgUpper >= hindBearishOrderBlock.low) + // + ; + if (!result) + { + // + hindBar.Clean(); + hindBullishOrderBlock.Clean(); + hindBearishOrderBlock.Clean(); + + // + medBar.Clean(); + medBullishOrderBlock.Clean(); + medBearishOrderBlock.Clean(); + + // + fvgUpper = 0; + fvgLower = 0; + + // + RemoveDraws(); + + // + return result; + } + } + +////////////////////////////////////////////////////////////////////////////////// + + // + result = hasLong || hasShort; + if (!result) + { + return result; + } + + // + if (hasLong) + { + // + // Long ... + + // + bool isHammer = !useHammer + ? false + : cBar.IsHammer(); + bool isMorningStar = !useMorningStar + ? false + : cBar.IsMorningStar(); + bool isBullishMarubozu = !useBullishEngulfing + ? false + : cBar.IsBullishMarubozu(); + bool isBullishEngulfing = !useBullishMaurubozu + ? false + : cBar.IsBullishEngulfing(); + + // + bool isBullishTrend = cBar.HasBullishTrend( + tmpHSW, + tmpLSW, + true // + ); + + // + // Find Swing Low Above of EMA ... + // Find Swing High Above of EMA ... + // Fins Swing Low Below of EMA ... + // Detect Bullish Pattern ... + hasLong = + // + true + // + && + // + ( + // + isHammer + // + || + // + isMorningStar + // + || + // + isBullishMarubozu + // + || + // + isBullishEngulfing + // + ) + // + ; + if (hasLong) + { + // + type = POSITION_TYPE_BUY; + + // + // sl = tmpLSW[ArraySize(tmpLSW) - 1].low; + } + } + + // + if (hasShort) + { + // + // Short ... + bool isShootingStar = !useShootingStar + ? false + : cBar.IsShootingStar(); + bool isEveningStar = !useEveningStar + ? false + : cBar.IsEveningStar(); + bool isBearishEngulfing = !useBearishEngulfing + ? false + : cBar.IsBearishEngulfing(); + bool isBearishMaurubozu = !useBearishMaurubozu + ? false + : cBar.IsBearishMarubozu(); + + // + bool isBearishTrend = cBar.HasBearishTrend( + tmpHSW, + tmpLSW, + true // + ); + + // + hasShort = + // + false + // + && + // + ( + // + isShootingStar + // + || + // + isEveningStar + // + || + // + isBearishEngulfing + // + || + // + isBearishMaurubozu + // + ) + // + ; + if (hasShort) + { + // + type = POSITION_TYPE_SELL; + + // + // sl = tmpHSW[ArraySize(tmpHSW) - 1].high; + } + } diff --git a/Experts/x-simple-test.ea.mq5 b/Experts/x-simple-test.ea.mq5 index 61467c13..341f2835 100644 --- a/Experts/x-simple-test.ea.mq5 +++ b/Experts/x-simple-test.ea.mq5 @@ -814,11 +814,9 @@ void FindSupport() // datetime at = NULL; - double support = 0; + XOHCL support; bool hasSupport = cBar.HasSupport( - support, - at // - ); + support); if (!hasSupport) { return; @@ -828,21 +826,7 @@ void FindSupport() eaBarTracker.Waits(); // - DrawTrendLine( - ChartID(), - "Support-" + ToString((int)at), - 0, - at, - support, - TimeCurrent(), - support, - clrAqua, - STYLE_DOT, - 1, - false, - false, - true // - ); + DrawSupport(support, ChartID()); // Print("Support Detected ..."); @@ -866,11 +850,9 @@ void FindResistance() // datetime at = NULL; - double resistance = 0; + XOHCL resistance; bool hasResistance = cBar.HasResistance( - resistance, - at // - ); + resistance); if (!hasResistance) { return; @@ -880,21 +862,7 @@ void FindResistance() eaBarTracker.Waits(); // - DrawTrendLine( - ChartID(), - "Resistance-" + ToString((int)at), - 0, - at, - resistance, - TimeCurrent(), - resistance, - clrMagenta, - STYLE_DOT, - 1, - false, - false, - true // - ); + DrawResistance(resistance, ChartID()); // Print("Resistance Detected ..."); @@ -917,19 +885,13 @@ void FindBullishTrend() } // - double hSwings[]; - datetime hSwingTimes[]; - - // - double lSwings[]; - datetime lSwingTimes[]; + XOHCL hSwings[]; + XOHCL lSwings[]; // bool isBullishTrend = cBar.HasBullishTrend( hSwings, - hSwingTimes, lSwings, - lSwingTimes, false // Method 2 ... ); if (!isBullishTrend) @@ -941,85 +903,13 @@ void FindBullishTrend() eaBarTracker.Waits(); // - DrawTrendLine( - ChartID(), - "HSING-BULLTrend-" + ToString((int)hSwingTimes[ArraySize(hSwingTimes) - 1]) + "," + ToString((int)hSwingTimes[0]), - 0, - hSwingTimes[ArraySize(hSwingTimes) - 1], - hSwings[ArraySize(hSwings) - 1], - hSwingTimes[0], - hSwings[0], - clrAqua, - STYLE_DOT, - 1, - false, - false, - true // + DrawBullishTrend( + cBar, + hSwings, + lSwings, + ChartID() // ); - // - DrawTrendLine( - ChartID(), - "LSING-BULLTrend-" + ToString((int)lSwingTimes[ArraySize(lSwingTimes) - 1]) + "," + ToString((int)lSwingTimes[0]), - 0, - lSwingTimes[ArraySize(lSwingTimes) - 1], - lSwings[ArraySize(lSwings) - 1], - lSwingTimes[0], - lSwings[0], - clrMagenta, - STYLE_DOT, - 1, - false, - false, - true // - ); - - // - // Draw Arrow On Swing Highs ... - for (int i = 0; i < ArraySize(hSwings); i++) - { - // - double iPrice = hSwings[i]; - datetime iTime = hSwingTimes[i]; - - // - DrawArrow( - ChartID(), - "HSIGN-BULLTrend-Arrow-" + ToString((int)iTime), - 0, - iTime, - iPrice, - 234, - ANCHOR_BOTTOM, - clrAqua, - STYLE_SOLID, - 2 // - ); - } - - // - // Draw Arrow On Swing Highs ... - for (int i = 0; i < ArraySize(lSwings); i++) - { - // - double iPrice = lSwings[i]; - datetime iTime = lSwingTimes[i]; - - // - DrawArrow( - ChartID(), - "LSIGN-BULLTrend-Arrow-" + ToString((int)iTime), - 0, - iTime, - iPrice, - 233, - ANCHOR_TOP, - clrMagenta, - STYLE_SOLID, - 2 // - ); - } - // Print("Bullish Trend Detected ..."); } @@ -1041,19 +931,13 @@ void FindBearishTrend() } // - double hSwings[]; - datetime hSwingTimes[]; - - // - double lSwings[]; - datetime lSwingTimes[]; + XOHCL hSwings[]; + XOHCL lSwings[]; // bool isBearishTrend = cBar.HasBearishTrend( hSwings, - hSwingTimes, lSwings, - lSwingTimes, false // Method 2 ... ); if (!isBearishTrend) @@ -1065,92 +949,21 @@ void FindBearishTrend() eaBarTracker.Waits(); // - DrawTrendLine( - ChartID(), - "HSING-BEARTrend-" + ToString((int)hSwingTimes[ArraySize(hSwingTimes) - 1]) + "," + ToString((int)hSwingTimes[0]), - 0, - hSwingTimes[ArraySize(hSwingTimes) - 1], - hSwings[ArraySize(hSwings) - 1], - hSwingTimes[0], - hSwings[0], - clrAqua, - STYLE_DOT, - 1, - false, - false, - true // + DrawBearishTrend( + cBar, + hSwings, + lSwings, + ChartID() // ); - // - DrawTrendLine( - ChartID(), - "LSING-BEARTrend-" + ToString((int)lSwingTimes[ArraySize(lSwingTimes) - 1]) + "," + ToString((int)lSwingTimes[0]), - 0, - lSwingTimes[ArraySize(lSwingTimes) - 1], - lSwings[ArraySize(lSwings) - 1], - lSwingTimes[0], - lSwings[0], - clrMagenta, - STYLE_DOT, - 1, - false, - false, - true // - ); - - // - // Draw Arrow On Swing Highs ... - for (int i = 0; i < ArraySize(hSwings); i++) - { - // - double iPrice = hSwings[i]; - datetime iTime = hSwingTimes[i]; - - // - DrawArrow( - ChartID(), - "HSIGN-BEARTrend-Arrow-" + ToString((int)iTime), - 0, - iTime, - iPrice, - 234, - ANCHOR_BOTTOM, - clrAqua, - STYLE_SOLID, - 2 // - ); - } - - // - // Draw Arrow On Swing Highs ... - for (int i = 0; i < ArraySize(lSwings); i++) - { - // - double iPrice = lSwings[i]; - datetime iTime = lSwingTimes[i]; - - // - DrawArrow( - ChartID(), - "LSIGN-BEARTrend-Arrow-" + ToString((int)iTime), - 0, - iTime, - iPrice, - 233, - ANCHOR_TOP, - clrMagenta, - STYLE_SOLID, - 2 // - ); - } - // Print("Bearish Trend Detected ..."); } // // Find Bullish Order Block ... -void FindBullishOrderBlock() { +void FindBullishOrderBlock() +{ // XOHCL cBar; bool isInited = cBar.Init( @@ -1168,7 +981,8 @@ void FindBullishOrderBlock() { bool hasOB = cBar.HasBullishOrderBlock( obBar // ); - if (!hasOB) { + if (!hasOB) + { return; } @@ -1178,7 +992,7 @@ void FindBullishOrderBlock() { // DrawTrendLine( ChartID(), - "BULLOB-H-" + ToString((int) obBar.time), + "BULLOB-H-" + ToString((int)obBar.time), 0, obBar.time, obBar.high, @@ -1195,7 +1009,7 @@ void FindBullishOrderBlock() { // DrawTrendLine( ChartID(), - "BULLOB-L-" + ToString((int) obBar.time), + "BULLOB-L-" + ToString((int)obBar.time), 0, obBar.time, obBar.low, @@ -1215,7 +1029,8 @@ void FindBullishOrderBlock() { // // Find Bearish Order Block ... -void FindBearishOrderBlock() { +void FindBearishOrderBlock() +{ // XOHCL cBar; bool isInited = cBar.Init( @@ -1233,7 +1048,8 @@ void FindBearishOrderBlock() { bool hasOB = cBar.HasBearishOrderBlock( obBar // ); - if (!hasOB) { + if (!hasOB) + { return; } @@ -1243,7 +1059,7 @@ void FindBearishOrderBlock() { // DrawTrendLine( ChartID(), - "BEAROB-H-" + ToString((int) obBar.time), + "BEAROB-H-" + ToString((int)obBar.time), 0, obBar.time, obBar.high, @@ -1260,7 +1076,7 @@ void FindBearishOrderBlock() { // DrawTrendLine( ChartID(), - "BEAROB-L-" + ToString((int) obBar.time), + "BEAROB-L-" + ToString((int)obBar.time), 0, obBar.time, obBar.low, diff --git a/Experts/x-test-strategies.ea.mq5 b/Experts/x-test-strategies.ea.mq5 index 03051f17..f0ee3392 100644 --- a/Experts/x-test-strategies.ea.mq5 +++ b/Experts/x-test-strategies.ea.mq5 @@ -2,7 +2,7 @@ // // SaherElm IT Center XTest MQL5 Expert Advisor // ------------------------------------------------- -// Name: XTestPivotsEA +// Name: XSTestEA // Description: an Exper Advisor which used XTestSetup // to Analyse Market ... // @@ -16,11 +16,11 @@ #property copyright "Copyright 2023, SaherElm IT Center" #property link "https://saherelm.ir" #property version "1.00" -#property description "SaherElm XTestPivotsEA" +#property description "SaherElm XSTestEA" #property strict // -#define ShortName "XTestPivotsEA" +#define ShortName "XSTestEA" // // Imports ... @@ -28,6 +28,7 @@ #include "../Helpers/x-saherelm.xcc.helper.mq5" // +#include "../Strategies/x-saherelm.xobvg.strategy.mq5" #include "../Strategies/x-saherelm.xsrbr.strategy.mq5" #include "../Strategies/x-saherelm.xemrng.strategy.mq5" @@ -50,7 +51,7 @@ double eaTPPoint = 200; double eaSLPoint = 100; // -double eaR2R = 1.5; +double eaR2R = 2; // // Variables ... @@ -79,6 +80,9 @@ int OnInit() return INIT_FAILED; } + // + drawPrefix = ShortName; + // // EventSetTimer(1); // EventSetMillisecondTimer(100); @@ -200,7 +204,8 @@ bool InitialEA() // bool useXSRBR = false; - bool useXEMRNG = true; + bool useXEMRNG = false; + bool useXOBVG = true; // // EURUSD ... @@ -257,8 +262,8 @@ bool InitialEA() eaSLPoint, // SL eaAllowLong, eaAllowShort, - eaMaxAllowedLongs, // Max Longs - eaMaxAllowedShorts, // Max Shorts + eaMaxAllowedLongs, // Max Longs + eaMaxAllowedShorts, // Max Shorts X_SIGNALLING_BOTH_DIRECTIONS // X_SIGNALIING_TREND_REVERSAL_DIRECTION // X_SIGNALLING_BOTH_DIRECTIONS // ); eurusdM15SRBRStrategy.SetAlertLogAlerts(true); @@ -292,6 +297,34 @@ bool InitialEA() eurusdM5EMRNGStrategy.SetAlertEnableAlerts(true); RegisterStrategy(eurusdM5EMRNGStrategy); } + + // + // XOBVG ... + if (useXOBVG) + { + // + XSCXOBVGStrategy *eurusdM5OBVGStrategy; + eurusdM5OBVGStrategy = new XSCXOBVGStrategy( + eurusdSymbol, + PERIOD_M5, + PERIOD_H2, // Analyzing Period + eaVolume, + eaR2R, // R2R ... + eaSlippage, + magicNumber, + false, // Use TP SL Point + false, // Use Max + eaTPPoint, // TP + eaSLPoint, // SL + eaAllowLong, + eaAllowShort, + eaMaxAllowedLongs, // Max Longs + eaMaxAllowedShorts // Max Shorts + ); + eurusdM5OBVGStrategy.SetAlertLogAlerts(true); + eurusdM5OBVGStrategy.SetAlertEnableAlerts(true); + RegisterStrategy(eurusdM5OBVGStrategy); + } } // @@ -324,8 +357,8 @@ bool InitialEA() eaSLPoint, // SL eaAllowLong, eaAllowShort, - eaMaxAllowedLongs, // Max Longs - eaMaxAllowedShorts, // Max Shorts + eaMaxAllowedLongs, // Max Longs + eaMaxAllowedShorts, // Max Shorts X_SIGNALLING_BOTH_DIRECTIONS // X_SIGNALIING_TREND_REVERSAL_DIRECTION // X_SIGNALLING_BOTH_DIRECTIONS // ); usdchfM5SRBRStrategy.SetAlertLogAlerts(true); @@ -391,8 +424,8 @@ bool InitialEA() eaSLPoint, // SL eaAllowLong, eaAllowShort, - eaMaxAllowedLongs, // Max Longs - eaMaxAllowedShorts, // Max Shorts + eaMaxAllowedLongs, // Max Longs + eaMaxAllowedShorts, // Max Shorts X_SIGNALLING_BOTH_DIRECTIONS // X_SIGNALIING_TREND_REVERSAL_DIRECTION // X_SIGNALLING_BOTH_DIRECTIONS // ); usdjpyM5SRBRStrategy.SetAlertLogAlerts(true); @@ -458,8 +491,8 @@ bool InitialEA() eaSLPoint, // SL eaAllowLong, eaAllowShort, - eaMaxAllowedLongs, // Max Longs - eaMaxAllowedShorts, // Max Shorts + eaMaxAllowedLongs, // Max Longs + eaMaxAllowedShorts, // Max Shorts X_SIGNALLING_BOTH_DIRECTIONS // X_SIGNALIING_TREND_REVERSAL_DIRECTION // X_SIGNALLING_BOTH_DIRECTIONS // ); gbpusdM5SRBRStrategy.SetAlertLogAlerts(true); @@ -525,8 +558,8 @@ bool InitialEA() eaSLPoint, // SL eaAllowLong, eaAllowShort, - eaMaxAllowedLongs, // Max Longs - eaMaxAllowedShorts, // Max Shorts + eaMaxAllowedLongs, // Max Longs + eaMaxAllowedShorts, // Max Shorts X_SIGNALLING_BOTH_DIRECTIONS // X_SIGNALIING_TREND_REVERSAL_DIRECTION // X_SIGNALLING_BOTH_DIRECTIONS // ); xauusdM5SRBRStrategy.SetAlertLogAlerts(true); diff --git a/Indicators/vwap.mq5 b/Indicators/vwap.mq5 new file mode 100644 index 00000000..e5fe0ac1 --- /dev/null +++ b/Indicators/vwap.mq5 @@ -0,0 +1,552 @@ +//+------------------------------------------------------------------+ +//| VWAP.mq5 | +//| Copyright 2015, SOL Digital Consultoria LTDA | +//| http://www.soldigitalconsultoria.com.br | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2015, SOL Digital Consultoria LTDA" +#property link "http://www.soldigitalconsultoria.com.br" +#property version "1.47" + +#property indicator_chart_window +#property indicator_buffers 8 +#property indicator_plots 8 + +//--- plot VWAP +#property indicator_label1 "VWAP Daily" +#property indicator_type1 DRAW_LINE +#property indicator_color1 clrRed +#property indicator_style1 STYLE_DASH +#property indicator_width1 2 + +#property indicator_label2 "VWAP Weekly" +#property indicator_type2 DRAW_LINE +#property indicator_color2 clrBlue +#property indicator_style2 STYLE_DASH +#property indicator_width2 2 + +#property indicator_label3 "VWAP Monthly" +#property indicator_type3 DRAW_LINE +#property indicator_color3 clrGreen +#property indicator_style3 STYLE_DASH +#property indicator_width3 2 + +#property indicator_label4 "VWAP Level 01" +#property indicator_type4 DRAW_LINE +#property indicator_color4 clrGray +#property indicator_style4 STYLE_DASH +#property indicator_width4 2 + +#property indicator_label5 "VWAP Level 02" +#property indicator_type5 DRAW_LINE +#property indicator_color5 clrYellow +#property indicator_style5 STYLE_DASH +#property indicator_width5 2 + +#property indicator_label6 "VWAP Level 03" +#property indicator_type6 DRAW_LINE +#property indicator_color6 clrGreen +#property indicator_style6 STYLE_DASH +#property indicator_width6 2 + +#property indicator_label7 "VWAP Level 04" +#property indicator_type7 DRAW_LINE +#property indicator_color7 clrBlack +#property indicator_style7 STYLE_DASH +#property indicator_width7 2 + +#property indicator_label8 "VWAP Level 05" +#property indicator_type8 DRAW_LINE +#property indicator_color8 clrBlue +#property indicator_style8 STYLE_DASH +#property indicator_width8 2 +//+------------------------------------------------------------------+ +//| | +//+------------------------------------------------------------------+ +enum DATE_TYPE + { + DAILY, + WEEKLY, + MONTHLY + }; +//+------------------------------------------------------------------+ +//| | +//+------------------------------------------------------------------+ +enum PRICE_TYPE + { + OPEN, + CLOSE, + HIGH, + LOW, + OPEN_CLOSE, + HIGH_LOW, + CLOSE_HIGH_LOW, + OPEN_CLOSE_HIGH_LOW + }; +//+------------------------------------------------------------------+ +//| | +//+------------------------------------------------------------------+ +datetime CreateDateTime(DATE_TYPE nReturnType=DAILY,datetime dtDay=D'2000.01.01 00:00:00',int pHour=0,int pMinute=0,int pSecond=0) + { + datetime dtReturnDate; + MqlDateTime timeStruct; + + TimeToStruct(dtDay,timeStruct); + timeStruct.hour = pHour; + timeStruct.min = pMinute; + timeStruct.sec = pSecond; + dtReturnDate=(StructToTime(timeStruct)); + + if(nReturnType==WEEKLY) + { + while(timeStruct.day_of_week!=0) + { + dtReturnDate=(dtReturnDate-86400); + TimeToStruct(dtReturnDate,timeStruct); + } + } + + if(nReturnType==MONTHLY) + { + timeStruct.day=1; + dtReturnDate=(StructToTime(timeStruct)); + } + + return dtReturnDate; + } + +sinput string Indicator_Name="Volume Weighted Average Price (VWAP)"; +input PRICE_TYPE Price_Type = CLOSE_HIGH_LOW; +input bool Enable_Daily = true; +input bool Enable_Weekly = true; +input bool Enable_Monthly = true; +input bool Enable_Level_01 = false; +input int VWAP_Level_01_Period = 5; +input bool Enable_Level_02 = false; +input int VWAP_Level_02_Period = 13; +input bool Enable_Level_03 = false; +input int VWAP_Level_03_Period = 20; +input bool Enable_Level_04 = false; +input int VWAP_Level_04_Period = 30; +input bool Enable_Level_05 = false; +input int VWAP_Level_05_Period = 40; + +bool Show_Daily_Value = true; +bool Show_Weekly_Value = true; +bool Show_Monthly_Value = true; + +double VWAP_Buffer_Daily[]; +double VWAP_Buffer_Weekly[]; +double VWAP_Buffer_Monthly[]; +double VWAP_Buffer_01[]; +double VWAP_Buffer_02[]; +double VWAP_Buffer_03[]; +double VWAP_Buffer_04[]; +double VWAP_Buffer_05[]; + +double nPriceArr[]; +double nTotalTPV[]; +double nTotalVol[]; +double nSumDailyTPV = 0, nSumWeeklyTPV = 0, nSumMonthlyTPV = 0; +double nSumDailyVol = 0, nSumWeeklyVol = 0, nSumMonthlyVol = 0; + +int nIdxDaily=0,nIdxWeekly=0,nIdxMonthly=0,nIdx=0; + +bool bIsFirstRun=true; + +ENUM_TIMEFRAMES LastTimePeriod=PERIOD_MN1; + +string sDailyStr = ""; +string sWeeklyStr = ""; +string sMonthlyStr = ""; +string sLevel01Str = ""; +string sLevel02Str = ""; +string sLevel03Str = ""; +string sLevel04Str = ""; +string sLevel05Str = ""; +datetime dtLastDay=CreateDateTime(DAILY),dtLastWeek=CreateDateTime(WEEKLY),dtLastMonth=CreateDateTime(MONTHLY); +//+------------------------------------------------------------------+ +//| | +//+------------------------------------------------------------------+ +int OnInit() + { + IndicatorSetInteger(INDICATOR_DIGITS,_Digits); + + SetIndexBuffer(0,VWAP_Buffer_Daily,INDICATOR_DATA); + SetIndexBuffer(1,VWAP_Buffer_Weekly,INDICATOR_DATA); + SetIndexBuffer(2,VWAP_Buffer_Monthly,INDICATOR_DATA); + SetIndexBuffer(3,VWAP_Buffer_01,INDICATOR_DATA); + SetIndexBuffer(4,VWAP_Buffer_02,INDICATOR_DATA); + SetIndexBuffer(5,VWAP_Buffer_03,INDICATOR_DATA); + SetIndexBuffer(6,VWAP_Buffer_04,INDICATOR_DATA); + SetIndexBuffer(7,VWAP_Buffer_05,INDICATOR_DATA); + + ObjectCreate(0,"VWAP_Daily",OBJ_LABEL,0,0,0); + ObjectSetInteger(0,"VWAP_Daily",OBJPROP_CORNER,3); + ObjectSetInteger(0,"VWAP_Daily",OBJPROP_XDISTANCE,180); + ObjectSetInteger(0,"VWAP_Daily",OBJPROP_YDISTANCE,40); + ObjectSetInteger(0,"VWAP_Daily",OBJPROP_COLOR,indicator_color1); + ObjectSetInteger(0,"VWAP_Daily",OBJPROP_FONTSIZE,7); + ObjectSetString(0,"VWAP_Daily",OBJPROP_FONT,"Verdana"); + ObjectSetString(0,"VWAP_Daily",OBJPROP_TEXT," "); + + ObjectCreate(0,"VWAP_Weekly",OBJ_LABEL,0,0,0); + ObjectSetInteger(0,"VWAP_Weekly",OBJPROP_CORNER,3); + ObjectSetInteger(0,"VWAP_Weekly",OBJPROP_XDISTANCE,180); + ObjectSetInteger(0,"VWAP_Weekly",OBJPROP_YDISTANCE,60); + ObjectSetInteger(0,"VWAP_Weekly",OBJPROP_COLOR,indicator_color2); + ObjectSetInteger(0,"VWAP_Weekly",OBJPROP_FONTSIZE,7); + ObjectSetString(0,"VWAP_Weekly",OBJPROP_FONT,"Verdana"); + ObjectSetString(0,"VWAP_Weekly",OBJPROP_TEXT," "); + + ObjectCreate(0,"VWAP_Monthly",OBJ_LABEL,0,0,0); + ObjectSetInteger(0,"VWAP_Monthly",OBJPROP_CORNER,3); + ObjectSetInteger(0,"VWAP_Monthly",OBJPROP_XDISTANCE,180); + ObjectSetInteger(0,"VWAP_Monthly",OBJPROP_YDISTANCE,80); + ObjectSetInteger(0,"VWAP_Monthly",OBJPROP_COLOR,indicator_color3); + ObjectSetInteger(0,"VWAP_Monthly",OBJPROP_FONTSIZE,7); + ObjectSetString(0,"VWAP_Monthly",OBJPROP_FONT,"Verdana"); + ObjectSetString(0,"VWAP_Monthly",OBJPROP_TEXT," "); + + ObjectCreate(0,"VWAP_Level_01",OBJ_LABEL,0,0,0); + ObjectSetInteger(0,"VWAP_Level_01",OBJPROP_CORNER,3); + ObjectSetInteger(0,"VWAP_Level_01",OBJPROP_XDISTANCE,180); + ObjectSetInteger(0,"VWAP_Level_01",OBJPROP_YDISTANCE,100); + ObjectSetInteger(0,"VWAP_Level_01",OBJPROP_COLOR,indicator_color4); + ObjectSetInteger(0,"VWAP_Level_01",OBJPROP_FONTSIZE,7); + ObjectSetString(0,"VWAP_Level_01",OBJPROP_FONT,"Verdana"); + ObjectSetString(0,"VWAP_Level_01",OBJPROP_TEXT," "); + + ObjectCreate(0,"VWAP_Level_02",OBJ_LABEL,0,0,0); + ObjectSetInteger(0,"VWAP_Level_02",OBJPROP_CORNER,3); + ObjectSetInteger(0,"VWAP_Level_02",OBJPROP_XDISTANCE,180); + ObjectSetInteger(0,"VWAP_Level_02",OBJPROP_YDISTANCE,120); + ObjectSetInteger(0,"VWAP_Level_02",OBJPROP_COLOR,indicator_color5); + ObjectSetInteger(0,"VWAP_Level_02",OBJPROP_FONTSIZE,7); + ObjectSetString(0,"VWAP_Level_02",OBJPROP_FONT,"Verdana"); + ObjectSetString(0,"VWAP_Level_02",OBJPROP_TEXT," "); + + ObjectCreate(0,"VWAP_Level_03",OBJ_LABEL,0,0,0); + ObjectSetInteger(0,"VWAP_Level_03",OBJPROP_CORNER,3); + ObjectSetInteger(0,"VWAP_Level_03",OBJPROP_XDISTANCE,180); + ObjectSetInteger(0,"VWAP_Level_03",OBJPROP_YDISTANCE,140); + ObjectSetInteger(0,"VWAP_Level_03",OBJPROP_COLOR,indicator_color6); + ObjectSetInteger(0,"VWAP_Level_03",OBJPROP_FONTSIZE,7); + ObjectSetString(0,"VWAP_Level_03",OBJPROP_FONT,"Verdana"); + ObjectSetString(0,"VWAP_Level_03",OBJPROP_TEXT," "); + + ObjectCreate(0,"VWAP_Level_04",OBJ_LABEL,0,0,0); + ObjectSetInteger(0,"VWAP_Level_04",OBJPROP_CORNER,3); + ObjectSetInteger(0,"VWAP_Level_04",OBJPROP_XDISTANCE,180); + ObjectSetInteger(0,"VWAP_Level_04",OBJPROP_YDISTANCE,160); + ObjectSetInteger(0,"VWAP_Level_04",OBJPROP_COLOR,indicator_color7); + ObjectSetInteger(0,"VWAP_Level_04",OBJPROP_FONTSIZE,7); + ObjectSetString(0,"VWAP_Level_04",OBJPROP_FONT,"Verdana"); + ObjectSetString(0,"VWAP_Level_04",OBJPROP_TEXT," "); + + ObjectCreate(0,"VWAP_Level_05",OBJ_LABEL,0,0,0); + ObjectSetInteger(0,"VWAP_Level_05",OBJPROP_CORNER,3); + ObjectSetInteger(0,"VWAP_Level_05",OBJPROP_XDISTANCE,180); + ObjectSetInteger(0,"VWAP_Level_05",OBJPROP_YDISTANCE,180); + ObjectSetInteger(0,"VWAP_Level_05",OBJPROP_COLOR,indicator_color8); + ObjectSetInteger(0,"VWAP_Level_05",OBJPROP_FONTSIZE,7); + ObjectSetString(0,"VWAP_Level_05",OBJPROP_FONT,"Verdana"); + ObjectSetString(0,"VWAP_Level_05",OBJPROP_TEXT," "); + + return(INIT_SUCCEEDED); + } +//+------------------------------------------------------------------+ +//| | +//+------------------------------------------------------------------+ +void OnDeinit(const int pReason) + { + ObjectDelete(0,"VWAP_Daily"); + ObjectDelete(0,"VWAP_Weekly"); + ObjectDelete(0,"VWAP_Monthly"); + ObjectDelete(0,"VWAP_Level_01"); + ObjectDelete(0,"VWAP_Level_02"); + ObjectDelete(0,"VWAP_Level_03"); + ObjectDelete(0,"VWAP_Level_04"); + ObjectDelete(0,"VWAP_Level_05"); + } +//+------------------------------------------------------------------+ +//| | +//+------------------------------------------------------------------+ +int OnCalculate(const int rates_total, + const int prev_calculated, + const datetime &time[], + const double &open[], + const double &high[], + const double &low[], + const double &close[], + const long &tick_volume[], + const long &volume[], + const int &spread[]) + { + + if(PERIOD_CURRENT!=LastTimePeriod) + { + bIsFirstRun=true; + LastTimePeriod=PERIOD_CURRENT; + } + + if(rates_total>prev_calculated || bIsFirstRun) + { + ArrayResize(nPriceArr,rates_total); + ArrayResize(nTotalTPV,rates_total); + ArrayResize(nTotalVol,rates_total); + + if(Enable_Daily) {nIdx = nIdxDaily; nSumDailyTPV = 0; nSumDailyVol = 0;} + if(Enable_Weekly) {nIdx = nIdxWeekly; nSumWeeklyTPV = 0; nSumWeeklyVol = 0;} + if(Enable_Monthly) {nIdx = nIdxMonthly; nSumMonthlyTPV = 0; nSumMonthlyVol = 0;} + + for(; nIdx=nIdxDaily)) + { + nSumDailyTPV += nTotalTPV[nIdx]; + nSumDailyVol += nTotalVol[nIdx]; + + if(nSumDailyVol) + VWAP_Buffer_Daily[nIdx]=(nSumDailyTPV/nSumDailyVol); + + if((sDailyStr!="VWAP Daily: "+(string)NormalizeDouble(VWAP_Buffer_Daily[nIdx],_Digits)) && Show_Daily_Value) + { + sDailyStr="VWAP Daily: "+(string)NormalizeDouble(VWAP_Buffer_Daily[nIdx],_Digits); + ObjectSetString(0,"VWAP_Daily",OBJPROP_TEXT,sDailyStr); + } + } + + if(Enable_Weekly && (nIdx>=nIdxWeekly)) + { + nSumWeeklyTPV += nTotalTPV[nIdx]; + nSumWeeklyVol += nTotalVol[nIdx]; + + if(nSumWeeklyVol) + VWAP_Buffer_Weekly[nIdx]=(nSumWeeklyTPV/nSumWeeklyVol); + + if((sWeeklyStr!="VWAP Weekly: "+(string)NormalizeDouble(VWAP_Buffer_Weekly[nIdx],_Digits)) && Show_Weekly_Value) + { + sWeeklyStr="VWAP Weekly: "+(string)NormalizeDouble(VWAP_Buffer_Weekly[nIdx],_Digits); + ObjectSetString(0,"VWAP_Weekly",OBJPROP_TEXT,sWeeklyStr); + } + } + + if(Enable_Monthly && (nIdx>=nIdxMonthly)) + { + nSumMonthlyTPV += nTotalTPV[nIdx]; + nSumMonthlyVol += nTotalVol[nIdx]; + + if(nSumMonthlyVol) + VWAP_Buffer_Monthly[nIdx]=(nSumMonthlyTPV/nSumMonthlyVol); + + if((sMonthlyStr!="VWAP Monthly: "+(string)NormalizeDouble(VWAP_Buffer_Monthly[nIdx],_Digits)) && Show_Monthly_Value) + { + sMonthlyStr="VWAP Monthly: "+(string)NormalizeDouble(VWAP_Buffer_Monthly[nIdx],_Digits); + ObjectSetString(0,"VWAP_Monthly",OBJPROP_TEXT,sMonthlyStr); + } + } + + dtLastDay=CreateDateTime(DAILY,time[nIdx]); + dtLastWeek=CreateDateTime(WEEKLY,time[nIdx]); + dtLastMonth=CreateDateTime(MONTHLY,time[nIdx]); + } + + if(Enable_Level_01) + { + int nStartPos=(prev_calculated>VWAP_Level_01_Period) ?(prev_calculated-VWAP_Level_01_Period) : VWAP_Level_01_Period; + for(nIdx=nStartPos; nIdxVWAP_Level_02_Period) ?(prev_calculated-VWAP_Level_02_Period) : VWAP_Level_02_Period; + for(nIdx=nStartPos; nIdxVWAP_Level_03_Period) ?(prev_calculated-VWAP_Level_03_Period) : VWAP_Level_03_Period; + for(nIdx=nStartPos; nIdxVWAP_Level_04_Period) ?(prev_calculated-VWAP_Level_04_Period) : VWAP_Level_04_Period; + for(nIdx=nStartPos; nIdxVWAP_Level_05_Period) ?(prev_calculated-VWAP_Level_05_Period) : VWAP_Level_05_Period; + for(nIdx=nStartPos; nIdx= 10) + { + // + oBar.Clean(); + oBullishOrderBlock.Clean(); + fvgUpper = 0; + fvgLower = 0; + fvgTouchDate = NULL; + isFVGTouched = false; + + // + RemoveDraws(); + } + + // + return result; + } + + // + type = POSITION_TYPE_BUY; + sl = cBar.low - atr; + } + + // + // Short ... + if (hasBearishOrderBlock) + { + // + DrawBearishOrderBlock( + oBearishOrderBlock, + ChartID() // + ); + + // + // Detect an FVG Inside Order Block in Trading Period ... + result = FindBearishFVGInsideOrderBlock(); + if (!result) + { + // + oBar.Clean(); + oBearishOrderBlock.Clean(); + fvgUpper = 0; + fvgLower = 0; + fvgTouchDate = NULL; + isFVGTouched = false; + + // + RemoveDraws(); + + // + return result; + } + + // + // Now We Have to Waits Until Price reached the FVG ... + if (!isFVGTouched) + { + // + isFVGTouched = hh3 > fvgLower; + fvgTouchDate = TimeCurrent(); + } + + // + result = isFVGTouched; + if (!result) + { + return result; + } + + // + // Now we Have to Wait for Pressure ... + + // + // Short ... + bool isShootingStar = !useShootingStar + ? false + : cBar.IsShootingStar(); + bool isEveningStar = !useEveningStar + ? false + : cBar.IsEveningStar(); + bool isBearishEngulfing = !useBearishEngulfing + ? false + : cBar.IsBearishEngulfing(); + bool isBearishMaurubozu = !useBearishMaurubozu + ? false + : cBar.IsBearishMarubozu(); + + // + bool isBearishTrend = cBar.HasBearishTrend( + tmpHSW, + tmpLSW, + true // + ); + + // + hasShort = + // + true + // + && + // + ( + // + isShootingStar + // + || + // + isEveningStar + // + || + // + isBearishEngulfing + // + || + // + isBearishMaurubozu + // + ) + // + ; + result = hasShort; + if (!result) + { + // + // Check FVG Touch Date ... + int cTimeSec = (int)TimeCurrent(); + int tTimeSec = (int)fvgTouchDate; + int periodSec = PeriodSeconds(mPeriod); + int touchAge = (cTimeSec - tTimeSec) / periodSec; + if (touchAge >= 10) + { + // + oBar.Clean(); + oBearishOrderBlock.Clean(); + fvgUpper = 0; + fvgLower = 0; + fvgTouchDate = NULL; + isFVGTouched = false; + + // + RemoveDraws(); + } + + // + return result; + } + + // + type = POSITION_TYPE_SELL; + sl = cBar.high + atr; + } + + // + result = hasLong || hasShort; + if (!result) + { + 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 + + // + XOSCInputs oscInputs; + XSCXOSCHelper *oscHelper; + + // + // Private ... +private: + // + // Props ... + + // + XOHCL oBar; + XOHCL oBullishOrderBlock; + XOHCL oBearishOrderBlock; + + // + double fvgUpper; + double fvgLower; + bool isFVGTouched; + datetime fvgTouchDate; + + // + void PrepareRequirements() + { + // + oscInputs.Default(); + oscHelper = new XSCXOSCHelper(); + oscHelper.Init( + mSymbol, + mPeriod, + oscInputs // + ); + } + + // + bool FindBullishFVGInsideOrderBlock() + { + // + bool result = false; + + // + result = oBullishOrderBlock.IsValid(); + if (!result) + { + return result; + } + + // + int totalBars = iBars( + mSymbol, + mPeriod // + ); + int maxAllowed = totalBars / 10; + + // + for (int i = 0; i < maxAllowed; i++) + { + // + XOHCL iBar; + result = iBar.Init( + mSymbol, + mPeriod, + i // + ); + if (!result) + { + break; + } + + // + result = iBar.HasBullishFVG( + fvgUpper, + fvgLower // + ); + if (!result) + { + continue; + } + + // + // Check FVG has Correlation by OB ... + result = + // + // Full Inside ... + (fvgUpper <= oBullishOrderBlock.high && + fvgLower >= oBullishOrderBlock.low) + // + || + // + (fvgUpper > oBullishOrderBlock.high && + fvgLower >= oBullishOrderBlock.low && + fvgLower <= oBullishOrderBlock.high) + // + || + // + (fvgUpper > oBullishOrderBlock.low && + fvgUpper <= oBullishOrderBlock.high && + fvgLower < oBullishOrderBlock.low) + // + ; + if (result) + { + // + DrawBullishFVG( + iBar, + fvgUpper, + fvgLower, + ChartID() // + ); + + // + break; + } + } + + // + return result; + } + + // + bool FindBearishFVGInsideOrderBlock() + { + // + bool result = false; + + // + result = oBearishOrderBlock.IsValid(); + if (!result) + { + return result; + } + + // + int totalBars = iBars( + mSymbol, + mPeriod // + ); + int maxAllowed = totalBars / 10; + + // + for (int i = 0; i < maxAllowed; i++) + { + // + XOHCL iBar; + result = iBar.Init( + mSymbol, + mPeriod, + i // + ); + if (!result) + { + break; + } + + // + result = iBar.HasBearishFVG( + fvgUpper, + fvgLower // + ); + if (!result) + { + continue; + } + + // + // Check FVG has Correlation by OB ... + result = + // + // Full Inside ... + (fvgUpper <= oBearishOrderBlock.high && + fvgLower >= oBearishOrderBlock.low) + // + || + // + (fvgUpper > oBearishOrderBlock.high && + fvgLower >= oBearishOrderBlock.low && + fvgLower <= oBearishOrderBlock.high) + // + || + // + (fvgUpper > oBearishOrderBlock.low && + fvgUpper <= oBearishOrderBlock.high && + fvgLower < oBearishOrderBlock.low) + // + ; + if (result) + { + // + DrawBearishFVG( + iBar, + fvgUpper, + fvgLower, + ChartID() // + ); + + // + break; + } + } + + // + return result; + } +}; + +// \ No newline at end of file diff --git a/Strategies/x-saherelm.xtdpvlmtrtm.strategy.mq5 b/Strategies/x-saherelm.xtdpvlmtrtm.strategy.mq5 new file mode 100644 index 00000000..4560eefc --- /dev/null +++ b/Strategies/x-saherelm.xtdpvlmtrtm.strategy.mq5 @@ -0,0 +1,446 @@ +/////////////////////////////////////////////////////// +// +// SaherElm IT Center MQL5 Class Library +// -------------------------------------- +// Name: XSCXEMRNGStrategy +// 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 + +// +// TODO: Complete this ... + +// +/// Imports ... +#include "../Helpers/x-saherelm.xtm.helper.mq5" +#include "../Helpers/x-saherelm.xstr.helper.mq5" +#include "../Classes/x-saherelm.xstrategy.class.mq5" + +// +string XEMRNGStartegyToken = "XEMRNG"; + +// +class XSCXEMRNGStrategy : public XSCBaseStrategy +{ + // + // Public ... +public: + // + // Constructor(s) ... + void XSCXEMRNGStrategy( + // + // Base ... + string _symbol, // Trading Symbol + ENUM_TIMEFRAMES _period, // Trading 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 // + ) : XSCBaseStrategy(_symbol, + _period, + _volume, + _r2r, + _slippage, + _magicNumber, + _useTPSLAsPoint, + _forceMaxTPSLAsPoint, + _tpPoint, + _slPoint, + _allowLong, + _allowShort, + _maxAllowedLongs, + _maxAllowedShorts // + ) + { + // + ConfigureRequirements(); + + // + SetAlertPrefix(GetTag()); + } + + // + // Deconstructor ... + void ~XSCXEMRNGStrategy() + { + delete mTMHelper; + } + + // + // Getter / Setter (s) ... + + // + // Overrides ... + + // + // Customize Strategy Identifier ... + string GetTag() override + { + return XEMRNGStartegyToken; + } + + // + // Check Conditions For Signal ... + bool HasSignal( + XSignal &signal // + ) override + { + // + bool result = false; + + // + 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; + + // + int start = 0; + int count = 5; + + // + double tms[]; + ArraySetAsSeries(tms, true); + mTMHelper.CopyMain( + start, + count, + tms // + ); + + // + XSTRConditions strConditions; + result = mSTRHelper.GetConditions( + strConditions, + start, + count // + ); + if (!result) + { + return result; + } + + // + XOHCL cBar; + result = cBar.Init( + mSymbol, + mPeriod, + 1 // + ); + if (!result) + { + return result; + } + + // + double hh5 = cBar.FindHighest( + 5, + MODE_HIGH // + ); + double ll5 = cBar.FindLowest( + 5, + MODE_LOW // + ); + + // + XOHCL pBar; + result = cBar.GetPreviousBar(pBar); + if (!result) + { + return result; + } + + // + bool hasLong = false; + bool hasShort = false; + + // + XOHCL tmpHSW[]; + XOHCL tmpLSW[]; + + // + bool useHammer = true; + bool useMorningStar = false; + bool useBullishEngulfing = true; + bool useBullishMaurubozu = true; + + // + bool useShootingStar = true; + bool useEveningStar = false; + bool useBearishEngulfing = true; + bool useBearishMaurubozu = true; + + // + // Long ... + + // + bool isHammer = !useHammer + ? false + : cBar.IsHammer(); + bool isMorningStar = !useMorningStar + ? false + : cBar.IsMorningStar(); + bool isBullishMarubozu = !useBullishEngulfing + ? false + : cBar.IsBullishMarubozu(); + bool isBullishEngulfing = !useBullishMaurubozu + ? false + : cBar.IsBullishEngulfing(); + + // + bool isBullishTrend = cBar.HasBullishTrend( + tmpHSW, + tmpLSW, + true // + ); + + // + // Find Swing Low Above of EMA ... + // Find Swing High Above of EMA ... + // Fins Swing Low Below of EMA ... + // Detect Bullish Pattern ... + hasLong = + // + cBar.IsBullish() + // + && + // + isBullishTrend + // + && + // + cBar.close > tms[1] + // + && + // + ll5 < tms[1] + // + && + // + strConditions.isTrendSwitchedToBullish + // + && + // + ( + // + isHammer + // + || + // + isMorningStar + // + || + // + isBullishMarubozu + // + || + // + isBullishEngulfing + // + ) + // + ; + if (hasLong) + { + // + type = POSITION_TYPE_BUY; + + // + sl = tmpLSW[ArraySize(tmpLSW) - 1].low; + } + + // + // Short ... + bool isShootingStar = !useShootingStar + ? false + : cBar.IsShootingStar(); + bool isEveningStar = !useEveningStar + ? false + : cBar.IsEveningStar(); + bool isBearishEngulfing = !useBearishEngulfing + ? false + : cBar.IsBearishEngulfing(); + bool isBearishMaurubozu = !useBearishMaurubozu + ? false + : cBar.IsBearishMarubozu(); + + // + bool isBearishTrend = cBar.HasBearishTrend( + tmpHSW, + tmpLSW, + true // + ); + + // + hasShort = + // + cBar.IsBearish() + // + && + // + isBearishTrend + // + && + // + cBar.close < tms[1] + // + && + // + hh5 > tms[1] + // + && + // + strConditions.isTrendSwitchedToBearish + // + && + // + ( + // + isShootingStar + // + || + // + isEveningStar + // + || + // + isBearishEngulfing + // + || + // + isBearishMaurubozu + // + ) + // + ; + if (hasShort) + { + // + type = POSITION_TYPE_SELL; + + // + sl = tmpHSW[ArraySize(tmpHSW) - 1].high; + } + + // + result = hasLong || hasShort; + if (!result) + { + 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 ... + + // + XTMInputs mTMInputs; // Tren Magic Inputs ... + XSCXTMHelper *mTMHelper; // Trend Magic Helper Class ... + + // + XSTRInputs mSTRInputs; // Super Trend Inputs ... + XSCXSTRHelper *mSTRHelper; // Super Trend Helper Class ... + + // + // Private ... +private: + // + + // + // Set Default TM Inputs ... + void ConfigureRequirements() + { + // + mTMInputs.maShift = 0; + mTMInputs.maPeriod = 100; + mTMInputs.maMethod = MODE_EMA; + mTMInputs.maAppliedTo = PRICE_CLOSE; + + // + mTMInputs.showLine = true; + + // + mTMHelper = new XSCXTMHelper(); + + // + mTMHelper.Init( + mSymbol, + mPeriod, + mTMInputs // + ); + + // + mSTRInputs.length = 14; + mSTRInputs.multiplier = 3.5; + mSTRInputs.appliedTo = PRICE_CLOSE; + + // + mSTRInputs.showTrends = true; + mSTRInputs.fillTrends = false; + + // + mSTRHelper = new XSCXSTRHelper(); + mSTRHelper.Init( + mSymbol, + mPeriod, + mSTRInputs // + ); + } +}; + +// \ No newline at end of file