การประยุกต์โมเดล ARIMA เพื่อสร้าง Expert Advisor ในการพยากรณ์ราคาสินทรัพย์
ARIMA เป็นโมเดลสำหรับการพยากรณ์ข้อมูลที่เปลี่ยนแปลงตามเวลา เช่น ยอดขาย หุ้น หรืออัตราเงินเฟ้อ โดยอาศัยความสัมพันธ์ของข้อมูลในอดีตเพื่อทำนายอนาคต เหมาะสำหรับข้อมูลที่มีแนวโน้มและไม่มีรูปแบบฤดูกาลชัดเจน
ส่วนประกอบของ ARIMA
- AR (AutoRegressive): ใช้ข้อมูลในอดีต (Lagged Values) ทำนายค่าปัจจุบัน
- I (Integrated): ทำข้อมูลให้คงที่ (Stationary) ด้วยการหาความแตกต่าง (Differencing)
- MA (Moving Average): ใช้ค่าความผิดพลาด (Errors) ในอดีตมาปรับปรุงการพยากรณ์
พารามิเตอร์ของ ARIMA
- p: จำนวน Lag ที่ใช้ในส่วน AR
- d: ระดับของการ Differencing
- q: จำนวน Lag ที่ใช้ในส่วน MA
ขั้นตอนการใช้งาน ARIMA
- เตรียมข้อมูล: ตรวจสอบความคงที่ (Stationarity) และปรับด้วย Differencing หากจำเป็น
- เลือกพารามิเตอร์ (p, d, q): ใช้ ACF (Autocorrelation Function) และ PACF (Partial Autocorrelation Function)
- สร้างโมเดลและพยากรณ์: ใช้โมเดล 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);
}
ขั้นตอนการใช้งาน
- ติดตั้ง EA บน MetaTrader 4:
- วางโค้ดในโฟลเดอร์ Experts
- คอมไพล์โค้ดใน MetaEditor
- ติดตั้ง EA บนกราฟ
- ปรับค่าพารามิเตอร์:
- กำหนดค่า p, d, q ให้เหมาะสมกับตลาด
- ทดสอบบนบัญชีเดโม่ก่อนใช้งานจริง
ข้อควรระวัง
- ตัวอย่างนี้ไม่ได้ 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());
}
}
}
ทิ้งคำตอบไว้
- 41 ฟอรัม
- 1,318 หัวข้อ
- 3,708 กระทู้
- 46 ออนไลน์
- 1,446 สมาชิก