Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Help with Formula to Find Trend Channel Midpoint

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

    Help with Formula to Find Trend Channel Midpoint

    I can't seem to figure out the correct formula to find the y-intercept of a Trend Channel. I've looped through the levels and gotten anchor points of the trendLevel (0 value) and parallelLevel (100 value) and applied the formulas for slope and y-intercept I found in 'YaxisProjectionofLine' sample script and this from forum post:
    Code:
                                 slope between anchors = (anchor2price - anchor1price) / (anchor2bar - anchor1bar)
                                   (slope = (y2 - y1)/(x2 - x1))
    
                                 yintercept = slope * (CurrentBar - anchor1bar)
                                    (yintercept = slope * (x2 - x1))​
    Without having anchor point for midpoint I'm pretty sure I need to take the range and divide by 2. I know it's not correct code and won't hold if anchor points are inverted, etc. This is what I have:
    Code:
                                    x1bbar = Bars.GetBar(subWave.TrendStartAnchor.Time);
                                    y1 = subWave.TrendStartAnchor.Price;
                                    y2 = subWave.TrendEndAnchor.Price;
                                    y3 = subWave.ParallelStartAnchor.Price;
                                 [B]   y4 = (y1 - y3) / 2 + y3; // this is trying to figure midpoint (50 Value)[/B]
                                    x1 = CurrentBar - Bars.GetBar(subWave.TrendStartAnchor.Time);
                                    x2 = CurrentBar - Bars.GetBar(subWave.TrendEndAnchor.Time);
                                    x3 = CurrentBar - Bars.GetBar(subWave.ParallelStartAnchor.Time);
                                    x2bbar = Bars.GetBar(subWave.ParallelStartAnchor.Time);
                                    
    
        
                                // First calculate the slope of trend channel
                                trendSlope = (subWave.TrendEndAnchor.Price - subWave.TrendStartAnchor.Price) / (Bars.GetBar(subWave.TrendEndAnchor.Time) -                              Bars.GetBar(subWave.TrendStartAnchor.Time));
    
    ​
    ​
                                // Now project the slope to a new bar
                                yintercept = trendSlope * (CurrentBar - x1bbar);
                                yinterceptParallel = trendSlope * (CurrentBar - x2bbar);
                               [B] yinterceptMid = ???[/B]
    
                                // To demonstrate that we know where the trend channel line is
                                Draw.Dot (this, "t1"+CurrentBar, true, 0, yintercept + y1, Brushes.Gold);
                                    
                                // To demonstrate that we know where the parallel channel line is
                                Draw.Dot (this, "t5"+CurrentBar, true, 0,  yinterceptParallel + y3, Brushes.Gold);
                                    
                                    // To demonstrate that we know where the mid channel line is
                                Draw.Dot (this, "t6"+CurrentBar, true, 0, [B]trendSlope * (y1 - y3) / 2 + y3[/B], Brushes.Gold);​​
    Attached Files

    #2
    Hello swjake,

    Thank you for your inquiry.

    To clarify, are you wanting to calculate the midpoint, and then calculate an intercept at some point between start and end of the price levels? Are you trying to get the intercept for a specific bar within that range, or the most recent bar which is CurrentBar?

    I look forward to your response.
    Gaby V.NinjaTrader Customer Service

    Comment


      #3
      Good Morning Gaby,

      I'm trying to get the yintercept for the midpoint @ the CurrentBar. The formula I worked up--and it's working so far and I'm somewhat in disbelief-- is:

      Code:
      (subWave.TrendStartAnchor.Price - subWave.ParallelStartAnchor.Price) / 2 + subWave.ParallelStartAnchor.Price = y4
      Which gives me midpoint "start anchor" and then I apply slope (yinterceptParallel + y4)

      So far if holds in upslope & downslope and inverted. But it doesn't seem quite right...

      Any suggestions or feedback is appreciated.

      Thanks!

      Comment


        #4
        Hello swjake,

        Can you please clarify what specifically seems not quite right? The image you posted seems to show a dot drawn on the CurrentBar/midline with the calculations you did.

        If you are confused why your formula works, we recommend you print out the values and then manually do the math to see what values are being generated based on your formula.

        Please let us know if we can assist further.
        Gaby V.NinjaTrader Customer Service

        Comment


          #5
          Thanks NinjaTrader_Gaby. I thought there might be a cleaner formula, but this works.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by DJ888, Today, 10:57 PM
          0 responses
          2 views
          0 likes
          Last Post DJ888
          by DJ888
           
          Started by MacDad, 02-25-2024, 11:48 PM
          7 responses
          158 views
          0 likes
          Last Post loganjarosz123  
          Started by Belfortbucks, Today, 09:29 PM
          0 responses
          7 views
          0 likes
          Last Post Belfortbucks  
          Started by zstheorist, Today, 07:52 PM
          0 responses
          7 views
          0 likes
          Last Post zstheorist  
          Started by pmachiraju, 11-01-2023, 04:46 AM
          8 responses
          151 views
          0 likes
          Last Post rehmans
          by rehmans
           
          Working...
          X