/////////////////////////////////////////////////////// // // SaherElm IT Center MQL4 XMA Signal Library // --------------------------------------------------- // saherelm useful tools and definitions ... // // // Maintainer: // ------------ // Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com) // ////////////////////////////////////////////////////// #property library #property copyright "Copyright 2023, SaherElm IT Center" #property link "https://www.saherelm.ir" #property version "1.00" #property strict // // Includes Logger library ... #include "../Libraries/x-saherelm.lib.mq4" // // Includes Models library ... #include "../Libraries/x-saherelm.xma.models.lib.mq4" // // Includes Drawing library ... #include "../Libraries/x-saherelm.draw.lib.mq4" // // START Global Requirement Functions ... // static int totalSignals = 0; static int totalLongSignals = 0; static int totalShortSignals = 0; // static double initialBalance = 0; // static double scTrendPower[]; static double scMinTrendPower = -1; static double scMaxTrendPower = -1; static datetime shortMarketCrossPointsTime[]; // static double mcTrendPower[]; static double mcMaxTrendPower = -1; static double mcMinTrendPower = -1; static datetime mediumMarketCrossPointsTime[]; // static double lcTrendPower[]; static double lcMaxTrendPower = -1; static double lcMinTrendPower = -1; static datetime longMarketCrossPointsTime[]; // static double minMidInSC = -1; static double maxMidInSC = -1; // // // Retrieve and Calculate XMA State ... XMAState GetXMAState( const int bar_index, // Bar Index ... // // Market Length ... const int marketLen, // Market Length ... // // XMA Inputs ... // // Market Specification Inpouts ... const int scFastMul, // Short Cycle Fast Multiplier ... const int scSlowMul, // Short Cycle Slow Multiplier ... const int mcFastMul, // Medium Cycle Fast Multiplier ... const int mcSlowMul, // Medium Cycle Slow Multiplier ... const int lcFastMul, // Long Cycle Fast Multiplier ... const int lcSlowMul, // Long Cycle Slow Multiplier ... // // Show/Hide Inputs ... const bool showSC, // Show Short Cycle const bool showSCCrossLines, // Show Short Cycle Vertical Lines const bool showMC, // Show Medium Cycle const bool showMCCrossLines, // Show Medium Cycle Vertical Lines const bool showLC, // Show Long Cycle const bool showLCCrossLines, // Show Long Cycle Vertical Lines // // Color Inputs ... const color sxFastColor, // Short Cycle Fast Color ... const color scSlowColor, // Short Cycle Slow Color ... const color mcFastColor, // Medium Cycle Fast Color ... const color mcSlowColor, // Medium Cycle Slow Color ... const color lcFastColor, // Medium Cycle Fast Color ... const color lcSlowColor // Medium Cycle Slow Color ... ) { // // Temp Result ... XMAState result = {}; // // START Reading Buffers ... // // // Define BufferIndexes ... int scFastBufIndex = 0; int scSlowBufIndex = 1; int mcFastBufIndex = 2; int mcSlowBufIndex = 3; int lcFastBufIndex = 4; int lcSlowBufIndex = 5; int midBufIndex = 6; // // START Short Cycle ... // // // Short Cycle Fast ... double scFast = iCustom( _Symbol, _Period, xmaIndicatorName, // // XMA Inputs ... // // Market Specification Inpouts ... marketLen, // Market Length ... scFastMul, // Short Cycle Fast Multiplier ... scSlowMul, // Short Cycle Slow Multiplier ... mcFastMul, // Medium Cycle Fast Multiplier ... mcSlowMul, // Medium Cycle Slow Multiplier ... lcFastMul, // Long Cycle Fast Multiplier ... lcSlowMul, // Long Cycle Slow Multiplier ... // // Show/Hide Inputs ... showSC, // Show Short Cycle showSCCrossLines, // Show Short Cycle Vertical Lines showMC, // Show Medium Cycle showMCCrossLines, // Show Medium Cycle Vertical Lines showLC, // Show Long Cycle showLCCrossLines, // Show Long Cycle Vertical Lines // // Color Inputs ... sxFastColor, // Short Cycle Fast Color ... scSlowColor, // Short Cycle Slow Color ... mcFastColor, // Medium Cycle Fast Color ... mcSlowColor, // Medium Cycle Slow Color ... lcFastColor, // Medium Cycle Fast Color ... lcSlowColor, // Medium Cycle Slow Color ... // scFastBufIndex, // Buffer Index ... bar_index ); result.scFast = scFast; // // Short Cycle Previous Fast ... double scFastPrev = iCustom( _Symbol, _Period, xmaIndicatorName, // // XMA Inputs ... // // Market Specification Inpouts ... marketLen, // Market Length ... scFastMul, // Short Cycle Fast Multiplier ... scSlowMul, // Short Cycle Slow Multiplier ... mcFastMul, // Medium Cycle Fast Multiplier ... mcSlowMul, // Medium Cycle Slow Multiplier ... lcFastMul, // Long Cycle Fast Multiplier ... lcSlowMul, // Long Cycle Slow Multiplier ... // // Show/Hide Inputs ... showSC, // Show Short Cycle showSCCrossLines, // Show Short Cycle Vertical Lines showMC, // Show Medium Cycle showMCCrossLines, // Show Medium Cycle Vertical Lines showLC, // Show Long Cycle showLCCrossLines, // Show Long Cycle Vertical Lines // // Color Inputs ... sxFastColor, // Short Cycle Fast Color ... scSlowColor, // Short Cycle Slow Color ... mcFastColor, // Medium Cycle Fast Color ... mcSlowColor, // Medium Cycle Slow Color ... lcFastColor, // Medium Cycle Fast Color ... lcSlowColor, // Medium Cycle Slow Color ... // scFastBufIndex, // Buffer Index ... bar_index + 1 ); result.scFastPrev = scFastPrev; // // Short Cycle Slow ... double scSlow = iCustom( _Symbol, _Period, xmaIndicatorName, // // XMA Inputs ... // // Market Specification Inpouts ... marketLen, // Market Length ... scFastMul, // Short Cycle Fast Multiplier ... scSlowMul, // Short Cycle Slow Multiplier ... mcFastMul, // Medium Cycle Fast Multiplier ... mcSlowMul, // Medium Cycle Slow Multiplier ... lcFastMul, // Long Cycle Fast Multiplier ... lcSlowMul, // Long Cycle Slow Multiplier ... // // Show/Hide Inputs ... showSC, // Show Short Cycle showSCCrossLines, // Show Short Cycle Vertical Lines showMC, // Show Medium Cycle showMCCrossLines, // Show Medium Cycle Vertical Lines showLC, // Show Long Cycle showLCCrossLines, // Show Long Cycle Vertical Lines // // Color Inputs ... sxFastColor, // Short Cycle Fast Color ... scSlowColor, // Short Cycle Slow Color ... mcFastColor, // Medium Cycle Fast Color ... mcSlowColor, // Medium Cycle Slow Color ... lcFastColor, // Medium Cycle Fast Color ... lcSlowColor, // Medium Cycle Slow Color ... // scSlowBufIndex, // Buffer Index ... bar_index ); result.scSlow = scSlow; // // Short Cycle Previous Slow ... double scSlowPrev = iCustom( _Symbol, _Period, xmaIndicatorName, // // XMA Inputs ... // // Market Specification Inpouts ... marketLen, // Market Length ... scFastMul, // Short Cycle Fast Multiplier ... scSlowMul, // Short Cycle Slow Multiplier ... mcFastMul, // Medium Cycle Fast Multiplier ... mcSlowMul, // Medium Cycle Slow Multiplier ... lcFastMul, // Long Cycle Fast Multiplier ... lcSlowMul, // Long Cycle Slow Multiplier ... // // Show/Hide Inputs ... showSC, // Show Short Cycle showSCCrossLines, // Show Short Cycle Vertical Lines showMC, // Show Medium Cycle showMCCrossLines, // Show Medium Cycle Vertical Lines showLC, // Show Long Cycle showLCCrossLines, // Show Long Cycle Vertical Lines // // Color Inputs ... sxFastColor, // Short Cycle Fast Color ... scSlowColor, // Short Cycle Slow Color ... mcFastColor, // Medium Cycle Fast Color ... mcSlowColor, // Medium Cycle Slow Color ... lcFastColor, // Medium Cycle Fast Color ... lcSlowColor, // Medium Cycle Slow Color ... // scSlowBufIndex, // Buffer Index ... bar_index + 1 ); result.scSlowPrev = scSlowPrev; // // END Short Cycle ... // // // START Medium Cycle ... // // // Medium Cycle Fast ... double mcFast = iCustom( _Symbol, _Period, xmaIndicatorName, // // XMA Inputs ... // // Market Specification Inpouts ... marketLen, // Market Length ... scFastMul, // Short Cycle Fast Multiplier ... scSlowMul, // Short Cycle Slow Multiplier ... mcFastMul, // Medium Cycle Fast Multiplier ... mcSlowMul, // Medium Cycle Slow Multiplier ... lcFastMul, // Long Cycle Fast Multiplier ... lcSlowMul, // Long Cycle Slow Multiplier ... // // Show/Hide Inputs ... showSC, // Show Short Cycle showSCCrossLines, // Show Short Cycle Vertical Lines showMC, // Show Medium Cycle showMCCrossLines, // Show Medium Cycle Vertical Lines showLC, // Show Long Cycle showLCCrossLines, // Show Long Cycle Vertical Lines // // Color Inputs ... sxFastColor, // Short Cycle Fast Color ... scSlowColor, // Short Cycle Slow Color ... mcFastColor, // Medium Cycle Fast Color ... mcSlowColor, // Medium Cycle Slow Color ... lcFastColor, // Medium Cycle Fast Color ... lcSlowColor, // Medium Cycle Slow Color ... // mcFastBufIndex, // Buffer Index ... bar_index ); result.mcFast = mcFast; // // Medium Cycle Previous Fast ... double mcFastPrev = iCustom( _Symbol, _Period, xmaIndicatorName, // // XMA Inputs ... // // Market Specification Inpouts ... marketLen, // Market Length ... scFastMul, // Short Cycle Fast Multiplier ... scSlowMul, // Short Cycle Slow Multiplier ... mcFastMul, // Medium Cycle Fast Multiplier ... mcSlowMul, // Medium Cycle Slow Multiplier ... lcFastMul, // Long Cycle Fast Multiplier ... lcSlowMul, // Long Cycle Slow Multiplier ... // // Show/Hide Inputs ... showSC, // Show Short Cycle showSCCrossLines, // Show Short Cycle Vertical Lines showMC, // Show Medium Cycle showMCCrossLines, // Show Medium Cycle Vertical Lines showLC, // Show Long Cycle showLCCrossLines, // Show Long Cycle Vertical Lines // // Color Inputs ... sxFastColor, // Short Cycle Fast Color ... scSlowColor, // Short Cycle Slow Color ... mcFastColor, // Medium Cycle Fast Color ... mcSlowColor, // Medium Cycle Slow Color ... lcFastColor, // Medium Cycle Fast Color ... lcSlowColor, // Medium Cycle Slow Color ... // mcFastBufIndex, // Buffer Index ... bar_index + 1 ); result.mcFastPrev = mcFastPrev; // // Medium Cycle Slow ... double mcSlow = iCustom( _Symbol, _Period, xmaIndicatorName, // // XMA Inputs ... // // Market Specification Inpouts ... marketLen, // Market Length ... scFastMul, // Short Cycle Fast Multiplier ... scSlowMul, // Short Cycle Slow Multiplier ... mcFastMul, // Medium Cycle Fast Multiplier ... mcSlowMul, // Medium Cycle Slow Multiplier ... lcFastMul, // Long Cycle Fast Multiplier ... lcSlowMul, // Long Cycle Slow Multiplier ... // // Show/Hide Inputs ... showSC, // Show Short Cycle showSCCrossLines, // Show Short Cycle Vertical Lines showMC, // Show Medium Cycle showMCCrossLines, // Show Medium Cycle Vertical Lines showLC, // Show Long Cycle showLCCrossLines, // Show Long Cycle Vertical Lines // // Color Inputs ... sxFastColor, // Short Cycle Fast Color ... scSlowColor, // Short Cycle Slow Color ... mcFastColor, // Medium Cycle Fast Color ... mcSlowColor, // Medium Cycle Slow Color ... lcFastColor, // Medium Cycle Fast Color ... lcSlowColor, // Medium Cycle Slow Color ... // mcSlowBufIndex, // Buffer Index ... bar_index ); result.mcSlow = mcSlow; // // Medium Cycle Previous Slow ... double mcSlowPrev = iCustom( _Symbol, _Period, xmaIndicatorName, // // XMA Inputs ... // // Market Specification Inpouts ... marketLen, // Market Length ... scFastMul, // Short Cycle Fast Multiplier ... scSlowMul, // Short Cycle Slow Multiplier ... mcFastMul, // Medium Cycle Fast Multiplier ... mcSlowMul, // Medium Cycle Slow Multiplier ... lcFastMul, // Long Cycle Fast Multiplier ... lcSlowMul, // Long Cycle Slow Multiplier ... // // Show/Hide Inputs ... showSC, // Show Short Cycle showSCCrossLines, // Show Short Cycle Vertical Lines showMC, // Show Medium Cycle showMCCrossLines, // Show Medium Cycle Vertical Lines showLC, // Show Long Cycle showLCCrossLines, // Show Long Cycle Vertical Lines // // Color Inputs ... sxFastColor, // Short Cycle Fast Color ... scSlowColor, // Short Cycle Slow Color ... mcFastColor, // Medium Cycle Fast Color ... mcSlowColor, // Medium Cycle Slow Color ... lcFastColor, // Medium Cycle Fast Color ... lcSlowColor, // Medium Cycle Slow Color ... // mcSlowBufIndex, // Buffer Index ... bar_index + 1 ); result.mcSlowPrev = mcSlowPrev; // // END Medium Cycle ... // // // START Long Cycle ... // // // Long Cycle Fast ... double lcFast = iCustom( _Symbol, _Period, xmaIndicatorName, // // XMA Inputs ... // // Market Specification Inpouts ... marketLen, // Market Length ... scFastMul, // Short Cycle Fast Multiplier ... scSlowMul, // Short Cycle Slow Multiplier ... mcFastMul, // Medium Cycle Fast Multiplier ... mcSlowMul, // Medium Cycle Slow Multiplier ... lcFastMul, // Long Cycle Fast Multiplier ... lcSlowMul, // Long Cycle Slow Multiplier ... // // Show/Hide Inputs ... showSC, // Show Short Cycle showSCCrossLines, // Show Short Cycle Vertical Lines showMC, // Show Medium Cycle showMCCrossLines, // Show Medium Cycle Vertical Lines showLC, // Show Long Cycle showLCCrossLines, // Show Long Cycle Vertical Lines // // Color Inputs ... sxFastColor, // Short Cycle Fast Color ... scSlowColor, // Short Cycle Slow Color ... mcFastColor, // Medium Cycle Fast Color ... mcSlowColor, // Medium Cycle Slow Color ... lcFastColor, // Medium Cycle Fast Color ... lcSlowColor, // Medium Cycle Slow Color ... // lcFastBufIndex, // Buffer Index ... bar_index ); result.lcFast = lcFast; // // Long Cycle Previous Fast ... double lcFastPrev = iCustom( _Symbol, _Period, xmaIndicatorName, // // XMA Inputs ... // // Market Specification Inpouts ... marketLen, // Market Length ... scFastMul, // Short Cycle Fast Multiplier ... scSlowMul, // Short Cycle Slow Multiplier ... mcFastMul, // Medium Cycle Fast Multiplier ... mcSlowMul, // Medium Cycle Slow Multiplier ... lcFastMul, // Long Cycle Fast Multiplier ... lcSlowMul, // Long Cycle Slow Multiplier ... // // Show/Hide Inputs ... showSC, // Show Short Cycle showSCCrossLines, // Show Short Cycle Vertical Lines showMC, // Show Medium Cycle showMCCrossLines, // Show Medium Cycle Vertical Lines showLC, // Show Long Cycle showLCCrossLines, // Show Long Cycle Vertical Lines // // Color Inputs ... sxFastColor, // Short Cycle Fast Color ... scSlowColor, // Short Cycle Slow Color ... mcFastColor, // Medium Cycle Fast Color ... mcSlowColor, // Medium Cycle Slow Color ... lcFastColor, // Medium Cycle Fast Color ... lcSlowColor, // Medium Cycle Slow Color ... // lcFastBufIndex, // Buffer Index ... bar_index + 1 ); result.lcFastPrev = lcFastPrev; // // Long Cycle Slow ... double lcSlow = iCustom( _Symbol, _Period, xmaIndicatorName, // // XMA Inputs ... // // Market Specification Inpouts ... marketLen, // Market Length ... scFastMul, // Short Cycle Fast Multiplier ... scSlowMul, // Short Cycle Slow Multiplier ... mcFastMul, // Medium Cycle Fast Multiplier ... mcSlowMul, // Medium Cycle Slow Multiplier ... lcFastMul, // Long Cycle Fast Multiplier ... lcSlowMul, // Long Cycle Slow Multiplier ... // // Show/Hide Inputs ... showSC, // Show Short Cycle showSCCrossLines, // Show Short Cycle Vertical Lines showMC, // Show Medium Cycle showMCCrossLines, // Show Medium Cycle Vertical Lines showLC, // Show Long Cycle showLCCrossLines, // Show Long Cycle Vertical Lines // // Color Inputs ... sxFastColor, // Short Cycle Fast Color ... scSlowColor, // Short Cycle Slow Color ... mcFastColor, // Medium Cycle Fast Color ... mcSlowColor, // Medium Cycle Slow Color ... lcFastColor, // Medium Cycle Fast Color ... lcSlowColor, // Medium Cycle Slow Color ... // lcSlowBufIndex, // Buffer Index ... bar_index ); result.lcSlow = lcSlow; // // Long Cycle Previous Slow ... double lcSlowPrev = iCustom( _Symbol, _Period, xmaIndicatorName, // // XMA Inputs ... // // Market Specification Inpouts ... marketLen, // Market Length ... scFastMul, // Short Cycle Fast Multiplier ... scSlowMul, // Short Cycle Slow Multiplier ... mcFastMul, // Medium Cycle Fast Multiplier ... mcSlowMul, // Medium Cycle Slow Multiplier ... lcFastMul, // Long Cycle Fast Multiplier ... lcSlowMul, // Long Cycle Slow Multiplier ... // // Show/Hide Inputs ... showSC, // Show Short Cycle showSCCrossLines, // Show Short Cycle Vertical Lines showMC, // Show Medium Cycle showMCCrossLines, // Show Medium Cycle Vertical Lines showLC, // Show Long Cycle showLCCrossLines, // Show Long Cycle Vertical Lines // // Color Inputs ... sxFastColor, // Short Cycle Fast Color ... scSlowColor, // Short Cycle Slow Color ... mcFastColor, // Medium Cycle Fast Color ... mcSlowColor, // Medium Cycle Slow Color ... lcFastColor, // Medium Cycle Fast Color ... lcSlowColor, // Medium Cycle Slow Color ... // lcSlowBufIndex, // Buffer Index ... bar_index + 1 ); result.lcSlowPrev = lcSlowPrev; // // END Long Cycle ... // // // START Makret Middleage ... // // // Market Middleage ... double marketMiddleage = iCustom( _Symbol, _Period, xmaIndicatorName, // // XMA Inputs ... // // Market Specification Inpouts ... marketLen, // Market Length ... scFastMul, // Short Cycle Fast Multiplier ... scSlowMul, // Short Cycle Slow Multiplier ... mcFastMul, // Medium Cycle Fast Multiplier ... mcSlowMul, // Medium Cycle Slow Multiplier ... lcFastMul, // Long Cycle Fast Multiplier ... lcSlowMul, // Long Cycle Slow Multiplier ... // // Show/Hide Inputs ... showSC, // Show Short Cycle showSCCrossLines, // Show Short Cycle Vertical Lines showMC, // Show Medium Cycle showMCCrossLines, // Show Medium Cycle Vertical Lines showLC, // Show Long Cycle showLCCrossLines, // Show Long Cycle Vertical Lines // // Color Inputs ... sxFastColor, // Short Cycle Fast Color ... scSlowColor, // Short Cycle Slow Color ... mcFastColor, // Medium Cycle Fast Color ... mcSlowColor, // Medium Cycle Slow Color ... lcFastColor, // Medium Cycle Fast Color ... lcSlowColor, // Medium Cycle Slow Color ... // midBufIndex, // Buffer Index ... bar_index ); result.marketMiddleage = marketMiddleage; // // Previous Market Middleage ... double marketMiddleagePrev = iCustom( _Symbol, _Period, xmaIndicatorName, // // XMA Inputs ... // // Market Specification Inpouts ... marketLen, // Market Length ... scFastMul, // Short Cycle Fast Multiplier ... scSlowMul, // Short Cycle Slow Multiplier ... mcFastMul, // Medium Cycle Fast Multiplier ... mcSlowMul, // Medium Cycle Slow Multiplier ... lcFastMul, // Long Cycle Fast Multiplier ... lcSlowMul, // Long Cycle Slow Multiplier ... // // Show/Hide Inputs ... showSC, // Show Short Cycle showSCCrossLines, // Show Short Cycle Vertical Lines showMC, // Show Medium Cycle showMCCrossLines, // Show Medium Cycle Vertical Lines showLC, // Show Long Cycle showLCCrossLines, // Show Long Cycle Vertical Lines // // Color Inputs ... sxFastColor, // Short Cycle Fast Color ... scSlowColor, // Short Cycle Slow Color ... mcFastColor, // Medium Cycle Fast Color ... mcSlowColor, // Medium Cycle Slow Color ... lcFastColor, // Medium Cycle Fast Color ... lcSlowColor, // Medium Cycle Slow Color ... // midBufIndex, // Buffer Index ... bar_index + 1 ); result.marketMiddleagePrev = marketMiddleagePrev; // // END Makret Middleage ... // // // END Reading Buffers ... // // // START Calculating ... // // datetime currentTime = iTime( _Symbol, _Period, bar_index ); // // Short Cycle ... bool isSCFastCrossOverSlow = scFast > scSlow && !(scFastPrev > scSlowPrev); result.isSCFastCrossOverSlow = isSCFastCrossOverSlow; // bool isSCFastCrossUnderSlow = scFast < scSlow && !(scFastPrev < scSlowPrev); result.isSCFastCrossUnderSlow = isSCFastCrossUnderSlow; // bool isSCTrendUp = scFast > scSlow && scFastPrev > scSlowPrev; result.isSCTrendUp = isSCTrendUp; // bool isSCTrendDown = !isSCTrendUp && scFast < scSlow && scFastPrev < scSlowPrev; result.isSCTrendDown = isSCTrendDown; // // Fill Cycle Min/Max Trend Powers ... double scTrendPowerCurrent = MathAbs(scFast - scSlow); ArrayResize( scTrendPower, ArraySize(scTrendPower) + 1 ); scTrendPower[0] = scTrendPowerCurrent; if (scTrendPowerCurrent > scMaxTrendPower) { scMaxTrendPower = scTrendPowerCurrent; } if ( scMinTrendPower < 0 || scTrendPowerCurrent < scMinTrendPower) { scMinTrendPower = scTrendPowerCurrent; } // // Fill Time Series ... if ( isSCFastCrossOverSlow || isSCFastCrossUnderSlow ) { // // Increase Array Size ... ArrayResize( shortMarketCrossPointsTime, ArraySize(shortMarketCrossPointsTime) + 1 ); // shortMarketCrossPointsTime[0] = currentTime; // scMinTrendPower = -1; scMaxTrendPower = -1; } // // Medium Cycle ... bool isMCFastCrossOverSlow = mcFast > mcSlow && !(mcFastPrev > mcSlowPrev); result.isMCFastCrossOverSlow = isMCFastCrossOverSlow; // bool isMCFastCrossUnderSlow = mcFast < mcSlow && !(mcFastPrev < mcSlowPrev); result.isMCFastCrossUnderSlow = isMCFastCrossUnderSlow; // bool isMCTrendUp = mcFast > mcSlow && mcFastPrev > mcSlowPrev; result.isMCTrendUp = isMCTrendUp; // bool isMCTrendDown = !isMCTrendUp && mcFast < mcSlow && mcFastPrev < mcSlowPrev; result.isMCTrendDown = isMCTrendDown; // // Fill Cycle Min/Max Trend Powers ... double mcTrendPowerCurrent = MathAbs(mcFast - mcSlow); ArrayResize( mcTrendPower, ArraySize(mcTrendPower) + 1 ); mcTrendPower[bar_index] = mcTrendPowerCurrent; if (mcTrendPowerCurrent > mcMaxTrendPower) { mcMaxTrendPower = mcTrendPowerCurrent; } if ( mcMinTrendPower < 0 || mcTrendPowerCurrent < mcMinTrendPower) { mcMinTrendPower = mcTrendPowerCurrent; } // // Fill Time Series ... if ( isMCFastCrossOverSlow || isMCFastCrossUnderSlow ) { // // Increase Array Size ... ArrayResize( mediumMarketCrossPointsTime, ArraySize(mediumMarketCrossPointsTime) + 1 ); // mediumMarketCrossPointsTime[0] = currentTime; // mcMinTrendPower = -1; mcMaxTrendPower = -1; } // // Long Cycle ... bool isLCFastCrossOverSlow = lcFast > lcSlow && !(lcFastPrev > lcSlowPrev); result.isLCFastCrossOverSlow = isLCFastCrossOverSlow; // bool isLCFastCrossUnderSlow = lcFast < lcSlow && !(lcFastPrev < lcSlowPrev); result.isLCFastCrossUnderSlow = isLCFastCrossUnderSlow; // bool isLCTrendUp = lcFast > lcSlow && lcFastPrev > lcSlowPrev; result.isLCTrendUp = isLCTrendUp; // bool isLCTrendDown = !isLCTrendUp && lcFast < lcSlow && lcFastPrev < lcSlowPrev; result.isLCTrendDown = isLCTrendDown; // // Fill Cycle Min/Max Trend Powers ... double lcTrendPowerCurrent = MathAbs(mcFast - mcSlow); ArrayResize( lcTrendPower, ArraySize(lcTrendPower) + 1 ); lcTrendPower[bar_index] = lcTrendPowerCurrent; if (lcTrendPowerCurrent > lcMaxTrendPower) { lcMaxTrendPower = lcTrendPowerCurrent; } if ( lcMinTrendPower < 0 || lcTrendPowerCurrent < lcMinTrendPower) { lcMinTrendPower = lcTrendPowerCurrent; } // // Fill Time Series ... if ( isLCFastCrossOverSlow || isLCFastCrossUnderSlow ) { // // Increase Array Size ... ArrayResize( longMarketCrossPointsTime, ArraySize(longMarketCrossPointsTime) + 1 ); // longMarketCrossPointsTime[0] = currentTime; // lcMinTrendPower = -1; lcMaxTrendPower = -1; } // // Reset MinMid and MaxMid ... if ( isSCFastCrossOverSlow || isSCFastCrossUnderSlow ) { // minMidInSC = -1; maxMidInSC = -1; } // // Find Hottest Area ... // bool isHottestCrossOver = isSCFastCrossOverSlow && isMCFastCrossOverSlow && isLCFastCrossOverSlow; result.isHottestCrossOver = isHottestCrossOver; // bool isHotCrossOver = !isHottestCrossOver && isSCFastCrossOverSlow && isMCFastCrossOverSlow; result.isHotCrossOver = isHotCrossOver; // bool isHottestCrossUnder = isSCFastCrossUnderSlow && isMCFastCrossUnderSlow && isLCFastCrossUnderSlow; result.isHottestCrossUnder = isHottestCrossUnder; // bool isHotCrossUnder = !isHottestCrossUnder && isMCFastCrossUnderSlow && isLCFastCrossUnderSlow; result.isHotCrossUnder = isHotCrossUnder; // // END Calculating ... // // // START Calculate Signal ... // // // Long: // Entry: // - prev candle open below Mid line ... // - current candle price above Mid Line ... // SL: // - prev candle High ... // Exit: // - r2r 1.5 // - close < mid ... // // Short: // Entry: // - prev candle open above Mid Line ... // - current candle price below Mid Line // SL: // - prev candle Low ... // Exit: // - r2r 1.5 // - close > mid // RefreshRates(); // double prevCandleOpen = iOpen( _Symbol, _Period, bar_index + 1 ); // double prevCandleHigh = iHigh( _Symbol, _Period, bar_index + 1 ); // double prevCandleLow = iLow( _Symbol, _Period, bar_index + 1 ); // // LONG Prices ... double longEntryPrice = SymbolInfoDouble(_Symbol, SYMBOL_ASK); double longClosePrice = SymbolInfoDouble(_Symbol, SYMBOL_BID); double longPriceGap = longClosePrice - longEntryPrice; // // SHORT Prices ... double shortEntryPrice = SymbolInfoDouble(_Symbol, SYMBOL_BID); double shortClosePrice = SymbolInfoDouble(_Symbol, SYMBOL_ASK); double shortPriceGap = shortEntryPrice - shortClosePrice; // bool hasLongSignal = true && isSCTrendDown && marketMiddleage > minMidInSC && prevCandleOpen < marketMiddleage && longEntryPrice > marketMiddleage ; result.hasLongSignal = hasLongSignal; // bool hasShortSignal = true && isSCTrendUp && marketMiddleage < maxMidInSC && prevCandleOpen > marketMiddleage && shortEntryPrice < marketMiddleage; result.hasShortSignal = hasShortSignal; // bool hasSignal = hasLongSignal || hasShortSignal; result.hasSignal = hasSignal; // // END Calculate Signal ... // // return result; } // // END Global Requirement Functions ... // // // START Signal Related Functions ... // // // Calculate Signal ... // in this function we calculate a signal exists in // specific bar or not ... XSignalRequest CalculateSignal( const int bar_index, // Bar Index ... // // Market Length ... const int marketLen, // Market Length ... // // Risk To Reward Ratio ... const double r2r, // // XMA Inputs ... // // Market Specification Inpouts ... const int scFastMul, // Short Cycle Fast Multiplier ... const int scSlowMul, // Short Cycle Slow Multiplier ... const int mcFastMul, // Medium Cycle Fast Multiplier ... const int mcSlowMul, // Medium Cycle Slow Multiplier ... const int lcFastMul, // Long Cycle Fast Multiplier ... const int lcSlowMul, // Long Cycle Slow Multiplier ... // // Show/Hide Inputs ... const bool showSC, // Show Short Cycle const bool showSCCrossLines, // Show Short Cycle Vertical Lines const bool showMC, // Show Medium Cycle const bool showMCCrossLines, // Show Medium Cycle Vertical Lines const bool showLC, // Show Long Cycle const bool showLCCrossLines, // Show Long Cycle Vertical Lines // // Color Inputs ... const color sxFastColor, // Short Cycle Fast Color ... const color scSlowColor, // Short Cycle Slow Color ... const color mcFastColor, // Medium Cycle Fast Color ... const color mcSlowColor, // Medium Cycle Slow Color ... const color lcFastColor, // Medium Cycle Fast Color ... const color lcSlowColor // Medium Cycle Slow Color ... ) { // // create temp result ... XSignalRequest result = {}; result.hasSignal = false; result.type = X_NONE; // XSignal signal = {}; // // Get Fully XMA Indicator State ... XMAState state = GetXMAState( // // Bar Index ... bar_index, // Bar Index ... // // Market Length ... marketLen, // Market Length ... // // XMA Inputs ... // // Market Specification Inpouts ... scFastMul, // Short Cycle Fast Multiplier ... scSlowMul, // Short Cycle Slow Multiplier ... mcFastMul, // Medium Cycle Fast Multiplier ... mcSlowMul, // Medium Cycle Slow Multiplier ... lcFastMul, // Long Cycle Fast Multiplier ... lcSlowMul, // Long Cycle Slow Multiplier ... // // Show/Hide Inputs ... showSC, // Show Short Cycle showSCCrossLines, // Show Short Cycle Vertical Lines showMC, // Show Medium Cycle showMCCrossLines, // Show Medium Cycle Vertical Lines showLC, // Show Long Cycle showLCCrossLines, // Show Long Cycle Vertical Lines // // Color Inputs ... sxFastColor, // Short Cycle Fast Color ... scSlowColor, // Short Cycle Slow Color ... mcFastColor, // Medium Cycle Fast Color ... mcSlowColor, // Medium Cycle Slow Color ... lcFastColor, // Medium Cycle Fast Color ... lcSlowColor // Medium Cycle Slow Color ... ); // if (!state.hasSignal) { // result.signal = signal; return result; } // totalSignals++; // // Define Price Related Variables ... // double price = 0; double priceGap; double sl = 0; double risk; double reward; double tp = 0; double xmaRiskToReward = r2r; datetime time = iTime( _Symbol, _Period, bar_index ); // RefreshRates(); // double prevCandleOpen = iOpen( _Symbol, _Period, bar_index + 1 ); // double prevCandleHigh = iHigh( _Symbol, _Period, bar_index + 1 ); // double prevCandleLow = iLow( _Symbol, _Period, bar_index + 1 ); // // LONG Prices ... double longEntryPrice = SymbolInfoDouble(_Symbol, SYMBOL_ASK); double longClosePrice = SymbolInfoDouble(_Symbol, SYMBOL_BID); double longPriceGap = longClosePrice - longEntryPrice; // // SHORT Prices ... double shortEntryPrice = SymbolInfoDouble(_Symbol, SYMBOL_BID); double shortClosePrice = SymbolInfoDouble(_Symbol, SYMBOL_ASK); double shortPriceGap = shortEntryPrice - shortClosePrice; // // Fill Signal Related Variables ... if (state.hasLongSignal) { // totalLongSignals++; // price = longEntryPrice; priceGap = longPriceGap; // sl = prevCandleLow; // risk = price - sl; reward = risk * xmaRiskToReward; tp = price + reward; } else if (state.hasShortSignal) { // totalShortSignals++; // price = shortEntryPrice; priceGap = shortPriceGap; // sl = prevCandleHigh; // risk = sl - price; reward = risk * xmaRiskToReward; tp = price - reward; } // // Normalize Prices ... price = NormalizeDouble(price, _Digits); sl = NormalizeDouble(sl, _Digits); tp = NormalizeDouble(tp, _Digits); // // Generate Signal Structure ... signal.type = state.hasLongSignal ? X_LONG : X_SHORT; signal.tp = tp; signal.sl = 0; // sl; signal.time = time; signal.symbol = _Symbol; signal.entryPrice = price; signal.id = totalLongSignals; // // Fill Result ... result.hasSignal = state.hasSignal; result.signal = signal; result.type = signal.type; // return result; } // // Retrieve Signal Bar Index ... int GetSignalBarIndex(XSignal &signal) { // // Temp Result ... int result = -1; // // Retriev Signal Bar Index ... result = iBarShift( _Symbol, _Period, signal.time ); // return result; } // // Generate a Unique ID for Signal ... string GenerateSignalTAG( const XSignal &signal, const string tag, const string longSignalLabel, const string shortSignalLabel ) { // // Generate Propper Label for Signal ... string label = signal.type == X_LONG ? longSignalLabel : signal.type == X_SHORT ? shortSignalLabel : "UN_KNOWN"; // // Generate Unique String Identifier for Signal ... string result = tag + "_[ID:" + IntegerToString(signal.id) + "]" + "_[" + label + "]" + "_[" + signal.symbol + "]" + "_[Time:" + TimeToStr(signal.time) + "]" + "_[TP:" + DoubleToString(signal.tp) + "]" + "_[SL:" + DoubleToString(signal.sl) + "]"; // return result; } // // Draw Signal Shapes on Chart ... bool DrawSignal( const XSignal &signal, long chartID = 0, int subWindowID = 0, const double positionThresholdFactor = 10, string signalTag = "X-Signal", string longSignalLabel = "X-Long", color longSignalColor = clrAqua, string shortSignalLabel = "X-Short", color shortSignalColor = clrFuchsia ) { // // Generate isLongSignal Var ... bool isLongSignal = signal.type == X_LONG; // // Signal Bar Index ... int bar_index = iBarShift( _Symbol, _Period, signal.time ); // // START Draw Signal Arrow ... // // // Arrow Shape Name ... string arrowName = StringConcatenate(signalTag, "_Arrow_", signal.id); // // Arrow Price ... double arrowPrice = 0; if (isLongSignal) { // arrowPrice = iLow( _Symbol, _Period, bar_index ); // arrowPrice = arrowPrice - (positionThresholdFactor * _Point); } else { // arrowPrice = iHigh( _Symbol, _Period, bar_index ); // arrowPrice = arrowPrice + (positionThresholdFactor * _Point); } // // Arrow Code ... uchar arrowCode = isLongSignal ? SYMBOL_ARROWUP : SYMBOL_ARROWDOWN; // // Arrow Anchor Point ... ENUM_ARROW_ANCHOR arrowAnchorPoint = isLongSignal ? ANCHOR_BOTTOM : ANCHOR_TOP; // // Arrow Color ... color arrowColor = isLongSignal ? longSignalColor : shortSignalColor; // // Draw Signal Arrow Shape ... bool result = DrawArrow( chartID, arrowName, subWindowID, signal.time, arrowPrice, arrowCode, arrowAnchorPoint, arrowColor, STYLE_SOLID, 3, false, false, false, 0 ); // // END Draw Signal Arrow ... // // // START Draw Signal SL/TP ... // string slLineName = StringConcatenate(signalTag, "_SL_", signal.id); string tpLineName = StringConcatenate(signalTag, "_TP_", signal.id); string entryLineName = StringConcatenate(signalTag, "_ENTRY_", signal.id); // color slColor = shortSignalColor; // isLongSignal ? shortSignalColor : longSignalColor; color tpColor = longSignalColor; // isLongSignal ? shortSignalColor : longSignalColor; color entryColor = clrGreen; // datetime time1 = iTime( _Symbol, _Period, bar_index + 1 ); // // Draw Enrty Price ... result = DrawTrendLine( chartID, entryLineName, subWindowID, time1, signal.entryPrice, signal.time, signal.entryPrice, entryColor, STYLE_SOLID, 2 ); // // Draw Signal SL ... if (signal.sl > 0) { // result = DrawTrendLine( chartID, slLineName, subWindowID, time1, signal.sl, signal.time, signal.sl, slColor, STYLE_SOLID, 2 ); } // // Draw Signal TP ... if (signal.tp > 0) { // result = DrawTrendLine( chartID, tpLineName, subWindowID, time1, signal.tp, signal.time, signal.tp, tpColor, STYLE_SOLID, 2 ); } // // END Draw Signal SL/TP ... // // // Start Draw Signal Label ... // // string labelName = StringConcatenate(signalTag, "_Label_", signal.id); // double labelPrice = isLongSignal ? arrowPrice - (positionThresholdFactor * _Point) : arrowPrice + (positionThresholdFactor * _Point); // string labelText = isLongSignal ? longSignalLabel : shortSignalLabel; // result = DrawText( chartID, labelName, subWindowID, signal.time, labelPrice, labelText, "Arial", 10, arrowColor, 0, ANCHOR_CENTER ); // // End Draw Signal Label ... // // return result; } // // Trade on Specific Signal ... bool TradeSignal( XSignal &signal, // string prefix, // double volumePercent, // // Long ... const string longSignalLabel, const color longSignalColor, // // Short ... const string shortSignalLabel, const color shortSignalColor ) { // // Temp Result ... bool result = false; // if (initialBalance == 0) { initialBalance = AccountInfoDouble(ACCOUNT_BALANCE); } // double balance = AccountInfoDouble(ACCOUNT_BALANCE); int openPositions = CountOpenTrades(); double volume = (volumePercent * initialBalance); // openPositions == 0 ? // (volumePercent * balance) : // (volumePercent * initialBalance); // if ( signal.type != X_LONG && signal.type != X_SHORT ) { return result; } // bool hasLongSignal = signal.type == X_LONG; // // Check TP and Entry Price ... if ( ( hasLongSignal && signal.entryPrice > signal.tp ) || ( !hasLongSignal && signal.entryPrice < signal.tp ) ) { return result; } // // Order Operation ... int operation = hasLongSignal ? OP_BUY : OP_SELL; // // Detect Trade Color ... color clr = hasLongSignal ? longSignalColor : shortSignalColor; // // Generate Signal Comment ... string label = hasLongSignal ? longSignalLabel : shortSignalLabel; // // Generate Signal Comment ... string comment = StringConcatenate( prefix, "_", signal.id, "_", label ); // // Placing Order ... int orderTicket = OrderSend( _Symbol, operation, volume, signal.entryPrice, 3, signal.sl, signal.tp, comment, signal.id, 0, clr ); // // Draw Signal Shape if Order Opened Successfully ... if (orderTicket >= 0) { // // Here i must to Draw Signal Shapes ... DrawSignal( signal, 0, 0, 50, prefix, // Signal Tag ... longSignalLabel, // Long Signal Label ... longSignalColor, shortSignalLabel, // Short Signal Label ... shortSignalColor ); // result = true; } // return result; } // // END Signal Related Functions ... // // // START Orders Functions ... // // // Convert To XSignal Model ... XSignal OrderToSignal() { // // Temp Result ... XSignal result = {}; result.type = X_NONE; // // Here we can get some usefull info based on selected order ... double swap = OrderSwap(); double lots = OrderLots(); int ticket = OrderTicket(); double profit = OrderProfit(); double stopLoss = OrderStopLoss(); double openPrice = OrderOpenPrice(); datetime openTime = OrderOpenTime(); int magicNumber = OrderMagicNumber(); double takeProfit = OrderTakeProfit(); // // find order type ... bool isLongOrder = OrderType() == OP_BUY; // // Conver Position to Signal ... result.sl = stopLoss; result.tp = takeProfit; result.time = openTime; result.symbol = _Symbol; result.id = magicNumber; result.entryPrice = openPrice; result.type = isLongOrder ? X_LONG : X_SHORT; // return result; } // // START Count Orders ... // // // Count Open Positions ... int CountOpenTrades() { // int result = 0; // // Loop through Orders ... for (int i = 0; i < OrdersTotal(); i++) { // // Select indexed Order ... if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { // // Check Order ... if (OrderSymbol() == _Symbol) { result++; } } } // // Return Result ... return result; } // // Count Open Long Positions ... int CountOpenLongTrades() { // int result = 0; // // Loop through Orders ... for (int i = 0; i < OrdersTotal(); i++) { // // Select indexed Order ... if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { // // Check Order ... if ( OrderSymbol() == _Symbol && OrderType() == OP_BUY ) { result++; } } } // // Return Result ... return result; } // // Count Open Short Positions ... int CountOpenShortTrades() { // int result = 0; // // Loop through Orders ... for (int i = 0; i < OrdersTotal(); i++) { // // Select indexed Order ... if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { // // Check Order ... if ( OrderSymbol() == _Symbol && OrderType() == OP_SELL ) { result++; } } } // // Return Result ... return result; } // // END Count Orders ... // // // START Close Orders ... // // // Close All Open Trades ... void CloseAllTrades() { // // Loop through all Positions ... for (int i = OrdersTotal(); i >= 0; i--) { // // Check Order Selected or not ... if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { // // Check Order Symbol is same as Current Symbol ... if (OrderSymbol() == _Symbol) { // double lots = OrderLots(); int ticket = OrderTicket(); // // find order type ... bool isLongOrder = OrderType() == OP_BUY; // // find close price ... double closePrice = isLongOrder ? SymbolInfoDouble(_Symbol, SYMBOL_BID) : SymbolInfoDouble(_Symbol, SYMBOL_ASK); // // Since because of equity ... // we have to ckose allpositions ... bool isOrderClosed = OrderClose( ticket, lots, closePrice, clrYellow ); } } } } // // Close All Open Long Trades ... void CloseAllLongTrades() { // // Loop through all Positions ... for (int i = OrdersTotal(); i >= 0; i--) { // // Check Order Selected or not ... if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { // // Check Order Symbol is same as Current Symbol ... if (OrderSymbol() == _Symbol) { // double lots = OrderLots(); int ticket = OrderTicket(); // // find order type ... bool isLongOrder = OrderType() == OP_BUY; if (isLongOrder) { // // find close price ... double closePrice = isLongOrder ? SymbolInfoDouble(_Symbol, SYMBOL_BID) : SymbolInfoDouble(_Symbol, SYMBOL_ASK); // // Since because of equity ... // we have to ckose allpositions ... bool isOrderClosed = OrderClose( ticket, lots, closePrice, clrYellow ); } } } } } // // Close All Open Short Trades ... void CloseAllShortTrades() { // // Loop through all Positions ... for (int i = OrdersTotal(); i >= 0; i--) { // // Check Order Selected or not ... if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { // // Check Order Symbol is same as Current Symbol ... if (OrderSymbol() == _Symbol) { // double lots = OrderLots(); int ticket = OrderTicket(); // // find order type ... bool isLongOrder = OrderType() == OP_BUY; if (!isLongOrder) { // // find close price ... double closePrice = isLongOrder ? SymbolInfoDouble(_Symbol, SYMBOL_BID) : SymbolInfoDouble(_Symbol, SYMBOL_ASK); // // Since because of equity ... // we have to ckose allpositions ... bool isOrderClosed = OrderClose( ticket, lots, closePrice, clrYellow ); } } } } } // // END Close Orders ... // // // END Orders Functions ... //