Higher high / Low ; Lower Low / High indicator

Created at 09 Oct 2012, 15:30
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
PS

PsykotropyK

Joined 17.09.2012

Higher high / Low ; Lower Low / High indicator
09 Oct 2012, 15:30


Hello all,

 

I developped an indicator on MT4 to find higher highs / higher lows during a bullish trend and lower lows / lower highs during a bearish trend. Change in trend were occuring when, during a bullish trend, the price broke the support (low) and on a bearish trend when my price broke the resistance (high). The result of the indicator was as following:

 

 

I transfered the code into a new indicator and editted it to fit with cAlgo programming standards. Here is my result :

 

using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Indicators
{
   [Indicator(IsOverlay = true)]
   public class SupportResistance: Indicator
   { 
      [Output("Resist")]
      public IndicatorDataSeries LineResist { get; set; }  

      [Output("Support")]
      public IndicatorDataSeries LineSupport { get; set; }
      
      [Parameter("Shift", DefaultValue = 0)]
      public int Shift{ get; set; }
 
      private int TrendMinor;     //1 = Bullish ^    -1 = Bearish v
      private int TrendMajor; 
 
      protected override void Initialize()
      {
         TrendMinor=1;
         TrendMajor=1;
         LineSupport[0] = MarketSeries.Low[0];
      }
 
      public override void Calculate(int i)
      {
         LineResist[i - Shift] = LineResist[i - Shift - 1];            
         LineSupport[i - Shift] = LineSupport[i - Shift - 1];

//+------------------------------------------------------------------+
//             MAJOR TREND REVERSAL CALCULATION
//+------------------------------------------------------------------+
         if(TrendMajor == 1 && MarketSeries.Low[i - Shift] < LineSupport[i - Shift])
// bullish trend with a new low breaking the support line ==> bearish trend { TrendMajor = -1; TrendMinor = -1; } else if(TrendMajor == -1 && MarketSeries.High[i - Shift] > LineResist[i - Shift])
// bearish trend with a new high breaking the resistance line ==> bullish trend { TrendMajor = 1; TrendMinor = 1; } //+------------------------------------------------------------------+ // Find Signals //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ // MAJOR BULLISH TREND //+------------------------------------------------------------------+ if(TrendMajor == 1) { //--- Bullish TrendMinor if(TrendMinor == 1) { if(MarketSeries.High[i - Shift] < MarketSeries.High[i - Shift - 1])
// High price stop going higher (signify end of minor bullish and possible higher high) { if(MarketSeries.High[i - Shift - 1] > LineResist[i - Shift - 1]) // High point
// If Higher high of previous minor trend is above resistance, change resistance value { LineResist[i - Shift] = MarketSeries.High[i - Shift - 1]; } TrendMinor = -1; } } else //--- Bearish TrendMinor { if(MarketSeries.Low[i - Shift] > MarketSeries.Low[i - Shift - 1])
// Low price stop going lower (signify end of minor bearish and possible higher low) { if(MarketSeries.Low[i - Shift - 1] > LineSupport[i - Shift - 1])
// If Lower Low of previous minor trend is above Support, change support value { LineSupport[i - Shift] = MarketSeries.Low[i - Shift - 1]; } TrendMinor = 1; } } } //+------------------------------------------------------------------+ // MAJOR BEARISH TREND //+------------------------------------------------------------------+ else { //--- Bearish TrendMinor if(TrendMinor == -1) { if(MarketSeries.Low[i - Shift] > MarketSeries.Low[i - Shift - 1]) { //--- End of bullish TrendMinor if(MarketSeries.Low[i - Shift - 1] < LineSupport[i - Shift - 1]) { LineSupport[i - Shift] = MarketSeries.Low[i - Shift - 1]; } TrendMinor = 1; } } else //--- Bullish TrendMinor { if(MarketSeries.High[i - Shift] < MarketSeries.High[i - Shift - 1]) { //--- End of bullish TrendMinor if(MarketSeries.High[i - Shift - 1] < LineResist[i - Shift - 1]) { LineResist[i - Shift] = MarketSeries.High[i - Shift - 1]; } TrendMinor = -1; } } } } } }



When I load the indicator into a new window, my support & resistance are not loaded with data. Can someone give me a hand on this? Should I try to initiate values for my support and resistance lines at the begining?

 

Thanks for any help

 

 


@PsykotropyK
Replies

admin
09 Oct 2012, 17:12

Yes you should initialize LineResist and LineSupport with some values and then return.

 

So,

 

        public override void Calculate(int i)
        {
            if(i < Shift+1)
            {
                LineResist[i] = someValue;
                LineSupport[i] = someValue;
                return;
            }

            LineResist[i - Shift] = LineResist[i - Shift - 1];
            LineSupport[i - Shift] = LineSupport[i - Shift - 1];
            
            // etc.

        }

 

Consider uploading it to the  indicators list



@admin

PsykotropyK
10 Oct 2012, 17:17

Thanks.

 

Still got a few issues to deal with, I will consider posting my indicator if it works the way I want.

 

Ciao


@PsykotropyK

PsykotropyK
11 Oct 2012, 14:40

Hi again,

 

I changed my MajorTrend/MinorTrend data to an array for 2 reasons :

 - I want to recalculate on several bar (1 or 2) every time because I realized looking at my indicator on real time that it has some erratic behaviour on the current bar, and so I wanna be sure that if it has it, it won't record a false value

 - I want to be able to create an other indicator that will show the major and minor trend.

 

Unfortunately, it doesn't work. The only differences with previous code is the correction you gave me (admin) and that I took out every calculation from the initialize part.

 

using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Indicators
{
     [Indicator(IsOverlay = true)]
     public class SupportResistance: Indicator
    { 
          [Output("Resist")]
        public IndicatorDataSeries LineResist { get; set; }  

          [Output("Support")]
        public IndicatorDataSeries LineSupport { get; set; }
  
          [Parameter("Shift", DefaultValue = 0)]
          public int Shift{ get; set; }
 
          public int[] TrendMinor;     //1 = Bullish ^    -1 = Bearish v
          public int[] TrendMajor; 
 

         protected override void Initialize()
        {

        }
 

         public override void Calculate(int i)
        {
            //Array.Resize(ref TrendMinor, TrendMinor.Length + 1);
            //Array.Resize(ref TrendMajor, TrendMajor.Length + 1);
            if(i < Shift + 2)
            {
                TrendMajor[i - Shift] = 1;
                TrendMinor[i - Shift] = 1;                
                LineResist[i - Shift] = MarketSeries.High[i - Shift];
                LineSupport[i - Shift] = MarketSeries.Low[i - Shift];
                return;

            }         
            LineResist[i - Shift] = LineResist[i - Shift - 1];            
              LineSupport[i - Shift] = LineSupport[i - Shift - 1];

//+------------------------------------------------------------------+
//             MAJOR TREND REVERSAL CALCULATION
//+------------------------------------------------------------------+
              if(TrendMajor[i - Shift - 1] == 1 && MarketSeries.Low[i - Shift] < LineSupport[i - Shift])
              {
                   TrendMajor[i - Shift] = -1;
                   TrendMinor[i - Shift] = -1;
              }
              else if(TrendMajor[i - Shift - 1] == -1 && MarketSeries.High[i - Shift] > LineResist[i - Shift])
            { 
                   TrendMajor[i - Shift] = 1;
                   TrendMinor[i - Shift] = 1;
              }
              else
              {
                  TrendMajor[i - Shift] = TrendMajor[i - Shift - 1];
                  TrendMinor[i - Shift] = TrendMinor[i - Shift - 1];
              }
//+------------------------------------------------------------------+

//             Find Signals
//+------------------------------------------------------------------+
 
//+------------------------------------------------------------------+
//             MAJOR BULLISH TREND
//+------------------------------------------------------------------+ 
              if(TrendMajor[i - Shift] == 1)
              {
                   //--- Bullish TrendMinor
                   if(TrendMinor[i - Shift] == 1)

                   {
                    if(MarketSeries.High[i - Shift] < MarketSeries.High[i - Shift - 1])
                    {
                         //--- End of bullish TrendMinor
                         if(MarketSeries.High[i - Shift - 1] > LineResist[i - Shift - 1])
                         {
                              LineResist[i - Shift] = MarketSeries.High[i - Shift - 1];
                         }
                         TrendMinor[i - Shift] = -1;

                    }            
                   }
                   else
                   //--- Bearish TrendMinor
                   {
                    if(MarketSeries.Low[i - Shift] > MarketSeries.Low[i - Shift - 1])
                    {
                         //--- End of bearish TrendMinor
                         if(MarketSeries.Low[i - Shift - 1] > LineSupport[i - Shift - 1])
                        {
                              LineSupport[i - Shift] = MarketSeries.Low[i - Shift - 1];
                         }
                         TrendMinor[i - Shift] = 1;

                    }
                   }
              }
//+------------------------------------------------------------------+
//             MAJOR BEARISH TREND
//+------------------------------------------------------------------+
              else
              {
                   //--- Bearish TrendMinor
                   if(TrendMinor[i - Shift] == -1)

                   {
                    if(MarketSeries.Low[i - Shift] > MarketSeries.Low[i - Shift - 1])
                    {
                         //--- End of bullish TrendMinor
                         if(MarketSeries.Low[i - Shift - 1] < LineSupport[i - Shift - 1])
                         {
                              LineSupport[i - Shift] = MarketSeries.Low[i - Shift - 1];
                         }
                         TrendMinor[i - Shift] = 1;

                    }
                   }
                   else
                   //--- Bullish TrendMinor
                   {
                    if(MarketSeries.High[i - Shift] < MarketSeries.High[i - Shift - 1])
                    {
                         //--- End of bullish TrendMinor
                         if(MarketSeries.High[i - Shift - 1] < LineResist[i - Shift - 1])
                         {
                              LineResist[i - Shift] = MarketSeries.High[i - Shift - 1];
                         }
                         TrendMinor[i - Shift] = -1;

                    }
                   }
              }
         } 
    }
}




@PsykotropyK

PsykotropyK
11 Oct 2012, 14:42

By the way, to force a recalc, I'm thinking on changing Calculate(int i) by Calculate(int index) and then having a for(int i=index-j; i<=index; i++) and add j as a parameter


@PsykotropyK

admin
11 Oct 2012, 15:48

Calculate() is triggered for each new bar, so I don't think you need to force recalculation. You can test with a print statement.

Let me know if the code below is what you are looking for.

 

using System;
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true)]
    public class SupportResistance : Indicator
    {
        [Output("Resist", Color = Colors.Blue)]
        public IndicatorDataSeries LineResist { get; set; }

        [Output("Support", Color = Colors.LightBlue)]
        public IndicatorDataSeries LineSupport { get; set; }

        [Parameter("Shift", DefaultValue = 0)]
        public int Shift { get; set; }

        public int[] TrendMinor = new int[1];     //1 = Bullish ^    -1 = Bearish v
        public int[] TrendMajor = new int[1];


        public override void Calculate(int index)
        {
            Array.Resize(ref TrendMinor, TrendMinor.Length + 1);
            Array.Resize(ref TrendMajor, TrendMajor.Length + 1);

            if (index < Shift + 1)
            {
                TrendMajor[index] = 1;
                TrendMinor[index] = 1;
                LineResist[index] = MarketSeries.High[index];
                LineSupport[index] = MarketSeries.Low[index];
                return;

            }

            LineResist[index - Shift] = LineResist[index - Shift - 1];
            LineSupport[index - Shift] = LineSupport[index - Shift - 1];

            //+------------------------------------------------------------------+
            //             MAJOR TREND REVERSAL CALCULATION
            //+------------------------------------------------------------------+
            if (TrendMajor[index - Shift - 1] == 1 && MarketSeries.Low[index - Shift] < LineSupport[index - Shift])
            {
                TrendMajor[index - Shift] = -1;
                TrendMinor[index - Shift] = -1;
            }
            else if (TrendMajor[index - Shift - 1] == -1 && MarketSeries.High[index - Shift] > LineResist[index - Shift])
            {
                TrendMajor[index - Shift] = 1;
                TrendMinor[index - Shift] = 1;
            }
            else
            {
                TrendMajor[index - Shift] = TrendMajor[index - Shift - 1];
                TrendMinor[index - Shift] = TrendMinor[index - Shift - 1];
            }
            //+------------------------------------------------------------------+

            //             Find Signals
            //+------------------------------------------------------------------+

            //+------------------------------------------------------------------+
            //             MAJOR BULLISH TREND
            //+------------------------------------------------------------------+ 
            if (TrendMajor[index - Shift] == 1)
            {
                //--- Bullish TrendMinor
                if (TrendMinor[index - Shift] == 1)
                {
                    if (MarketSeries.High[index - Shift] < MarketSeries.High[index - Shift - 1])
                    {
                        //--- End of bullish TrendMinor
                        if (MarketSeries.High[index - Shift - 1] > LineResist[index - Shift - 1])
                        {
                            LineResist[index - Shift] = MarketSeries.High[index - Shift - 1];
                        }
                        TrendMinor[index - Shift] = -1;

                    }
                }
                else
                //--- Bearish TrendMinor
                {
                    if (MarketSeries.Low[index - Shift] > MarketSeries.Low[index - Shift - 1])
                    {
                        //--- End of bearish TrendMinor
                        if (MarketSeries.Low[index - Shift - 1] > LineSupport[index - Shift - 1])
                        {
                            LineSupport[index - Shift] = MarketSeries.Low[index - Shift - 1];
                        }
                        TrendMinor[index - Shift] = 1;

                    }
                }
            }
            //+------------------------------------------------------------------+
            //             MAJOR BEARISH TREND
            //+------------------------------------------------------------------+
            else
            {
                //--- Bearish TrendMinor
                if (TrendMinor[index - Shift] == -1)
                {
                    if (MarketSeries.Low[index - Shift] > MarketSeries.Low[index - Shift - 1])
                    {
                        //--- End of bullish TrendMinor
                        if (MarketSeries.Low[index - Shift - 1] < LineSupport[index - Shift - 1])
                        {
                            LineSupport[index - Shift] = MarketSeries.Low[index - Shift - 1];
                        }
                        TrendMinor[index - Shift] = 1;

                    }
                }
                else
                //--- Bullish TrendMinor
                {
                    if (MarketSeries.High[index - Shift] < MarketSeries.High[index - Shift - 1])
                    {
                        //--- End of bullish TrendMinor
                        if (MarketSeries.High[index - Shift - 1] < LineResist[index - Shift - 1])
                        {
                            LineResist[index - Shift] = MarketSeries.High[index - Shift - 1];
                        }
                        TrendMinor[index - Shift] = -1;

                    }
                }
            }
        }
    }
}

 


@admin

PsykotropyK
11 Oct 2012, 17:39

Sure it is triggered with the bar but also with the tick. My problem currently is that my code (for reasons beyhond my current understanding) give erratic behavior. When I load the indicator, it is properly calculated on historical data. When tick occured, it has trouble to always identify a previous high or previous low. It work better if I use a shift = 1 as every tick will launch a calculation based on previous bars only. But still there are a few errors that never occur when the process is launch on historical datas. So the idea it to always do the calculation on the past 2/3 bars.

 

I'll check the code ASAP and let you know. If everything's fine, I'll post the indicator.

 


@PsykotropyK