MT5 chart grid - page 2

 
the strategy consists in identifying the first price of the grid on the y axis at the top of the graph, note that this price must correspond to the price which is displayed after 3 zooming of the graph.

in real time, after some movements of the chart this price changes, either up or down; I would like to make sure that there is an alert if there is a difference of 0.5 when changing this price.

the price in x3 zoom must always be the starting price on which the difference in variations must be based.

I tried to do it with some personal codes but I quickly realized that it was really not that; it blocks me.


here is what i wrote as code :


/ input
input double AlertThreshold = 0.5;

int previousTickCount;
double previousValue;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   previousTickCount = 0;
   previousValue = 0.0;
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   // Oactual ticks (new zoom)
   int currentTickCount = ChartGetInteger(0, CHART_VISIBLE_BARS);

   // check if it is zoom x3
   if(currentTickCount != previousTickCount)
     {
      // first visible value on the graph
      double firstVisibleValue = iClose(Symbol(), 0, ChartGetInteger(0, CHART_FIRST_VISIBLE_BAR));

      // check variation
      if(MathAbs(firstVisibleValue - previousValue) >= AlertThreshold)
        {
         if(firstVisibleValue > previousValue)
           {
            Alert("positive Variation from ", DoubleToString(firstVisibleValue - previousValue, _Digits));
           }
         else
           {
            Alert("negative Variation from ", DoubleToString(previousValue - firstVisibleValue, _Digits));
           }
        }

      previousTickCount = currentTickCount;
      previousValue = firstVisibleValue;
     }
  }
//+------------------------------------------------------------------+


here is an image where i circled the seed value as an example


price

 

The way I would think about this is to calculate the highest highs from the beginning of the current month to now, since the top candle wick of the highest price corresponds to the highest value on the graph.

When you say 0.5, you could say 500 points, but the value of 1 point differs between different symbols.

 
phade #:

The way I would think about this is to calculate the highest highs from the beginning of the current month to now, since the top candle wick of the highest price corresponds to the highest value on the graph.

When you say 0.5, you could say 500 points, but the value of 1 point differs between different instruments.

I attached a script

I admit it's a great idea.

 

I made a mistake saying 500*_Point would be 0.5

You can find out through a print.

so if you want to find a 0.5 rate different in the price you should change the "required_distance" variable to this:

double required_difference = 50000 * point_size;

Test:

Print("50000 points means ", required_difference);


//+------------------------------------------------------------------+
//|                                      HighestPriceAlerter.mq5 |
//|                                 Copyright 2023, YourNameHere    |
//|                                              https://www.example.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, YourNameHere"
#property link      "https://www.example.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1
#property indicator_type1   DRAW_ARROW
#property indicator_color1  clrRed
#property indicator_width1  2
#property indicator_label1  "DailyHighAlert"


input int alert_frequency_in_minutes = 15; // Adjust this to control how frequently the alerts are triggered

