การใช้ machine learning algorithm ใน EA

IUX Markets Bonus

ตัวอย่างการใช้ machine learning algorithm ใน EA

การนำ machine learning มาใช้ใน EA เป็นแนวทางขั้นสูงที่สามารถเพิ่มประสิทธิภาพในการวิเคราะห์ตลาดและการตัดสินใจเทรด ในตัวอย่างนี้ เราจะใช้ simple neural network เพื่อทำนายทิศทางของราคาในอนาคต

EA ใช้ machine learning
EA ใช้ machine learning

ข้อดีของการใช้ machine learning ใน EA:

  1. สามารถเรียนรู้และปรับตัวกับรูปแบบตลาดที่ซับซ้อน
  2. ลดอคติจากการตัดสินใจของมนุษย์
  3. สามารถประมวลผลข้อมูลจำนวนมากได้อย่างรวดเร็ว
  4. มีความยืดหยุ่นในการปรับใช้กับสภาวะตลาดที่เปลี่ยนแปลง

ตัวอย่าง EA ที่ใช้ simple neural network:

ในตัวอย่างนี้ เราจะใช้ neural network อย่างง่ายที่มี input layer, 1 hidden layer และ output layer เพื่อทำนายทิศทางของราคาในอนาคต

 



#property copyright "Your Name"
#property link      "https://www.example.com"
#property version   "1.00"
#property strict

#include <Math\Alglib\alglib.mqh>

// Input parameters
input int InputNeurons = 5;
input int HiddenNeurons = 3;
input int TrainingPeriod = 1000;
input int PredictionPeriod = 10;
input double LearningRate = 0.1;
input double LotSize = 0.01;

// Global variables
CMultilayerPerceptronShell NeuralNetwork;
int handle_rsi;
double inputData[];
double outputData[];

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
    // Initialize RSI indicator
    handle_rsi = iRSI(NULL, 0, 14, PRICE_CLOSE);
    if(handle_rsi == INVALID_HANDLE) {
        Print("Failed to create RSI indicator");
        return INIT_FAILED;
    }
    
    // Initialize and train neural network
    if(!InitializeAndTrainNetwork()) {
        Print("Failed to initialize and train neural network");
        return INIT_FAILED;
    }
    
    return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
    // Clean up
    IndicatorRelease(handle_rsi);
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
    // Check for open positions
    if(PositionExists())
        return;
    
    // Prepare input data
    if(!PrepareInputData())
        return;
    
    // Make prediction
    double prediction[];
    NeuralNetwork.Process(inputData, prediction);
    
    // Open order based on prediction
    if(prediction[0] > 0.5) {
        // Bullish prediction
        double askPrice = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
        OpenOrder(ORDER_TYPE_BUY, askPrice);
    }
    else {
        // Bearish prediction
        double bidPrice = SymbolInfoDouble(_Symbol, SYMBOL_BID);
        OpenOrder(ORDER_TYPE_SELL, bidPrice);
    }
}

//+------------------------------------------------------------------+
//| Initialize and train neural network                              |
//+------------------------------------------------------------------+
bool InitializeAndTrainNetwork()
{
    // Create neural network
    NeuralNetwork.Create(InputNeurons, HiddenNeurons, 1);
    
    // Prepare training data
    double trainingInputs[];
    double trainingOutputs[];
    if(!PrepareTrainingData(trainingInputs, trainingOutputs))
        return false;
    
    // Train network
    CMLPReportShell report;
    NeuralNetwork.Train(trainingInputs, trainingOutputs, TrainingPeriod, LearningRate, report);
    
    return true;
}

//+------------------------------------------------------------------+
//| Prepare training data                                            |
//+------------------------------------------------------------------+
bool PrepareTrainingData(double &inputs[], double &outputs[])
{
    int dataSize = TrainingPeriod + PredictionPeriod;
    
    // Allocate arrays
    ArrayResize(inputs, TrainingPeriod * InputNeurons);
    ArrayResize(outputs, TrainingPeriod);
    
    // Get RSI values
    double rsi[];
    if(CopyBuffer(handle_rsi, 0, 0, dataSize, rsi) != dataSize)
        return false;
    
    // Prepare data
    for(int i = 0; i < TrainingPeriod; i++) {
        for(int j = 0; j < InputNeurons; j++) { inputs[i * InputNeurons + j] = rsi[i + j]; } outputs[i] = rsi[i + InputNeurons] > rsi[i + InputNeurons - 1] ? 1 : 0;
    }
    
    return true;
}

//+------------------------------------------------------------------+
//| Prepare input data for prediction                                |
//+------------------------------------------------------------------+
bool PrepareInputData()
{
    ArrayResize(inputData, InputNeurons);
    
    // Get RSI values
    double rsi[];
    if(CopyBuffer(handle_rsi, 0, 0, InputNeurons, rsi) != InputNeurons)
        return false;
    
    // Prepare input data
    for(int i = 0; i < InputNeurons; i++) { inputData[i] = rsi[i]; } return true; } //+------------------------------------------------------------------+ //| Check if position exists | //+------------------------------------------------------------------+ bool PositionExists() { return (PositionsTotal() > 0);
}

//+------------------------------------------------------------------+
//| Open a new order                                                 |
//+------------------------------------------------------------------+
void OpenOrder(ENUM_ORDER_TYPE orderType, double price)
{
    MqlTradeRequest request = {};
    MqlTradeResult result = {};
    
    request.action = TRADE_ACTION_DEAL;
    request.symbol = _Symbol;
    request.volume = LotSize;
    request.type = orderType;
    request.price = price;
    request.deviation = 10;
    request.magic = 123456;
    
    if(!OrderSend(request, result))
        Print("Error opening order: ", GetLastError());
    else
        Print("Order opened successfully. Ticket: ", result.order);
}

 

อธิบายองค์ประกอบของโค้ด:
  1. Input Parameters:
    • กำหนดโครงสร้างของ neural network และพารามิเตอร์สำหรับการเทรนและการเทรด
  2. OnInit():
    • เริ่มต้น RSI indicator และ neural network
  3. OnTick():
    • เตรียมข้อมูล input, ทำนายทิศทางราคา และเปิดออเดอร์ตามผลการทำนาย
  4. InitializeAndTrainNetwork():
    • สร้างและเทรน neural network ด้วยข้อมูลในอดีต
  5. PrepareTrainingData():
    • เตรียมข้อมูลสำหรับการเทรน neural network
  6. PrepareInputData():
    • เตรียมข้อมูล input สำหรับการทำนาย
  7. OpenOrder():
    • เปิดออเดอร์ใหม่ตามผลการทำนายของ neural network
 Exness Promotion
PNFPB Install PWA using share icon

For IOS and IPAD browsers, Install PWA using add to home screen in ios safari browser or add to dock option in macos safari browser