/////////////////////////////////////////////////////// // // SaherElm IT Center MQL5 Class Library // -------------------------------------- // Name: XChartObjects // Description: provides all require Chart Objects ... // // // Maintainer: // ------------ // Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com) // ////////////////////////////////////////////////////// // // Global Properties ... #property library #property copyright "Copyright 2023, SaherElm IT Center" #property link "https://www.saherelm.ir" #property version "1.00" #property strict // #include "../Libraries/x-saherelm.x-poi.lib.mq5" #include "../Libraries/x-saherelm.x-trade.lib.mq5" // #include #include #include #include #include #include // // Definitions ... enum ENUM_XCHARTOBJECTS { // // Global ... X_BOX_OBJ = 7861, X_BAR_ARROW_OBJ = 7862, // X_SIGNAL_OBJ = 8860, X_OHCL_OBJ = 8863, X_RR_OBJ = 8864, // // 9860 Custom Object ... }; enum ENUM_X_CHART_IDS { X_AT, // AT X_TO, // TO X_FROM, // FROM X_UPPER, // UPPER X_LOWER, // LOWER X_ENTRY, // ENTRY X_TARGET, // TARGET X_SL, // SL X_TP, // TP X_BAR_H_SHW, // HSHW X_BAR_BDY, // BODY X_BAR_L_SHW, // LSHW }; string ToString(ENUM_X_CHART_IDS value) { // string result = NULL; // result = EnumToString(value); // return result; } struct XPOIStyle { // // Properties ... int width; color clr; ENUM_LINE_STYLE style; // // Constructor ... XPOIStyle() { Default(); } // // Tools ... // // Cleanup Model ... void Clean() { // width = 1; clr = CLR_NONE; style = STYLE_SOLID; // ZeroMemory(this); } // // Default Props ... void Default() { // width = 1; clr = CLR_NONE; style = STYLE_SOLID; } }; // // Extensions ... /** * Converts a XBoxZone struct to XSignal instance ... * * @param box: XBoxZone instance reference, provide source info ... * @param signal: XSignal instance reference, holds prepared converted signal ... * * @return ( bool ) */ bool ToSignal( XBoxZone &box, XSignal &signal, double &rewardRatios[], double additionalSL = 0 // ) { // bool result = false; // signal.Clean(); // result = box.IsValid() && HasChild(rewardRatios); if (!result) { return result; } // XOHCL entryBar; int entryIDX = box.ToIndex(); if (entryIDX > 0) { entryIDX--; } result = entryBar.Init( box.symbol, box.period, entryIDX // ); if (!result) { // entryBar.Clean(); // return result; } // bool isBullish = box.IsBullish(); bool isBearish = box.IsBearish(); // ENUM_POSITION_TYPE iType = isBullish ? POSITION_TYPE_BUY : POSITION_TYPE_SELL; ENUM_X_POSITION_TYPES iXType = isBullish ? X_POSITION_TYPE_LONG : X_POSITION_TYPE_SHORT; // double points = GetPoints(box.symbol); // double entry = isBullish ? box.upper : box.lower; entry = entryBar.open; // double sl = isBullish ? box.lower : box.upper; if (additionalSL > 0) { // sl = isBullish ? sl - additionalSL : sl + additionalSL; } // double risk = MathAbs(entry - sl); // double tp = 0; // // Calculate Target ... XTarget targets[]; // double tmp[]; int idx = -1; XTarget iTarget; bool has = false; double iRatio = 0; double iReward = 0; double iRewardPrice = 0; Copy( rewardRatios, tmp // ); while (HasChild(tmp)) { // idx = -1; iRatio = GetMaxWidthIndex( tmp, idx // ); // has = IsValidIndex(idx); if (!has) { break; } // has = iRatio > 0; // ArrayRemove( tmp, idx, 1 // ); // bool canSetTP = tp <= 0; // iReward = iRatio * risk; // iRewardPrice = isBullish ? entry + iReward : entry - iReward; if (canSetTP) { tp = iRewardPrice; } else { // iTarget.target = iRewardPrice; AddRef( iTarget, targets // ); // iTarget.Clean(); } } Clean(tmp); // // Filling Signal ... signal.tp = tp; signal.sl = sl; signal.type = iType; signal.comment = ""; signal.entry = entry; signal.time = box.to; signal.volume = 0.01; signal.provider = box.type; signal.symbol = box.symbol; signal.period = box.period; signal.mode = X_ORDER_MODE_MARKET; // if (HasChild(targets)) { // Copy( targets, signal.targets // ); } // result = signal.IsValid(); // // Cleanup Resources ... // if (!result) { signal.Clean(); } // Clean(tmp); Clean(targets); iTarget.Clean(); entryBar.Clean(); // return result; } // // Implementation ... // // Global Chart Objects ... // // Bas Chart Object ... class XCBaseObject : public CChartObject { // public: // // // Getter(s) / Setter(s) ... /** * Get Object Specified Name ... * * @return ( string ) */ string ObjName() { return mObjName; } /** * Set Object Specified Name ... * * @param value: String ... */ void ObjName(string value) { mObjName = value; } // private: // // // Props ... string mObjName; // Object Specified Name ... // }; // // Bar Arrow Object ... class XCBarArrowObject : public XCBaseObject { // public: // /** * Creator ... * * @param chart_id: Long, Specified Chart ID ... * @param name: String, Specified Object ID ... * @param window: Integer, Specified Chart Window ID ... * @param swing: XCSwing instance ... * * @return ( bool ) */ bool CreateByBar( long chart_id, string name, int window, int arrow, XOHCL &bar, ENUM_X_PRICE priceType, double threshold = 5 // ) { // bool result = false; // result = // bar.IsValid() && IsValid(name) // ; if (!result) { return result; } // datetime time = bar.time; double price = bar.GetPrice(priceType); // if (threshold > 0) { // double thresholdPrice = threshold * GetPoints(bar.symbol); if (priceType == X_PRICE_HIGH) { price += thresholdPrice; } else if (priceType == X_PRICE_LOW) { price -= thresholdPrice; } } // result = mArrow.Create( chart_id, name, window, time, price, (char)arrow // ); // if (result) { ObjName(name); } // return result; } // // Virtual ... // // Setter(s) ... /** * Set Arrow Anchor ... * * @param value: ENUM_ARROW_ANCHOR member ... */ void ArrowAnchor(ENUM_ARROW_ANCHOR value) { mArrow.Anchor(value); } /** * Set Arrow Color ... * * @param value: Color ... */ void ArrowColor(color value) { mArrow.Color(value); } /** * Set Arrow Width ... * * @param value: Integer ... */ void ArrowWidth(int value) { mArrow.Width(value); } // private: // CChartObjectArrow mArrow; // }; // // Signal ... class XCSignalObject : public XCBaseObject { // public: // // // Constructor ... void XCSignalObject() { } // // Deconstructor ... void ~XCSignalObject() { } /** * Create a Signal Object on Chart ... * * @param chart_id: Long ... * @param name: String ... * @param window: Integer ... * @param signal: XSignal instance ... * @param length: int, Specified Length of Signal ... * * @return ( bool ) */ bool Create( long chart_id, const int window, XSignal &signal, int length = 3, string prefix = NULL // ) { // bool result = false; // length = NormalizeInt(length, 3); // result = signal.IsValid(); if (!result) { return result; } // string name = signal.GetTag(); if (IsValid(prefix)) { name = prefix + "_" + name; } string nameMD5 = ToMD5(name); // int delay = (PeriodSeconds(signal.period) * length); datetime time1 = (datetime)((int)signal.time - delay); datetime time2 = (datetime)((int)signal.time + delay); // // TP ... string tpName = ToString(X_TP) + "_" + nameMD5; result = mTPTrend.Create( chart_id, tpName, window, time1, signal.tp, time2, signal.tp // ); if (!result) { return result; } // // SL ... string slName = ToString(X_SL) + "_" + nameMD5; result = mSLTrend.Create( chart_id, slName, window, time1, signal.sl, time2, signal.sl // ); if (!result) { // mTPTrend.Delete(); return result; } // // TARGET ... bool hasTargets = HasChild(signal.targets); if (hasTargets) { // int targetsCount = ArraySize(signal.targets); for (int i = 0; i < targetsCount; i++) { // string targetName = ToString(X_TARGET) + "_" + ToXString(i) + "_" + nameMD5; CChartObjectTrend *mTargetTrend; mTargetTrend = new CChartObjectTrend(); result = mTargetTrend.Create( chart_id, targetName, window, time1, signal.targets[i].target, time2, signal.targets[i].target // ); if (!result) { // mTPTrend.Delete(); mSLTrend.Delete(); break; } // mTargetTrends.Add(mTargetTrend); } } // // ENTRY ... string entrName = ToString(X_ENTRY) + "_" + nameMD5; result = mEntryTrend.Create( chart_id, entrName, window, time1, signal.entry, time2, signal.entry // ); if (!result) { // mTPTrend.Delete(); mSLTrend.Delete(); mTargetTrends.Clear(); return result; } // // Set Object Name ... ObjName(name); // return result; } // // Setter(s) ... // // TP ... /** * Set TP Width ... * * @param value: Integer ... */ void TPWidth(int value) { // if (value < 1) { value = 1; } // mTPTrend.Width(value); } /** * Set TP Color ... * * @param value: Color ... */ void TPColor(color value) { mTPTrend.Color(value); } /** * Set TP Style ... * * @param value: ENUM_LINE_STYLE ... */ void TPStyle(ENUM_LINE_STYLE value) { mTPTrend.Style(value); } // // SL ... /** * Set SL Width ... * * @param value: Integer ... */ void SLWidth(int value) { // if (value < 1) { value = 1; } // mSLTrend.Width(value); } /** * Set SL Color ... * * @param value: Color ... */ void SLColor(color value) { mSLTrend.Color(value); } /** * Set SL Style ... * * @param value: ENUM_LINE_STYLE ... */ void SLStyle(ENUM_LINE_STYLE value) { mSLTrend.Style(value); } // // TARGET ... /** * Set Target Width ... * * @param value: Integer ... */ void TargetWidth(int value) { // if (value < 1) { value = 1; } // for (int i = 0; i < mTargetTrends.Total(); i++) { // CChartObjectTrend *iTrend = (CChartObjectTrend *)mTargetTrends.At(i); iTrend.Width(value); } } /** * Set Target Color ... * * @param value: Color ... */ void TargetColor(color value) { // for (int i = 0; i < mTargetTrends.Total(); i++) { // CChartObjectTrend *iTrend = (CChartObjectTrend *)mTargetTrends.At(i); iTrend.Color(value); } } /** * Set Target Style ... * * @param value: ENUM_LINE_STYLE ... */ void TargetStyle(ENUM_LINE_STYLE value) { // for (int i = 0; i < mTargetTrends.Total(); i++) { // CChartObjectTrend *iTrend = (CChartObjectTrend *)mTargetTrends.At(i); iTrend.Style(value); } } // // ENTRY ... /** * Set Entry Width ... * * @param value: Integer ... */ void EntryWidth(int value) { // if (value < 1) { value = 1; } // mEntryTrend.Width(value); } /** * Set Entry Color ... * * @param value: Color ... */ void EntryColor(color value) { mEntryTrend.Color(value); } /** * Set Entry Style ... * * @param value: ENUM_LINE_STYLE ... */ void EntryStyle(ENUM_LINE_STYLE value) { mEntryTrend.Style(value); } /** * Retrieve Object Type ... * * @return ( ENUM_XCHARTOBJECTS ) */ virtual ENUM_XCHARTOBJECTS Type() { return X_SIGNAL_OBJ; } // private: // // CChartObjectTrend mTPTrend; CChartObjectTrend mSLTrend; CChartObjectTrend mEntryTrend; CArrayObj mTargetTrends; }; // // XRR Object ... class XCRRObject : public XCSignalObject { // // Public ... public: // // Constructor ... bool CreateBoxRR( long chart_id, const int window, XBoxZone &box, double &rewardRatios[], int length = 3, string prefix = NULL // ) { // bool result = false; // length = NormalizeInt(length, 3); // result = box.IsValid(); if (!result) { return result; } // XSignal signal; result = ToSignal( box, signal, rewardRatios // ); if (!result) { // signal.Clean(); // return result; } // result = Create( chart_id, window, signal, length, prefix // ); // signal.Clean(); // return result; } }; // // OHCL, Bar Object ... class XCOHCLObject : public XCBaseObject { // public: // /** * Creator ... * * @param chart_id: Long, Specified Chart ID ... * @param window: Integer, Specified Chart Window ID ... * @param pivot: XPVPivot instance ... * @param prefix: String ... * * @return ( bool ) */ bool Create( long chart_id, int window, XOHCL &bar, datetime to, string prefix = NULL // ) { // bool result = false; // result = bar.IsValid() && IsValid(to); if (!result) { return result; } // string name = bar.GetTag(); if (IsValid(prefix)) { name = prefix + "_" + name; } string nameMD5 = ToMD5(name); // // High Shadow ... string hShadowName = ToString(X_BAR_H_SHW) + "_" + nameMD5; // result = mHighShadowRect.Create( chart_id, hShadowName, window, bar.time, bar.high, to, bar.GetUp() // ); if (!result) { return result; } // // Body ... string bodyName = ToString(X_BAR_BDY) + "_" + nameMD5; // result = mBodyRect.Create( chart_id, bodyName, window, bar.time, bar.GetUp(), to, bar.GetDown() // ); if (!result) { // mHighShadowRect.Delete(); mHighShadowRect.Detach(); return result; } // // Low Shadow ... string lShadowName = ToString(X_BAR_L_SHW) + "_" + nameMD5; // result = mLowShadowRect.Create( chart_id, lShadowName, window, bar.time, bar.GetDown(), to, bar.low // ); if (!result) { // mBodyRect.Delete(); mBodyRect.Detach(); mHighShadowRect.Delete(); mHighShadowRect.Detach(); return result; } // if (result) { ObjName(name); } // return result; } // // Getter(s) / Setter(s) ... /** * Set High Shadow Width ... * * @param value: Integer ... */ void HighShadowWidth(int value) { // if (value < 1) { value = 1; } // mHighShadowRect.Width(value); } /** * Set High Shadow Color ... * * @param value: Color ... */ void HighShadowColor(color value) { mHighShadowRect.Color(value); } /** * Set High Shadow Style ... * * @param value: ENUM_LINE_STYLE member ... */ void HighShadowStyle(ENUM_LINE_STYLE value) { mHighShadowRect.Style(value); } /** * Set High Shadow Fill ... * * @param value: Boolean ... */ void HighShadowFill(bool value) { mHighShadowRect.Fill(value); } /** * Set Body Width ... * * @param value: Integer ... */ void BodyWidth(int value) { // if (value < 1) { value = 1; } // mBodyRect.Width(value); } /** * Set Body Color ... * * @param value: Color ... */ void BodyColor(color value) { mBodyRect.Color(value); } /** * Set Body Style ... * * @param value: ENUM_LINE_STYLE member ... */ void BodyStyle(ENUM_LINE_STYLE value) { mBodyRect.Style(value); } /** * Set Body Fill ... * * @param value: Boolean ... */ void BodyFill(bool value) { mBodyRect.Fill(value); } /** * Set Low Shadow Width ... * * @param value: Integer ... */ void LowShadowWidth(int value) { // if (value < 1) { value = 1; } // mLowShadowRect.Width(value); } /** * Set Low Shadow Color ... * * @param value: Color ... */ void LowShadowColor(color value) { mLowShadowRect.Color(value); } /** * Set Low Shadow Style ... * * @param value: ENUM_LINE_STYLE member ... */ void LowShadowStyle(ENUM_LINE_STYLE value) { mLowShadowRect.Style(value); } /** * Set Low Shadow Fill ... * * @param value: Boolean ... */ void LowShadowFill(bool value) { mLowShadowRect.Fill(value); } void BarSpes( int width = 1, color clr = clrNONE, ENUM_LINE_STYLE style = STYLE_SOLID, bool fill = false // ) { // BodyWidth(width); LowShadowWidth(width); HighShadowWidth(width); // BodyColor(clr); LowShadowColor(clr); HighShadowColor(clr); // BodyStyle(style); LowShadowStyle(style); HighShadowStyle(style); // BodyFill(fill); LowShadowFill(fill); HighShadowFill(fill); } // // Virtual ... /** * Retrieve Object Type ... * * @return ( ENUM_XCHARTOBJECTS ) */ virtual ENUM_XCHARTOBJECTS Type() { return X_OHCL_OBJ; } // private: // // // Props ... CChartObjectRectangle mHighShadowRect; CChartObjectRectangle mBodyRect; CChartObjectRectangle mLowShadowRect; }; // // Box Object ...k class XCBoxObject : public XCBaseObject { // public: // /** * Creator ... * * @param chart_id: Long, Specified Chart ID ... * @param window: Integer, Specified Chart Window ID ... * @param pivot: XPVPivot instance ... * @param prefix: String ... * * @return ( bool ) */ bool Create( long chart_id, int window, string name, double upper, double lower, datetime from, datetime to, datetime at = NULL, string prefix = NULL // ) { // bool result = false; // result = upper > 0 && lower > 0 && to > from && IsValid(to) && IsValid(name) && IsValid(from) && upper > lower; if (!result) { return result; } // if (IsValid(prefix)) { name = prefix + "_" + name; } string nameMD5 = ToMD5(name); // result = mRect.Create( chart_id, name, window, from, upper, to, lower // ); if (!result) { return result; } // if (result) { // if (IsValid(at)) { // result = mAt.Create( chart_id, name + ToString(X_AT), window, at // ); } // if (!result) { // mAt.Detach(); mAt.Delete(); // mRect.Detach(); mRect.Delete(); } } // if (result) { ObjName(name); } // return result; } // // Getter(s) / Setter(s) ... /** * Set Width ... * * @param value: Integer ... */ void BoxWidth(int value) { // if (value < 1) { value = 1; } // mAt.Width(value); mRect.Width(value); } /** * Set Color ... * * @param value: Color ... */ void BoxColor(color value) { // mAt.Color(value); mRect.Color(value); } /** * Set Style ... * * @param value: ENUM_LINE_STYLE member ... */ void BoxStyle(ENUM_LINE_STYLE value) { // mAt.Style(value); mRect.Style(value); } /** * Set Fill ... * * @param value: Boolean ... */ void BoxFill(bool value) { mRect.Fill(value); } // // Virtual ... /** * Retrieve Object Type ... * * @return ( ENUM_XCHARTOBJECTS ) */ virtual ENUM_XCHARTOBJECTS Type() { return X_BOX_OBJ; } // private: // // // Props ... CChartObjectVLine mAt; CChartObjectRectangle mRect; }; //