Files:
Error.jpg
101 kb
In mql5, you need to use the MqlDateTime struct
please see all differences in the code
//+------------------------------------------------------------------+ //| MondayBreak.mq5 | //| Copyright 2023, MetaQuotes Ltd. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2023, MetaQuotes Ltd." #property link "https://www.mql5.com" #property version "1.00" #property indicator_chart_window #property indicator_buffers 2 #property indicator_plots 2 #property indicator_type1 DRAW_LINE #property indicator_color1 clrSkyBlue #property indicator_width1 2 #property indicator_type2 DRAW_LINE #property indicator_color2 clrRed #property indicator_width2 2 #define SUNDAY 0 #define MONDAY 1 #define TUESDAY 2 #define WEDNESDAY 3 #define THURSDAY 4 #define FRIDAY 5 #define SATURDAY 6 // Indicator buffers for Monday High and Low double MondayHighBuffer[]; double MondayLowBuffer[]; // Variables to store Monday's High and Low double MondayHigh = 0; double MondayLow = 0; // Variable to store the timestamp of the most recent Monday datetime lastMondayTime = 0; // Structure to hold date and time components MqlDateTime dt; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { // Indicator buffers mapping SetIndexBuffer(0, MondayHighBuffer); SetIndexBuffer(1, MondayLowBuffer); // Set buffer labels for display PlotIndexSetString(0, PLOT_LABEL, "Monday High"); PlotIndexSetString(1, PLOT_LABEL, "Monday Low"); // Initialize buffers with empty values ArrayInitialize(MondayHighBuffer, 0); ArrayInitialize(MondayLowBuffer, 0); return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[]) { // start searching from the most recent bar and go backward (rates_total-1 is the last bar) for(int i = rates_total - 1; i >= 0; i--) { TimeToStruct(time[i], dt); // Convert time to MqlDateTime structure // Check if this bar is a Monday and it's a new week if(dt.day_of_week == MONDAY) { // Reset the MondayHigh and MondayLow MondayHigh = high[i]; MondayLow = low[i]; if(high[i] > MondayHigh) MondayHigh = high[i]; if(low[i] < MondayLow) MondayLow = low[i]; break; // break out of the loop so it doesn't iterate backwards to previous Mondays } } // Populate the buffers with the most recent Monday's high and low values for(int i = 0; i < rates_total; i++) { MondayHighBuffer[i] = MondayHigh; MondayLowBuffer[i] = MondayLow; } // Check if the current price breaches Monday's High or Low double currentPrice = close[rates_total - 1]; if(currentPrice > MondayHigh) { // Breach of Monday's High Comment("Price has breached Monday's High!"); } else if(currentPrice < MondayLow) { // Breach of Monday's Low Comment("Price has breached Monday's Low!"); } else { Comment(""); // Clear comment if no breach } return(rates_total); } //+------------------------------------------------------------------+
I didn't do much debugging.
Using this approach by taking the high and low buffers from the OnCalculate signature means that you will need to be on the D1 timeframe to get the correct outcome. These buffers signify the OHLC of the current period. In this case it might even be appropriate to automatically trigger the D1 timeframe when the indicator starts by writing this in the initializer:
ChartSetSymbolPeriod(0, Symbol(), PERIOD_D1);
Thank you so much. Your assistance is much appreciated. Cheers!
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register