/////////////////////////////////////////////////////// // // SaherElm IT Center MQL4 Signal Draw Global 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.osc.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 int countedBars = 0; static bool isNewBar = false; // static XMAState xmaState; static XOSCState oscState; static XADXState adxState; static XMAOverView xmaOverview; static XZigZagState zigZagState; static XZigZagOverView zigZagOverView; // static double zigZagValues[]; static datetime zigZagTimes[]; // static double highs[]; static datetime highTimes[]; // static double lows[]; static datetime lowTimes[]; // static double scTrendPowerMax; static double scTrendPowerMin; static double scTrendPowers[]; static datetime scTrendPowerTimes[]; // static double mcTrendPowerMax; static double mcTrendPowerMin; static double mcTrendPowers[]; static datetime mcTrendPowerTimes[]; // static double lcTrendPowerMax; static double lcTrendPowerMin; static double lcTrendPowers[]; // // Retrieve ZigZag State ... XZigZagState GetZigZagState( const int bar_index, int zDpt = 7, int zDev = 5, int zBkStp = 3 ) { // XZigZagState result = {}; // // Free Arrays ... ArrayFree(zigZagTimes); ArrayFree(zigZagValues); // int counter = bar_index; int numberOfZigZagValues = zDpt; // // START Retrieve ZigZag ... // // int lastNonZeroIndex = 0; while (lastNonZeroIndex < numberOfZigZagValues - 1) { // // Retrieve ZigZag Value ... double zigZagValue = iCustom( _Symbol, _Period, zigZagIndicatorName, zDpt, // Depth ... zDev, // Deviation ... zBkStp, // BackStep ... 0, // Buffer Index ... counter // Bar Index ... ); // if (zigZagValue != 0.0) { // // Retrieve Non Zero ZigZag Time ... datetime zigZagTime = iTime( _Symbol, _Period, counter ); // ArrayResize( zigZagTimes, ArraySize(zigZagTimes) + 1 ); ArrayResize( zigZagValues, ArraySize(zigZagValues) + 1 ); zigZagValues[lastNonZeroIndex] = zigZagValue; zigZagTimes[lastNonZeroIndex] = zigZagTime; // // Increase Index Value ... lastNonZeroIndex++; } // // Increase Bar Index ... counter++; } // double lastValue = zigZagValues[0]; datetime lastValueTime = zigZagTimes[0]; // double lastPrevValue = zigZagValues[1]; datetime lastPrevValueTime = zigZagTimes[1]; // double lastPrevPrevValue = zigZagValues[2]; datetime lastPrevPrevValueTime = zigZagTimes[2]; // // END Retrieve ZigZag ... // // result.zigZag = lastValue; result.zigZagTime = lastValueTime; // result.zigZagPrev = lastPrevValue; result.zigZagPrevTime = lastPrevValueTime; // result.zigZagPrevPrev = lastPrevPrevValue; result.zigZagPrevPrevTime = lastPrevPrevValueTime; // double delta = lastPrevPrevValue - lastPrevValue; result.delta = delta; // bool isWaitForTrendUp = delta > 0; result.isWaitForTrendUp = !isWaitForTrendUp; // bool isWaitForTrendDown = delta < 0; result.isWaitForTrendDown = !isWaitForTrendDown; // double low = isWaitForTrendUp ? lastPrevValue : lastPrevPrevValue; datetime lowTime = isWaitForTrendUp ? lastPrevValueTime : lastPrevPrevValueTime; int lowBarIndex = iBarShift( _Symbol, _Period, lowTime ); // result.low = low; result.lowTime = lowTime; result.lowBarIndex = lowBarIndex; // double high = isWaitForTrendUp ? lastPrevPrevValue : lastPrevValue; datetime highTime = isWaitForTrendUp ? lastPrevPrevValueTime : lastPrevValueTime; int highBarIndex = iBarShift( _Symbol, _Period, highTime ); // result.high = high; result.highTime = highTime; result.highBarIndex = highBarIndex; // return result; } // // Retrieve ZigZag Overview ... XZigZagOverView GetZigZagOverView() { // XZigZagOverView result = {}; // // Free Arrays ... ArrayFree(highs); ArrayFree(lows); ArrayFree(lowTimes); ArrayFree(highTimes); // // Validate Arrays ... int valuesBufferSize = ArraySize(zigZagValues); if ( valuesBufferSize <= 1 || valuesBufferSize != ArraySize(zigZagTimes) ) { return result; } // // Calculate First high, low ... bool isFirstItemLow = zigZagValues[0] < zigZagValues[1]; // int pos = 0; int index = 0; while (index <= valuesBufferSize - 1) { // ArrayResize( lows, ArraySize(lows) + 1 ); lows[pos] = isFirstItemLow ? zigZagValues[index] : zigZagValues[index + 1]; // ArrayResize( lowTimes, ArraySize(lowTimes) + 1 ); lowTimes[pos] = isFirstItemLow ? zigZagTimes[index] : zigZagTimes[index + 1]; // ArrayResize( highs, ArraySize(highs) + 1 ); highs[pos] = isFirstItemLow ? zigZagValues[index + 1] : zigZagValues[index]; // ArrayResize( highTimes, ArraySize(highTimes) + 1 ); highTimes[pos] = isFirstItemLow ? zigZagTimes[index + 1] : zigZagTimes[index]; // pos++; // index += 2; } // // Now we have going to calculate Trends Based on highs and lows ... // these calculations done using Highs and Lows Buffer ... // double lastLow = 0; double lastHigh = 0; bool isTrendUp = true; bool isTrendDown = true; double smoothValue = (5 * _Point); // int highBufferSize = ArraySize(highs); if (highBufferSize > 2) { // for (int i = 0; i <= highBufferSize - 1; i++) { // lastHigh = i + 1 <= highBufferSize - 1 ? highs[i + 1] : 0 ; // isTrendUp = isTrendUp && ( highs[i] >= lastHigh || MathAbs(highs[i] - lastHigh) <= smoothValue ); } } else { // isTrendUp = false; } result.isTrendUp = isTrendUp; // int lowsBufferSize = ArraySize(lows); if (lowsBufferSize > 2) { // for (int i = 0; i <= lowsBufferSize - 1; i++) { // lastLow = i + 1 <= lowsBufferSize - 1 ? lows[i + 1] : 0 ; // isTrendDown = isTrendDown && ( lows[i] <= lastLow || MathAbs(lows[i] - lastLow) <= smoothValue ); } } else { // isTrendDown = false; } result.isTrendDown = isTrendDown; // return result; } // // Retrieve State ... XOSCState GetOSCState( const int bar_index, const int foLen, const double foMul, const int slLen, const double slMul ) { // // Define Trend Direction Related Values ... // XOSCState result = {}; // // START Extract Data from Oscillator ... // // // Fast OSC ... double fastOSC = iCustom( _Symbol, _Period, oscIndicatorName, foLen, foMul, slLen, slMul, 0, // Fast OSC Buffer Index ... bar_index ); result.fastOSC = fastOSC; // // Prev FastOSC ... double fastOSCPrev = iCustom( _Symbol, _Period, oscIndicatorName, foLen, foMul, slLen, slMul, 0, // Fast OSC Buffer Index ... bar_index + 1 ); result.fastOSCPrev = fastOSCPrev; // // Slow OSC ... double slowOSC = iCustom( _Symbol, _Period, oscIndicatorName, foLen, foMul, slLen, slMul, 1, // Slow OSC Buffer Index ... bar_index ); result.slowOSC = slowOSC; // // Prev Slow OSC ... double slowOSCPrev = iCustom( _Symbol, _Period, oscIndicatorName, foLen, foMul, slLen, slMul, 1, // Slow OSC Buffer Index ... bar_index + 1 ); result.slowOSCPrev = slowOSCPrev; // // Power OSC ... double powerOSC = iCustom( _Symbol, _Period, oscIndicatorName, foLen, foMul, slLen, slMul, 2, // Power OSC Buffer Index ... bar_index ); result.powerOSC = powerOSC; // double powerOSCPrev = iCustom( _Symbol, _Period, oscIndicatorName, foLen, foMul, slLen, slMul, 2, // Power OSC Buffer Index ... bar_index + 1 ); result.powerOSCPrev = powerOSCPrev; // // Var OSC ... double varOSC = iCustom( _Symbol, _Period, oscIndicatorName, foLen, foMul, slLen, slMul, 3, // Var OSC Buffer Index ... bar_index ); result.varOSC = varOSC; // double varOSCPrev = iCustom( _Symbol, _Period, oscIndicatorName, foLen, foMul, slLen, slMul, 3, // Var OSC Buffer Index ... bar_index + 1 ); result.varOSCPrev = varOSCPrev; // // Tan FS OSC ... double tanFSOSC = iCustom( _Symbol, _Period, oscIndicatorName, foLen, foMul, slLen, slMul, 4, // Tan F/S OSC Buffer Index ... bar_index ); result.tanFSOSC = tanFSOSC; // // END Extract Data from Oscillator ... // // // Calculations ... // bool isOSCCrossOver = fastOSC > slowOSC && !(fastOSCPrev > slowOSCPrev); result.isOSCCrossOver = isOSCCrossOver; // bool isOSCCrossUnder = fastOSC < slowOSC && !(fastOSCPrev < slowOSCPrev); result.isOSCCrossUnder = isOSCCrossUnder; // return result; } // // Check Market Ranging or not based on OSC ... bool IsOSCMarketRanging( // const int bar_index, // Bar Index ... const int marketLen, // Market Length ... // const double rangingThreshold, // Detect Ranging based On Power OSC ... // // OSC Specifications ... const int foLen, // Fast OSC Length ... const double foMul, // Fast OSC Multiplier ... const int slLen, // Slow OSC Length ... const double slMul // Slow OSC Multiplier ... ) { // bool result = false; // // Detect Buffer Size ... const int bufferSize = marketLen + bar_index; double powerOSCBuffer[]; ArraySetAsSeries( powerOSCBuffer, true ); ArrayResize( powerOSCBuffer, bufferSize ); // // Loop through Market for Extract PowerOSC from OSC Indicator ... for (int i = bufferSize - 1; i >= 0; i--) { // // Retrieve OSC State at Specific Bar Index ... XOSCState state = GetOSCState( i, foLen, foMul, slLen, slMul ); // powerOSCBuffer[i] = state.powerOSC; } // // now we have to extract min and max value of Splitted Buffer ... XMinMax minMax = GetBufferMinMax(powerOSCBuffer); // // now Check Min and Max Direction and Compare them with Ranging Value ... result = MathAbs(minMax.min) > rangingThreshold || MathAbs(minMax.max) > rangingThreshold; // return result; } // // Retrieve and Calculate XMA State ... XMAState GetXMAState( // // Market Specification Inpouts ... const int bar_index, // Bar Index ... const int marketLen, // Market Length ... // // XMA Inputs ... 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 ... // // return result; } XMAOverView GetXMAOverView( // // Market Specification Inpouts ... const int bar_index, // Bar Index ... const int marketLen, // Market Length // // XMA Inputs ... 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 ... ) { // XMAOverView result = {}; // // Clear Arrays ... ArrayFree(scTrendPowers); ArrayFree(scTrendPowerTimes); // ArrayFree(mcTrendPowers); ArrayFree(mcTrendPowerTimes); // ArrayFree(lcTrendPowers); // scTrendPowerMin = 0; scTrendPowerMax = 0; // mcTrendPowerMin = 0; mcTrendPowerMax = 0; // lcTrendPowerMin = 0; lcTrendPowerMax = 0; // bool isSCFastCrossOverSlow = false; bool isSCFastCrossUnderSlow = false; // bool isMCFastCrossOverSlow = false; bool isMCFastCrossUnderSlow = false; // // Create a Loop for retrieveing info ... int index = bar_index; while ( !(isSCFastCrossOverSlow && isSCFastCrossUnderSlow && isMCFastCrossOverSlow && isMCFastCrossUnderSlow ) ) { // // Retrieve State of XMA ... XMAState state = GetXMAState( // // Bar Index ... 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 ... ); // // Filling Arrays ... datetime time = iTime( _Symbol, _Period, index ); // // START Short Cycle ... // // int scTrendPowerSize = ArraySize(scTrendPowers); if (bar_index > 0 && scTrendPowerSize == 0) { // int delta = bar_index; ArrayResize( scTrendPowers, delta ); ArrayResize( scTrendPowerTimes, delta ); // // Fill Array by Zero ... for (int i=0; i < bar_index; i++) { scTrendPowers[i] = 0; scTrendPowerTimes[i] = 0; } } else { // ArrayResize( scTrendPowers, scTrendPowerSize + 1 ); ArrayResize( scTrendPowerTimes, scTrendPowerSize + 1 ); } // double scTPower = MathAbs(state.scFast - state.scSlow); scTrendPowers[index] = scTPower; scTrendPowerTimes[index] = time; // // Calculate Min and Max ... scTrendPowerMin = scTrendPowerMin == 0 || scTPower < scTrendPowerMin ? scTPower : scTrendPowerMin; scTrendPowerMax = scTPower > scTrendPowerMax ? scTPower : scTrendPowerMax; // // Check Cross Over ... if (!isSCFastCrossOverSlow) { // isSCFastCrossOverSlow = state.scFast > state.scSlow && !(state.scFastPrev > state.scSlowPrev); } // // Check Cross Under ... if (!isSCFastCrossUnderSlow) { // isSCFastCrossUnderSlow = state.scFast < state.scSlow && !(state.scFastPrev < state.scSlowPrev); } // // END Short Cycle ... // // // START Medium Cycle ... // // int mcTrendPowerSize = ArraySize(mcTrendPowers); if (bar_index > 0 && mcTrendPowerSize == 0) { // int delta = bar_index; ArrayResize( mcTrendPowers, delta ); ArrayResize( mcTrendPowerTimes, delta ); // // Fill Array by Zero ... for (int i=0; i < bar_index; i++) { mcTrendPowers[i] = 0; mcTrendPowerTimes[i] = 0; } } else { // ArrayResize( mcTrendPowers, mcTrendPowerSize + 1 ); ArrayResize( mcTrendPowerTimes, mcTrendPowerSize + 1 ); } // double mcTPower = MathAbs(state.mcFast - state.mcSlow); mcTrendPowers[index] = mcTPower; mcTrendPowerTimes[index] = time; // // Calculate Min and Max ... mcTrendPowerMin = mcTrendPowerMin == 0 || mcTPower < mcTrendPowerMin ? mcTPower : mcTrendPowerMin; mcTrendPowerMax = mcTPower > mcTrendPowerMax ? mcTPower : mcTrendPowerMax; // // Check Cross Over ... if (!isMCFastCrossOverSlow) { // isMCFastCrossOverSlow = state.mcFast > state.mcSlow && !(state.mcFastPrev > state.mcSlowPrev); } // // Check Cross Under ... if (!isMCFastCrossUnderSlow) { // isMCFastCrossUnderSlow = state.mcFast < state.mcSlow && !(state.mcFastPrev < state.mcSlowPrev); } // // END Medium Cycle ... // // // START Long Cycle ... // // int lcTrendPowerSize = ArraySize(lcTrendPowers); if (bar_index > 0 && lcTrendPowerSize == 0) { // int delta = bar_index; ArrayResize( lcTrendPowers, delta ); // // Fill Array by Zero ... for (int i=0; i < bar_index; i++) { lcTrendPowers[i] = 0; } } else { // ArrayResize( lcTrendPowers, lcTrendPowerSize + 1 ); } // double lcTPower = MathAbs(state.lcFast - state.lcSlow); lcTrendPowers[index] = lcTPower; // // Calculate Min and Max ... lcTrendPowerMin = lcTrendPowerMin == 0 || lcTPower < lcTrendPowerMin ? lcTPower : lcTrendPowerMin; lcTrendPowerMax = lcTPower > lcTrendPowerMax ? lcTPower : lcTrendPowerMax; // // END Long Cycle ... // // // Fill Calculations ... // index++; } // return result; } // // Retrieve XADX State ... XADXState GetADXState( // const int bar_index, // Bar Index ... // const int marketLen // Market Length ... ) { // // Temp Result ... XADXState result = {}; // // Define Buffer Index ... int adxBufferIndex = 0; int positiveBufferIndex = 1; int negativeBufferIndex = 2; // ENUM_APPLIED_PRICE appliedPrice = PRICE_WEIGHTED; // // START Recieve Buffers ... // // // TRENDPOWER ... double trendPower = iCustom( // // Global Data ... _Symbol, _Period, // // Indicator Name ... adxIndicatorName, // // Indicator Inputs ... marketLen, // Averaging Period ... appliedPrice, // Averaging Applied Price ... // // Buffer Index adxBufferIndex, // ADX Trend Power Buffer Index ... // // Shift or Bar Index ... bar_index ); result.trendPower = trendPower; // // TRENDPOWER Prev ... double trendPowerPrev = iCustom( // // Global Data ... _Symbol, _Period, // // Indicator Name ... adxIndicatorName, // // Indicator Inputs ... marketLen, // Averaging Period ... appliedPrice, // Averaging Applied Price ... // // Buffer Index adxBufferIndex, // ADX Trend Power Buffer Index ... // // Shift or Bar Index ... bar_index + 1 ); result.trendPowerPrev = trendPowerPrev; // // UP Dir ... double upDirection = iCustom( // // Global Data ... _Symbol, _Period, // // Indicator Name ... adxIndicatorName, // // Indicator Inputs ... marketLen, // Averaging Period ... appliedPrice, // Averaging Applied Price ... // // Buffer Index positiveBufferIndex, // Up Direction Buffer Index ... // // Shift or Bar Index ... bar_index ); result.upDirection = upDirection; // // UP Dir Prev ... double upDirectionPrev = iCustom( // // Global Data ... _Symbol, _Period, // // Indicator Name ... adxIndicatorName, // // Indicator Inputs ... marketLen, // Averaging Period ... appliedPrice, // Averaging Applied Price ... // // Buffer Index positiveBufferIndex, // Up Direction Buffer Index ... // // Shift or Bar Index ... bar_index + 1 ); result.upDirectionPrev = upDirectionPrev; // // DOWN Dir ... double downDirection = iCustom( // // Global Data ... _Symbol, _Period, // // Indicator Name ... adxIndicatorName, // // Indicator Inputs ... marketLen, // Averaging Period ... appliedPrice, // Averaging Applied Price ... // // Buffer Index negativeBufferIndex, // Down Direction Buffer Index ... // // Shift or Bar Index ... bar_index ); result.downDirection = downDirection; // // DOWN Dir Prev ... double downDirectionPrev = iCustom( // // Global Data ... _Symbol, _Period, // // Indicator Name ... adxIndicatorName, // // Indicator Inputs ... marketLen, // Averaging Period ... appliedPrice, // Averaging Applied Price ... // // Buffer Index negativeBufferIndex, // Down Direction Buffer Index ... // // Shift or Bar Index ... bar_index + 1 ); result.downDirectionPrev = downDirectionPrev; // // END Recieve Buffers ... // // // Calculations ... // bool isUpTrend = upDirection > downDirection && upDirectionPrev > downDirectionPrev; result.isUpTrend = isUpTrend; // bool isDownTrend = upDirection < downDirection && upDirectionPrev < downDirectionPrev; result.isDownTrend = isDownTrend; // return result; } // // Calculate Fib Level ... double GetFibonacciLevel( double upPrice, double downPrice, double level, X_DIRECTION direction ) { // double ling = upPrice - downPrice; double pLevel = (ling / 100) * (level * 100); // double result = direction == X_UP ? upPrice - pLevel : downPrice + pLevel; result = NormalizeDouble(result, _Digits); // 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 Specifications ... const int marketLen, // Market Length ... const double r2r, // Risk To Reward Ratio ... // // XMA Inputs ... // 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 ... // // OSC Inputs ... const int fastOSCLen, // FastOSC Length ... const double fastOSCMul, // FastOSC Multiplier ... const int slowOSCLen, // SlowOSC Length ... const double slowOSCMul, // SlowOSC Multiplier ... const double minPowOSC, // Minimum Signal Power ... const double minVarOSC, // Minimum Var Power ... // // ZIGZAG Inputs ... const int zDev, // ZigZag Deviation ... const int zBkStep // ZigZag Back Step ... ) { // // create temp result ... XSignalRequest result = {}; result.hasSignal = false; result.type = X_NONE; // XSignal signal = {}; // // Retrieve ZigZag State ... // // START Capturing Signal ... // // // START Retrieve Statets ... // // Reading Oscillator State ... oscState = GetOSCState( bar_index, fastOSCLen, fastOSCMul, slowOSCLen, slowOSCMul ); // // Retrieve ZigZag State ... zigZagState = GetZigZagState( bar_index, marketLen, zDev, zBkStep ); // zigZagOverView = GetZigZagOverView(); // // Retrieve Market Trend ... xmaState = 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 ... ); // xmaOverview = GetXMAOverView( // // 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 ... ); // // Retrieve ADX State ... adxState = GetADXState( // bar_index, // Bar Index ... // marketLen // Market Length ... ); // // END Retrieve Statets ... // // // START States Calculations ... // // // START PRICE Calculations ... // RefreshRates(); // double candleLow = iLow( _Symbol, _Period, bar_index ); // double candleHigh = iHigh( _Symbol, _Period, bar_index ); // // 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; // // END PRICE Calculations ... // // // START LONG Signals ... // bool hasLongSignal = false; // // XMA Based Signals ... hasLongSignal = // // XMA ... // // SC Fast Cross Over Market Middleage ... xmaState.scFast > xmaState.marketMiddleage && !(xmaState.scFastPrev > xmaState.marketMiddleage) // // when Short Cycle is Trending Up ... && xmaState.scFast > xmaState.scSlow && xmaState.scFastPrev > xmaState.scSlowPrev // // When Medium Cycle is Trending Up ... && xmaState.mcFast > xmaState.mcSlow && xmaState.mcFastPrev > xmaState.mcSlowPrev // // and Medium Cycle TrendPower is Going Up ... && MathAbs(xmaState.mcFast - xmaState.mcSlow) > MathAbs(xmaState.mcFastPrev - xmaState.mcSlowPrev) // // When Long Cycle is Trending Up ... && xmaState.lcFast > xmaState.lcSlow && xmaState.lcFastPrev > xmaState.lcSlowPrev // // and Long Cycle TrendPower is Going Up ... && MathAbs(xmaState.lcFast - xmaState.lcSlow) > MathAbs(xmaState.lcFastPrev - xmaState.lcSlowPrev) // // Short Cycle Slow bigger thhan Medium Cycle Fast ... && xmaState.scSlow > xmaState.mcFast // // OSC ... // Check OSC Fast and Slow is Less than 0.5 ... && oscState.fastOSC < 0.5 && oscState.slowOSC < 0.5 && oscState.fastOSCPrev < 0.5 && oscState.slowOSCPrev < 0.5 && oscState.fastOSC > oscState.fastOSCPrev // // Check OSC Minimum Signal Power ... // this Helps for Detect Market exits from Ranging ... && minPowOSC > 0 ? // // Check Conditions only if value provided ... oscState.powerOSCPrev < 0 && MathAbs(oscState.powerOSCPrev) > minPowOSC : // // ... other wise, ignore ... true // // Check Previous Candle Signal Power must be Lower than // Current Signal Power and the Direction Must be Up Trend ... && oscState.powerOSCPrev < oscState.powerOSC // // Also we Have to Check Market not ranging ... && !IsOSCMarketRanging( bar_index + 1, marketLen, minPowOSC, fastOSCLen, fastOSCMul, slowOSCLen, slowOSCMul ) // // ZIGZAG ... && zigZagState.isWaitForTrendDown && !zigZagState.isWaitForTrendUp // // XADX ... // TODO: add XADX Base Conditions for Long Trades ... // When ADX Trend Power bigger than 20 and less than 50 ... && !adxState.trendPower < 23 && !adxState.trendPowerPrev < 23 // // Check Trend Direction for Signals ... && adxState.upDirection > adxState.downDirection && adxState.upDirectionPrev > adxState.downDirectionPrev // && adxState.trendPower < 50 // && adxState.trendPower > adxState.trendPowerPrev // // PRICE Checking ... // price must be less than Candle High at openning time ... && longEntryPrice <= candleHigh // // also Candle High and Low must be above than lc Fast & lc Slow ... && longEntryPrice > xmaState.lcFast && longEntryPrice > xmaState.lcSlow && ( !IsSharpBullishHappens( bar_index, marketLen * 2 ) || IsSharpBearishHappens( bar_index, marketLen * 2 ) ) ; // // END LONG Signals ... // // // START SHORT Signals ... // bool hasShortSignal = false; // // Recieve Signals Based on XOSC ... // hasShortSignal = // oscState.hasShortSignal // ; // // END SHORT Signals ... // // // Check Signal Exists regardless of Long or Short ... bool hasSignal = hasLongSignal || hasShortSignal; // // Prevent from Going forward if there is not a Signal ... if (!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 r2rRatio = r2r; // datetime time = iTime( _Symbol, _Period, bar_index ); // // Fill Signal Related Variables ... if (hasLongSignal) { // totalLongSignals++; // price = longEntryPrice; priceGap = longPriceGap; // // // // Setting Up SL Based on Prev Candle ... // sl = prevCandleLow; // // Setting Up SL Based on Last Market Lowest ... sl = GetMarketLowestLow( bar_index, marketLen ); // // // // Setting Up SL Based on Prev Candle Less Low Price ... // sl = GetLowLessCandlePrice( // bar_index, // price // ); // // Check if ZigZag shows TrendUp increase r2rRatio ... if ( ( xmaState.marketMiddleage < highs[0] && xmaState.marketMiddleage > candleLow ) || ( zigZagOverView.isTrendUp && !zigZagOverView.isTrendDown ) ) { // r2rRatio = r2r + (r2r * 0.6); } // risk = price - sl; reward = risk * r2rRatio; tp = price + reward; // // Check TP Based On ZigZag High .. if ( tp > zigZagState.high && !zigZagOverView.isTrendUp ) { tp = zigZagState.high - (5 * _Point); } // // Check TP based on Spread ... if (tp < price + priceGap) { tp = tp + priceGap; } } else if (hasShortSignal) { // totalShortSignals++; // price = shortEntryPrice; priceGap = shortPriceGap; // // // // Setting Up SL Based On Prev Candle ... // sl = prevCandleHigh; // // // // Setting Up SL Based on Last Market Highest ... // sl = GetMarketHighestHigh( // bar_index, // marketLen // ); // // Setting Up SL Based on Prev Candle Great High Price ... sl = GetHighGreatCandlePrice( bar_index, price ); // risk = sl - price; reward = risk * r2r; tp = price - reward; } // // Normalize Prices ... price = NormalizeDouble(price, _Digits); sl = NormalizeDouble(sl, _Digits); tp = NormalizeDouble(tp, _Digits); // // Generate Signal Structure ... signal.type = hasLongSignal ? X_LONG : X_SHORT; signal.tp = tp; signal.sl = 0; // sl; signal.time = time; signal.symbol = _Symbol; signal.entryPrice = price; signal.id = totalLongSignals; // // END Capturing Signal ... // // // Fill Result ... result.hasSignal = hasSignal; result.signal = signal; result.type = signal.type; // return result; } // // START Signal Verification Functions ... // // // END Signal Verification Functions ... // // // START Market State Functions ... // // // END Market State Functions ... // // // 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); // 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 ... bool CloseAllTrades() { // // Loop through all Positions ... bool result = false; 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 ); // if (isOrderClosed) { // LogMessage( StringConcatenate( "Order [", ticket, "] Closed because of All Trade Closing issue ..." ) ); } // result = result || isOrderClosed; } } } // return result; } // // Close All Open Long Trades ... bool CloseAllLongTrades() { // // Loop through all Positions ... bool result = false; 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 ); // if (isOrderClosed) { // LogMessage( StringConcatenate( "Order [", ticket, "] Closed because of All Long Trade Closing issue ..." ) ); } // result = result || isOrderClosed; } } } } // return result; } // // Close All Open Short Trades ... bool CloseAllShortTrades() { // // Loop through all Positions ... bool result = false; 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 ); // if (isOrderClosed) { // LogMessage( StringConcatenate( "Order [", ticket, "] Closed because of All Short Trade Closing issue ..." ) ); } // result = result || isOrderClosed; } } } } // return result; } // // Close All In Profit Trades ... bool CloseAllInProfitTrades() { // // Loop through all Positions ... bool result = false; 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(); double profit = OrderProfit(); // // find order type ... bool isLongOrder = OrderType() == OP_BUY; // // find close price ... double closePrice = isLongOrder ? SymbolInfoDouble(_Symbol, SYMBOL_BID) : SymbolInfoDouble(_Symbol, SYMBOL_ASK); // if (profit > 0) { // // Since because of equity ... // we have to ckose allpositions ... bool isOrderClosed = OrderClose( ticket, lots, closePrice, clrYellow ); // if (isOrderClosed) { // LogMessage( StringConcatenate( "Order [", ticket, "] Closed because of InProfit Trade Closing issue ..." ) ); } // result = result || isOrderClosed; } } } } // return result; } // // Close All Long Time Trades ... bool CloseAllLongTimeTrades( const int passedCandles ) { // bool result = false; // if (passedCandles <= 0) { return result; } // int currentBarIndex = iBarShift( _Symbol, _Period, TimeCurrent() ); // // 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(); double profit = OrderProfit(); datetime openTime = OrderOpenTime(); int openBarIndex = iBarShift( _Symbol, _Period, openTime ); // if ( openBarIndex - currentBarIndex >= passedCandles || ( profit > 0.5 && openBarIndex - currentBarIndex >= passedCandles - (passedCandles / 3) ) ) { // // 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 ); // if (isOrderClosed) { // LogMessage( StringConcatenate( "Order [", ticket, "] Closed because of Long Time issue ..." ) ); } // result = result || isOrderClosed; } } } } // return result; } // // Close all trades which passed Maximum Drawdown ... bool CloseAllMaximumDrawDownPassedTrades( const double maxDrDownPercentPerPTrade ) { // if ( initialBalance <= 0 || maxDrDownPercentPerPTrade <= 0 ) { return false; } // // Retrieve Account Balance ... double balance = AccountInfoDouble(ACCOUNT_BALANCE); double maxAllowedDrawDown = -1 * (initialBalance * maxDrDownPercentPerPTrade); // // Loop through all Positions ... bool result = false; 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(); double profit = OrderProfit(); // if ( profit < 0 && MathAbs(profit) >= MathAbs(maxAllowedDrawDown) ) { // // 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 ); // if (isOrderClosed) { // LogMessage( StringConcatenate( "Order [", ticket, "] Closed because of Maximum DarwDown issue ..." ) ); } // result = result || isOrderClosed; } } } } // return result; } // // END Close Orders ... // // // END Orders Functions ... //