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

do not allow selecting object

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

    do not allow selecting object

    Good morning, I created some rectangles in the graph and I wanted to make them unable to be selected.

    i trying this commands.... however, it allows you to select and only after it removes the selection. I want to not allow selecting or even double clicking to edit.

    Code:
    string novaString = "Baixa_50"+draw.Tag.Substring(5);
    double perc_50_baixa = (maior + menor)/2;
    Rectangle myRec = Draw.Rectangle(this,draw.Tag, false, firstAnchorTime, maior, Time[0], menor, Brushes.Transparent, Brushes.Red, 20, false);
    myRec.IsSelected = false;
    myRec.IsLocked = true;
    if(Ativar50){
    NinjaTrader.NinjaScript.DrawingTools.Line linha = Draw.Line(this,novaString, false, firstAnchorTime, perc_50_baixa, Time[0], perc_50_baixa, Brushes.Red, DashStyleHelper.DashDot, 1,false);
    linha.IsLocked = true;
    }

    #2
    Hello,

    To ensure that the rectangles you draw on the NinjaTrader chart cannot be selected or edited, you need to set the properties of the DrawingTool appropriately. Specifically, you should set the IsLocked property to true and ensure that IsSelected is false. The IsLocked property prevents the user from selecting or editing the drawing object.

    Here's how you can achieve this:
    1. Create the Rectangle: Draw the rectangle and set its properties.
    2. Ensure the Rectangle is Locked and Not Selected: Set IsLocked to true and IsSelected to false.

    Here’s the modified version of your code:

    HTML Code:
    #region Using declarations
    using System;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript.Strategy;
    using NinjaTrader.NinjaScript.Indicators;
    using NinjaTrader.NinjaScript.DrawingTools;
    #endregion
    
    namespace NinjaTrader.NinjaScript.Strategies
    {
        public class CustomRectangleStrategy : Strategy
        {
            private DateTime firstAnchorTime;
            private double maior;
            private double menor;
            private string drawTag;
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description = @"Strategy with custom rectangles that cannot be selected or edited.";
                    Name = "CustomRectangleStrategy";
                    Calculate = Calculate.OnEachTick;
                    IsOverlay = true;
                }
            }
    
            protected override void OnBarUpdate()
            {
                // Ensure we have enough bars to work with
                if (CurrentBar < 10)
                    return;
    
                // Example values for rectangle
                firstAnchorTime = Time[10];
                maior = High[10];
                menor = Low[10];
                drawTag = "MyRectangle";
    
                // Draw the rectangle
                Rectangle myRec = Draw.Rectangle(this, drawTag, false, firstAnchorTime, maior, Time[0], menor, Brushes.Transparent, Brushes.Red, 20, false);
                myRec.IsSelected = false;
                myRec.IsLocked = true;
    
                // Example for a line
                string novaString = "Baixa_50" + drawTag.Substring(5);
                double perc_50_baixa = (maior + menor) / 2;
                Line linha = Draw.Line(this, novaString, false, firstAnchorTime, perc_50_baixa, Time[0], perc_50_baixa, Brushes.Red, DashStyleHelper.DashDot, 1, false);
                linha.IsLocked = true;
            }
        }
    }
    ​
    Explanation
    1. OnStateChange Method:
      • Sets the default properties for the strategy.
    2. OnBarUpdate Method:
      • Ensures there are enough bars to work with (e.g., at least 10 bars).
      • Sets example values for the rectangle's anchor points (firstAnchorTime, maior, menor).
      • Draws the rectangle with Draw.Rectangle, and immediately sets IsSelected to false and IsLocked to true.
      • Draws a line with Draw.Line and sets IsLocked to true.
    Key Points
    • IsLocked: Setting IsLocked to true ensures the drawing object cannot be selected or edited.
    • IsSelected: Setting IsSelected to false ensures the drawing object is not selected initially.

    By setting these properties appropriately, you ensure that the rectangles and other drawing objects you create on the chart are not selectable or editable by the user, even when double-clicked.
    Ryan S.NinjaTrader Customer Service

    Comment


      #3
      Hi,

      this modification did not work. I can still select the drawn rectangle.

      In the example you showed, it allows you to select and after updating the bar, it deselects the selection made
      Last edited by Rafaelseki; 05-30-2024, 09:45 AM.

      Comment


        #4
        To ensure that the rectangle and line are never selectable or editable, you need to set the IsLocked and IsSelected properties immediately after drawing the objects. However, this won't prevent them from being initially selectable if they are being redrawn on each bar update. You need to ensure they are drawn only once or when certain conditions are met, and lock them immediately.

        Here's the revised code that ensures the drawing tools are only drawn and locked once, or based on specific conditions: Revised Code

        HTML Code:
        #region Using declarations
        using System;
        using NinjaTrader.Cbi;
        using NinjaTrader.Gui.Tools;
        using NinjaTrader.NinjaScript;
        using NinjaTrader.Data;
        using NinjaTrader.NinjaScript.Strategy;
        using NinjaTrader.NinjaScript.Indicators;
        using NinjaTrader.NinjaScript.DrawingTools;
        #endregion
        
        namespace NinjaTrader.NinjaScript.Strategies
        {
            public class CustomRectangleStrategy : Strategy
            {
                private DateTime firstAnchorTime;
                private double maior;
                private double menor;
                private string drawTag;
                private bool isRectangleDrawn = false;
                private bool isLineDrawn = false;
        
                protected override void OnStateChange()
                {
                    if (State == State.SetDefaults)
                    {
                        Description = @"Strategy with custom rectangles that cannot be selected or edited.";
                        Name = "CustomRectangleStrategy";
                        Calculate = Calculate.OnEachTick;
                        IsOverlay = true;
                    }
                }
        
                protected override void OnBarUpdate()
                {
                    // Ensure we have enough bars to work with
                    if (CurrentBar < 10)
                        return;
        
                    // Draw the rectangle and line only once
                    if (!isRectangleDrawn)
                    {
                        // Example values for rectangle
                        firstAnchorTime = Time[10];
                        maior = High[10];
                        menor = Low[10];
                        drawTag = "MyRectangle";
        
                        // Draw the rectangle
                        Rectangle myRec = Draw.Rectangle(this, drawTag, false, firstAnchorTime, maior, Time[0], menor, Brushes.Transparent, Brushes.Red, 20, false);
                        myRec.IsSelected = false;
                        myRec.IsLocked = true;
                        isRectangleDrawn = true;
        
                        // Example for a line
                        string novaString = "Baixa_50" + drawTag.Substring(5);
                        double perc_50_baixa = (maior + menor) / 2;
                        Line linha = Draw.Line(this, novaString, false, firstAnchorTime, perc_50_baixa, Time[0], perc_50_baixa, Brushes.Red, DashStyleHelper.DashDot, 1, false);
                        linha.IsLocked = true;
                        isLineDrawn = true;
                    }
                }
            }
        }
        ​
        Explanation
        1. Flags to Track Drawing State:
          • isRectangleDrawn and isLineDrawn are boolean flags used to ensure that the rectangle and line are drawn only once.
        2. Conditional Drawing:
          • In the OnBarUpdate method, check if the rectangle and line have already been drawn. If not, proceed to draw them and set the flags to true to prevent them from being drawn again.
        3. Locking and Deselecting Immediately:
          • Immediately after drawing the rectangle and line, set IsSelected to false and IsLocked to true to ensure they are not selectable or editable.

        This approach ensures that your drawing tools are locked and not selectable from the moment they are created, preventing any user interaction that could modify them.
        Ryan S.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by crewtrades, 06-13-2024, 10:50 AM
        4 responses
        30 views
        0 likes
        Last Post crewtrades  
        Started by kingsteven, Today, 08:34 PM
        1 response
        3 views
        0 likes
        Last Post bltdavid  
        Started by biegea, Today, 08:54 PM
        0 responses
        3 views
        0 likes
        Last Post biegea
        by biegea
         
        Started by rene69851, Today, 08:36 PM
        0 responses
        2 views
        0 likes
        Last Post rene69851  
        Started by chartish, 04-11-2019, 12:40 PM
        15 responses
        3,832 views
        0 likes
        Last Post joselube001  
        Working...
        X