forked from mnbvc188199/Warrior_EA
- Replaced standard library signal modules with custom implementations to allow for named patterns and improved voting. - Added new input parameters for module weights, allowing for optimization of individual signal contributions. - Enhanced the management of trades with new options for breakeven and management cut. - Introduced a mechanism for dynamic ranking of signal weights based on historical performance. - Improved initialization logic to ensure proper registration of filters and handling of trading conditions. - Added detailed logging for trading permissions and account status during initialization.
228 lines
8.3 KiB
MQL5
228 lines
8.3 KiB
MQL5
//+------------------------------------------------------------------+
| |
//| Warrior_EA |
| |
//| AnimateDread |
| |
//| |
| |
//+------------------------------------------------------------------+
| |
#include "DatabaseConnectionManager.mqh"
| |
#include "DatabaseOperationsManager.mqh"
| |
#include "DatabaseFileSystemManager.mqh"
| |
#include "DatabaseVersionManager.mqh"
| |
class CDatabaseManager
| |
{
| |
private:
| |
string m_dbPath;
| |
int m_databaseHandle;
| |
bool m_isDatabaseOpen; // Tracks if the database is currently open
| |
| |
CDatabaseConnectionManager m_dbConnectionManager;
| |
CDatabaseVersionManager m_dbVersionManager;
| |
CDatabaseFileSystemManager m_dbFileSystemManager;
| |
CDatabaseOperationsManager m_dbOperationsManager;
| |
| |
public:
| |
CDatabaseManager()
| |
{
| |
m_isDatabaseOpen = false;
| |
}
| |
| |
~CDatabaseManager()
| |
{
| |
CloseDatabase();
| |
}
| |
| |
bool Init(const string& dbVersion, string& dirStructureArray[], const string& dbName)
| |
{
| |
string currentPath;
| |
// Build the database folder path
| |
for(int i = 0; i < ArraySize(dirStructureArray); ++i)
| |
{
| |
currentPath += (i == 0) ? dirStructureArray[i] : "\\" + dirStructureArray[i];
| |
}
| |
string dbFolderPath = currentPath;
| |
string versionFilePath = dbFolderPath + "\\" + "DatabaseVersion";
| |
m_dbPath = dbFolderPath + "\\" + dbName;
| |
string storedDbVersion = m_dbVersionManager.ReadStoredDbVersion(versionFilePath);
| |
// Handling when the stored database version is "NA"
| |
if(storedDbVersion == "NA")
| |
{
| |
if(!m_dbFileSystemManager.CreateDirectory(dbFolderPath))
| |
{
| |
return false;
| |
}
| |
m_dbFileSystemManager.CleanDirectory(dbFolderPath);
| |
m_dbVersionManager.UpdateStoredDbVersion(versionFilePath, dbVersion);
| |
}
| |
else
| |
if(storedDbVersion == "ERROR")
| |
{
| |
// ReadStoredDbVersion() already retried the FileOpen 5x internally (1s sleep each) -
| |
// a second wrapper loop here would just re-run that whole sequence again for no
| |
// meaningfully better odds. Treat one exhausted "ERROR" as a hard Init() failure.
| |
return false;
| |
}
| |
// If current version doesn't match the stored version (and it's not "NA" or "ERROR")
| |
if(storedDbVersion != dbVersion && storedDbVersion != "")
| |
{
| |
m_dbFileSystemManager.CleanDirectory(dbFolderPath);
| |
m_dbVersionManager.UpdateStoredDbVersion(versionFilePath, dbVersion);
| |
}
| |
// Attempt to initialize the database connection
| |
if(!m_dbConnectionManager.Init(m_dbPath))
| |
{
| |
return false;
| |
}
| |
return true;
| |
}
| |
| |
bool Deinit()
| |
{
| |
m_dbConnectionManager.TransactionCommit();
| |
m_dbConnectionManager.CloseDatabase();
| |
return(true);
| |
}
| |
| |
bool OpenDatabase()
| |
{
| |
// Check if the database is already open
| |
if(m_isDatabaseOpen)
| |
{
| |
// Database is already open, no need to open it again
| |
PrintVerbose("Database already open.");
| |
return true;
| |
}
| |
if(m_dbConnectionManager.OpenDatabase())
| |
{
| |
m_databaseHandle = m_dbConnectionManager.GetDatabaseHandle();
| |
m_dbOperationsManager.SetDatabaseHandle(m_databaseHandle);
| |
m_isDatabaseOpen = true; // Update the state to reflect that the database is now open
| |
return true;
| |
}
| |
else
| |
return false;
| |
}
| |
| |
bool CloseDatabase()
| |
{
| |
if(m_dbConnectionManager.CloseDatabase())
| |
{
| |
m_databaseHandle = 0; // Reset database handle
| |
m_dbOperationsManager.SetDatabaseHandle(m_databaseHandle);
| |
m_isDatabaseOpen = false; // Update the state to reflect that the database is now closed
| |
return true;
| |
}
| |
else
| |
return false;
| |
}
| |
| |
bool BeginTransaction()
| |
{
| |
if(!m_isDatabaseOpen)
| |
{
| |
if(!OpenDatabase())
| |
return false;
| |
}
| |
return m_dbConnectionManager.TransactionBegin();
| |
}
| |
| |
bool CommitTransaction()
| |
{
| |
return m_dbConnectionManager.TransactionCommit();
| |
}
| |
| |
bool RollbackTransaction()
| |
{
| |
return m_dbConnectionManager.TransactionRollback();
| |
}
| |
| |
bool CreateTable(string tableName, string tableSchema)
| |
{
| |
return m_dbOperationsManager.CreateTable(tableName, tableSchema);
| |
}
| |
| |
bool DeleteTable(string tableName)
| |
{
| |
return m_dbOperationsManager.DeleteTable(tableName);
| |
}
| |
| |
//--- add a column to an EXISTING table in place - see DatabaseOperationsManager::ColumnExists()
| |
//--- for why this exists instead of a dbVersion bump (which wipes the directory).
| |
bool EnsureColumn(string tableName, string columnName, string columnType)
| |
{
| |
return m_dbOperationsManager.EnsureColumn(tableName, columnName, columnType);
| |
}
| |
| |
bool InsertTradeRecord(string tableName, const string &columns[], const string &values[])
| |
{
| |
return m_dbOperationsManager.InsertTradeRecord(tableName, columns, values);
| |
}
| |
bool DeleteWhere(string tableName, const string whereClause, const string ¶ms[])
| |
{
| |
return m_dbOperationsManager.DeleteWhere(tableName, whereClause, params);
| |
}
| |
//--- a statement with no result set and no parameters (CREATE INDEX ...)
| |
bool ExecuteQuery(const string query)
| |
{
| |
return m_dbOperationsManager.Execute(query);
| |
}
| |
| |
template <typename T>
| |
bool FetchTradeRecords(string tableName, T &tradeRecordStructure, T &tradeRecords[])
| |
{
| |
return m_dbOperationsManager.FetchTradeRecords(tableName, tradeRecordStructure, tradeRecords);
| |
}
| |
| |
bool FetchRecordCount(string tableName, int &count)
| |
{
| |
return m_dbOperationsManager.FetchRecordCount(tableName, count);
| |
}
| |
| |
bool FetchOpenTradeEntry(string tableName, string pattern, string direction, double &entryPrice, bool &found)
| |
{
| |
return m_dbOperationsManager.FetchOpenTradeEntry(tableName, pattern, direction, entryPrice, found);
| |
}
| |
| |
bool FetchNewestTimeKey(string tableName, long &key, bool &found, const bool quiet = false)
| |
{
| |
return m_dbOperationsManager.FetchNewestTimeKey(tableName, key, found, quiet);
| |
}
| |
| |
bool TableExists(const string tableName)
| |
{
| |
return m_dbOperationsManager.TableExists(tableName);
| |
}
| |
bool FetchExpectancy(string tableName, long beforeKey, int &n, double &meanR,
| |
const int regime = -1)
| |
{
| |
return m_dbOperationsManager.FetchExpectancy(tableName, beforeKey, n, meanR, regime);
| |
}
| |
bool FetchWinLossCounts(string tableName, long beforeKey, int &wins, int &losses)
| |
{
| |
return m_dbOperationsManager.FetchWinLossCounts(tableName, beforeKey, wins, losses);
| |
}
| |
| |
bool UpdateTradeRecord(string tableName, const string &columns[], const string &values[], string pattern, string direction)
| |
{
| |
return m_dbOperationsManager.UpdateTradeRecord(tableName, columns, values, pattern, direction);
| |
}
| |
| |
bool DeleteOldestEntry(string tableName)
| |
{
| |
return m_dbOperationsManager.DeleteOldestEntry(tableName);
| |
}
| |
| |
//--- Panel "Reset" button equivalent for the pattern-confidence DB (was previously only reachable
| |
//--- headless via the trainingMode input at Init() time). Closes the connection if open, deletes the
| |
//--- DB file, and reopens - the next UpdateSignalsWeights() pass recreates tables lazily (CreateTable
| |
//--- is IF-NOT-EXISTS), so no explicit re-init is needed here.
| |
bool ResetDatabase()
| |
{
| |
bool wasOpen = m_isDatabaseOpen;
| |
if(wasOpen)
| |
CloseDatabase();
| |
ResetLastError();
| |
if(!m_dbFileSystemManager.DeleteFile(m_dbPath) && GetLastError() != 5002) // 5002: file does not exist
| |
Print(__FUNCTION__ + ": failed to delete " + m_dbPath + ", error " + IntegerToString(GetLastError()));
| |
return wasOpen ? OpenDatabase() : true;
| |
}
| |
};
| |
//+------------------------------------------------------------------+
|