75 lines
3.1 KiB
MQL5
75 lines
3.1 KiB
MQL5
|
|
//+------------------------------------------------------------------+
|
||
|
|
//| CloseAllPositionsOnChart.mq5 |
|
||
|
|
//| Copyright 2024, Google Gemini & MetaQuotes |
|
||
|
|
//| https://www.mql5.com|
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
#property copyright "Copyright 2024, Google Gemini & MetaQuotes"
|
||
|
|
#property link "https://www.mql5.com"
|
||
|
|
#property version "1.10"
|
||
|
|
#property description "Script untuk menutup SEMUA posisi (buy/sell) pada simbol di chart aktif."
|
||
|
|
|
||
|
|
// Menampilkan jendela konfirmasi sebelum script berjalan
|
||
|
|
//#property script_show_confirm
|
||
|
|
|
||
|
|
// Mengimpor library standar untuk fungsi trading
|
||
|
|
#include <Trade\Trade.mqh>
|
||
|
|
|
||
|
|
// Membuat instance dari kelas CTrade untuk eksekusi trading
|
||
|
|
CTrade trade;
|
||
|
|
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
//| Fungsi utama yang dieksekusi saat script dimulai |
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
void OnStart()
|
||
|
|
{
|
||
|
|
// 1. Mendapatkan simbol dari chart yang sedang aktif
|
||
|
|
string currentSymbol = _Symbol;
|
||
|
|
int closedCount = 0; // Variabel untuk menghitung posisi yang berhasil ditutup
|
||
|
|
int totalPositionsOnSymbol = 0;
|
||
|
|
|
||
|
|
Print("Memulai proses penutupan semua posisi untuk simbol: ", currentSymbol);
|
||
|
|
|
||
|
|
// 2. Lakukan perulangan melalui SEMUA posisi yang terbuka
|
||
|
|
// Kita melakukan loop dari belakang ke depan (i--). Ini sangat penting!
|
||
|
|
// Karena saat sebuah posisi ditutup, jumlah total posisi (PositionsTotal()) akan berkurang,
|
||
|
|
// dan indeks posisi lain bisa bergeser. Loop mundur mencegah kita melewatkan posisi apa pun.
|
||
|
|
for(int i = PositionsTotal() - 1; i >= 0; i--)
|
||
|
|
{
|
||
|
|
// Ambil simbol dari posisi pada indeks 'i'
|
||
|
|
string positionSymbol = PositionGetSymbol(i);
|
||
|
|
|
||
|
|
// 3. Cek apakah simbol posisi ini sama dengan simbol chart aktif
|
||
|
|
if(positionSymbol == currentSymbol)
|
||
|
|
{
|
||
|
|
totalPositionsOnSymbol++; // Tambah hitungan posisi yang ditemukan
|
||
|
|
|
||
|
|
// Ambil nomor tiket unik dari posisi ini
|
||
|
|
ulong ticket = PositionGetTicket(i);
|
||
|
|
|
||
|
|
// 4. Coba tutup posisi menggunakan nomor tiketnya
|
||
|
|
if(trade.PositionClose(ticket, 10)) // Slippage 10 poin
|
||
|
|
{
|
||
|
|
Print("Sukses: Posisi #", ticket, " untuk simbol ", currentSymbol, " berhasil ditutup.");
|
||
|
|
closedCount++;
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
Print("Error: Gagal menutup posisi #", ticket, ". Alasan: ", trade.ResultComment());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 5. Berikan laporan akhir setelah loop selesai
|
||
|
|
if(totalPositionsOnSymbol == 0)
|
||
|
|
{
|
||
|
|
Print("Informasi: Tidak ada posisi terbuka yang ditemukan untuk simbol ", currentSymbol, ".");
|
||
|
|
//Alert("Tidak ada posisi terbuka untuk ", currentSymbol, ".");
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
string summary = StringFormat("Proses Selesai.\nBerhasil menutup %d dari %d posisi untuk %s.", closedCount, totalPositionsOnSymbol, currentSymbol);
|
||
|
|
Print(summary);
|
||
|
|
//Alert(summary);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
//+------------------------------------------------------------------+
|