MarketPredictor

Download script
Profit factor:
0.00

This script is a sophisticated trading tool designed to predict future market prices by combining advanced mathematical models with statistical simulations. Instead of relying on simple price indicators, it analyzes the underlying "physics" of price movement—specifically looking at waves, complex patterns, and probability.

Here is a breakdown of how the logic works in plain English:

1. The Multi-Layered Prediction Engine

The core of the script is its ability to view price action through three different mathematical lenses simultaneously. It calculates three distinct "components" of the market to form a complete picture:

  • Sinusoidal Component (Wave Analysis): This part of the logic treats market prices like physical waves. It uses trigonometry to identify recurring cycles and periodic rhythms in the data, helping the script understand if the market is currently in a "swing" phase.
  • Fractal Component (Frequency Analysis): This is one of the most advanced parts of the script. It uses a technique called Fast Fourier Transform (FFT). Imagine taking a complex piece of music and breaking it down into individual notes; FFT does this with price data. It identifies the "dominant frequency"—the strongest hidden rhythm in the price movement—and translates that into a predictive value.
  • Sigmoid Component (Volatility & Thresholds): This component focuses on how quickly and aggressively the price is moving. By using an "S-curve" (Sigmoid function) combined with volatility data, it determines if a price move is reaching a tipping point or if it is stabilizing.

2. Dynamic Parameter Optimization

The script is designed to be self-adjusting. It does not use static numbers for its calculations; instead, it constantly updates its internal settings based on current market conditions:

  • It calculates the Moving Average of recent prices to establish a "baseline" (the center point of the market).
  • It monitors Market Volatility (using the ATR indicator) to adjust its sensitivity. If the market becomes wild and unpredictable, the script scales its reaction accordingly.

3. Monte Carlo Simulations (The Probability Test)

Before making a final prediction, the script performs a "stress test" on its findings using a Monte Carlo Simulation.

Instead of just guessing where the price will go next, the script runs thousands of tiny "what-if" scenarios in a split second. It takes the current price and simulates thousands of possible random paths it could take based on existing volatility. By averaging all these thousands of simulated outcomes, it arrives at a statistically grounded "expected price."

4. Execution Logic

Once the prediction is finalized, the script follows a strict set of rules to enter the market:

  • The Buy Signal: If the predicted price is significantly higher than the current price (surpassing a specific safety threshold), the script looks for an opening to enter a "Buy" position.
  • The Sell Signal: Conversely, if the prediction shows a significant drop below the current price, it seeks an opening for a "Sell" position.
  • Safety Checks: The script ensures it doesn't open multiple overlapping trades on the same asset and only executes when there are no existing positions, maintaining a clean trading history.

Summary for the User

In essence, this script acts like a high-frequency laboratory. It looks at the market's "heartbeat" (waves), its "DNA" (frequencies), and its "mood" (volatility), runs thousands of simulations to see what is most likely to happen next, and then places trades based on that statistical probability.

Price Data Components
Orders Execution
It automatically opens orders when conditions are reached
Indicators Used
Indicator of the average true range
16 Views
0 Downloads
0 Favorites
MarketPredictor
ÿþ//+------------------------------------------------------------------+

//|                                         MarketPredictor          | 

//|                                         Mustafa Seyyid Sahin     |

//+------------------------------------------------------------------+



#property strict



// Structure to represent complex numbers

struct Complex

{

    double re; // Real part of the complex number

    double im; // Imaginary part of the complex number

};



// Input parameters that can be adjusted by the user

input double inputAlpha = 0.1;          // Amplitude for the Sinusoidal component

input double inputBeta = 0.1;           // Weight for the Fractal component

input double inputGamma = 0.1;          // Damping constant for the Fractal component

input double inputKappa = 1.0;          // Sensitivity parameter for the Sigmoid function

input double inputMu = 1.0;             // Mean of price movements

input double inputSigma = 10.0;         // Threshold for Buy/Sell

input int MonteCarloSimulations = 1000; // Number of Monte Carlo simulations

input int magicNumber = 123456;         // Unique identifier for orders



// Internal variables to store optimized parameters

double alpha;

double beta;

double gamma;

double kappa;

double mu;

double sigma;



// Global variable to store the current price

double P_t;



//+------------------------------------------------------------------+

//| Expert initialization function                                   |

//+------------------------------------------------------------------+

int OnInit()

