EA31337 / EA31337-classes

📦📈 EA31337 framework (MQL library for writing trading Expert Advisors, indicators and scripts)
https://ea31337.github.io/EA31337-classes
GNU General Public License v3.0
186 stars 98 forks source link

Calculate psychological numbers #704

Open kenorb opened 1 year ago

kenorb commented 1 year ago

E.g.

   int levelsToCalculate = MathMin(numberOfLevels, rates_total);

   for (int i = 0; i < levelsToCalculate; i++)
     {
      double levelPrice = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_BID) / MathPow(10, MarketInfo(_Symbol, MODE_DIGITS) - 1), MarketInfo(_Symbol, MODE_DIGITS));
      levelPrice = levelPrice + i * MarketInfo(_Symbol, MODE_POINT);

      double roundedLevel = NormalizeDouble(levelPrice, MarketInfo(_Symbol, MODE_DIGITS));

      // Draw horizontal lines at rounded levels
      BufferSupportResistance()[i] = roundedLevel;
     }
kenorb commented 1 year ago

Indicator:

MQL4 indicator that plots horizontal lines on the chart based on the closest psychological levels. The levels are calculated by rounding the current bid price to the nearest psychological level and then drawing horizontal lines at those levels.

#property indicator_separate_window

// Input parameters
input int numberOfLevels = 5; // Number of psychological levels to calculate

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   IndicatorBuffers(1);
   SetIndexBuffer(0, BufferSupportResistance());
   SetIndexStyle(0, DRAW_LINE);
   SetIndexLabel(0, "Psychological Levels");
   SetIndexBuffer(1, NULL); // We only need one buffer
   SetIndexEmptyValue(0, 0.0); // Set the default value for the buffer

   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[])
{
   int levelsToCalculate = MathMin(numberOfLevels, rates_total);

   for (int i = 0; i < levelsToCalculate; i++)
   {
      double levelPrice = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_BID) / MathPow(10, MarketInfo(_Symbol, MODE_DIGITS - 1)), MarketInfo(_Symbol, MODE_DIGITS));
      levelPrice = levelPrice + i * MarketInfo(_Symbol, MODE_POINT);

      double roundedLevel = NormalizeDouble(levelPrice, MarketInfo(_Symbol, MODE_DIGITS));

      // Draw horizontal lines at rounded levels
      BufferSupportResistance()[i] = roundedLevel;
   }

   return(rates_total);
}

//+------------------------------------------------------------------+
//| Custom indicator buffer for support and resistance levels        |
//+------------------------------------------------------------------+
double& BufferSupportResistance()
{
   static double buffer[];
   return(buffer);
}

It will display horizontal lines representing the calculated psychological levels based on the bid price.

Oscillator:

#property indicator_separate_window

// Input parameters
input int numberOfLevels = 5; // Number of psychological levels to calculate

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   IndicatorBuffers(1);
   SetIndexBuffer(0, BufferPsychologicalOscillator());
   SetIndexStyle(0, DRAW_LINE);
   SetIndexLabel(0, "Psychological Oscillator");
   SetIndexEmptyValue(0, 0.0); // Set the default value for the buffer

   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[])
{
   int levelsToCalculate = MathMin(numberOfLevels, rates_total);

   double currentBid = SymbolInfoDouble(_Symbol, SYMBOL_BID);

   for (int i = 0; i < levelsToCalculate; i++)
   {
      double levelPrice = NormalizeDouble(currentBid / MathPow(10, MarketInfo(_Symbol, MODE_DIGITS - 1)), MarketInfo(_Symbol, MODE_DIGITS));
      levelPrice = levelPrice + i * MarketInfo(_Symbol, MODE_POINT);

      double roundedLevel = NormalizeDouble(levelPrice, MarketInfo(_Symbol, MODE_DIGITS));

      // Calculate the difference between the current bid and the rounded level
      double oscillatorValue = currentBid - roundedLevel;

      // Assign the oscillator value to the buffer
      BufferPsychologicalOscillator()[i] = oscillatorValue;
   }

   return(rates_total);
}

//+------------------------------------------------------------------+
//| Custom indicator buffer for psychological oscillator             |
//+------------------------------------------------------------------+
double& BufferPsychologicalOscillator()
{
   static double buffer[];
   return(buffer);
}

It will display an oscillator that represents the difference between the current bid price and the calculated psychological levels. Positive values indicate that the bid price is above the psychological level, and negative values indicate that it's below.