MarketPredictor

Price Data Components
Orders Execution
It automatically opens orders when conditions are reached
Indicators Used
Indicator of the average true range
0 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;

        }

    }

}

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---