{

    // Initialize internal variables with input values

    alpha = inputAlpha;

    beta = inputBeta;

    gamma = inputGamma;

    kappa = inputKappa;

    mu = inputMu;

    sigma = inputSigma;

    

    // Initialize the current price with the symbol's current Bid price

    P_t = SymbolInfoDouble(_Symbol, SYMBOL_BID);

    

    // Seed the random number generator with the current time

    MathSrand((int)TimeCurrent());

    

    // Additional initializations can be added here if necessary

    

    return(INIT_SUCCEEDED); // Return initialization status

}

//+------------------------------------------------------------------+

//| Expert deinitialization function                                 |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

{

    // Cleanup tasks to perform when the EA is removed

    // For example, closing open files or releasing resources

}

//+------------------------------------------------------------------+

//| Expert tick function                                             |

//+------------------------------------------------------------------+

void OnTick()

{

    // Optimize parameters on each tick

    OptimizeParameters();



    // Predict the next price and execute trading decisions

    PredictNextPrice();

}

//+------------------------------------------------------------------+

//| Calculation of the Sinusoidal component                         |

//+------------------------------------------------------------------+

double CalculateSinComponent(int t)

{

    // Generate a random frequency omega between 0 and 2À

    double omega = ((double)MathRand() / 32767.0) * 2.0 * M_PI;

    

    // Calculate the Sinusoidal component with the given amplitude and frequency

    return alpha * MathSin(omega * t);

}

//+------------------------------------------------------------------+

//| Calculation of the Fractal component using FFT                    |

//+------------------------------------------------------------------+

double CalculateFractalComponentFFT()

{

    int N = 128; // Number of data points for FFT (must be a power of 2)

    

    // Check if there are enough bars (price data) available

    if(Bars(_Symbol, PERIOD_CURRENT) < N)

        return 0.0; // Return 0 if not enough data is available

    

    // Collect historical closing prices

    double closePrices[];

    ArraySetAsSeries(closePrices, true); // Treat data as a series (latest data first)

    if(CopyClose(_Symbol, PERIOD_CURRENT, 0, N, closePrices) <= 0)

    {

        // Print error message and return 0 if copying fails

        Print("Error copying closing prices: ", GetLastError());

        return 0.0;

    }

    

    // Create an array of complex numbers for FFT

    Complex data[];

    ArrayResize(data, N); // Resize array to N elements

    for(int i = 0; i < N; i++)

    {

        data[i].re = closePrices[i]; // Real part is the closing price

        data[i].im = 0.0;            // Imaginary part is 0

    }

    

    // Perform FFT (Fast Fourier Transform)

    FFT(data, false); // 'false' indicates forward FFT

    

    // Calculate the spectrum (magnitude of frequencies)

    double spectrum[];

    ArrayResize(spectrum, N / 2); // Only the first N/2 frequencies are relevant

    for(int i = 0; i < N / 2; i++)

    {

        spectrum[i] = MathSqrt(data[i].re * data[i].re + data[i].im * data[i].im);

    }

    

    // Find the dominant frequency (highest magnitude)

    double maxSpectrum = 0.0;

    int maxIndex = 0;

    for(int i = 1; i < N / 2; i++) // Start at 1 to skip the DC component

    {

        if(spectrum[i] > maxSpectrum)

        {

            maxSpectrum = spectrum[i];

            maxIndex = i;

        }

    }

    

    // Calculate the dominant frequency based on the index

    double dominantFrequency = (double)maxIndex / N;

    

    // Calculate the Fractal component using the dominant frequency

    double fractalComponent = beta * dominantFrequency;

    

    return fractalComponent; // Return the calculated Fractal component

}

//+------------------------------------------------------------------+

//| Calculation of the Sigmoid component                             |

//+------------------------------------------------------------------+

double CalculateSigmoidComponent()

{

    // Calculate the volatility jump Delta_t using ATR (Average True Range)

    double Delta_t = iATR(_Symbol, PERIOD_CURRENT, 14);

    

    // Retrieve the current price

    double P_t_local = SymbolInfoDouble(_Symbol, SYMBOL_BID);

    

    // Calculate the Sigmoid function based on kappa, mu, and the current price

    double sigmoid = 1.0 / (1.0 + MathExp(-kappa * (P_t_local - mu)));

    

    // Return the Sigmoid component weighted by ATR

    return Delta_t / sigmoid;

}

//+------------------------------------------------------------------+

//| Function to perform Monte Carlo simulations                      |

//+------------------------------------------------------------------+

double SimulatePrice()

