/////////////////////////////////////////////////////// // // 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 XZigZagState zigZagState; // // Retrieve ZigZag State ... XZigZagState GetZigZagState( const int bar_index, int zDpt = 7, int zDev = 5, int zBkStp = 3 ) { // XZigZagState result = {}; // int counter = bar_index; // double lastValue = 0.0; datetime lastValueTime = 0; // double lastPrevValue = 0.0; datetime lastPrevValueTime = 0; // // START Retrieve ZigZag ... // while ( lastValue == 0.0 || lastPrevValue == 0.0 ) { // // ZigZag ... double zigZag = iCustom( _Symbol, _Period, zigZagIndicatorName, zDpt, // Depth ... zDev, // Deviation ... zBkStp, // BackStep ... 0, // Buffer Index ... counter ); // if (zigZag != 0.0) { // if (lastValue == 0.0) { // lastValue = zigZag; lastValueTime = iTime( _Symbol, _Period, counter ); } else if (lastPrevValue == 0.0) { // lastPrevValue = zigZag; lastPrevValueTime = iTime( _Symbol, _Period, counter ); } } // counter++; } // // END Retrieve ZigZag ... // // result.zigZag = lastValue; result.zigZagTime = lastValueTime; // result.zigZagPrev = lastPrevValue; result.zigZagPrevTime = lastPrevValueTime; // double delta = lastPrevValue - lastValue; result.delta = delta; // bool isWaitForTrendUp = delta > 0; result.isWaitForTrendUp = isWaitForTrendUp; // bool isWaitForTrendDown = delta < 0; result.isWaitForTrendDown = isWaitForTrendDown; // double low = isWaitForTrendUp ? lastValue : lastPrevValue; datetime lowTime = isWaitForTrendUp ? lastValueTime : lastPrevValueTime; int lowBarIndex = iBarShift( _Symbol, _Period, lowTime ); // result.low = low; result.lowTime = lowTime; result.lowBarIndex = lowBarIndex; // double high = isWaitForTrendUp ? lastPrevValue : lastValue; datetime highTime = isWaitForTrendUp ? lastPrevValueTime : lastValueTime; int highBarIndex = iBarShift( _Symbol, _Period, highTime ); // result.high = high; result.highTime = highTime; result.highBarIndex = highBarIndex; // // Check Signals Occured exactly after high or low ... bool isLowIsLastLow = false; bool isHighIsLastHigh = false; for (int i = zBkStp - 1; i <= 0; i--) { // double lLow = iLow( _Symbol, _Period, i ); // isLowIsLastLow = isLowIsLastLow || lLow == low || MathAbs(lLow - low) < 0.05; // double lHigh = iHigh( _Symbol, _Period, i ); // isHighIsLastHigh = high || lHigh == high || MathAbs(lHigh - high) < 0.05; } // result.isLowIsLastLow = isLowIsLastLow; result.isHighIsLastHigh = isHighIsLastHigh; // 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( 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 ... // // 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 ); // // 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 ... ); // // 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 ... bool xmaHasLongSignal = // // 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 // // 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 ) // // XADX ... // TODO: add XADX Base Conditions for Long Trades ... // // 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 ; // // Has Some Good Points ... // double oscTreshold = 0.5; bool oscHasLongSignal = false // // // // OSC ... // oscState.isOSCCrossOver // // // // XMA ... // // // // // // PRICE ... // && longEntryPrice <= candleHigh ; // hasLongSignal = xmaHasLongSignal || oscHasLongSignal; // // 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; // 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 // ); // risk = price - sl; reward = risk * r2r; tp = price + reward; // // Check TP for prevent from 5 Pips ... // double pipsPrice = PipsToPrice(2); // if (tp > price + pipsPrice) { // tp = price + pipsPrice; // } // // 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(); datetime openTime = OrderOpenTime(); int openBarIndex = iBarShift( _Symbol, _Period, openTime ); // if (openBarIndex - currentBarIndex >= passedCandles) { // // 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 ... //