/////////////////////////////////////////////////////// // // SaherElm IT Center MQL5 XCEHull Indicator // --------------------------------------------- // Name: XCEH // Description: Ce Hull Trend Indicator ... // // // 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 XCEH Indicator" #property strict // // START Constants ... // #define ShortName "XCEH" // // END Constants ... // // // START Inputs ... // input int atrLength = 22; // Period input double atrMultiplier = 3.0; // Multiplier input bool useClose = true; // Use Close Price for Extremums // // Includes Logging Library ... #include "../Libraries/x-saherelm.log.lib.mq5" // // Includes Common Library ... #include "../Libraries/x-saherelm.common.lib.mq5" // // Includes Draw Library ... #include "../Libraries/x-saherelm.draw.lib.mq5" // // START Buffers ... // #property indicator_chart_window // #property indicator_buffers 7 #property indicator_plots 1 // #define emaBufferIndex 0 #define atrBufferIndex 1 #define ceLongStopBufferIndex 2 #define ceShortStopBufferIndex 3 #define ceDirectionBufferIndex 4 #define ceLongBufferIndex 5 #define ceShortBufferIndex 6 // double emaBuffer[]; double atrBuffer[]; double ceLongStopBuffer[]; double ceShortStopBuffer[]; double ceDirectionBuffer[]; double ceLongBuffer[]; double ceShortBuffer[]; // #property indicator_label1 "XEMA" #property indicator_type1 DRAW_COLOR_LINE #property indicator_color1 clrBlue #property indicator_style1 STYLE_SOLID #property indicator_width1 1 // // END Buffers ... // // // START Global Definitions: Variables, Properties and etc ... // int atrHandler = INVALID_HANDLE; int maHandler = INVALID_HANDLE; double lastLongStopValue; double lastShortStopValue; // ulong signalsCount = 0; // // END Global Definitions: Variables, Properties and etc ... // // // START Event Handlers ... // // // Initialization ... int OnInit() { // // Validate Inputs ... if (!ValidateInputs()) { return INIT_PARAMETERS_INCORRECT; } // logTag = ShortName; drawPrefix = ShortName; // // Initializing ATR Handler ... atrHandler = iATR( _Symbol, _Period, atrLength); if (atrHandler == INVALID_HANDLE) { // string message = "Error Initializing ATR Handler ..."; LogMessage(message); // return INIT_FAILED; } // // Initializing MA Handler ... maHandler = iMA( _Symbol, _Period, atrLength, 0, MODE_EMA, PRICE_CLOSE); if (maHandler == INVALID_HANDLE) { // string message = "Error Initializing Ma Handler ..."; LogMessage(message); // return INIT_FAILED; } // // 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 // // Here we can handle De Initialization Reasons ... IndicatorRelease(maHandler); IndicatorRelease(atrHandler); // // De Init Draw Library ... OnDeinitDrawLibrary(); } // // Calculations ... // // Calculating what we want ... int OnCalculate( // // total Candles on chart ... const int rates_total, // // total calculated Candles on charts ... const int prev_calculated, // // history of Candles Open Time ... const datetime &time[], // // history of Candles Open Price ... const double &open[], // // history of Candles High Price ... const double &high[], // // history of Candles Low Price ... const double &low[], // // history of Candles Close Price ... const double &close[], // // history of Tick Volumes on Candle ... const long &tick_volume[], // // history of Trade Volumes ... const long &volume[], // // history of Candles Spread Price ... const int &spread[]) { // // 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 ... int maxLength = MathMax(atrLength, 1); // // Check Calculated Bars ... int atrCalculatedBars = BarsCalculated(atrHandler); int maCalculatedBars = BarsCalculated(maHandler); if ( maCalculatedBars < maxLength || atrCalculatedBars < maxLength ) { return prev_calculated; } // limit = (prev_calculated > rates_total || prev_calculated <= 0) ? rates_total : (rates_total - prev_calculated) + 1; // // Buffers Copy ... int copiedATRs = CopyBuffer(atrHandler, 0, 0, limit, atrBuffer); int copiedMAs = CopyBuffer(maHandler, 0, 0, limit, emaBuffer); if ( copiedMAs <= 0 || copiedATRs <= 0 ) { return prev_calculated; } // // Main Loop ... for (int i = limit - 1; i >= 0 && !IsStopped(); i--) { // // Do Buffers Calculation ... CalculateBuffers(i); } // return rates_total; } // // END Event Handlers ... // // // START Functions ... // // // Validate Input Args for Initialization ... bool ValidateInputs() { // bool result = false; // // Validate Args ... if (atrLength >= 2) { result = true; } // return result; } // // Define Indexes and Styles ... void DefineBuffers() { // // ATR Buffer ... ArraySetAsSeries(atrBuffer, true); SetIndexBuffer(atrBufferIndex, atrBuffer, INDICATOR_CALCULATIONS); // // CE Long Stop Buffer ... ArraySetAsSeries(ceLongStopBuffer, true); SetIndexBuffer(ceLongStopBufferIndex, ceLongStopBuffer, INDICATOR_CALCULATIONS); // // CE Short Stop Buffer ... ArraySetAsSeries(ceShortStopBuffer, true); SetIndexBuffer(ceShortStopBufferIndex, ceShortStopBuffer, INDICATOR_CALCULATIONS); // // CE Direction Buffer ... ArraySetAsSeries(ceDirectionBuffer, true); SetIndexBuffer(ceDirectionBufferIndex, ceDirectionBuffer, INDICATOR_CALCULATIONS); // // CE Long Buffer ... ArraySetAsSeries(ceLongBuffer, true); SetIndexBuffer(ceLongBufferIndex, ceLongBuffer, INDICATOR_CALCULATIONS); // // CE Short Buffer ... ArraySetAsSeries(ceShortBuffer, true); SetIndexBuffer(ceShortBufferIndex, ceShortBuffer, INDICATOR_CALCULATIONS); // // EMA Buffer ... // string emaBufferLabel = "EMA " + "(" + (string)atrLength + ")"; ArraySetAsSeries(emaBuffer, true); SetIndexBuffer(emaBufferIndex, emaBuffer, INDICATOR_DATA); PlotIndexSetString(emaBufferIndex, PLOT_LABEL, emaBufferLabel); } // // Set Indicator Short Name and also we can define Buffers Labels ... void SetIndicatorName() { // string indicatorShortName = ""; StringConcatenate(indicatorShortName, ShortName, " ", "(", atrLength, ")"); // IndicatorSetString(INDICATOR_SHORTNAME, indicatorShortName); } // // Calculate Buffers ... void CalculateBuffers(int bar_index) { // // Requirements ... XOHCL candle = GetCandle(bar_index); XOHCL prevCandle = GetCandle(bar_index + 1); // bool hasPrefredSize = ArraySize(ceDirectionBuffer) > bar_index + 1; // double atrValue = atrBuffer[bar_index] * atrMultiplier; // // Highest and Lowest Value ... double mHighValue = useClose ? GetHighestHigh(_Symbol, _Period, MODE_CLOSE, atrLength, bar_index) : GetHighestHigh(_Symbol, _Period, MODE_HIGH, atrLength, bar_index); double mLowValue = useClose ? GetLowestLow(_Symbol, _Period, MODE_CLOSE, atrLength, bar_index) : GetLowestLow(_Symbol, _Period, MODE_LOW, atrLength, bar_index); // // Calculate CE Long Stop ... double ceLongStopValue = mHighValue - atrValue; lastLongStopValue = hasPrefredSize && ceLongStopBuffer[bar_index + 1] > 0 ? ceLongStopBuffer[bar_index + 1] : ceLongStopValue; ceLongStopBuffer[bar_index] = prevCandle.close > lastLongStopValue ? MathMax(ceLongStopValue, lastLongStopValue) : ceLongStopValue; // // Calculate CE Short Stop ... double ceShortStopValue = mLowValue + atrValue; lastShortStopValue = hasPrefredSize && ceShortStopBuffer[bar_index + 1] > 0 ? ceShortStopBuffer[bar_index + 1] : ceShortStopValue; ceShortStopBuffer[bar_index] = prevCandle.close < lastShortStopValue ? MathMin(ceShortStopValue, lastShortStopValue) : ceShortStopValue; // // Calculate CE Direction ... double ceDirectionValue = 1; ceDirectionValue = candle.close > lastShortStopValue ? 1 : candle.close < lastLongStopValue ? -1 : ceDirectionValue; ceDirectionBuffer[bar_index] = ceDirectionValue; // // Calculate Signals ... bool hasLongSignal = hasPrefredSize ? ceDirectionValue == 1 && ceDirectionBuffer[bar_index + 1] == -1 : false; bool hasShortSignal = hasPrefredSize ? ceDirectionValue == -1 && ceDirectionBuffer[bar_index + 1] == 1 : false; // ceLongBuffer[bar_index] = hasLongSignal ? 1 : 0; ceShortBuffer[bar_index] = hasShortSignal ? 1 : 0; // if (bar_index > 400) { return; } // // Draw Long or Short Signals ... if (hasLongSignal || hasShortSignal) { // signalsCount++; // bool isLong = hasLongSignal; // string signalType = isLong ? "Long" : "Short"; // string signalName = signalType + "(" + (string) signalsCount + ")"; // LogMessage(signalName); // datetime signalTime = iTime( _Symbol, _Period, bar_index ); // string signalArrowName = signalName + "_A"; uchar signalArrowCode = isLong ? 225 : 226; ENUM_ARROW_ANCHOR signalArrowAnchor = isLong ? ANCHOR_BOTTOM : ANCHOR_TOP; color signalArrowColor = isLong ? clrAqua : clrFuchsia; double signalArrowPrice = isLong ? candle.low : candle.high; // if (IsDrawExists(signalArrowName)) { RemoveDraw(signalArrowName); } // bool isDrawn = DrawArrow( 0, signalArrowName, 0, signalTime, signalArrowPrice, signalArrowCode, signalArrowAnchor, signalArrowColor ); if (isDrawn) { // LogMessage(signalName + " Drawn ..."); } } } // // END Functions ... //