{

    // Check if the number of simulations is greater than 0

    if(MonteCarloSimulations <= 0)

    {

        Print("MonteCarloSimulations must be greater than 0.");

        return P_t; // Return current price as fallback

    }

    

    double sum = 0.0; // Sum of simulated prices

    

    // Perform the specified number of simulations

    for(int i = 0; i < MonteCarloSimulations; i++)

    {

        // Generate a random factor between -0.5 and 0.5

        double randomFactor = ((double)MathRand() / 32767.0) - 0.5;

        

        // Calculate the simulated price based on the current price and random factor

        double simulatedPrice = P_t + randomFactor * sigma;

        

        sum += simulatedPrice; // Add the simulated price to the sum

    }

    

    // Calculate the average of the simulated prices

    return sum / MonteCarloSimulations;

}

//+------------------------------------------------------------------+

//| Predict the next price                                           |

//+------------------------------------------------------------------+

double PredictNextPrice()

{

    // Update the current price

    P_t = SymbolInfoDouble(_Symbol, SYMBOL_BID);



    // Determine the time index (number of bars) for the current symbol and timeframe

    int t = Bars(_Symbol, PERIOD_CURRENT);

    if (t < 0)

    {

        // Print error message and return 0 if retrieving bar count fails

        Print("Error retrieving bar count: ", GetLastError());

        return 0.0;

    }



    // Optional: Calculate individual components

    // double sinComponent = CalculateSinComponent(t);

    // double fractalComponent = CalculateFractalComponentFFT();

    // double sigmoidComponent = CalculateSigmoidComponent();

    

    // Alternatively, use Monte Carlo simulation for price prediction

    double P_t1 = SimulatePrice();

    

    // Execute trading decisions based on the prediction

    ExecuteTrade(P_t1);

    

    return P_t1; // Return the predicted price movement

}

//+------------------------------------------------------------------+

//| Trading logic                                                    |

//+------------------------------------------------------------------+

void ExecuteTrade(double P_t1)

{

    // Retrieve the current price

    double currentPrice = SymbolInfoDouble(_Symbol, SYMBOL_BID);



    // Structures for trade request and result

    MqlTradeRequest request;

    MqlTradeResult result;

    ZeroMemory(request); // Initialize trade request structure to zero

    ZeroMemory(result);  // Initialize trade result structure to zero



    // Buy condition

    if(P_t1 > currentPrice + sigma)

    {

        // Check if there are no open positions for the symbol

        if(!PositionSelect(_Symbol))

        {

            // Configure the buy order

            request.action    = TRADE_ACTION_DEAL;                          // Action: Immediate trade

            request.symbol    = _Symbol;                                    // Symbol, e.g., EURUSD

            request.volume    = 0.1;                                        // Trade volume (lot size)

            request.type      = ORDER_TYPE_BUY;                             // Order type: Buy

            request.price     = SymbolInfoDouble(_Symbol, SYMBOL_ASK);      // Price to buy

            request.deviation = 10;                                         // Maximum deviation in points

            request.magic     = magicNumber;                                // Unique identifier for the order

            request.comment   = "Buy Order";                                // Order comment



            // Send the buy order

            if(!OrderSend(request, result))

            {

                // Print error message if order placement fails

                Print("Error placing buy order: ", result.comment);

            }

            else

            {

                // Confirm successful placement of the buy order

                Print("Buy order successfully placed. Ticket: ", result.order);

            }

        }

    }

    // Sell condition

    else if(P_t1 < currentPrice - sigma)

    {

        // Check if there are no open positions for the symbol

        if(!PositionSelect(_Symbol))

        {

            // Configure the sell order

            request.action    = TRADE_ACTION_DEAL;                          // Action: Immediate trade

            request.symbol    = _Symbol;                                    // Symbol, e.g., EURUSD

            request.volume    = 0.1;                                        // Trade volume (lot size)

            request.type      = ORDER_TYPE_SELL;                            // Order type: Sell

            request.price     = SymbolInfoDouble(_Symbol, SYMBOL_BID);      // Price to sell

            request.deviation = 10;                                         // Maximum deviation in points

            request.magic     = magicNumber;                                // Unique identifier for the order

            request.comment   = "Sell Order";                               // Order comment



            // Send the sell order

            if(!OrderSend(request, result))

            {

                // Print error message if order placement fails

                Print("Error placing sell order: ", result.comment);

            }

            else

            {

                // Confirm successful placement of the sell order

                Print("Sell order successfully placed. Ticket: ", result.order);

            }

        }

    }

}

//+------------------------------------------------------------------+

//| Parameter optimization                                           |

//+------------------------------------------------------------------+

void OptimizeParameters()