//--- Indicator buffers
double dailyHighBuffer[];
int bars_per_day = 1;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
    //--- indicator buffers mapping
    SetIndexBuffer(0, dailyHighBuffer, INDICATOR_DATA);

    //--- set the initial value for the indicator buffer
    ArrayInitialize(dailyHighBuffer, 0);
    
    PlotIndexSetInteger(0, PLOT_ARROW, 233); //shift the arrow vertically
    PlotIndexSetInteger(0, PLOT_ARROW_SHIFT, 50); //shift the arrow vertically
    
    
    //--- Calculate the number of bars in a day (assuming a 24-hour forex market)
    int period_seconds = PeriodSeconds(PERIOD_M15); // use 15 minute intervals for judgement (must use a time-based period)
    if (period_seconds == 0)
    {
        Print("Error: Invalid period specified for the indicator.");
        return 0;
    }

    // Calculate bars_per_day only for time-based periods (greater than 0)

    if (period_seconds >= 60) // Period is 1 minute or higher
    {
        bars_per_day = 24 * 60 / period_seconds;
    }

    // Make sure bars_per_day is not zero
    if (bars_per_day == 0)
    {
        Print("Error: Invalid period specified for the indicator.");
        return 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[])
{
    //--- Check for invalid inputs
    if (rates_total <= 0 || prev_calculated < 0)
        return 0;

    //--- Calculate the current bar's index in the rates array
    int current_bar_index = rates_total - 1;

    //--- Calculate the current month's starting index and ending index
    MqlDateTime mqlTime;
    MqlDateTime bar_mqlTime;
    
    datetime current_time = time[current_bar_index];
  
    TimeToStruct(current_time, mqlTime);
    int month_start_index = 0;
    
    while (month_start_index < rates_total)
    {
        datetime bar_time = time[month_start_index];      
        TimeToStruct(bar_time, bar_mqlTime);
        
        if (bar_mqlTime.year == mqlTime.year && bar_mqlTime.mon == mqlTime.mon)
            break;
        month_start_index++;
    }
    
    int month_end_index = rates_total - 1;

    //--- Find the highest high of the current month
    double monthly_high = high[month_start_index];
    for (int i = month_start_index + 1; i <= month_end_index; i++)
    {
        if (high[i] > monthly_high)
            monthly_high = high[i];
    }

    //--- Store the monthly high in the indicator buffer
    dailyHighBuffer[0] = monthly_high;

/*** Don't need to make this alert

    Test the alert to get the monthly high value
    --- Check if the current price reaches the monthly high
    if (close[0] >= monthly_high)
    {
        //--- Trigger an alert
        static datetime last_alert_time = TimeCurrent();
        datetime current_time = TimeCurrent();
        if (int(current_time - last_alert_time) >= alert_frequency_in_minutes * 60)
        {
            Alert("Price has reached the monthly high: ", DoubleToString(monthly_high, _Digits));
            last_alert_time = current_time;
        }
    }
***/  
    //--- Check if the current price reaches the monthly high with a difference of 0.5 (50,000 points) and then make an alert
    double current_price = close[current_bar_index];
    double price_difference = MathAbs(current_price - monthly_high);
    double point_size = SymbolInfoDouble(Symbol(), SYMBOL_POINT);
    double required_difference = 50000 * point_size;

    if (price_difference >= required_difference)
    {
        //--- Trigger an alert
        static datetime last_alert_time = TimeCurrent(); // static variable to hold the timestamp when condition occurred
        datetime current_time = TimeCurrent(); // time constantly changing (non static)
        if (int(current_time - last_alert_time) >= alert_frequency_in_minutes * 60)
        {
            Alert("Price has reached a difference of 0.5 to the months highs: ", DoubleToString(monthly_high, _Digits));
            last_alert_time = current_time;
        }
    }

    return rates_total;
}


void OnDeinit(const int reason)
{
    // Clear the buffer and reset the alert
    ArrayInitialize(dailyHighBuffer, 0);

    // Reset any previous alerts
    ResetLastError();
    MessageBox("Indicator unloaded. Previous alerts have been cleared.", "Indicator Unloaded", MB_ICONINFORMATION);
}
 
excuse my delay.

I would like to know if it is possible to calculate the gap between MT5 grids
 
Tete Adate Adjete #:
excuse my delay.

I would like to know if it is possible to calculate the gap between MT5 grids

I don't know if there is an existing function to do so. I found through my own indicator which uses OnChartEvent that y = 9 at the highest grid line value. Then y seems to increase as you move down to the next subsequent horizontal line on the grid (which is a bit strange...but that's the way the software works). Each grid box is a gap of roughly 32 pixels.


Now without using OnChartEvent you can do something like this to do things based on the grid line price values:

int OnInit(){
   
  double chartHeightInPixels = ChartGetInteger(0, CHART_HEIGHT_IN_PIXELS);
  double priceRange = ChartGetDouble(0, CHART_PRICE_MAX) - ChartGetDouble(0, CHART_PRICE_MIN);
  double pixelsPerPrice = chartHeightInPixels / priceRange;
  
  double pixelY = 9;  // the highest horizontal line on the grid
  double priceAtPixel = ChartGetDouble(0, CHART_PRICE_MAX) - (pixelY / pixelsPerPrice);
  
  Print("Price at this y-val: ", priceAtPixel);

  return(INIT_SUCCEEDED);
}

It's hacky and complicated, and maybe there is something I don't know about, but perhaps this can work for your use case (if you just keep adding 32 to the pixelY variable, and start at value of 9)

Files: