Giter Site home page Giter Site logo

Comments (2)

kenorb avatar kenorb commented on August 28, 2024

Indicator:

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Blue
#property indicator_color2 Red

//--- input parameters
input int period = 20; // Number of bars to use for support/resistance line calculation

//--- indicator buffers
double SupportBuffer[];
double ResistanceBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   // Set indicator short name
   IndicatorShortName("Support & Resistance Lines");

   // Set indicator buffers
   SetIndexBuffer(0, SupportBuffer);
   SetIndexBuffer(1, ResistanceBuffer);

   // Plot the indicator on a separate window
   IndicatorDigits(4);
   IndicatorSetInteger(INDICATOR_DIGITS, 4);
   IndicatorSetInteger(INDICATOR_WIDTH1, 2);
   IndicatorSetInteger(INDICATOR_WIDTH2, 2);

   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 startIdx = MathMax(0, rates_total - period);
   int limit = rates_total - startIdx;

   // Calculate the least squares regression line (support line) and resistance line
   double sumX = 0, sumYLow = 0, sumYHigh = 0, sumXYLow = 0, sumXYHigh = 0, sumXX = 0;
   for (int i = startIdx; i < rates_total; i++)
   {
      double x = i - startIdx;
      double yLow = low[i];
      double yHigh = high[i];

      sumX += x;
      sumYLow += yLow;
      sumYHigh += yHigh;
      sumXYLow += x * yLow;
      sumXYHigh += x * yHigh;
      sumXX += x * x;
   }

   double supportSlope = (period * sumXYLow - sumX * sumYLow) / (period * sumXX - sumX * sumX);
   double supportIntercept = (sumYLow - supportSlope * sumX) / period;

   double resistanceSlope = (period * sumXYHigh - sumX * sumYHigh) / (period * sumXX - sumX * sumX);
   double resistanceIntercept = (sumYHigh - resistanceSlope * sumX) / period;

   // Calculate the support and resistance line values
   for (int i = startIdx; i < rates_total; i++)
   {
      double x = i - startIdx;
      SupportBuffer[i] = supportSlope * x + supportIntercept;
      ResistanceBuffer[i] = resistanceSlope * x + resistanceIntercept;
   }

   return (rates_total);
}

Oscillator:

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Blue

//--- input parameters
input int period = 20; // Number of bars to use for support/resistance line calculation

//--- indicator buffers
double OscillatorBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   // Set indicator short name
   IndicatorShortName("Support & Resistance Oscillator");

   // Set indicator buffers
   SetIndexBuffer(0, OscillatorBuffer);

   // Plot the indicator on a separate window
   IndicatorDigits(4);
   IndicatorSetInteger(INDICATOR_DIGITS, 4);
   IndicatorSetInteger(INDICATOR_WIDTH1, 2);

   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 startIdx = MathMax(0, rates_total - period);
   int limit = rates_total - startIdx;

   // Calculate the least squares regression line (support line) and resistance line
   double sumX = 0, sumYLow = 0, sumYHigh = 0, sumXYLow = 0, sumXYHigh = 0, sumXX = 0;
   for (int i = startIdx; i < rates_total; i++)
   {
      double x = i - startIdx;
      double yLow = low[i];
      double yHigh = high[i];

      sumX += x;
      sumYLow += yLow;
      sumYHigh += yHigh;
      sumXYLow += x * yLow;
      sumXYHigh += x * yHigh;
      sumXX += x * x;
   }

   double supportSlope = (period * sumXYLow - sumX * sumYLow) / (period * sumXX - sumX * sumX);
   double supportIntercept = (sumYLow - supportSlope * sumX) / period;

   double resistanceSlope = (period * sumXYHigh - sumX * sumYHigh) / (period * sumXX - sumX * sumX);
   double resistanceIntercept = (sumYHigh - resistanceSlope * sumX) / period;

   // Calculate the oscillator values
   for (int i = startIdx; i < rates_total; i++)
   {
      double x = i - startIdx;
      double supportValue = supportSlope * x + supportIntercept;
      double resistanceValue = resistanceSlope * x + resistanceIntercept;

      // Calculate the oscillator value as the distance between the current price and the support/resistance lines
      OscillatorBuffer[i] = close[i] - (supportValue + resistanceValue) / 2;
   }

   return (rates_total);
}

from ea31337-classes.

kenorb avatar kenorb commented on August 28, 2024
// Define input parameters
input int loopback = 10; // Number of candles to consider
input double stepIncrement = 0.0005; // Price step increment
input double tolerance = 0.0002; // Tolerance range

// Declare variables
double highest = iHigh(Symbol(), Period(), 0);
double lowest = iLow(Symbol(), Period(), 0);
double storeLevel[];
int commonPoint;

// Calculate highest and lowest levels
for (int i = 1; i <= loopback; i++) {
    double high = iHigh(Symbol(), Period(), i);
    double low = iLow(Symbol(), Period(), i);
    
    if (high > highest) highest = high;
    if (low < lowest) lowest = low;
}

// Initialize tmp with the first high encountered
double tmp = iHigh(Symbol(), Period(), loopback);

// Loop through the price range
for (double actPrice = lowest; actPrice <= highest; actPrice += stepIncrement) {
    int touchedCount = 0;

    for (int i = 1; i <= loopback; i++) {
        double high = iHigh(Symbol(), Period(), i);
        double topRange = tmp + tolerance;
        double bottomRange = tmp - tolerance;

        // Check if the high is within the tolerance range
        if (high <= topRange && high >= bottomRange) {
            touchedCount++;
        }

        // Update tmp if touchedCount reaches 3
        if (touchedCount == 3) {
            storeLevel[i] = tmp;
            touchedCount = 0;
        }
    }

    // Update tmp with the current actPrice
    tmp = actPrice;
}

from ea31337-classes.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.