Soal 2: Sebagai seorang trader forex, Anda memiliki strategi trading berbasis analisis teknikal menggunakan kombinasi Moving Average dan Relative Strength Index (RSI). Buatlah sebuah EA menggunakan MQL4 yang tidak hanya memasukkan kedua indikator tersebut sebagai filter untuk entry dan exit, tetapi juga mampu mengoptimalkan parameter-parameter indikator tersebut secara otomatis berdasarkan kondisi pasar terkini.
A.
void ManagePortfolio() {
double accountBalance = AccountBalance();
double riskPerTrade = 0.02; // Risiko per transaksi dalam persentase modal
double maxRisk = accountBalance * riskPerTrade;
double volatilities[] = {0.015, 0.012, 0.018}; // Volatilitas masing-masing pasangan mata uang
double lotSizes[];
for (int i = 0; i < ArraySize(volatilities); i++) {
lotSizes[i] = maxRisk / (volatilities[i] * 1000); // 1000 adalah nilai per pip untuk pasangan mata uang tertentu
Print("Lot size for pair ", i, ": ", lotSizes[i]);
}
}
B.
void TradingStrategy() {
double maValue = iMA(Symbol(), 0, 14, 0, MODE_SMA, PRICE_CLOSE, 0);
double rsiValue = iRSI(Symbol(), 0, 14, PRICE_CLOSE, 0);
if (Close[1] < maValue && Close > maValue && rsiValue < 30) {
// Syarat untuk Buy
OrderSend(Symbol(), OP_BUY, 0.1, Ask, 3, 0, 0, "Buy Order", 12345, 0, Green);
} else if (Close[1] > maValue && Close < maValue && rsiValue > 70) {
// Syarat untuk Sell
OrderSend(Symbol(), OP_SELL, 0.1, Bid, 3, 0, 0, "Sell Order", 12345, 0, Red);
}
}
C.
void AdjustStopLoss() {
double currentBid = Bid;
double stopLoss = OrderStopLoss();
if (currentBid > stopLoss) {
// Naikkan stop loss jika harga bergerak naik
double newStopLoss = currentBid - 100 * Point; // Contoh penyesuaian sebesar 100 pips
OrderModify(OrderTicket(), OrderOpenPrice(), newStopLoss, OrderTakeProfit(), 0, Green);
}
}
D.
void AdaptiveTimeFrameEA() {
int currentTF = Period();
if (currentTF == PERIOD_H1) {
// Strategi untuk H1
double maValue = iMA(Symbol(), PERIOD_H1, 14, 0, MODE_SMA, PRICE_CLOSE, 0);
// ... Lakukan operasi trading untuk H1
} else if (currentTF == PERIOD_D1) {
// Strategi untuk D1
double maValue = iMA(Symbol(), PERIOD_D1, 14, 0, MODE_SMA, PRICE_CLOSE, 0);
// ... Lakukan operasi trading untuk D1
}
}
E.
void AnalyzeTradingResults() {
double totalProfit = 0;
double totalLoss = 0;
int totalTrades = 0;
for (int i = 0; i < OrdersHistoryTotal(); i++) {
if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) {
if (OrderType() == OP_BUY) {
totalProfit += OrderProfit();
} else if (OrderType() == OP_SELL) {
totalLoss += OrderLoss();
}
totalTrades++;
}
}
double averageProfit = totalProfit / totalTrades;
double averageLoss = totalLoss / totalTrades;
Print("Average Profit per trade: ", averageProfit);
Print("Average Loss per trade: ", averageLoss);
}