Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Crossovers and Displaced Moving Averages

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Crossovers and Displaced Moving Averages

    I'm developing a strategy that generates signals, in part, from a crossover of a moving average and a displaced moving average. Now, moving average crossovers are simple enough to code for ...

    if (CrossAbove(sma1, sma2, nBarsAgo) bullishSignal = true;

    However, I'm not entirely sure that I'm coding correctly for a crossover that involves a displaced moving average. Initially I assumed that the Displacement property on a SMA shifted the entire series, but I believe it only shifts the way it's painted visually, on a plot. So I tried coding my crossover like this:

    if (CrossAbove(sma, dma[shift], nBarsAgo) bullishSignal = true;

    Where [shift] is the amount that I want to displace the second moving average. I'm not sure that this is working the way I assume it would.

    I'm aware that I could simply write some clumsy code that checks to see if (sma[0] > dma[shift]) and (ska[1] < dma[shift+1]), but that gets old fast, especially if you're doing it several times. Plus, I like code that's self-documenting.

    Can the CrossAbove() and CrossBelow() methods be used with displaced moving averages? And if so, what's the proper way to code this?

    #2
    Hello pbailey19,

    Thank you for your post.

    What do you mean by shift and displacement? Are you referring to the Displacement setting in the Indicators menu of the chart? Or possible the Period or the barsAgo index?

    Comment


      #3
      In both places, I'm talking about displacement. For example: "a 6-period SMA, with a Displacement of 4". On the chart, this will draw a 6-period SMA that is shifted to the right. Now, if I overlay a *normal* non-displaced SMA on top of that, like a 3-period SMA - obviously I'll visually see the crossovers. I want to make sure I'm using the right code to capture the same crossover points, algorithmically.

      Comment


        #4
        Hello pbailey19,

        Thank you for your response.

        Using your latest example it would be if(SMA(3)[0] > SMA(6)[4]);.

        Comment


          #5
          Thanks, Patrick, but that didn't quite answer my question. I wanted to know what the correct syntax was, using the CrossAbove() and CrossBelow() methods. Another reason that I wanted to use these methods, was to take advantage of the "look-back n bars" functionality. So that I can code something like this:

          CrossAbove(SMA(3), DMA(6), n) ... -? or ?- .... CrossAbove(SMA(3), DMA(6)[4], n)

          Which - I hope - means "has the SMA crossed over the displaced DMA, in the last n bars?"

          Comment


            #6
            Hello ,

            Thank you for your response.

            We would need something like the following code, as there is no method to displace the values in the CrossAbove or Below:
            Code:
                    #region Variables
            		private DataSeries displacedMA;
                    #endregion
            
                    protected override void Initialize()
                    {
            			displacedMA = new DataSeries(this);
                    }
                    
                    protected override void OnBarUpdate()
                    {
            			displacedMA.Set(SMA(6)[4]);
            			if(CrossAbove(SMA(3), displacedMA, 1))
            			{
            				// Do something.
            			}
            		}

            Comment


              #7
              There's an old saying in computer science: Any problem can be solved with an extra layer of abstraction. Your suggestion did the trick, and I did it one better: I simply created a new custom indicator for Displaced Moving Averages. The code is ridiculously simple. But by treating the displaced MA as a whole separate DataSeries, that seems to make CrossAbove() and CrossBelow() happy. The functions are properly detecting crossovers, and the 'look-back' parameters works like you'd expect it to, also. Thanks!

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by argusthome, Yesterday, 10:06 AM
              0 responses
              17 views
              0 likes
              Last Post argusthome  
              Started by NabilKhattabi, 03-06-2026, 11:18 AM
              0 responses
              16 views
              0 likes
              Last Post NabilKhattabi  
              Started by Deep42, 03-06-2026, 12:28 AM
              0 responses
              14 views
              0 likes
              Last Post Deep42
              by Deep42
               
              Started by TheRealMorford, 03-05-2026, 06:15 PM
              0 responses
              9 views
              0 likes
              Last Post TheRealMorford  
              Started by Mindset, 02-28-2026, 06:16 AM
              0 responses
              36 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Working...
              X