library(mt5R)
library(forecast)
library(xts)
# Fetch data
symbol <- "EURUSD"
data_xts <- MT5.Quick_GetSymbol(symbol, iTF = 60, iRows = 1000, xts = TRUE)
close_prices <- data_xts$Close
# Initialize parameters
initial_balance <- 10000
balance <- initial_balance
position <- 0 # 0: no position; 1: long; -1: short
entry_price <- NA
exit_price <- NA
trade_size <- 1 # number of lots
lot_size <- 100000 # standard lot size in Forex
n <- length(close_prices)
# Vectors to store history
balance_history <- rep(NA, n)
position_history <- rep(NA, n)
action_history <- rep(NA, n)
prices <- as.numeric(close_prices)
# Starting point (ensure enough data for the initial model)
t0 <- 50
for (t in t0:(n - 3)) {
data_t <- prices[1:t]
model <- auto.arima(data_t)
fc <- forecast(model, h = 3)
# Get price sequence
last_price <- prices[t]
price_sequence <- c(last_price, as.numeric(fc$mean))
# Determine action based on forecast
if (price_sequence[2] > price_sequence[1] &&
price_sequence[3] > price_sequence[2] &&
price_sequence[4] > price_sequence[3]) {
action <- "Buy"
} else if (price_sequence[2] < price_sequence[1] &&
price_sequence[3] < price_sequence[2] &&
price_sequence[4] < price_sequence[3]) {
action <- "Sell"
} else {
action <- "Hold"
}
action_history[t] <- action
# Update position and balance
if (position == 0) {
if (action == "Buy") {
entry_price <- prices[t + 1]
position <- 1
} else if (action == "Sell") {
entry_price <- prices[t + 1]
position <- -1
}
} else if (position == 1) {
if (action == "Sell") {
exit_price <- prices[t + 1]
profit <- (exit_price - entry_price) * lot_size * trade_size
balance <- balance + profit
entry_price <- exit_price
position <- -1
}
} else if (position == -1) {
if (action == "Buy") {
exit_price <- prices[t + 1]
profit <- (entry_price - exit_price) * lot_size * trade_size
balance <- balance + profit
entry_price <- exit_price
position <- 1
}
}
# Mark-to-market balance
current_price <- prices[t + 1]
if (position == 1) {
unrealized_profit <- (current_price - entry_price) * lot_size * trade_size
} else if (position == -1) {
unrealized_profit <- (entry_price - current_price) * lot_size * trade_size
} else {
unrealized_profit <- 0
}
balance_history[t] <- balance + unrealized_profit
position_history[t] <- position
}
# Close any open position at the end
if (position != 0) {
exit_price <- prices[n]
if (position == 1) {
profit <- (exit_price - entry_price) * lot_size * trade_size
} else {
profit <- (entry_price - exit_price) * lot_size * trade_size
}
balance <- balance + profit
position <- 0
balance_history[n] <- balance
} else {
balance_history[n] <- balance
}
# Result
total_profit <- balance - initial_balance
cat("Total Profit/Loss:", total_profit, "\n")
# Plot balance over time
plot(balance_history, type = 'l', main = "Balance Over Time", xlab = "Time", ylab = "Balance")
This code adds an automatic ARIMA model that forecasts three steps ahead using your EURUSD data. If the forecasted price increases three times in a row, it initiates a 'Buy'; if it decreases three times, it initiates a 'Sell'. A virtual balance tracker is included for testing, simulating trades based on these signals. The balance is updated according to the profit or loss from each trade, and a plot of the balance over time is generated for visual analysis.