mql5/Include/Example1/CSignal.mqh

87 lines
3.2 KiB
MQL5
Raw Permalink Normal View History

2025-08-30 11:40:16 +00:00
//+------------------------------------------------------------------+
//| CSignal.mqh |
//| Copyright 2025, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "SoloTrade Official, Solomon"
#property link "https://www.mql5.com/en/users/SoloTradeOfficial"
#property version "1.00"
#property description "THE WORLD MOST INTELLIGENT FOREX AI/ALGO BOTS EVER IN"
"HUMAN HISTORY OF FINTECH, BY SOLOMON ESHUN"
"\nTrading Bots in MQL5"
// Polymorphism
// Base Class - The "Vehicle"
class CSignal {
public:
// This is a virtual function. It's a placeholder.
// We expect derived classes to provide their own real implementation.
virtual bool checkCondition() {
Print("This is the generic signal. It does nothing.");
return false; // Default behavior
}
};
// Derived Class 1 - The "Car"
class CMACrossoverSignal : public CSignal {
public:
// We OVERRIDE the base class function with our specific logic
virtual bool override checkCondition() {
// --- FAKE LOGIC FOR DEMONSTRATION ---
// In a real EA, you'd calculate MAs here.
// Let's pretend the fast MA just crossed above the slow MA.
Print("MA Crossover Signal: BUY condition met!");
return true;
}
};
// Derived Class 2 - The "Boat"
class CRsiSignal : public CSignal {
public:
// We OVERRIDE the base class function with our specific logic
virtual bool override checkCondition() {
// --- FAKE LOGIC FOR DEMONSTRATION ---
// In a real EA, you'd calculate the RSI value.
// Let's pretend RSI is oversold.
Print("RSI Signal: BUY condition met! (RSI is oversold)");
return true;
}
};
// Putting it all together in the main part of the program
void OnStart() {
// Create a pointer to the BASE class type.
// This pointer is flexible and can hold any object that INHERITS from CSignal.
CSignal *myCurrentSignal;
//--- Scenario 1: We decide to use the Moving Average Crossover signal ---
Print("--- Using MA Crossover Strategy ---");
myCurrentSignal = new CMACrossoverSignal(); // The pointer now holds a CMACrossoverSignal object
// We call the function. MQL5 sees it's a virtual function and
// automatically calls the OVERRIDDEN version from CMACrossoverSignal.
if (myCurrentSignal.checkCondition()) {
// Place Buy Order
Print("Placing trade based on the active signal.");
}
// Clean up memory
delete myCurrentSignal;
Print("\n--- Switching to RSI Strategy ---");
//--- Scenario 2: Now we want to use the RSI signal ---
myCurrentSignal = new CRsiSignal(); // The SAME pointer now holds a CRsiSignal object!
// We call the EXACT SAME line of code. But because myCurrentSignal now
// points to a CRsiSignal object, the RSI version of checkCondition() is called.
// This is polymorphism in action!
if (myCurrentSignal.checkCondition()) {
// Place Buy Order
Print("Placing trade based on the active signal.");
}
// Clean up memory
delete myCurrentSignal;
}
//+------------------------------------------------------------------+