{

    // Example adjustment: Calculate the moving average of the last N closing prices

    double movingAverage = 0.0; // Variable to store the moving average

    int period = 14;             // Period for the moving average



    // Check if there are enough bars (price data) available

    if(Bars(_Symbol, PERIOD_CURRENT) < period)

        return; // Exit if not enough data is available



    // Collect historical closing prices

    double closePrices[];

    ArraySetAsSeries(closePrices, true); // Treat data as a series (latest data first)

    if(CopyClose(_Symbol, PERIOD_CURRENT, 0, period, closePrices) <= 0)

    {

        // Print error message and exit the function if copying fails

        Print("Error copying closing prices for optimization: ", GetLastError());

        return;

    }



    // Calculate the moving average

    for(int i = 0; i < period; i++)

        movingAverage += closePrices[i];

    movingAverage /= period; // Compute the average



    // Adjust the mean mu based on the moving average

    mu = movingAverage;



    // Adjust alpha based on volatility (ATR)

    double atr = iATR(_Symbol, PERIOD_CURRENT, period); // Calculate ATR

    if(atr > 0.0)

        alpha = atr * 0.1; // Set alpha proportional to volatility

    else

        alpha = inputAlpha; // Fallback to input value if ATR is unavailable

}

//+------------------------------------------------------------------+

//| FFT Function                                                     |

//+------------------------------------------------------------------+

void FFT(Complex &x[], bool invert)

{

    int n = ArraySize(x); // Determine the size of the array

    if(n <= 1)

        return; // Exit if the array has only one element

    

    // Recursively split the array into even and odd indices

    Complex even[];

    Complex odd[];

    ArrayResize(even, n / 2); // Resize the 'even' array to half the size

    ArrayResize(odd, n / 2);  // Resize the 'odd' array to half the size

    

    for(int i = 0; i < n / 2; i++)

    {

        even[i] = x[2 * i];     // All even elements

        odd[i] = x[2 * i + 1];  // All odd elements

    }

    

    // Recursively apply FFT to the even and odd parts

    FFT(even, invert);

    FFT(odd, invert);

    

    // Calculate the complex roots of unity (Twiddle factors)

    for(int k = 0; k < n / 2; k++)

    {

        double angle = 2.0 * M_PI * k / n * (invert ? 1 : -1); // Angle for Twiddle factors

        Complex t;

        t.re = MathCos(angle); // Real part of the Twiddle factor

        t.im = MathSin(angle); // Imaginary part of the Twiddle factor

        

        // Multiply odd[k] with the Twiddle factor t

        Complex temp;

        temp.re = odd[k].re * t.re - odd[k].im * t.im;

        temp.im = odd[k].re * t.im + odd[k].im * t.re;

        

        // Add the even components

        x[k].re += temp.re;

        x[k].im += temp.im;

        

        // Subtract the even components for the second half

        x[k + n / 2].re = even[k].re - temp.re;

        x[k + n / 2].im = even[k].im - temp.im;

    }

    

    // If performing an inverse FFT, divide each element by n

    if(invert)

    {

        for(int i = 0; i < n; i++)

        {

            x[i].re /= 2.0;

            x[i].im /= 2.0;

        }

    }

}

Profitability Reports

PERFORMANCE BY SYMBOL

Expected payoff

Average expected payoff across the available backtests for each symbol.

Positive Negative
4 reports
AUD/USD Jul 2025 - Sep 2025
0.00
Total Trades 0
Won Trades 0
Lost trades 0
Win Rate 0.0 %
Expected payoff 0.00
Gross Profit 0.00
Gross Loss 0.00
Total Net Profit 0.00
-100%
-50%
0%
50%
100%
EUR/USD Jul 2025 - Sep 2025
0.00
Total Trades 0
Won Trades 0
Lost trades 0
Win Rate 0.0 %
Expected payoff 0.00
Gross Profit 0.00
Gross Loss 0.00
Total Net Profit 0.00
-100%
-50%
0%
50%
100%
GBP/AUD Jul 2025 - Sep 2025
0.00
Total Trades 0
Won Trades 0
Lost trades 0
Win Rate 0.0 %
Expected payoff 0.00
Gross Profit 0.00
Gross Loss 0.00
Total Net Profit 0.00
-100%
-50%
0%
50%
100%
GBP/CAD Jul 2025 - Sep 2025
0.00
Total Trades 0
Won Trades 0
Lost trades 0
Win Rate 0.0 %
Expected payoff 0.00
Gross Profit 0.00
Gross Loss 0.00
Total Net Profit 0.00
-100%
-50%
0%
50%
100%

Comments