340 lines
11 KiB
MQL5
340 lines
11 KiB
MQL5
//+------------------------------------------------------------------+
|
|
//| QuantumSignals.mqh |
|
|
//| Quantum Signal Generation and Analysis |
|
|
//| Based on IBM Quantum Phase Estimation Concepts |
|
|
//+------------------------------------------------------------------+
|
|
#property copyright "ForexTrader Quantum EA"
|
|
#property link "https://github.com/simonokwundue-ops/Experienced-FX-Trader"
|
|
#property version "1.00"
|
|
#property strict
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| Quantum Signal Structure |
|
|
//+------------------------------------------------------------------+
|
|
struct QuantumSignal
|
|
{
|
|
double confidence; // Signal confidence (0-100)
|
|
int direction; // 1=Buy, -1=Sell, 0=Neutral
|
|
double quantumScore; // Quantum probability score
|
|
double trendStrength; // Quantum trend strength
|
|
string binarySequence; // Binary price sequence
|
|
double probabilityUp; // Probability of upward movement
|
|
double probabilityDown; // Probability of downward movement
|
|
int horizonLength; // Prediction horizon length
|
|
bool isValid; // Signal validity flag
|
|
};
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| Quantum Signal Generator Class |
|
|
//+------------------------------------------------------------------+
|
|
class CQuantumSignalGenerator
|
|
{
|
|
private:
|
|
string m_symbol;
|
|
ENUM_TIMEFRAMES m_timeframe;
|
|
int m_lookbackPeriod;
|
|
int m_horizonLength;
|
|
double m_confidenceThreshold;
|
|
|
|
// Binary encoding parameters
|
|
static const int QUANTUM_BITS = 256;
|
|
|
|
// Quantum simulation parameters (based on IBM article)
|
|
static const int NUM_QUBITS = 22;
|
|
static const long QUANTUM_PARAM_A = 70000000;
|
|
static const long QUANTUM_PARAM_N = 17000000;
|
|
|
|
public:
|
|
//--- Constructor
|
|
CQuantumSignalGenerator(string symbol, ENUM_TIMEFRAMES timeframe,
|
|
int lookback=256, int horizon=10)
|
|
{
|
|
m_symbol = symbol;
|
|
m_timeframe = timeframe;
|
|
m_lookbackPeriod = lookback;
|
|
m_horizonLength = horizon;
|
|
m_confidenceThreshold = 60.0; // Default confidence threshold
|
|
}
|
|
|
|
//--- Set confidence threshold
|
|
void SetConfidenceThreshold(double threshold) { m_confidenceThreshold = threshold; }
|
|
|
|
//--- Generate quantum signal
|
|
QuantumSignal GenerateSignal()
|
|
{
|
|
QuantumSignal signal;
|
|
signal.isValid = false;
|
|
|
|
// Get price data
|
|
MqlRates rates[];
|
|
ArraySetAsSeries(rates, true);
|
|
int copied = CopyRates(m_symbol, m_timeframe, 0, m_lookbackPeriod, rates);
|
|
|
|
if(copied < m_lookbackPeriod)
|
|
{
|
|
Print("QuantumSignals: Failed to copy sufficient price data");
|
|
return signal;
|
|
}
|
|
|
|
// Convert prices to binary sequence
|
|
signal.binarySequence = PricesToBinary(rates);
|
|
|
|
// Analyze quantum market state
|
|
QuantumStateAnalysis state = AnalyzeQuantumState(signal.binarySequence);
|
|
|
|
// Calculate quantum probabilities
|
|
signal.probabilityUp = state.upProbability;
|
|
signal.probabilityDown = state.downProbability;
|
|
signal.quantumScore = state.dominantProbability;
|
|
signal.trendStrength = MathAbs(state.upProbability - state.downProbability);
|
|
|
|
// Determine direction based on quantum analysis
|
|
if(signal.probabilityUp > signal.probabilityDown &&
|
|
signal.trendStrength > 0.15) // Minimum trend strength 15%
|
|
{
|
|
signal.direction = 1; // Buy
|
|
signal.confidence = signal.quantumScore;
|
|
}
|
|
else if(signal.probabilityDown > signal.probabilityUp &&
|
|
signal.trendStrength > 0.15)
|
|
{
|
|
signal.direction = -1; // Sell
|
|
signal.confidence = signal.quantumScore;
|
|
}
|
|
else
|
|
{
|
|
signal.direction = 0; // Neutral
|
|
signal.confidence = 0;
|
|
}
|
|
|
|
signal.horizonLength = m_horizonLength;
|
|
signal.isValid = (signal.confidence >= m_confidenceThreshold);
|
|
|
|
return signal;
|
|
}
|
|
|
|
//--- Convert price series to binary sequence
|
|
string PricesToBinary(const MqlRates &rates[])
|
|
{
|
|
string binary = "";
|
|
|
|
for(int i = ArraySize(rates) - 2; i >= 0; i--)
|
|
{
|
|
// Compare close prices to create binary sequence
|
|
// 1 = price increased, 0 = price decreased/equal
|
|
if(rates[i].close > rates[i+1].close)
|
|
binary += "1";
|
|
else
|
|
binary += "0";
|
|
}
|
|
|
|
return binary;
|
|
}
|
|
|
|
//--- Quantum state analysis structure
|
|
struct QuantumStateAnalysis
|
|
{
|
|
double upProbability;
|
|
double downProbability;
|
|
double dominantProbability;
|
|
int dominantDirection;
|
|
};
|
|
|
|
//--- Analyze quantum state (simplified QPE simulation)
|
|
QuantumStateAnalysis AnalyzeQuantumState(const string binarySequence)
|
|
{
|
|
QuantumStateAnalysis state;
|
|
|
|
// Count ones and zeros in binary sequence
|
|
int ones = 0;
|
|
int zeros = 0;
|
|
int length = StringLen(binarySequence);
|
|
|
|
for(int i = 0; i < length; i++)
|
|
{
|
|
if(StringGetCharacter(binarySequence, i) == '1')
|
|
ones++;
|
|
else
|
|
zeros++;
|
|
}
|
|
|
|
// Calculate base probabilities
|
|
double baseUpProb = (double)ones / length;
|
|
double baseDownProb = (double)zeros / length;
|
|
|
|
// Apply quantum-inspired weighting (recent data more important)
|
|
double weightedUp = 0;
|
|
double weightedDown = 0;
|
|
double totalWeight = 0;
|
|
|
|
for(int i = 0; i < length; i++)
|
|
{
|
|
// Exponential decay weighting (more recent = higher weight)
|
|
double weight = MathExp(-0.01 * (length - i - 1));
|
|
totalWeight += weight;
|
|
|
|
if(StringGetCharacter(binarySequence, i) == '1')
|
|
weightedUp += weight;
|
|
else
|
|
weightedDown += weight;
|
|
}
|
|
|
|
// Normalize weighted probabilities
|
|
state.upProbability = weightedUp / totalWeight;
|
|
state.downProbability = weightedDown / totalWeight;
|
|
|
|
// Add quantum coherence factor (simulates quantum superposition effects)
|
|
double coherenceFactor = CalculateCoherenceFactor(binarySequence);
|
|
|
|
// Apply coherence to enhance dominant probability
|
|
if(state.upProbability > state.downProbability)
|
|
{
|
|
state.dominantProbability = state.upProbability * (1.0 + coherenceFactor * 0.2);
|
|
state.dominantDirection = 1;
|
|
}
|
|
else
|
|
{
|
|
state.dominantProbability = state.downProbability * (1.0 + coherenceFactor * 0.2);
|
|
state.dominantDirection = -1;
|
|
}
|
|
|
|
// Normalize to percentage
|
|
state.dominantProbability = MathMin(state.dominantProbability * 100, 100.0);
|
|
|
|
return state;
|
|
}
|
|
|
|
//--- Calculate quantum coherence factor
|
|
double CalculateCoherenceFactor(const string binarySequence)
|
|
{
|
|
int length = StringLen(binarySequence);
|
|
if(length < 2) return 0;
|
|
|
|
// Count consecutive patterns (coherence indicator)
|
|
int consecutivePatterns = 0;
|
|
int maxConsecutive = 0;
|
|
int currentConsecutive = 1;
|
|
|
|
for(int i = 1; i < length; i++)
|
|
{
|
|
if(StringGetCharacter(binarySequence, i) ==
|
|
StringGetCharacter(binarySequence, i-1))
|
|
{
|
|
currentConsecutive++;
|
|
}
|
|
else
|
|
{
|
|
if(currentConsecutive > maxConsecutive)
|
|
maxConsecutive = currentConsecutive;
|
|
currentConsecutive = 1;
|
|
}
|
|
}
|
|
|
|
if(currentConsecutive > maxConsecutive)
|
|
maxConsecutive = currentConsecutive;
|
|
|
|
// Coherence factor based on pattern consistency
|
|
// Higher consecutive patterns = higher coherence
|
|
double coherence = (double)maxConsecutive / length;
|
|
|
|
return MathMin(coherence, 1.0);
|
|
}
|
|
|
|
//--- Calculate quantum signal score combining multiple factors
|
|
double CalculateQuantumScore(const QuantumSignal &signal,
|
|
double classicalScore)
|
|
{
|
|
// Weighted combination of quantum and classical signals
|
|
double quantumWeight = 0.6; // 60% quantum, 40% classical
|
|
double classicalWeight = 0.4;
|
|
|
|
// Quantum component
|
|
double quantumComponent = signal.quantumScore * signal.trendStrength;
|
|
|
|
// Combined score
|
|
double combinedScore = (quantumComponent * quantumWeight +
|
|
classicalScore * classicalWeight);
|
|
|
|
return MathMin(combinedScore, 100.0);
|
|
}
|
|
|
|
//--- Get recent price volatility (for risk adjustment)
|
|
double GetRecentVolatility()
|
|
{
|
|
double atr[];
|
|
ArraySetAsSeries(atr, true);
|
|
|
|
int atrHandle = iATR(m_symbol, m_timeframe, 14);
|
|
if(atrHandle == INVALID_HANDLE)
|
|
return 0;
|
|
|
|
if(CopyBuffer(atrHandle, 0, 0, 1, atr) <= 0)
|
|
{
|
|
IndicatorRelease(atrHandle);
|
|
return 0;
|
|
}
|
|
|
|
double volatility = atr[0];
|
|
IndicatorRelease(atrHandle);
|
|
|
|
return volatility;
|
|
}
|
|
};
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| Quantum-Enhanced Multi-Strategy Signal Combiner |
|
|
//+------------------------------------------------------------------+
|
|
class CQuantumMultiStrategy
|
|
{
|
|
private:
|
|
CQuantumSignalGenerator *m_quantumGen;
|
|
|
|
public:
|
|
//--- Constructor
|
|
CQuantumMultiStrategy(string symbol, ENUM_TIMEFRAMES timeframe)
|
|
{
|
|
m_quantumGen = new CQuantumSignalGenerator(symbol, timeframe);
|
|
}
|
|
|
|
//--- Destructor
|
|
~CQuantumMultiStrategy()
|
|
{
|
|
if(CheckPointer(m_quantumGen) == POINTER_DYNAMIC)
|
|
delete m_quantumGen;
|
|
}
|
|
|
|
//--- Combine quantum and classical signals
|
|
double CombineSignals(double maScore, double rsiScore,
|
|
double bbScore, double macdScore)
|
|
{
|
|
// Get quantum signal
|
|
QuantumSignal qSignal = m_quantumGen.GenerateSignal();
|
|
|
|
if(!qSignal.isValid)
|
|
return 0;
|
|
|
|
// Calculate classical composite score
|
|
double classicalScore = (maScore + rsiScore + bbScore + macdScore) / 4.0;
|
|
|
|
// Combine with quantum analysis
|
|
double finalScore = m_quantumGen.CalculateQuantumScore(qSignal, classicalScore);
|
|
|
|
return finalScore;
|
|
}
|
|
|
|
//--- Get quantum signal direction
|
|
int GetQuantumDirection()
|
|
{
|
|
QuantumSignal signal = m_quantumGen.GenerateSignal();
|
|
return signal.direction;
|
|
}
|
|
|
|
//--- Get quantum confidence
|
|
double GetQuantumConfidence()
|
|
{
|
|
QuantumSignal signal = m_quantumGen.GenerateSignal();
|
|
return signal.confidence;
|
|
}
|
|
};
|
|
|
|
//+------------------------------------------------------------------+
|