/////////////////////////////////////////////////////// // SaherElm IT Center MQL5 Indicator // ------------------------------------------- // Name: XCHTD // Description: XCHTD Cycles ... // // Maintainer: // ------------ // Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com) // ////////////////////////////////////////////////////// // // Global Properties ... #property copyright "Copyright 2023, SaherElm IT Center" #property link "https://saherelm.ir" #property version "1.00" #property description "SaherElm XCHTD Indicator" #property strict // #define ShortName "XCHTD" // // Includes Common Library ... #include "../Libraries/x-saherelm.common.lib.mq5" // enum ENUM_XCHTD_MODE { XCHTD_POWER_MODE = 0, // Using Power XCHTD_PRESSURE_MODE = 1, // Using Pressure XCHTD_TYPE_MODE = 2, // Using Type XCHTD_STRONG_MODE = 3, // Using Power and Pressures }; // // Inputs ... // input group "Market"; input int length = 14; // Length input ENUM_XCHTD_MODE mode = XCHTD_POWER_MODE; // Mode // input group "Cycles"; // input group "Short"; input ENUM_X_PERIOD_METHOD scMethod = X_PERIOD_AUTO; // How to Find Period input ENUM_TIMEFRAMES scPeriod = NULL; // Time Period // input group "Medium"; input ENUM_X_PERIOD_METHOD mcMethod = X_PERIOD_AUTO; // How to Find Period input ENUM_TIMEFRAMES mcPeriod = NULL; // Time Period // input group "Long"; input ENUM_X_PERIOD_METHOD lcMethod = X_PERIOD_AUTO; // How to Find Period input ENUM_TIMEFRAMES lcPeriod = NULL; // Time Period // input group "Hind"; input ENUM_X_PERIOD_METHOD hcMethod = X_PERIOD_AUTO; // How to Find Period input ENUM_TIMEFRAMES hcPeriod = NULL; // Time Period // // Presentation ... input group "Presentation"; // // Current ... input bool showCurrent = true; // Show Current Cycle input bool showShort = true; // Show Short Cycle input bool showMedium = true; // Show Medium Cycle input bool showLong = true; // Show Long Cycle input bool showHind = true; // Show Hind Cycle // // Buffers ... // #property indicator_chart_window // #property indicator_buffers 10 #property indicator_plots 0 // // Current ... // #define cBullBufferIndex 0 double cBullBuffer[]; // #property indicator_label1 "XCHTD C Bull" #property indicator_type1 DRAW_LINE #property indicator_color1 clrLime #property indicator_style1 STYLE_SOLID // #define cBearBufferIndex 1 double cBearBuffer[]; // #property indicator_label2 "XCHTD C Bear" #property indicator_type2 DRAW_LINE #property indicator_style2 STYLE_SOLID #property indicator_color2 clrRed // // Short ... // #define sBullBufferIndex 2 double sBullBuffer[]; // #property indicator_label3 "XCHTD S Bull" #property indicator_type3 DRAW_LINE #property indicator_color3 clrLime #property indicator_style3 STYLE_SOLID // #define sBearBufferIndex 3 double sBearBuffer[]; // #property indicator_label4 "XCHTD S Bear" #property indicator_type4 DRAW_LINE #property indicator_style4 STYLE_SOLID #property indicator_color4 clrRed // // Medium ... // #define mBullBufferIndex 4 double mBullBuffer[]; // #property indicator_label5 "XCHTD M Bull" #property indicator_type5 DRAW_LINE #property indicator_color5 clrLime #property indicator_style5 STYLE_SOLID // #define mBearBufferIndex 5 double mBearBuffer[]; // #property indicator_label6 "XCHTD M Bear" #property indicator_type6 DRAW_LINE #property indicator_style6 STYLE_SOLID #property indicator_color6 clrRed // // Long ... // #define lBullBufferIndex 6 double lBullBuffer[]; // #property indicator_label7 "XCHTD L Bull" #property indicator_type7 DRAW_LINE #property indicator_color7 clrLime #property indicator_style7 STYLE_SOLID // #define lBearBufferIndex 7 double lBearBuffer[]; // #property indicator_label8 "XCHTD L Bear" #property indicator_type8 DRAW_LINE #property indicator_style8 STYLE_SOLID #property indicator_color8 clrRed // // Hind ... // #define hBullBufferIndex 8 double hBullBuffer[]; // #property indicator_label9 "XCHTD H Bull" #property indicator_type9 DRAW_LINE #property indicator_color9 clrLime #property indicator_style9 STYLE_SOLID // #define hBearBufferIndex 9 double hBearBuffer[]; // #property indicator_label10 "XCHTD H Bear" #property indicator_type10 DRAW_LINE #property indicator_style10 STYLE_SOLID #property indicator_color10 clrRed // // Variables, Properties and etc ... // int maxLength; // // Current ... int mCMarketLength = length; // // Short ... int mSCycleLength = 0; int mSMarketLength = 0; ENUM_TIMEFRAMES mSCPeriod = NULL; // // Medium ... int mMCycleLength = 0; int mMMarketLength = 0; ENUM_TIMEFRAMES mMCPeriod = NULL; // // Long ... int mLCycleLength = 0; int mLMarketLength = 0; ENUM_TIMEFRAMES mLCPeriod = NULL; // // Hind ... int mHCycleLength = 0; int mHMarketLength = 0; ENUM_TIMEFRAMES mHCPeriod = NULL; // // Event Handlers ... // // Initialization ... int OnInit() { // // Validate Inputs ... if (!ValidateInputs()) { return INIT_PARAMETERS_INCORRECT; } // if (!InitMarketCycles()) { return INIT_PARAMETERS_INCORRECT; } // // because in some cases we may have more than one input for // calculation and we must prevent any calculation // untill we pass the biggest input length, here we get max Input length // and then wait until pass it ... maxLength = ExtractMaxLengthOfInputs(); // // Define Index Buffers ... DefineBuffers(); // // Set Indicator ShortName ... SetIndicatorName(); // // Init Succeed ... return INIT_SUCCEEDED; } // // DeInitialization ... void OnDeinit(const int reason) { // // REASON_PROGRAM 0 The EA has stopped working calling the ExpertRemove() function // REASON_REMOVE 1 Program removed from a chart // REASON_RECOMPILE 2 Program recompiled // REASON_CHARTCHANGE 3 A symbol or a chart period is changed // REASON_CHARTCLOSE 4 Chart closed // REASON_PARAMETERS 5 Inputs changed by a user // REASON_ACCOUNT 6 Another account has been activated or reconnection to the trade server has occurred due to changes in the account settings // REASON_TEMPLATE 7 Another chart template applied // REASON_INITFAILED 8 The OnInit() handler returned a non-zero value // REASON_CLOSE 9 Terminal closed } // // Calculating what we want ... int OnCalculate( const int rates_total, // Total Bars on Chart ... const int prev_calculated, // Total Calculated Bars on Charts ... const datetime &time[], // History of Open Time ... const double &open[], // History of Open Price ... const double &high[], // History of High Price ... const double &low[], // History of Low Price ... const double &close[], // History of Close Price ... const long &tick_volume[], // History of Tick Volumes on Bar ... const long &volume[], // History of Trade Volumes ... const int &spread[] // History of Spread Price ... ) { // // Prepare Buffers ... ArraySetAsSeries(time, true); ArraySetAsSeries(open, true); ArraySetAsSeries(high, true); ArraySetAsSeries(low, true); ArraySetAsSeries(close, true); ArraySetAsSeries(tick_volume, true); ArraySetAsSeries(volume, true); ArraySetAsSeries(spread, true); // // this counts Available Bars ... int limit; // // because in some cases we may have more than one input for // calculation and we must prevent any calculation // untill we pass the biggest input length, here we get max Input length // and then wait until pass it ... // limit = prev_calculated < maxLength ? rates_total - (maxLength + 1) : rates_total - prev_calculated; // // Main Loop ... for (int i = limit; i >= 0 && !IsStopped(); i--) { // CalculateBuffers( i, prev_calculated, rates_total, open, high, close, low, tick_volume // ); } // return rates_total; } // // Functions ... // // Validate Input Args for Initialization ... bool ValidateInputs() { // bool result = false; // result = // length >= 2 && // (IsValid(scMethod, scPeriod) && IsValid(mcMethod, mcPeriod) && IsValid(lcMethod, lcPeriod) && IsValid(hcMethod, hcPeriod)) // ; // return result; } // // Initialize Market Cycles ... bool InitMarketCycles() { // bool result = false; // // Current Cycle Initialization ... // int cPeriodSeconds = PeriodSeconds(_Period); // // Short Cycle Initialization ... // // Find Cycle Period ... if (scMethod == X_PERIOD_AUTO) { // // Select Period ... mSCPeriod = GetCyclePeriod( X_MARKET_CYCLE_SHORT, _Period // ); } else { mSCPeriod = scPeriod; } // result = IsValid(mSCPeriod); if (!result) { return result; } // mSCycleLength = PeriodSeconds(mSCPeriod) / cPeriodSeconds; mSMarketLength = mSCycleLength * mCMarketLength; result = mSCycleLength > 0; if (!result) { return result; } // // Medium Cycle Initialization ... // // Find Cycle Period ... if (mcMethod == X_PERIOD_AUTO) { // // Select Period ... mMCPeriod = GetCyclePeriod( X_MARKET_CYCLE_MEDIUM, _Period // ); } else { mMCPeriod = mcPeriod; } // result = IsValid(mMCPeriod); if (!result) { return result; } // mMCycleLength = PeriodSeconds(mMCPeriod) / cPeriodSeconds; mMMarketLength = mMCycleLength * mCMarketLength; result = mMCycleLength > 0; if (!result) { return result; } // // Long Cycle Initialization ... // // Find Cycle Period ... if (lcMethod == X_PERIOD_AUTO) { // // Select Period ... mLCPeriod = GetCyclePeriod( X_MARKET_CYCLE_LONG, _Period // ); } else { mLCPeriod = lcPeriod; } // result = IsValid(mLCPeriod); if (!result) { return result; } // mLCycleLength = PeriodSeconds(mLCPeriod) / cPeriodSeconds; mLMarketLength = mLCycleLength * mCMarketLength; result = mLCycleLength > 0; if (!result) { return result; } // // Hind Cycle Initialization ... // // Find Cycle Period ... if (hcMethod == X_PERIOD_AUTO) { // // Select Period ... mHCPeriod = GetCyclePeriod( X_MARKET_CYCLE_HIND, _Period // ); } else { mHCPeriod = hcPeriod; } // result = IsValid(mHCPeriod); if (!result) { return result; } // mHCycleLength = PeriodSeconds(mHCPeriod) / cPeriodSeconds; mHMarketLength = mHCycleLength * mCMarketLength; result = mHCycleLength > 0; if (!result) { return result; } // return result; } // // Retrieve all Exists Input Max Length ... // use for Start Of Drawing ... int ExtractMaxLengthOfInputs() { // int result = 0; // result = MathMax(mCMarketLength, mSMarketLength); result = MathMax(result, mMMarketLength); result = MathMax(result, mLMarketLength); result = MathMax(result, mHMarketLength); // return result; } // // Define Indexes and Styles ... void DefineBuffers() { // // Current ... // // ENUM_DRAW_TYPE cDrawType = showCurrent ? DRAW_LINE : DRAW_NONE; // ArraySetAsSeries(cBullBuffer, true); SetIndexBuffer(cBullBufferIndex, cBullBuffer, INDICATOR_CALCULATIONS); // PlotIndexSetInteger(cBullBufferIndex, PLOT_DRAW_TYPE, cDrawType); // PlotIndexSetInteger(cBullBufferIndex, PLOT_SHOW_DATA, showCurrent); // ArraySetAsSeries(cBearBuffer, true); SetIndexBuffer(cBearBufferIndex, cBearBuffer, INDICATOR_CALCULATIONS); // PlotIndexSetInteger(cBearBufferIndex, PLOT_DRAW_TYPE, cDrawType); // PlotIndexSetInteger(cBearBufferIndex, PLOT_SHOW_DATA, showCurrent); // // Short ... // // ENUM_DRAW_TYPE sDrawType = showShort ? DRAW_LINE : DRAW_NONE; // ArraySetAsSeries(sBullBuffer, true); SetIndexBuffer(sBullBufferIndex, sBullBuffer, INDICATOR_CALCULATIONS); // PlotIndexSetInteger(sBullBufferIndex, PLOT_DRAW_TYPE, sDrawType); // PlotIndexSetInteger(sBullBufferIndex, PLOT_SHOW_DATA, showShort); // ArraySetAsSeries(sBearBuffer, true); SetIndexBuffer(sBearBufferIndex, sBearBuffer, INDICATOR_CALCULATIONS); // PlotIndexSetInteger(sBearBufferIndex, PLOT_DRAW_TYPE, sDrawType); // PlotIndexSetInteger(sBearBufferIndex, PLOT_SHOW_DATA, showShort); // // Medium ... // // ENUM_DRAW_TYPE mDrawType = showMedium ? DRAW_LINE : DRAW_NONE; // ArraySetAsSeries(mBullBuffer, true); SetIndexBuffer(mBullBufferIndex, mBullBuffer, INDICATOR_CALCULATIONS); // PlotIndexSetInteger(mBullBufferIndex, PLOT_DRAW_TYPE, mDrawType); // PlotIndexSetInteger(mBullBufferIndex, PLOT_SHOW_DATA, showMedium); // ArraySetAsSeries(mBearBuffer, true); SetIndexBuffer(mBearBufferIndex, mBearBuffer, INDICATOR_CALCULATIONS); // PlotIndexSetInteger(mBearBufferIndex, PLOT_DRAW_TYPE, mDrawType); // PlotIndexSetInteger(mBearBufferIndex, PLOT_SHOW_DATA, showMedium); // // Long ... // // ENUM_DRAW_TYPE lDrawType = showLong ? DRAW_LINE : DRAW_NONE; // ArraySetAsSeries(lBullBuffer, true); SetIndexBuffer(lBullBufferIndex, lBullBuffer, INDICATOR_CALCULATIONS); // PlotIndexSetInteger(lBullBufferIndex, PLOT_DRAW_TYPE, lDrawType); // PlotIndexSetInteger(lBullBufferIndex, PLOT_SHOW_DATA, showLong); // ArraySetAsSeries(lBearBuffer, true); SetIndexBuffer(lBearBufferIndex, lBearBuffer, INDICATOR_CALCULATIONS); // PlotIndexSetInteger(lBearBufferIndex, PLOT_DRAW_TYPE, lDrawType); // PlotIndexSetInteger(lBearBufferIndex, PLOT_SHOW_DATA, showLong); // // Hind ... // // ENUM_DRAW_TYPE hDrawType = showHind ? DRAW_LINE : DRAW_NONE; // ArraySetAsSeries(hBullBuffer, true); SetIndexBuffer(hBullBufferIndex, hBullBuffer, INDICATOR_CALCULATIONS); // PlotIndexSetInteger(hBullBufferIndex, PLOT_DRAW_TYPE, hDrawType); // PlotIndexSetInteger(hBullBufferIndex, PLOT_SHOW_DATA, showHind); // ArraySetAsSeries(hBearBuffer, true); SetIndexBuffer(hBearBufferIndex, hBearBuffer, INDICATOR_CALCULATIONS); // PlotIndexSetInteger(hBearBufferIndex, PLOT_DRAW_TYPE, hDrawType); // PlotIndexSetInteger(hBearBufferIndex, PLOT_SHOW_DATA, showHind); } // // Set Indicator Short Name and also we can define Buffers Labels ... void SetIndicatorName() { IndicatorSetString(INDICATOR_SHORTNAME, ShortName); } // // Calculations ... // // Calculate Buffers ... void CalculateBuffers( int bar_index, // Selected Bar Index const int prevCalculated, const int ratesTotal, const double &open[], const double &high[], const double &close[], const double &low[], const long &tickVolume[] // ) { // CalculateCycles( bar_index, prevCalculated, ratesTotal, open, high, close, low, tickVolume // ); } // void CalculateCycles( int bar_index, // Selected Bar Index const int prevCalculated, const int ratesTotal, const double &open[], const double &high[], const double &close[], const double &low[], const long &tickVolume[] // ) { // // CURRENT ... CalculateCycle( bar_index, prevCalculated, ratesTotal, open, high, close, low, tickVolume, // mCMarketLength, showCurrent, cBullBuffer, cBearBuffer // ); // // SHORT ... CalculateCycle( bar_index, prevCalculated, ratesTotal, open, high, close, low, tickVolume, // mSMarketLength, showShort, sBullBuffer, sBearBuffer // ); // // MEDIUM ... CalculateCycle( bar_index, prevCalculated, ratesTotal, open, high, close, low, tickVolume, // mMMarketLength, showMedium, mBullBuffer, mBearBuffer // ); // // LONG ... CalculateCycle( bar_index, prevCalculated, ratesTotal, open, high, close, low, tickVolume, // mLMarketLength, showLong, lBullBuffer, lBearBuffer // ); // // HIND ... CalculateCycle( bar_index, prevCalculated, ratesTotal, open, high, close, low, tickVolume, // mHMarketLength, showHind, hBullBuffer, hBearBuffer // ); } // void CalculateCycle( int bar_index, // Selected Bar Index const int prevCalculated, const int ratesTotal, const double &open[], const double &high[], const double &close[], const double &low[], const long &tickVolume[], // int _length, // Calculation Length bool _show, double &_bull[], double &_bear[] // ) { // double bullPower = 0; double bearPower = 0; // double lowShadow = 0; double highShadow = 0; // double bullPressure = 0; double bearPressure = 0; // double bullRange = 0; double bearRange = 0; // for (int i = bar_index; i < bar_index + _length; i++) { // bool isBullish = // close[i] > open[i] // ; // bool isBearish = // close[i] < open[i] // ; // double iUpper = MathMax(open[i], close[i]); double iLower = MathMin(open[i], close[i]); // double iBody = iUpper - iLower; double iRange = high[i] - low[i]; // double iHighShadow = high[i] - iUpper; double iLowShadow = iLower - low[i]; // if (isBullish) { // bullPower += iBody; bullRange += iRange; bearPressure += iHighShadow; } else if (isBearish) { // bearPower += iBody; bearRange += iRange; bullPressure += iLowShadow; } // lowShadow += iLowShadow; highShadow += iHighShadow; } // if (mode == XCHTD_POWER_MODE) { // _bull[bar_index] = bullPower; _bear[bar_index] = bearPower; } else if (mode == XCHTD_PRESSURE_MODE) { // _bull[bar_index] = bullPressure; _bear[bar_index] = bearPressure; } else if (mode == XCHTD_TYPE_MODE) { // _bull[bar_index] = bullRange; _bear[bar_index] = bearRange; } else if (mode == XCHTD_STRONG_MODE) { // _bull[bar_index] = bullPower + bullPressure; _bear[bar_index] = bearPower + bearPressure; } } //