// Neural network parameters
int inputSize = 3; // Number of input neurons
int hiddenSize = 5; // Number of hidden neurons
int outputSize = 1; // Number of output neurons
// Weights and biases (simplified)
double weightsInputHidden[inputSize][hiddenSize];
double weightsHiddenOutput[hiddenSize][outputSize];
double biasHidden[hiddenSize];
double biasOutput[outputSize];
//+------------------------------------------------------------------+
//| Initialize neural network weights |
//+------------------------------------------------------------------+
void InitializeNeuralNetwork()
{
// Random initialization
for(int i = 0; i < inputSize; i++)
{
for(int j = 0; j < hiddenSize; j++)
{
weightsInputHidden[i][j] = MathRand()/32767.0 - 0.5; // Random between -0.5 and 0.5
}
}
for(int i = 0; i < hiddenSize; i++)
{
for(int j = 0; j < outputSize; j++)
{
weightsHiddenOutput[i][j] = MathRand()/32767.0 - 0.5;
}
biasHidden[i] = MathRand()/32767.0 - 0.5;
}
for(int i = 0; i < outputSize; i++)
{
biasOutput[i] = MathRand()/32767.0 - 0.5;
}
}
//+------------------------------------------------------------------+
//| Sigmoid activation function |
//+------------------------------------------------------------------+
double Sigmoid(double x)
{
return 1.0 / (1.0 + MathExp(-x));
}
//+------------------------------------------------------------------+
//| Forward propagation |
//+------------------------------------------------------------------+
double ForwardPropagate(double inputs[])
{
double hidden[hiddenSize];
double output = 0;
// Calculate hidden layer
for(int i = 0; i < hiddenSize; i++)
{
hidden[i] = biasHidden[i];
for(int j = 0; j < inputSize; j++)
{
hidden[i] += inputs[j] * weightsInputHidden[j][i];
}
hidden[i] = Sigmoid(hidden[i]);
}
// Calculate output
for(int i = 0; i < hiddenSize; i++)
{
output += hidden[i] * weightsHiddenOutput[i][0];
}
output += biasOutput[0];
output = Sigmoid(output);
return output;
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// Initialize network if needed
static bool initialized = false;
if(!initialized)
{
InitializeNeuralNetwork();
initialized = true;
}
// Get input data
double inputs[inputSize];
inputs[0] = iRSI(NULL, 0, 14, PRICE_CLOSE, 0);
inputs[1] = iMACD(NULL, 0, 12, 26, 9, PRICE_CLOSE, MODE_MAIN, 0) -
iMACD(NULL, 0, 12, 26, 9, PRICE_CLOSE, MODE_SIGNAL, 0);
inputs[2] = iAD(NULL, 0, 14, 0);
// Get AI decision
double decision = ForwardPropagate(inputs);
// Make trading decision
if(decision > 0.7 && OrdersTotal() == 0)
{
OrderSend(Symbol(), OP_BUY, 0.1, Ask, 3, 0, 0, "AI Buy", 0, 0, clrGreen);
}
else if(decision < 0.3 && OrdersTotal() == 0)
{
OrderSend(Symbol(), OP_SELL, 0.1, Bid, 3, 0, 0, "AI Sell", 0, 0, clrRed);
}
}
// AI Parameters
input int lookbackPeriod = 50; // Number of bars to analyze
input int inputSize = 5; // Number of input features
input int hiddenSize = 10; // Number of hidden neurons
input int outputSize = 1; // Number of output neurons
input double learningRate = 0.01; // Learning rate for training
input int trainingFrequency = 100; // How often to retrain (in bars)
// Neural Network Structure
double weightsInputHidden[inputSize][hiddenSize];
double weightsHiddenOutput[hiddenSize][outputSize];
double biasHidden[hiddenSize];
double biasOutput[outputSize];
double momentum[inputSize][hiddenSize];
double momentumHidden[hiddenSize][outputSize];
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// Initialize weights randomly
for(int i = 0; i < inputSize; i++)
{
for(int j = 0; j < hiddenSize; j++)
{
weightsInputHidden[i][j] = (MathRand()/32767.0 - 0.5) * 0.1;
momentum[i][j] = 0;
}
}
for(int i = 0; i < hiddenSize; i++)
{
for(int j = 0; j < outputSize; j++)
{
weightsHiddenOutput[i][j] = (MathRand()/32767.0 - 0.5) * 0.1;
momentumHidden[i][j] = 0;
}
biasHidden[i] = (MathRand()/32767.0 - 0.5) * 0.1;
}
for(int i = 0; i < outputSize; i++)
{
biasOutput[i] = (MathRand()/32767.0 - 0.5) * 0.1;
}
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Sigmoid activation function |
//+------------------------------------------------------------------+
double Sigmoid(double x)
{
return 1.0 / (1.0 + MathExp(-x));
}
//+------------------------------------------------------------------+
//| Forward propagation |
//+------------------------------------------------------------------+
double ForwardPropagate(double inputs[])
{
double hidden[hiddenSize];
double output = 0;
// Calculate hidden layer
for(int i = 0; i < hiddenSize; i++)
{
hidden[i] = biasHidden[i];
for(int j = 0; j < inputSize; j++)
{
hidden[i] += inputs[j] * weightsInputHidden[j][i];
}
hidden[i] = Sigmoid(hidden[i]);
}
// Calculate output
for(int i = 0; i < hiddenSize; i++)
{
output += hidden[i] * weightsHiddenOutput[i][0];
}
output += biasOutput[0];
output = Sigmoid(output);
return output;
}
//+------------------------------------------------------------------+
//| Backpropagation training |
//+------------------------------------------------------------------+
void TrainNetwork(double inputs[], double target)
{
// Forward pass
double hidden[hiddenSize];
double output = 0;
for(int i = 0; i < hiddenSize; i++)
{
hidden[i] = biasHidden[i];
for(int j = 0; j < inputSize; j++)
{
hidden[i] += inputs[j] * weightsInputHidden[j][i];
}
hidden[i] = Sigmoid(hidden[i]);
}
for(int i = 0; i < hiddenSize; i++)
{
output += hidden[i] * weightsHiddenOutput[i][0];
}
output += biasOutput[0];
output = Sigmoid(output);
// Backward pass
double error = target - output;
double outputGradient = output * (1 - output) * error;
// Update output weights and bias
for(int i = 0; i < hiddenSize; i++)
{
momentumHidden[i][0] = 0.9 * momentumHidden[i][0] + 0.1 * outputGradient * hidden[i];
weightsHiddenOutput[i][0] += learningRate * momentumHidden[i][0];
}
biasOutput[0] += learningRate * outputGradient;
// Calculate hidden layer errors
double hiddenGradients[hiddenSize];
for(int i = 0; i < hiddenSize; i++)
{
hiddenGradients[i] = hidden[i] * (1 - hidden[i]) * outputGradient * weightsHiddenOutput[i][0];
}
// Update input weights and biases
for(int i = 0; i < hiddenSize; i++)
{
biasHidden[i] += learningRate * hiddenGradients[i];
for(int j = 0; j < inputSize; j++)
{
momentum[i][j] = 0.9 * momentum[i][j] + 0.1 * hiddenGradients[i] * inputs[j];
weightsInputHidden[j][i] += learningRate * momentum[i][j];
}
}
}
//+------------------------------------------------------------------+
//| Generate AI features from price data |
//+------------------------------------------------------------------+
void GenerateFeatures(double &features[])
{
// Feature 1: Price momentum (current price vs. lookback)
features[0] = (iClose(NULL, 0, 0) - iClose(NULL, 0, lookbackPeriod)) / iClose(NULL, 0, lookbackPeriod);
// Feature 2: Price volatility (standard deviation)
double sum = 0, sumSq = 0;
for(int i = 0; i < lookbackPeriod; i++)
{
double price = iClose(NULL, 0, i);
sum += price;
sumSq += price * price;
}
double mean = sum / lookbackPeriod;
double variance = (sumSq / lookbackPeriod) - (mean * mean);
features[1] = MathSqrt(MathAbs(variance));
// Feature 2: Price acceleration (change in momentum)
double momentum1 = (iClose(NULL, 0, 0) - iClose(NULL, 0, 1)) / iClose(NULL, 0, 1);
double momentum2 = (iClose(NULL, 0, 1) - iClose(NULL, 0, 2)) / iClose(NULL, 0, 2);
features[2] = momentum1 - momentum2;
// Feature 3: Recent high/low ratio
double high = iHigh(NULL, 0, 0);
double low = iLow(NULL, 0, 0);
features[3] = (high - low) / low;
// Feature 4: Time-based feature (normalized bar index)
features[4] = (double)Bars / (double)lookbackPeriod;
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
static datetime lastTrainingTime = 0;
static int barCount = 0;
// Generate features
double features[inputSize];
GenerateFeatures(features);
// Forward pass to get prediction
double prediction = ForwardPropagate(features);
// Determine trading signal
bool buySignal = (prediction > 0.7) && (OrdersTotal() == 0);
bool sellSignal = (prediction < 0.3) && (OrdersTotal() == 0);
// Execute trades with risk management
if(buySignal)
{
double lotSize = 0.1; // Adjust based on your account size
OrderSend(Symbol(), OP_BUY, lotSize, Ask, 3, 0, 0, "AI Buy", 0, 0, clrGreen);
}
else if(sellSignal)
{
double lotSize = 0.1; // Adjust based on your account size
OrderSend(Symbol(), OP_SELL, lotSize, Bid, 3, 0, 0, "AI Sell", 0, 0, clrRed);
}
// Periodic training
barCount++;
if(barCount >= trainingFrequency)
{
// Generate training data (simplified - in real implementation you'd need historical data)
double trainingFeatures[inputSize];
GenerateFeatures(trainingFeatures);
// Determine target (1 for profitable bar, 0 otherwise)
double target = 0;
if(iClose(NULL, 0, 0) > iClose(NULL, 0, 1)) // Simple profit target
target = 1;
TrainNetwork(trainingFeatures, target);
barCount = 0;
}
}