From 42d4f17288ed5c7d63a19a6db1f346adde40e9d6 Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Sat, 9 Mar 2024 18:05:51 +0330 Subject: [PATCH] add xce indicators ... --- Indicators/chandelier_exit.mq5 | Bin 0 -> 11578 bytes Indicators/x-saherelm.xcehull.indicator.mq5 | 497 ++++++++++++++++++++ 2 files changed, 497 insertions(+) create mode 100644 Indicators/chandelier_exit.mq5 create mode 100644 Indicators/x-saherelm.xcehull.indicator.mq5 diff --git a/Indicators/chandelier_exit.mq5 b/Indicators/chandelier_exit.mq5 new file mode 100644 index 0000000000000000000000000000000000000000..907a3efd9137f0dc1b8067a4c941f0016f3ba113 GIT binary patch literal 11578 zcmdT}{ZHFQ7=C}AwEw|vC6tU%Mu4_!3mspRkm!n_AF9w4Q4-n+0$G3pYxl2jd!BoJ zxwCyv?9^15qByqi?*0DGzyHk3yv)kn6nuV^ALN1bWgzFccJO;Cw-U*bdA`MSS3b%o zyp7D~zVzf&4yBE~XL4Wi&Y$AknViI@^enH*^T%={zXE;_5W9f;(BSP&1eY{kf`^}i{xd3ePbO8#;b04s}kinUuc>@wS#65SAGs-E#^#=EsF}2gLQ);9Q zijTm{C3qW13-{!VT5myKy5k&od3p`O_#jtuj1Q%90l7RWb-E9Uo&!Hm@>A|Kt(A7W z5TD+ZJz0}?;ATg*<#*XCMGLK-)C^*HM|c{@Cg68~Wgqr@E9-c7h37I7x&VK-IA`hH zm)G(h6mEgeO?icWBp{WFSVPoX}Ei8-@?m!-UxV9(ad$AI} z9i5|u^ zRNojWu0t-imROJj`9m7Ql{WBaS2n@dY3@q+OcqZw7mr=Z;_+9Kcy#5NvDE3Q31^4& ze1)BCK$f(_bJ(H!U9v9JcdTu@CUbY?FZ{ZS_!#HUU=8k#`d9Jm*C9RA(Fdt~tnI%7 zciZ?<0yU`{TFKj&A=Q4&O3N-?53{@J<3s2lhp-~llZ@>ox_R-{=8sc^Xdv{DH(1I3i_0>QhvMj%p(&}<7k-Maq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A6OB<7&!$+O%?Ir;!Ik=_S|Uii}do)`GUCUCD*ZIg?Hh)l2-VN>nSE>U2g<#_7!Z z{c4WX?RwZZ>}sCQ>^h~Mt&gqDG|y*s=ATvx<-sVTv;00`Wdv=L>#S+APZ2-UEKttR zcxIHJt_~!$j|KB_tsfaKcmz~`B}cs z=n12AX4k`hX5})<&j{Vc#;l(e*ICxjEdG$6*|j{Kr6Z>Q>`!^L_VQ;r)A?`BfA1-+ A=Kufz literal 0 HcmV?d00001 diff --git a/Indicators/x-saherelm.xcehull.indicator.mq5 b/Indicators/x-saherelm.xcehull.indicator.mq5 new file mode 100644 index 00000000..ff6408ab --- /dev/null +++ b/Indicators/x-saherelm.xcehull.indicator.mq5 @@ -0,0 +1,497 @@ +/////////////////////////////////////////////////////// +// +// 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 ... +//