Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Drawings not showing up.

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

    Drawings not showing up.

    I have a script to draw ranges, from swing point to swing point. First tried using rectangles, some would show up while other ranges would be recognized (found this through prints) but not draw. When enabling -> disabling the strategy, it changes between the ones it draws and others it didn't draw. I also tried using lines, which does the same thing (sometimes draws these, sometimes those). Any idea why that happens?
    In the script below ->

    region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.Indicators;
    using NinjaTrader.NinjaScript.DrawingTools;
    #endregion

    //This namespace holds Strategies in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class BullBearBreakers : Strategy
    {
    private List<Range> bullishranges = new List<Range>();
    private List<Range> bearishranges = new List<Range>();

    private bool isSwingHigh;
    private bool isSwingLow;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Breakers Combined.";
    Name = "BullBearBreakers";
    Calculate = Calculate.OnBarClose;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 0;
    StartBehavior = StartBehavior.WaitUntilFlat;
    TimeInForce = TimeInForce.Gtc;
    TraceOrders = false;
    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;
    BarsRequiredToTrade = 20;
    // Disable this property for performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    IsInstantiatedOnEachOptimizationIteration = true;
    }
    else if (State == State.Configure)
    {
    }
    }

    protected override void OnBarUpdate()
    {
    if (CurrentBar < 3) return;

    isSwingHigh = High[1] > High[0] && High[1] > High[2];
    isSwingLow = Low[1] < Low[0] && Low[1] < Low[2];

    IdentifyBullishRanges();
    IdentifyBearishRanges();

    DrawRanges();

    Print("Bearish Ranges: "+ bearishranges.Count);
    Print("Bullish Ranges: "+ bullishranges.Count);

    }

    private void IdentifyBullishRanges()
    {
    if (isSwingLow)
    {
    bullishranges.Add(new Range
    {
    LowBar = CurrentBar - 1,
    Low = Low[1],
    IsActive = true,
    IsBullish = true
    });
    }

    if (isSwingHigh && bullishranges.Count > 0)
    {
    var lastRange = bullishranges[bullishranges.Count - 1];
    if (lastRange.IsActive && (lastRange.HighBar == -1 || High[1] > lastRange.High))
    {
    lastRange.HighBar = CurrentBar - 1;
    lastRange.High = High[1];
    }
    }
    }

    private void IdentifyBearishRanges()
    {
    if (isSwingHigh)
    {
    bearishranges.Add(new Range
    {
    HighBar = CurrentBar - 1,
    High = High[1],
    IsActive = true,
    IsBullish = false
    });
    }

    if (isSwingLow && bearishranges.Count > 0)
    {
    var lastRange = bearishranges[bearishranges.Count - 1];
    if (lastRange.IsActive && (lastRange.LowBar == -1 || Low[1] < lastRange.Low))
    {
    lastRange.LowBar = CurrentBar - 1;
    lastRange.Low = Low[1];
    }
    }
    }

    private void DrawRanges()
    {
    foreach (var range in bearishranges)
    {
    if (range.HighBar != -1 && range.LowBar != -1)
    {
    string rectangleId = $"BearishBreaker_{DateTime.Now.Ticks}";
    range.DrawingIds.Add(rectangleId);

    int barsAgoStart = CurrentBar - range.HighBar;
    int barsAgoEnd = CurrentBar - range.LowBar;

    Draw.Line(this, rectangleId, false, barsAgoStart, range.Low, barsAgoEnd, range.Low, Brushes.Red, DashStyleHelper.Solid, 3);
    Draw.Line(this, rectangleId + CurrentBar, false, barsAgoEnd, range.High, barsAgoStart, range.High, Brushes.Red, DashStyleHelper.Solid, 3);
    //Draw.Rectangle(this, rectangleId, false, barsAgoStart, range.High, barsAgoEnd, range.Low, Brushes.Red, Brushes.Transparent, 25);
    }
    }

    foreach (var range in bullishranges)
    {
    if (range.HighBar != -1 && range.LowBar != -1)
    {
    string rectangleId = $"BullishBreaker_{DateTime.Now.Ticks}";
    range.DrawingIds.Add(rectangleId);

    int barsAgoStart = CurrentBar - range.LowBar;
    int barsAgoEnd = CurrentBar - range.HighBar;

    Draw.Line(this, rectangleId, false, barsAgoStart, range.Low, barsAgoEnd, range.Low, Brushes.Green, DashStyleHelper.Solid, 3);
    Draw.Line(this, rectangleId + CurrentBar, false, barsAgoEnd, range.High, barsAgoStart, range.High, Brushes.Green, DashStyleHelper.Solid, 3);
    //Draw.Rectangle(this, rectangleId, false, barsAgoEnd, range.High, barsAgoStart, range.Low, Brushes.Green, Brushes.Transparent, 25);
    }
    }
    }

    private class Range
    {
    public int LowBar { get; set; } = -1;
    public int HighBar { get; set; } = -1;
    public double Low { get; set; }
    public double High { get; set; }
    public bool IsActive { get; set; }
    public bool IsInvalidated { get; set; } = false;
    public bool HasLL_HH { get; set; } = false;
    public double LL_HH { get; set; }
    public bool IsBullish { get; set; }
    public List<string> DrawingIds { get; set; } = new List<string>();
    }

    }
    }

    #2
    Hello,

    Thank you for your post.

    The first Line object with the tag "rectangleId" is being re-created on each call of DrawRanges() since the tag is being reused - is this intended? I see you are using rectangleId + CurrentBar for the second Line object, this line will create a new object each time.

    If you reuse the same tag, each time you call a drawing object the same drawing object will be referenced and the existing drawing object will be modified instead of a new object being created. To create a new drawing object, use a new unique string as the tag each time you call a Draw method. If you would like both lines to create a new drawing object each time, I recommend changing the tag name to something unique and adding CurrentBar as well, for example "rectangleId1" + CurrentBar.


    To understand why the script is behaving as it is, such as placing not placing drawing objects when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

    In the strategy add prints (outside of any conditions) that print the date time of the bar and all values compared in every condition that places an order.

    The prints should include the time of the bar and should print all values from all variables and all hard coded values in all conditions that must evaluate as true for this action to be triggered. It is very important to include a text label for each value and for each comparison operator in the print to understand what is being compared in the condition sets.

    Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

    I am happy to assist you with analyzing the output from the output window.

    Run or backtest the script and when the output from the output window appears save this by right-clicking the output window and selecting Save As... -> give the output file a name and save -> then attach the output text file to your reply.

    Below is a link to a forum post that demonstrates using informative prints to understand behavior and includes a link to a video recorded using the Strategy Builder to add prints.


    Please let me know if I may further assist with analyzing the output or if you need any assistance creating a print.​

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    647 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    369 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    108 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    572 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    573 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X