การแจ้งเตือน
ลบทั้งหมด

การประยุกต์โมเดล ARIMA เพื่อสร้าง Expert Advisor ในการพยากรณ์ราคาสินทรัพย์

1 กระทู้
1 ผู้ใช้
0 Reactions
62 เข้าชม
James Albert
(@james-albert)
สมาชิก
Rank F
เข้าร่วม: 6 เดือน ที่ผ่านมา
กระทู้: 128
หัวข้อเริ่มต้น  

ARIMA เป็นโมเดลสำหรับการพยากรณ์ข้อมูลที่เปลี่ยนแปลงตามเวลา เช่น ยอดขาย หุ้น หรืออัตราเงินเฟ้อ โดยอาศัยความสัมพันธ์ของข้อมูลในอดีตเพื่อทำนายอนาคต เหมาะสำหรับข้อมูลที่มีแนวโน้มและไม่มีรูปแบบฤดูกาลชัดเจน

ส่วนประกอบของ ARIMA

  1. AR (AutoRegressive): ใช้ข้อมูลในอดีต (Lagged Values) ทำนายค่าปัจจุบัน
  2. I (Integrated): ทำข้อมูลให้คงที่ (Stationary) ด้วยการหาความแตกต่าง (Differencing)
  3. MA (Moving Average): ใช้ค่าความผิดพลาด (Errors) ในอดีตมาปรับปรุงการพยากรณ์

พารามิเตอร์ของ ARIMA

  • p: จำนวน Lag ที่ใช้ในส่วน AR
  • d: ระดับของการ Differencing
  • q: จำนวน Lag ที่ใช้ในส่วน MA

ขั้นตอนการใช้งาน ARIMA

  1. เตรียมข้อมูล: ตรวจสอบความคงที่ (Stationarity) และปรับด้วย Differencing หากจำเป็น
  2. เลือกพารามิเตอร์ (p, d, q): ใช้ ACF (Autocorrelation Function) และ PACF (Partial Autocorrelation Function)
  3. สร้างโมเดลและพยากรณ์: ใช้โมเดล ARIMA ทำนายค่าข้อมูลในอนาคต

โค้ดตัวอย่าง EA ที่ใช้โมเดล ARIMA ใน MQL4

ด้านล่างนี้คือโครงสร้างของ EA (Expert Advisor) สำหรับ MetaTrader 4:

ส่วนกำหนดคุณสมบัติ

#property copyright "James Albert"

#property link      "https://www.mql5.com"

#property version   "1.00"

#property strict

  • ระบุชื่อเจ้าของ EA, ลิงก์อ้างอิง และเวอร์ชัน

พารามิเตอร์นำเข้า (Input Parameters)

input int p = 1;       // AR order

input int d = 1;       // Differencing order

input int q = 1;       // MA order

input int forecast_horizon = 1; // ระยะเวลาพยากรณ์ (bars)

ตัวแปรเก็บข้อมูล

double coeffAR[];      // ค่าสัมประสิทธิ์ AR

double coeffMA[];      // ค่าสัมประสิทธิ์ MA

double forecast;       // ค่าที่พยากรณ์

double PriceSeries[];  // ชุดข้อมูลราคา

ฟังก์ชันเริ่มต้นและปิดการทำงาน

int OnInit()

{

   Print("ARIMA Forecast EA Initialized.");

   return(INIT_SUCCEEDED);

}

 

void OnDeinit(const int reason)

{

   Print("ARIMA Forecast EA Deinitialized.");

}

ฟังก์ชัน OnTick

void OnTick()

{

   Arisma();

}

ฟังก์ชัน Arisma

  1. ตรวจสอบข้อมูล:

if(Bars < p + d + q + forecast_horizon)

{

   Print("Not enough data for ARIMA model.");

   return;

}

   2. คำนวณ Differencing:

ArraySetAsSeries(PriceSeries, true);

CopyClose(Symbol(), Period(), 0, Bars, PriceSeries);

 

double diffPrices[];

ArrayResize(diffPrices, Bars - d);

for(int i = d; i < Bars; i++)

{

   diffPrices[i - d] = PriceSeries[i] - PriceSeries[i - d];

}

  3. กำหนดค่าสัมประสิทธิ์ AR และ MA:

ArrayResize(coeffAR, p);

ArrayResize(coeffMA, q);

for(int i = 0; i < p; i++) coeffAR[i] = 0.5;

for(int i = 0; i < q; i++) coeffMA[i] = 0.5;

  4. คำนวณค่าพยากรณ์:

forecast = 0.0;

 

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

   forecast += coeffAR[i] * diffPrices[Bars - d - 1 - i];

 

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

   forecast += coeffMA[i] * (PriceSeries[Bars - 1 - i] - diffPrices[Bars - d - 1 - i]);

 

forecast += PriceSeries[Bars - d - 1];

    5. ตรรกะการซื้อขาย:

double currentPrice = Close[0];

if(forecast > currentPrice)

{

   if(OrdersTotal() == 0)

      OrderSend(Symbol(), OP_BUY, 0.01, Ask, 3, 0, 0, "Buy based on ARIMA forecast", 0, 0, Blue);

}

else if(forecast < currentPrice)

