Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Creating a plot at N% retracement of a MIN/MAX range (N period)

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

    Creating a plot at N% retracement of a MIN/MAX range (N period)

    Hi all

    I would like to know if the following public indicator (TcFAutoFibos - NinjaTrader Ecosystem​ - thanks @Xusto91​ !) could be extended or modified to create a plot/signal when price retraces to a certain ratio level of the MIN/MAX range? Using the fib drawing tool is not a must, though a nice to keep for visuals.

    Example pseudo code for a retracement in an uptrend market - ratio can be 0.5, 0.618, etc
    // the last swing high comes AFTER the last swing low

    if highest_swing_time > lowest_swing_time:
    (max_level - (max_level-min_level)*ratio)

    Example pseudo code for retracement in a downtrend market - ratio can be 0.5, 0.618, etc
    // the last swing high comes BEFORE the last swing low

    if highest_swing_time < lowest_swing_time:
    (min_level + (max_level-min_level)*ratio)


    Right now the indicator draws an autofib looking back N period (Strength param), but does not generate plots or signals for retracement levels.​ In the end the idea would be to think of a strategy leveraging those signals.

    Code excerpt

    Code:
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Automatically draw fibonacci retracements from swing highs and lows in the market.";
                    Name                                        = "TcFAutoFibos";
                    Calculate                                    = Calculate.OnBarClose;
                    IsOverlay                                    = true;
                    DisplayInDataBox                            = true;
                    DrawOnPricePanel                            = true;
                    DrawHorizontalGridLines                        = true;
                    DrawVerticalGridLines                        = true;
                    PaintPriceMarkers                            = true;
                    ScaleJustification                            = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                    //Disable this property if your indicator requires custom values that cumulate with each new market data event.
                    //See Help Guide for additional information.
                    IsSuspendedWhileInactive                    = true;
                    Strength                    = 20;
                }
                else if (State == State.Configure)
                {
                    min=MIN(Low,Strength);
                    max=MAX(High,Strength);
                }
            }
    
            protected override void OnBarUpdate()
            {
                if (CurrentBar<Strength)
                    return;
    
                if (High[0]>max[1])
                    barsSinceHigh=0;
                else if (barsSinceHigh>-1 && IsFirstTickOfBar)
                    barsSinceHigh++;
    
                if (Low[0]<min[1])
                    barsSinceLow=0;
                else if (barsSinceLow>-1 && IsFirstTickOfBar)
                    barsSinceLow++;
    
                if (Math.Min(barsSinceHigh,barsSinceLow)<=0)
                    return;
    
                if (barsSinceHigh<barsSinceLow)            
                    fib=Draw.FibonacciRetracements(this,"fib",IsAutoScale,barsSinceLow,Low[barsSinceLow],barsSinceHigh,High[barsSinceHigh]);            
                else
                    fib=Draw.FibonacciRetracements(this,"fib",IsAutoScale,barsSinceHigh,High[barsSinceHigh],barsSinceLow,Low[barsSinceLow]);
            }
    
            #region Properties
            [Range(1, int.MaxValue)]
            [NinjaScriptProperty]
            [Display(Name="Strength", Order=1, GroupName="Parameters")]
            public int Strength
            { get; set; }
            #endregion
    
        }
    }​
    Thanks in advance for any inputs
    Last edited by mazpat_3; 03-11-2023, 05:25 PM.

    #2
    Hello mazpat_3,

    Thanks for your note.

    The AddPlot() method would be used to add plots to a custom NinjaScript.

    You may then assign a value to each of the added plots in the script so that those values plot on the chart.

    AddPlot(): https://ninjatrader.com/support/help...t8/addplot.htm

    To get the price of the prices from the Fibonacci Retracements drawing in the script, you could loop through the PriceLevels collection of the Fibonacci Retracements and use GetPrice(). For example, the code below may get the 50% price level of the Fibonacci Retracements drawing.

    Code:
    double price = 0;
    foreach (PriceLevel p in fib.PriceLevels)
    {
        if (p.Value == 50)
        {
             double totalPriceRange = myRetracements.EndAnchor.Price - myRetracements.StartAnchor.Price;
             price = p.GetPrice(myRetracements.StartAnchor.Price, totalPriceRange, true);
        }
    }
    
    Print(price);
    Draw.FibonacciRetracements(): https://ninjatrader.com/support/help...tracements.htm
    PriceLevels collection: https://ninjatrader.com/support/help...htsub=GetPrice

    Please let me know if I may assist further.
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    566 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    330 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    101 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    547 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    548 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X