сделай чтобы было другие осциляторы и индиткаторы и чтобы он выбирал по индикаторам а не…
- User
сделай чтобы было другие осциляторы и индиткаторы и чтобы он выбирал по индикаторам а не все сразу
ibrary(quantmod) library(mt5R)
Получаем данные
symbol <- "EURUSD" data <- MT5.Quick_GetSymbol(symbol, 60, 100, TRUE)
Извлекаем цены закрытия
close <- as.vector(data$Close)
Функция для оценки стратегии
evaluate_strategy <- function(short_window, long_window, rsi_period, rsi_threshold) {
Вычисляем скользящие средние
short_ma <- SMA(close, n = short_window) long_ma <- SMA(close, n = long_window)
Вычисляем RSI
rsi <- RSI(close, n = rsi_period)
Генерируем сигналы
signals <- ifelse(short_ma > long_ma & rsi < rsi_threshold, "Buy", ifelse(short_ma < long_ma & rsi > 100 - rsi_threshold, "Sell", "Hold"))
Убираем NA значения
signals[is.na(signals)] <- "Hold"
Оценка прибыльности
returns <- diff(close) # Изменения цен strategy_returns <- ifelse(signals[-1] == "Buy", returns[-length(returns)], ifelse(signals[-1] == "Sell", -returns[-length(returns)], 0))
total_profit <- sum(strategy_returns, na.rm = TRUE) return(total_profit) }
Параметры для оптимизации
short_windows <- 2:10 long_windows <- 10:20 rsi_periods <- 5:14 rsi_thresholds <- c(30, 40, 50)
Хранение результатов
results <- data.frame(Short_MA = integer(), Long_MA = integer(), RSI_Period = integer(), RSI_Threshold = integer(), Total_Profit = numeric())
Оптимизация
for (short_window in short_windows) { for (long_window in long_windows) { for (rsi_period in rsi_periods) { for (rsi_threshold in rsi_thresholds) { profit <- evaluate_strategy(short_window, long_window, rsi_period, rsi_threshold) results <- rbind(results, data.frame(Short_MA = short_window, Long_MA = long_window, RSI_Period = rsi_period, RSI_Threshold = rsi_threshold, Total_Profit = profit)) } } } }
Находим наилучшие параметры
best_strategy <- results[which.max(results$Total_Profit), ]
Выводим результаты
cat("Лучшие параметры стратегии:\n") print(best_strategy)
- Assistant