{

   if(OrdersTotal() == 0)

      OrderSend(Symbol(), OP_SELL, 0.01, Bid, 3, 0, 0, "Sell based on ARIMA forecast", 0, 0, Red);

}

ขั้นตอนการใช้งาน

  1. ติดตั้ง EA บน MetaTrader 4:
    • วางโค้ดในโฟลเดอร์ Experts
    • คอมไพล์โค้ดใน MetaEditor
    • ติดตั้ง EA บนกราฟ
  2. ปรับค่าพารามิเตอร์:
    • กำหนดค่า p, d, q ให้เหมาะสมกับตลาด
  3. ทดสอบบนบัญชีเดโม่ก่อนใช้งานจริง

ข้อควรระวัง

  • ตัวอย่างนี้ไม่ได้ Optimize หรือ Train โมเดลอย่างเต็มรูปแบบ
  • ควรปรับแต่งค่าสัมประสิทธิ์ AR และ MA ให้เหมาะสม
  • ทดสอบ EA ก่อนใช้งานจริงเสมอ

หมายเหตุ: โค้ดนี้เป็นเพียงตัวอย่างเบื้องต้น ผู้ใช้งานสามารถปรับปรุงเพื่อเพิ่มประสิทธิภาพตามความต้องการ.

Source Code :

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

//|                                                @ARIMA Forecast EA.mq4 |

//|                                                                   James Albert |

//|                                                     https://www.mql5.com |

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

#property copyright "James Albert"

#property link      "https://www.mql5.com"

#property version   "1.00"

#property strict

 

// Input parameters for ARIMA settings

input int p = 1;       // AR order

input int d = 1;       // Differencing order

input int q = 1;       // MA order

input int forecast_horizon = 1; // Forecast horizon (in bars)

 

// Global variables

double coeffAR[];  // Coefficients for AR terms

double coeffMA[];  // Coefficients for MA terms

double forecast;   // Forecasted price value

 

double PriceSeries[];  // Historical price series

 

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

//| Expert initialization function                                              |

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

int OnInit()

{

   Print("ARIMA Forecast EA Initialized.");

   return(INIT_SUCCEEDED);

}

 

 

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

//| Expert deinitialization function                                          |

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

void OnDeinit(const int reason)

{

   Print("ARIMA Forecast EA Deinitialized.");

}

 

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

//| Expert tick function                                                         |

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

void OnTick()

{

  Arisma();

}

 

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

//| Arisma function                                                               |

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

void Arisma()

{

   int totalBars = Bars;

 

   // Ensure we have enough data

   if(totalBars < p + d + q + forecast_horizon)

   {

      Print("Not enough data for ARIMA model.");

      return;

   }

 

   // Get historical prices

   ArraySetAsSeries(PriceSeries, true);

   CopyClose(Symbol(), Period(), 0, totalBars, PriceSeries);

 

   // Perform differencing (d order)

   double diffPrices[];

   ArrayResize(diffPrices, totalBars - d);

   for(int i = d; i < totalBars; i++)

   {

      diffPrices[i - d] = PriceSeries[i] - PriceSeries[i - d];

   }

 

   // Fit AR and MA coefficients (simplified example: random initialization)

   ArrayResize(coeffAR, p);

   ArrayResize(coeffMA, q);

   for(int i = 0; i < p; i++) coeffAR[i] = 0.5; // Example AR coefficients

   for(int i = 0; i < q; i++) coeffMA[i] = 0.5; // Example MA coefficients

 

   // Forecast next price (simplified ARIMA calculation)

   forecast = 0.0;

 

   // Apply AR terms

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

   {

      forecast += coeffAR[i] * diffPrices[totalBars - d - 1 - i];

   }

 

   // Apply MA terms

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

   {

      forecast += coeffMA[i] * (PriceSeries[totalBars - 1 - i] - diffPrices[totalBars - d - 1 - i]);

   }

 

   // Reverse differencing to get actual price forecast

   forecast += PriceSeries[totalBars - d - 1];

 

   // Output forecasted price

   Print("Forecasted Price: ", forecast);

 

   // Example: Trading logic based on forecast

   double currentPrice = Close[0];

   int ticket;

   if(forecast > currentPrice)

   {

      if(OrdersTotal() == 0) // Open a buy order if no orders are open

      {

         ticket = OrderSend(Symbol(), OP_BUY, 0.01, Ask, 3, 0, 0, "Buy based on ARIMA forecast", 0, 0, Blue);

         if(ticket < 0)

            Print("OrderSend failed with error #", GetLastError());

      }

   }

   else if(forecast < currentPrice)

   {

      if(OrdersTotal() == 0) // Open a sell order if no orders are open

      {

         ticket = OrderSend(Symbol(), OP_SELL, 0.01, Bid, 3, 0, 0, "Sell based on ARIMA forecast", 0, 0, Red);

         if(ticket < 0)

            Print("OrderSend failed with error #", GetLastError());

      }

   }

}

 


   
อ้างอิง

ทิ้งคำตอบไว้

ชื่อผู้แต่ง

อีเมลผู้เขียน

ตำแหน่ง *

You are not allowed to attach files on this forum. It is possible that you have not reached the minimum required number of posts, or your user group does not have permission to attach files in this forum.
 
ดูตัวอย่าง แก้ไข 0 ครั้ง บันทึกแล้ว
แบ่งปัน: