last ...
This commit is contained in:
@@ -0,0 +1,167 @@
|
||||
///////////////////////////////////////////////////////
|
||||
//
|
||||
// SaherElm IT Center MQL5 Indicator
|
||||
// -------------------------------------------------
|
||||
// Name: XCXCAEADataCollector ...
|
||||
// Description: Class for XCAEA Data Collector ...
|
||||
//
|
||||
//
|
||||
// Maintainer:
|
||||
// ------------
|
||||
// Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com)
|
||||
//
|
||||
//////////////////////////////////////////////////////
|
||||
//
|
||||
// Global Properties ...
|
||||
#property library
|
||||
#property copyright "Copyright 2023, SaherElm IT Center"
|
||||
#property link "https://saherelm.ir"
|
||||
#property version "1.00"
|
||||
#property description "SaherElm Class for XCAEA Data Collector"
|
||||
#property strict
|
||||
|
||||
//
|
||||
// Imports ...
|
||||
#include "../../Classes/x-saherelm.base.class.mq5"
|
||||
|
||||
//
|
||||
// Definitions ...
|
||||
|
||||
//
|
||||
// Implementation ...
|
||||
class XCXCAEADataCollector : public XCBase
|
||||
{
|
||||
//
|
||||
// Public ...
|
||||
public:
|
||||
//
|
||||
// Constructors ...
|
||||
XCXCAEADataCollector()
|
||||
{
|
||||
}
|
||||
|
||||
//
|
||||
// Deconstructor ...
|
||||
~XCXCAEADataCollector()
|
||||
{
|
||||
}
|
||||
|
||||
//
|
||||
// Properties ...
|
||||
|
||||
//
|
||||
string Path()
|
||||
{
|
||||
return mPath;
|
||||
}
|
||||
|
||||
//
|
||||
void Path(string value)
|
||||
{
|
||||
//
|
||||
// Normalizing Collector Path ...
|
||||
if (IsValid(value))
|
||||
{
|
||||
mPath = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
mPath = "XCXCAEADataCollector";
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Tools ...
|
||||
|
||||
//
|
||||
string GetFilePath(string fileName)
|
||||
{
|
||||
//
|
||||
string result = "";
|
||||
|
||||
//
|
||||
result =
|
||||
//
|
||||
mPath + "\\" + fileName + ".x121.log"
|
||||
//
|
||||
;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
int GetFileHandlerForWrite(string filePath)
|
||||
{
|
||||
//
|
||||
int result = INVALID_HANDLE;
|
||||
|
||||
//
|
||||
if (!IsValid(filePath))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
result = FileOpen(
|
||||
filePath,
|
||||
FILE_READ | FILE_WRITE | FILE_TXT //
|
||||
);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save Specified Content into Specified File Name ...
|
||||
*
|
||||
* @param fileName: string, file name ...
|
||||
* @param content: string, content ...
|
||||
*
|
||||
* @return ( bool )
|
||||
*/
|
||||
bool Save(
|
||||
string fileName,
|
||||
string content //
|
||||
)
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
result = IsValid(content);
|
||||
if (!result)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
//
|
||||
int mHandler = GetFileHandlerForWrite(fileName);
|
||||
result = mHandler != INVALID_HANDLE;
|
||||
if (!result)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
FileWrite(mHandler, content);
|
||||
FileFlush(mHandler);
|
||||
FileClose(mHandler);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Protected ...
|
||||
protected:
|
||||
//
|
||||
// Private ...
|
||||
private:
|
||||
//
|
||||
// Props ...
|
||||
|
||||
//
|
||||
string mPath; // Base Collector Path ...
|
||||
};
|
||||
@@ -24,6 +24,7 @@
|
||||
#include "../../Classes/x-saherelm.x-alert.class.mq5"
|
||||
#include "../../Classes/x-saherelm.x-poi.drawer.class.mq5"
|
||||
#include "../../Classes/x-saherelm.x-trade.class.mq5"
|
||||
#include "../Classes/xcaea.x-data.collector.class.mq5"
|
||||
#include "../Libraries/xcaea.signaller.lib.mq5"
|
||||
|
||||
//
|
||||
@@ -32,13 +33,25 @@
|
||||
//
|
||||
enum ENUM_XCAEA_TRADE_FINALIZATION
|
||||
{
|
||||
XCAEA_TRADE_FINAL_NONE,
|
||||
XCAEA_TRADE_FINAL_TP,
|
||||
XCAEA_TRADE_FINAL_SL,
|
||||
XCAEA_TRADE_FINAL_CLOSE_IN_PROFIT,
|
||||
XCAEA_TRADE_FINAL_CLOSE_IN_LOSE,
|
||||
XCAEA_TRADE_FINAL_NONE, // None
|
||||
XCAEA_TRADE_FINAL_TP, // TP
|
||||
XCAEA_TRADE_FINAL_SL, // SL
|
||||
XCAEA_TRADE_FINAL_CLOSE_IN_PROFIT, // Profit
|
||||
XCAEA_TRADE_FINAL_CLOSE_IN_LOSE, // Lost
|
||||
};
|
||||
|
||||
string ToString(ENUM_XCAEA_TRADE_FINALIZATION value)
|
||||
{
|
||||
//
|
||||
string result = NULL;
|
||||
|
||||
//
|
||||
result = EnumToString(value);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
struct XCAEATrade
|
||||
{
|
||||
@@ -52,6 +65,11 @@ struct XCAEATrade
|
||||
XCAEAStrategyConditions conditions;
|
||||
ENUM_XCAEA_TRADE_FINALIZATION finalize;
|
||||
|
||||
//
|
||||
datetime signaledAt;
|
||||
datetime executedAt;
|
||||
datetime finalizedAt;
|
||||
|
||||
//
|
||||
// Constructor ...
|
||||
XCAEATrade()
|
||||
@@ -76,6 +94,11 @@ struct XCAEATrade
|
||||
conditions.Clean();
|
||||
finalize = XCAEA_TRADE_FINAL_NONE;
|
||||
|
||||
//
|
||||
signaledAt = NULL;
|
||||
executedAt = NULL;
|
||||
finalizedAt = NULL;
|
||||
|
||||
//
|
||||
ZeroMemory(this);
|
||||
}
|
||||
@@ -132,6 +155,142 @@ struct XCAEATrade
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate Summary Info ...
|
||||
*
|
||||
* @return ( string )
|
||||
*/
|
||||
string Summary()
|
||||
{
|
||||
//
|
||||
string result = NULL;
|
||||
|
||||
//
|
||||
result =
|
||||
//
|
||||
ToString("Swap: ", swap) +
|
||||
ToString("Profit: ", profit) +
|
||||
ToString("Commission: ", commission) +
|
||||
ToString("Finalize: ", ToString(finalize)) +
|
||||
ToString("PositionID: ", positionID) +
|
||||
ToString("----------------") + "\n" +
|
||||
ToString("Signaled At: ", ToFormatString(signaledAt)) +
|
||||
ToString("Executed At: ", ToFormatString(executedAt)) +
|
||||
ToString("Finalized At: ", ToFormatString(finalizedAt))
|
||||
//
|
||||
;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Data Collection File Name ...
|
||||
*
|
||||
* @return ( string )
|
||||
*/
|
||||
string GetFileName()
|
||||
{
|
||||
//
|
||||
string result = NULL;
|
||||
|
||||
//
|
||||
bool hasSignal = HasSignal();
|
||||
bool isExecuted = IsExecuted();
|
||||
bool isFinalized = IsFinalized();
|
||||
if (!hasSignal && !isExecuted && !isFinalized)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
result =
|
||||
//
|
||||
signal.symbol + "\\" +
|
||||
ToString(signal.type) + "\\" +
|
||||
(profit >= 0 ? "Profit" : "Loss") + "\\" +
|
||||
ToString(positionID) + "_" +
|
||||
ToString(signal.period) + "_" +
|
||||
ToFormatString(signaledAt)
|
||||
//
|
||||
;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts Model to String Representation ...
|
||||
*
|
||||
* @param onlySignals: Boolean, Specified Represent Only Signal or not ...
|
||||
*
|
||||
* @return ( string )
|
||||
*/
|
||||
string ToString(
|
||||
bool includeSummary = true,
|
||||
bool includeSignal = true,
|
||||
bool includeConditions = true //
|
||||
)
|
||||
{
|
||||
//
|
||||
string result = NULL;
|
||||
|
||||
//
|
||||
// Generating Model Summary ...
|
||||
string summary = Summary();
|
||||
|
||||
//
|
||||
// Generating Signal Summary ...
|
||||
string signalSummary = signal.ToString();
|
||||
|
||||
//
|
||||
// Generating Conditions Summary ...
|
||||
string conditionsSummary = conditions
|
||||
.conditions
|
||||
.GenerateSummary(
|
||||
true,
|
||||
true,
|
||||
true,
|
||||
false // Ignore False Conditions ...
|
||||
);
|
||||
|
||||
//
|
||||
if (includeSummary)
|
||||
{
|
||||
//
|
||||
result +=
|
||||
ToString("----------------") + "\n" +
|
||||
ToString("Summary: ") + "\n" +
|
||||
ToString("----------------") + "\n" +
|
||||
summary + "\n";
|
||||
}
|
||||
|
||||
//
|
||||
if (includeSignal)
|
||||
{
|
||||
//
|
||||
result +=
|
||||
ToString("----------------") + "\n" +
|
||||
ToString("Signal: ") + "\n" +
|
||||
ToString("----------------") + "\n" +
|
||||
signalSummary + "\n";
|
||||
}
|
||||
|
||||
//
|
||||
if (includeConditions)
|
||||
{
|
||||
//
|
||||
result +=
|
||||
ToString("----------------") + "\n" +
|
||||
ToString("Conditions: ") + "\n" +
|
||||
ToString("----------------") + "\n" +
|
||||
conditionsSummary + "\n";
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
@@ -146,24 +305,61 @@ class XCXCAEATradeManager : public XCBaseAlert
|
||||
XCTrade *trader;
|
||||
bool drawSignal;
|
||||
XCPOIDrawer *drawer;
|
||||
XCXCAEADataCollector *collector;
|
||||
|
||||
//
|
||||
// Constructors ...
|
||||
XCXCAEATradeManager(XCTrade *&_trader)
|
||||
XCXCAEATradeManager(
|
||||
XCTrade *&_trader,
|
||||
string _path = NULL // Base Path
|
||||
)
|
||||
{
|
||||
//
|
||||
trader = _trader;
|
||||
drawSignal = true;
|
||||
drawer = new XCPOIDrawer();
|
||||
|
||||
//
|
||||
SaveTrades(false);
|
||||
|
||||
//
|
||||
// Normalizing Collector Path ...
|
||||
if (!IsValid(_path))
|
||||
{
|
||||
_path = "X121XCAEATradeManager" + "\\" + trader.mAccount.GetCompany();
|
||||
}
|
||||
|
||||
//
|
||||
collector = new XCXCAEADataCollector();
|
||||
collector.Path(_path);
|
||||
}
|
||||
|
||||
//
|
||||
// De Constructors ...
|
||||
~XCXCAEATradeManager()
|
||||
{
|
||||
//
|
||||
Clean(trades);
|
||||
|
||||
//
|
||||
ZeroMemory(trader);
|
||||
ZeroMemory(drawer);
|
||||
ZeroMemory(collector);
|
||||
}
|
||||
|
||||
//
|
||||
// Properties ...
|
||||
|
||||
//
|
||||
bool SaveTrades()
|
||||
{
|
||||
return mSaveTrades;
|
||||
}
|
||||
|
||||
//
|
||||
void SaveTrades(bool value)
|
||||
{
|
||||
mSaveTrades = value;
|
||||
}
|
||||
|
||||
//
|
||||
@@ -362,6 +558,17 @@ class XCXCAEATradeManager : public XCBaseAlert
|
||||
//
|
||||
double jTarget = targets[j];
|
||||
|
||||
//
|
||||
// Check Target is Applied Before or Not ...
|
||||
has =
|
||||
!IsValidIndex(trades[idx].signal.appliedTargetIDX)
|
||||
? true
|
||||
: trades[idx].signal.appliedTargetIDX < j;
|
||||
if (!has)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
//
|
||||
// Check Traget Validation ...
|
||||
has =
|
||||
@@ -403,6 +610,9 @@ class XCXCAEATradeManager : public XCBaseAlert
|
||||
);
|
||||
if (isModified)
|
||||
{
|
||||
//
|
||||
trades[idx].signal.appliedTargetIDX = j;
|
||||
|
||||
//
|
||||
string message = prefix +
|
||||
ToString(iPosition.type) +
|
||||
@@ -500,6 +710,8 @@ class XCXCAEATradeManager : public XCBaseAlert
|
||||
iTrade.conditions = conditions;
|
||||
iTrade.positionID = positionID;
|
||||
iTrade.commission = commission;
|
||||
iTrade.signaledAt = signal.time;
|
||||
iTrade.executedAt = TimeCurrent();
|
||||
|
||||
//
|
||||
AddRef(
|
||||
@@ -535,7 +747,7 @@ class XCXCAEATradeManager : public XCBaseAlert
|
||||
// Apply Required Data ...
|
||||
trades[idx].swap = deal.swap;
|
||||
trades[idx].profit = deal.profit;
|
||||
trades[idx].commission = deal.commission;
|
||||
trades[idx].finalizedAt = TimeCurrent();
|
||||
trades[idx].finalize = XCAEA_TRADE_FINAL_TP;
|
||||
|
||||
//
|
||||
@@ -543,6 +755,11 @@ class XCXCAEATradeManager : public XCBaseAlert
|
||||
managedTPCount++;
|
||||
|
||||
//
|
||||
// Save Trade ...
|
||||
Save(trades[idx]);
|
||||
|
||||
//
|
||||
RemoveTrade(idx);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -568,7 +785,7 @@ class XCXCAEATradeManager : public XCBaseAlert
|
||||
// Apply Required Data ...
|
||||
trades[idx].swap = deal.swap;
|
||||
trades[idx].profit = deal.profit;
|
||||
trades[idx].commission = deal.commission;
|
||||
trades[idx].finalizedAt = TimeCurrent();
|
||||
trades[idx].finalize = XCAEA_TRADE_FINAL_SL;
|
||||
|
||||
//
|
||||
@@ -576,6 +793,11 @@ class XCXCAEATradeManager : public XCBaseAlert
|
||||
managedSLCount++;
|
||||
|
||||
//
|
||||
// Save Trade ...
|
||||
Save(trades[idx]);
|
||||
|
||||
//
|
||||
RemoveTrade(idx);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -604,6 +826,7 @@ class XCXCAEATradeManager : public XCBaseAlert
|
||||
// Apply Required Data ...
|
||||
trades[idx].swap = position.swap;
|
||||
trades[idx].profit = position.profit;
|
||||
trades[idx].finalizedAt = TimeCurrent();
|
||||
trades[idx].commission = position.commission;
|
||||
trades[idx].finalize = isInProfit
|
||||
? XCAEA_TRADE_FINAL_CLOSE_IN_PROFIT
|
||||
@@ -624,6 +847,11 @@ class XCXCAEATradeManager : public XCBaseAlert
|
||||
}
|
||||
|
||||
//
|
||||
// Save Trade ...
|
||||
Save(trades[idx]);
|
||||
|
||||
//
|
||||
RemoveTrade(idx);
|
||||
}
|
||||
|
||||
//
|
||||
@@ -752,18 +980,118 @@ class XCXCAEATradeManager : public XCBaseAlert
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a Trade From List ...
|
||||
*
|
||||
* @param index: int ...
|
||||
*/
|
||||
void RemoveTrade(int index)
|
||||
{
|
||||
//
|
||||
bool has =
|
||||
HasChild(trades) &&
|
||||
IsValidIndex(index) &&
|
||||
index < ArraySize(trades);
|
||||
if (!has)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
ArrayRemove(
|
||||
trades,
|
||||
index,
|
||||
1 //
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
// Protected ...
|
||||
protected:
|
||||
//
|
||||
// Tools ...
|
||||
|
||||
//
|
||||
string GetFilePath(XCAEATrade &trade)
|
||||
{
|
||||
//
|
||||
string fileName = trade.GetFileName();
|
||||
|
||||
//
|
||||
return GetFilePath(fileName);
|
||||
}
|
||||
|
||||
//
|
||||
string GetFilePath(string fileName)
|
||||
{
|
||||
//
|
||||
string result = NULL;
|
||||
|
||||
//
|
||||
string mPath = collector.Path();
|
||||
|
||||
//
|
||||
result =
|
||||
//
|
||||
mPath + "\\" + "Trades" + "\\" + fileName + ".x121.log"
|
||||
//
|
||||
;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
bool Save(
|
||||
XCAEATrade &trade,
|
||||
bool includeSummary = true,
|
||||
bool includeSignal = true,
|
||||
bool includeConditions = true //
|
||||
)
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
if (!mSaveTrades)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
string filePath = GetFilePath(trade);
|
||||
result = IsValid(filePath);
|
||||
if (!result)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
string content = trade.ToString(
|
||||
includeSummary,
|
||||
includeSignal,
|
||||
includeConditions //
|
||||
);
|
||||
|
||||
//
|
||||
result = collector.Save(
|
||||
filePath,
|
||||
content //
|
||||
);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Private ...
|
||||
private:
|
||||
//
|
||||
// Props ...
|
||||
|
||||
//
|
||||
bool mSaveTrades;
|
||||
|
||||
//
|
||||
int slCount;
|
||||
int tpCount;
|
||||
|
||||
Reference in New Issue
Block a user