4kk4.MQL5/Indicators/KG Average HLCC.mq5
clarissya 2394964283 Scripts
2026-04-27 07:14:01 +07:00

165 行
无行尾
15 KiB
MQL5

//+------------------------------------------------------------------+
//| KG Average HLCC.mq5 |
//| Copyright © 2024, Generated by Gemini AI (Google) |
//| Original MQL4 Copyright: Copyright © 2008, Goen |
//| Link: http://www.forexindo.com/forum/showthread.php?t=95 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, Goen. MQ5 Conversion by Gemini AI."
#property link " "
#property version "1.00"
#property indicator_chart_window
#property indicator_buffers 3 // Jumlah total buffer
#property indicator_plots 3 // Jumlah seri gambar
// Plot 1: Rata-rata Bulanan
#property indicator_label1 "Average Month" // Label untuk plot 1
#property indicator_type1 DRAW_LINE // Jenis gambar: garis
#property indicator_color1 clrAqua // Warna untuk plot 1
#property indicator_style1 STYLE_SOLID // Gaya garis: solid
#property indicator_width1 2 // Lebar garis
// Plot 2: Rata-rata Mingguan
#property indicator_label2 "Average Week"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrMagenta
#property indicator_style2 STYLE_SOLID
#property indicator_width2 2
// Plot 3: Rata-rata Harian
#property indicator_label3 "Average Day"
#property indicator_type3 DRAW_LINE
#property indicator_color3 clrLime
#property indicator_style3 STYLE_SOLID
#property indicator_width3 2
//---- Buffer indikator
double AverageMonthBuffer[]; // Buffer untuk rata-rata bulanan
double AverageWeekBuffer[]; // Buffer untuk rata-rata mingguan
double AverageDayBuffer[]; // Buffer untuk rata-rata harian
//+------------------------------------------------------------------+
//| Fungsi inisialisasi indikator kustom |
//+------------------------------------------------------------------+
int OnInit()
{
// Menghubungkan buffer dengan plot
SetIndexBuffer(0, AverageMonthBuffer, INDICATOR_DATA); //
PlotIndexSetString(0, PLOT_LABEL, "Average Month"); //
PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE); // Nilai kosong untuk plot
SetIndexBuffer(1, AverageWeekBuffer, INDICATOR_DATA); //
PlotIndexSetString(1, PLOT_LABEL, "Average Week"); //
PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, EMPTY_VALUE);
SetIndexBuffer(2, AverageDayBuffer, INDICATOR_DATA); //
PlotIndexSetString(2, PLOT_LABEL, "Average Day"); //
PlotIndexSetDouble(2, PLOT_EMPTY_VALUE, EMPTY_VALUE);
IndicatorSetString(INDICATOR_SHORTNAME, "KG Average HLCC"); //
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Fungsi perhitungan indikator kustom |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total, // Jumlah total bar dalam riwayat harga saat ini
const int prev_calculated, // Jumlah bar yang dihitung pada panggilan sebelumnya
const datetime &time[], // Array waktu pembukaan bar
const double &open[], // Array harga pembukaan
const double &high[], // Array harga tertinggi
const double &low[], // Array harga terendah
const double &close[], // Array harga penutupan
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
int first_bar_to_calculate;
if(prev_calculated == 0) // Jika ini adalah perhitungan pertama
{
ArrayInitialize(AverageMonthBuffer, EMPTY_VALUE);
ArrayInitialize(AverageWeekBuffer, EMPTY_VALUE);
ArrayInitialize(AverageDayBuffer, EMPTY_VALUE);
first_bar_to_calculate = 0; // Hitung semua bar
}
else if (rates_total < prev_calculated) // Jika ada perubahan riwayat besar (misalnya, hole data)
{
// Lebih aman untuk menginisialisasi ulang dan memulai dari 0
ArrayInitialize(AverageMonthBuffer, EMPTY_VALUE);
ArrayInitialize(AverageWeekBuffer, EMPTY_VALUE);
ArrayInitialize(AverageDayBuffer, EMPTY_VALUE);
first_bar_to_calculate = 0;
}
else
{
first_bar_to_calculate = prev_calculated - 1; // Hitung ulang bar terakhir dan bar baru
if (first_bar_to_calculate < 0) first_bar_to_calculate = 0;
}
// Buffer untuk mengambil data harga dari timeframe yang lebih tinggi
double htf_high[1], htf_low[1], htf_close[1];
for(int i = first_bar_to_calculate; i < rates_total; i++)
{
if (time[i] == 0) continue; // Lewati jika waktu bar tidak valid
// Rata-rata Bulanan: (Tinggi + Rendah + Tutup + Tutup) / 4 dari bulan SEBELUMNYA yang telah selesai
// Menggunakan iBarShift untuk menemukan bar yang sesuai pada timeframe MN1, lalu +1 untuk mendapatkan bar sebelumnya yang telah selesai.
int shift_m = iBarShift(_Symbol, PERIOD_MN1, time[i], false) + 1; //
if(shift_m > 0 && // Pastikan shift valid
CopyHigh(_Symbol, PERIOD_MN1, shift_m, 1, htf_high) == 1 && // Ambil data High
CopyLow(_Symbol, PERIOD_MN1, shift_m, 1, htf_low) == 1 && // Ambil data Low
CopyClose(_Symbol, PERIOD_MN1, shift_m, 1, htf_close) == 1) // Ambil data Close
{
AverageMonthBuffer[i] = (htf_high[0] + htf_low[0] + htf_close[0] + htf_close[0]) / 4.0; //
}
else // Jika gagal mendapatkan data
{
if (i > 0 && AverageMonthBuffer[i-1] != EMPTY_VALUE) {
AverageMonthBuffer[i] = AverageMonthBuffer[i-1]; // Gunakan nilai sebelumnya jika ada
} else {
AverageMonthBuffer[i] = EMPTY_VALUE; // Jika tidak, set ke nilai kosong
}
}
// Rata-rata Mingguan: (Tinggi + Rendah + Tutup + Tutup) / 4 dari minggu SEBELUMNYA yang telah selesai
int shift_w = iBarShift(_Symbol, PERIOD_W1, time[i], false) + 1; //
if(shift_w > 0 &&
CopyHigh(_Symbol, PERIOD_W1, shift_w, 1, htf_high) == 1 &&
CopyLow(_Symbol, PERIOD_W1, shift_w, 1, htf_low) == 1 &&
CopyClose(_Symbol, PERIOD_W1, shift_w, 1, htf_close) == 1)
{
AverageWeekBuffer[i] = (htf_high[0] + htf_low[0] + htf_close[0] + htf_close[0]) / 4.0; //
}
else
{
if (i > 0 && AverageWeekBuffer[i-1] != EMPTY_VALUE) {
AverageWeekBuffer[i] = AverageWeekBuffer[i-1];
} else {
AverageWeekBuffer[i] = EMPTY_VALUE;
}
}
// Rata-rata Harian: (Tinggi + Rendah + Tutup + Tutup) / 4 dari hari SEBELUMNYA yang telah selesai
int shift_d = iBarShift(_Symbol, PERIOD_D1, time[i], false) + 1; //
if(shift_d > 0 &&
CopyHigh(_Symbol, PERIOD_D1, shift_d, 1, htf_high) == 1 &&
CopyLow(_Symbol, PERIOD_D1, shift_d, 1, htf_low) == 1 &&
CopyClose(_Symbol, PERIOD_D1, shift_d, 1, htf_close) == 1)
{
AverageDayBuffer[i] = (htf_high[0] + htf_low[0] + htf_close[0] + htf_close[0]) / 4.0; //
}
else
{
if (i > 0 && AverageDayBuffer[i-1] != EMPTY_VALUE) {
AverageDayBuffer[i] = AverageDayBuffer[i-1];
} else {
AverageDayBuffer[i] = EMPTY_VALUE;
}
}
}
return(rates_total); // Kembalikan jumlah bar yang dihitung
}
//+------------------------------------------------------------------+