From 8c6d88d49fe9c127b5cb43979b37505278e8b16c Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Thu, 30 May 2024 14:16:04 +0330 Subject: [PATCH] Add also Support Opposit Signal Generating by Position to XSignal struct ... --- Classes/x-saherelm.x121.provider.class.mq5 | 10 - Libraries/x-saherelm.xtrade.lib.mq5 | 807 +++++++++++---------- 2 files changed, 429 insertions(+), 388 deletions(-) diff --git a/Classes/x-saherelm.x121.provider.class.mq5 b/Classes/x-saherelm.x121.provider.class.mq5 index a3ef93c7..b801430c 100644 --- a/Classes/x-saherelm.x121.provider.class.mq5 +++ b/Classes/x-saherelm.x121.provider.class.mq5 @@ -2980,17 +2980,7 @@ public: // string symbol = GetSymbol(); ENUM_TIMEFRAMES period = GetPeriod(); - double tpPoint = 60; - double slPoint = 30; double volume = GetStaticVolume(); // * signalPusher; - double tpPrice = PointToPrice( - tpPoint, - symbol // - ); - double slPrice = PointToPrice( - slPoint, - symbol // - ); ENUM_POSITION_TYPE type = hasLong ? POSITION_TYPE_BUY : POSITION_TYPE_SELL; diff --git a/Libraries/x-saherelm.xtrade.lib.mq5 b/Libraries/x-saherelm.xtrade.lib.mq5 index 30f06ffd..4d82435a 100644 --- a/Libraries/x-saherelm.xtrade.lib.mq5 +++ b/Libraries/x-saherelm.xtrade.lib.mq5 @@ -126,6 +126,384 @@ enum ENUM_X_GUARD_ACTIONS // // Structs ... +// +// Model an Open Position ... +struct XPosition +{ + // + // Props ... + + // + // Magic Number ... + ulong magic; + + // + // Ticket ID ... + ulong ticket; + + // + // Tradinng Symbol ... + string symbol; + + // + // Trading Period ... + ENUM_TIMEFRAMES period; + + // + // Position Type ... + ENUM_POSITION_TYPE type; + + // + // Take Profit ... + double tp; + + // + // Stop Loss ... + double sl; + + // + // Position Current Profit ... + double profit; + + // + // Position Open Price ... + double entry; + + // + // Current Symbol Price ... + double price; + + // + // Position Swap ... + double swap; + + // + // Position Commission ... + double commission; + + // + // Volume ... + double volume; + + // + // Position Open Time ... + datetime openAt; + + // + // Sifnal Provider ... + string provider; + + // + // Position Comment ... + string comment; + + // + // Constructor ... + XPosition() + { + Clean(); + } + + // + // Initializers ... + + bool ByIndex(int index) + { + // + bool result = false; + + // + static CPositionInfo mPositionInfo; + + // + result = mPositionInfo.SelectByIndex(index); + if (!result) + { + return result; + } + + // + ulong mTicket = mPositionInfo.Ticket(); + + // + result = ByTicket(mTicket); + + // + return result; + } + + bool ByTicket(ulong mTicket) + { + // + bool result = false; + + // + static CPositionInfo mPositionInfo; + + // + result = mPositionInfo.SelectByTicket(mTicket); + if (!result) + { + return result; + } + + // + swap = mPositionInfo.Swap(); + magic = mPositionInfo.Magic(); + openAt = mPositionInfo.Time(); + sl = mPositionInfo.StopLoss(); + ticket = mPositionInfo.Ticket(); + symbol = mPositionInfo.Symbol(); + tp = mPositionInfo.TakeProfit(); + profit = mPositionInfo.Profit(); + volume = mPositionInfo.Volume(); + comment = mPositionInfo.Comment(); + entry = mPositionInfo.PriceOpen(); + type = mPositionInfo.PositionType(); + price = mPositionInfo.PriceCurrent(); + commission = mPositionInfo.Commission(); + + // + period = ExtractPeriod(comment); + provider = ExtractProvider(comment); + + // + return result; + } + + // + // Tools ... + + // + // Cleanup ... + void Clean() + { + magic = 0; + ticket = 0; + tp = 0; + sl = 0; + swap = 0; + entry = 0; + price = 0; + profit = 0; + volume = 0; + openAt = 0; + commission = 0; + + // + type = NULL; + period = NULL; + + // + symbol = NULL; + comment = NULL; + provider = NULL; + } + + // + // Validate ... + bool IsValid() + { + // + bool result = false; + + // + result = + // + IsValid(symbol) && + NotEmpty(ticket) + // + ; + + // + return result; + } + + // + // Retrieve Entry Price ... + double GetEntry() + { + // + double result = 0; + + // + if (!IsValid()) + { + return result; + } + + // + result = GetEntry( + symbol, + type + // + ); + + // + return result; + } + + // + // Retrieve Exit ... + double GetExit() + { + // + double result = 0; + + // + if (!IsValid()) + { + return result; + } + + // + result = GetExit( + symbol, + type + // + ); + + // + return result; + } + + // + // Get Spread by Point ... + double GetSpread() + { + // + double result = 0; + + // + if (!IsValid()) + { + return result; + } + + // + result = PriceToPoint(GetSpread(symbol)); + + // + return result; + } + + // + // Calculate Age ... + int GetAge( + ENUM_TIMEFRAMES mPeriod // Calculate Age based on this period + ) + { + return GetAge(this, mPeriod); + } + + // + // Check this instance is Same os Given instance or not ... + bool IsSameAs( + const XPosition &value // instance for Compare + ) + { + // + bool result = false; + + // + result = + // + type == value.type && + magic == value.magic && + entry == value.entry && + ticket == value.ticket && + volume == value.volume && + symbol == value.symbol && + provider == value.provider + // + ; + + // + return result; + } + + // + // Find index in a Collection ... + int FindIndex( + const XPosition &values[] // Collection to Find + ) + { + // + return FindIndex( + this, + values + // + ); + } + + // + // Check Items Passed Specific Filters or not ... + bool IsFiltersPassed( + string mSymbol = NULL, // Trading Symbol + string mProvider = NULL, // Signal Provider + ENUM_TIMEFRAMES mPeriod = NULL, // Trading Timeframe + ENUM_POSITION_TYPE mType = NULL, // Long (Buy), Short (Sell) + ulong mMagic = NULL, // Magic Number + bool mFilterByMagic = true // Get Only Self Open Positions + ) + { + // + bool result = false; + + // + result = + // + // Magic Filter ... + IsMagicFilterPassed( + mMagic, + mFilterByMagic, + this + // + ) + // + && + // + // Symbol Filter ... + IsSymbolFilterPassed( + mSymbol, + this + // + ) + // + && + // + // Provider Filter ... + IsProviderFilterPassed( + mProvider, + this + // + ) + // + && + // + // Period Filter ... + IsPeriodFilterPassed( + mPeriod, + this + // + ) + // + && + // + // Type Filter ... + IsTypeFilterPassed( + mType, + this + // + ) + // + ; + + // + return result; + } +}; + // // Model a Signal ... struct XSignal @@ -704,6 +1082,57 @@ struct XSignal return result; } + // + // Prepare Opposit Signal based On a Position ... + bool PrepareOpposit( + XPosition &source // Source Data + ) + { + // + bool result = false; + + // + ENUM_POSITION_TYPE mType = GetOpposit(source.type); + double mEntry = GetEntry(source.symbol, mType); + + // + bool isLong = IsLong(mType); + + // + double risk = MathAbs(source.entry - source.sl); + double reward = MathAbs(source.tp - source.entry); + + // + double mSL = isLong + ? mEntry - risk + : mEntry + risk; + + // + double mTP = isLong + ? mEntry + reward + : mEntry - reward; + + // + return Prepare( + source.symbol, + source.provider, + source.period, + mType, + X_ORDER_MODE_MARKET, + mEntry, + source.volume, + X_SIGNAL_USE_NOTHING, + mSL, + mTP // + ); + + // + result = IsValid(); + + // + return result; + } + // // Cleanup ... void Clean() @@ -1269,384 +1698,6 @@ struct XSignal } }; -// -// Model an Open Position ... -struct XPosition -{ - // - // Props ... - - // - // Magic Number ... - ulong magic; - - // - // Ticket ID ... - ulong ticket; - - // - // Tradinng Symbol ... - string symbol; - - // - // Trading Period ... - ENUM_TIMEFRAMES period; - - // - // Position Type ... - ENUM_POSITION_TYPE type; - - // - // Take Profit ... - double tp; - - // - // Stop Loss ... - double sl; - - // - // Position Current Profit ... - double profit; - - // - // Position Open Price ... - double entry; - - // - // Current Symbol Price ... - double price; - - // - // Position Swap ... - double swap; - - // - // Position Commission ... - double commission; - - // - // Volume ... - double volume; - - // - // Position Open Time ... - datetime openAt; - - // - // Sifnal Provider ... - string provider; - - // - // Position Comment ... - string comment; - - // - // Constructor ... - XPosition() - { - Clean(); - } - - // - // Initializers ... - - bool ByIndex(int index) - { - // - bool result = false; - - // - static CPositionInfo mPositionInfo; - - // - result = mPositionInfo.SelectByIndex(index); - if (!result) - { - return result; - } - - // - ulong mTicket = mPositionInfo.Ticket(); - - // - result = ByTicket(mTicket); - - // - return result; - } - - bool ByTicket(ulong mTicket) - { - // - bool result = false; - - // - static CPositionInfo mPositionInfo; - - // - result = mPositionInfo.SelectByTicket(mTicket); - if (!result) - { - return result; - } - - // - swap = mPositionInfo.Swap(); - magic = mPositionInfo.Magic(); - openAt = mPositionInfo.Time(); - sl = mPositionInfo.StopLoss(); - ticket = mPositionInfo.Ticket(); - symbol = mPositionInfo.Symbol(); - tp = mPositionInfo.TakeProfit(); - profit = mPositionInfo.Profit(); - volume = mPositionInfo.Volume(); - comment = mPositionInfo.Comment(); - entry = mPositionInfo.PriceOpen(); - type = mPositionInfo.PositionType(); - price = mPositionInfo.PriceCurrent(); - commission = mPositionInfo.Commission(); - - // - period = ExtractPeriod(comment); - provider = ExtractProvider(comment); - - // - return result; - } - - // - // Tools ... - - // - // Cleanup ... - void Clean() - { - magic = 0; - ticket = 0; - tp = 0; - sl = 0; - swap = 0; - entry = 0; - price = 0; - profit = 0; - volume = 0; - openAt = 0; - commission = 0; - - // - type = NULL; - period = NULL; - - // - symbol = NULL; - comment = NULL; - provider = NULL; - } - - // - // Validate ... - bool IsValid() - { - // - bool result = false; - - // - result = - // - IsValid(symbol) && - NotEmpty(ticket) - // - ; - - // - return result; - } - - // - // Retrieve Entry Price ... - double GetEntry() - { - // - double result = 0; - - // - if (!IsValid()) - { - return result; - } - - // - result = GetEntry( - symbol, - type - // - ); - - // - return result; - } - - // - // Retrieve Exit ... - double GetExit() - { - // - double result = 0; - - // - if (!IsValid()) - { - return result; - } - - // - result = GetExit( - symbol, - type - // - ); - - // - return result; - } - - // - // Get Spread by Point ... - double GetSpread() - { - // - double result = 0; - - // - if (!IsValid()) - { - return result; - } - - // - result = PriceToPoint(GetSpread(symbol)); - - // - return result; - } - - // - // Calculate Age ... - int GetAge( - ENUM_TIMEFRAMES mPeriod // Calculate Age based on this period - ) - { - return GetAge(this, mPeriod); - } - - // - // Check this instance is Same os Given instance or not ... - bool IsSameAs( - const XPosition &value // instance for Compare - ) - { - // - bool result = false; - - // - result = - // - type == value.type && - magic == value.magic && - entry == value.entry && - ticket == value.ticket && - volume == value.volume && - symbol == value.symbol && - provider == value.provider - // - ; - - // - return result; - } - - // - // Find index in a Collection ... - int FindIndex( - const XPosition &values[] // Collection to Find - ) - { - // - return FindIndex( - this, - values - // - ); - } - - // - // Check Items Passed Specific Filters or not ... - bool IsFiltersPassed( - string mSymbol = NULL, // Trading Symbol - string mProvider = NULL, // Signal Provider - ENUM_TIMEFRAMES mPeriod = NULL, // Trading Timeframe - ENUM_POSITION_TYPE mType = NULL, // Long (Buy), Short (Sell) - ulong mMagic = NULL, // Magic Number - bool mFilterByMagic = true // Get Only Self Open Positions - ) - { - // - bool result = false; - - // - result = - // - // Magic Filter ... - IsMagicFilterPassed( - mMagic, - mFilterByMagic, - this - // - ) - // - && - // - // Symbol Filter ... - IsSymbolFilterPassed( - mSymbol, - this - // - ) - // - && - // - // Provider Filter ... - IsProviderFilterPassed( - mProvider, - this - // - ) - // - && - // - // Period Filter ... - IsPeriodFilterPassed( - mPeriod, - this - // - ) - // - && - // - // Type Filter ... - IsTypeFilterPassed( - mType, - this - // - ) - // - ; - - // - return result; - } -}; - // // Model an Order ... struct XOrder