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

Draw from Indicators

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

    Draw from Indicators


    I am getting the following compilation error when trying to compile the following code:

    The name 'Draw' does not exist in the current context
    I appreciate your help.

    Code:
    using System.Windows.Media;
    using NinjaTrader.Gui.NinjaScript;
    using NinjaTrader.NinjaScript;
    
    namespace NinjaTraderAddOnProject
    {[INDENT]public class AddArrows : IndicatorRenderBase
    {[/INDENT][INDENT=2]protected override void OnStateChange()
    {[/INDENT][INDENT=3]if (State == State.SetDefaults)
    {[/INDENT][INDENT=4]Description = @"Add arrows";
    Name = "AddArrows";
    Calculate = Calculate.OnBarClose;
    IsOverlay = false;
    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;[/INDENT][INDENT=3]}
    else if (State == State.Configure)
    {
    
    }[/INDENT][INDENT=2]}
    
    protected override void OnBarUpdate()
    {[/INDENT][INDENT=3]// Paints a red down arrown on a red bar
    if (Close[0] < Open[0])
    {[/INDENT][INDENT=4]Draw.ArrowDown(this, CurrentBar.ToString(), true, 0, High[0] + TickSize, Brushes.Red);[/INDENT][INDENT=3]}
    
    // Paints a green up arrown on a greeb bar
    if (Open[0] < Close[0])
    {[/INDENT][INDENT=4]Draw.ArrowDown(this, CurrentBar.ToString(), true, 0, High[0] + TickSize, Brushes.Green);[/INDENT][INDENT=3]}[/INDENT][INDENT=2]}[/INDENT][INDENT]}[/INDENT]
     }

    #2
    Hello danielsilva,

    You are missing some required using statements, please open the SMA indicator and copy the using statements from the top of the file and replace the using statements in your script. The various namespaces that indicators have listed by default are used for NinjaScript functionality and are needed when actively developing scripts, its best to not remove the using statements that the script has by default.

    JesseNinjaTrader Customer Service

    Comment


      #3
      Hello Jesse,

      I still get the same error message. Could you point out which using statement is supposed to bring in the "Draw" class?

      This is what I have now:


      Code:
      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      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.DrawingTools;
      
      namespace NinjaTraderAddOnProject
      {[INDENT]public class AddArrows : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
      {[/INDENT][INDENT=2]protected override void OnStateChange(
      {[/INDENT][INDENT=3]if (State == State.SetDefaults)
      {[/INDENT][INDENT=4]Description = @"Add arrows";
      Name = "AddArrows";
      Calculate = Calculate.OnBarClose;
      IsOverlay = false;
      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;[/INDENT][INDENT=3]}
      else if (State == State.Configure)
      {
      }[/INDENT][INDENT=2]}
      
      protected override void OnBarUpdate()
      {[/INDENT][INDENT=3]// Paints a red down arrown on a red bar
      if (Close[0] < Open[0])
      {[/INDENT][INDENT=4]Draw.ArrowDown(this, CurrentBar.ToString(), true, 0, High[0] + TickSize, Brushes.Red);[/INDENT][INDENT=3]}
      
      // Paints a green up arrown on a greeb bar
      if (Open[0] < Close[0])
      {[/INDENT][INDENT=4]Draw.ArrowDown(this, CurrentBar.ToString(), true, 0, High[0] + TickSize, Brushes.Green);[/INDENT][INDENT=3]}[/INDENT][INDENT=2]}[/INDENT][INDENT]}[/INDENT]
       
       }

      Comment


        #4
        Something is off, because when I paste the exact same code into NinjaScript Editor it says that the Draw class exists:

        Click image for larger version

Name:	Screen Shot 2022-08-08 at 6.13.55 PM.png
Views:	59
Size:	32.9 KB
ID:	1211515

        But the same type does not show up when I look into that namespace in Visual Studio:

        Click image for larger version

Name:	Screen Shot 2022-08-08 at 6.14.25 PM.png
Views:	45
Size:	51.3 KB
ID:	1211516

        Comment


          #5
          Hello danielsilva,

          The namespace is shown in your image: using NinjaTrader.NinjaScript.DrawingTools;

          Visual studio won't have the full context for all types used in NinjaScript and it also is not used to compile so some items may display differently or not at all in visual studio. You can use it as a general text editor and save files, that would trigger the NinjaScript editor to compile. As long as the NinjaScript editor compiles then that code is working normally.

          JesseNinjaTrader Customer Service

          Comment


            #6
            Hello Jesse,

            Is there any change you can make it available for Visual Studio?

            Thanks.

            Comment


              #7
              Hello danielsilva,

              You can use that code in a NinjaScript file which can be viewed or edited with visual studio. The picture you attached is not where the Draw partial class exists so it shouldn't be there, you can't browse to that partial class because that is inside each of the drawing tools code. You still need to use that namespace to be able to use the Drawing tools in your indicator.

              If you are still having difficulty with the error I would suggest to generate a new script using the NinjaScript editor so that the correct references are added for the using statements.



              JesseNinjaTrader Customer Service

              Comment


                #8
                Hello Jesse,

                All the namespaces are there in the using imports.
                I did what you suggested and created a brand new Indicator through the NinjaScript Editor, but the code fails to compile in Visual Studio because the "Draw" class is not found.

                Are you able to compile this code inside Visual Studio?

                These are the DLLs that I referenced:
                NinjaTrader.Core
                NinjaTrader.Gui

                Here is the code generated by the NinjaScript Editor:
                Code:
                #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.DrawingTools;
                #endregion
                
                //This namespace holds Indicators in this folder and is required. Do not change it.
                
                namespace NinjaTrader.NinjaScript.Indicators
                {[INDENT]public class MyCustomIndicator : Indicator
                {[/INDENT][INDENT=2]protected override void OnStateChange()
                {[/INDENT][INDENT=3]if (State == State.SetDefaults)
                {[/INDENT][INDENT=4]Description = @"Enter the description for your new custom Indicator here.";
                Name = "MyCustomIndicator";
                Calculate = Calculate.OnBarClose;
                IsOverlay = false;
                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;[/INDENT][INDENT=3]}
                else if (State == State.Configure)
                {
                }[/INDENT][INDENT=2]}
                [/INDENT][INDENT=2]protected override void OnBarUpdate()
                {[/INDENT][INDENT=3]//Add your custom indicator logic here.
                Draw.ArrowDown(this, CurrentBar.ToString(), true, 0, High[0] + TickSize, Brushes.Red);[/INDENT][INDENT=2]}[/INDENT][INDENT]}[/INDENT]
                 }
                
                #region NinjaScript generated code. Neither change nor remove.
                namespace NinjaTrader.NinjaScript.Indicators
                {
                public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
                {
                private MyCustomIndicator[] cacheMyCustomIndicator;
                
                public MyCustomIndicator MyCustomIndicator()
                {
                return MyCustomIndicator(Input);
                }
                
                public MyCustomIndicator MyCustomIndicator(ISeries<double> input)
                {
                if (cacheMyCustomIndicator != null)
                for (int idx = 0; idx < cacheMyCustomIndicator.Length; idx++)
                if (cacheMyCustomIndicator[idx] != null && cacheMyCustomIndicator[idx].EqualsInput(input))
                return cacheMyCustomIndicator[idx];
                return CacheIndicator<MyCustomIndicator>(new MyCustomIndicator(), input, ref cacheMyCustomIndicator);
                }
                }
                }
                
                namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
                {
                public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
                {
                public Indicators.MyCustomIndicator MyCustomIndicator()
                {
                return indicator.MyCustomIndicator(Input);
                }
                
                public Indicators.MyCustomIndicator MyCustomIndicator(ISeries<double> input)
                {
                return indicator.MyCustomIndicator(input);
                }
                }
                }
                
                namespace NinjaTrader.NinjaScript.Strategies
                {
                public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
                {
                public Indicators.MyCustomIndicator MyCustomIndicator()
                {
                return indicator.MyCustomIndicator(Input);
                }
                
                public Indicators.MyCustomIndicator MyCustomIndicator(ISeries<double> input)
                {
                return indicator.MyCustomIndicator(input);
                }
                }
                }
                
                #endregion

                Comment


                  #9
                  Hello danielsilva,

                  Visual studio is not used for compiling, please avoid trying to error check using visual studio. Visual studio is at most a text editor when being used with NinjaScript, you can't compile or use any of the build features because it does not have all the information. NinjaTrader does the compiling and has all the information. When using visual studio you can generally turn off errors and warnings because they won't be entirely relevant, that is really just going to be used as syntax highlighting and will display some type information which it has the context for.

                  Are you able to see this working using the NinjaScript editor?

                  JesseNinjaTrader Customer Service

                  Comment


                    #10
                    Hello Jesse,

                    That is exactly why I asked if you could make all the information available for Visual Studio.

                    Comment


                      #11
                      Hello danielsilva,

                      As noted visual studio does not have all of the required information and is not intended to be used for building. You can't add something to make it work different, you still need to compile from the NinjaScript editor.

                      Visual studio can be used as a syntax editor and only to Save files. When you save a file the NinjaScript editor (has to be open) will detect that and compile. The only other use for visual studio would be debugging which you can attach to the platform in debug mode to use breakpoints. It otherwise cannot be used for any of its normal build related features. https://ninjatrader.com/support/help...=visual+studio

                      As long as you can compile using the NinjaScript editor then you have solved the problem that you posted about, you just need to add that namespace to the script so that it can actually compile. The warnings or errors in visual studio that may still exist are not relevant.

                      If you want to view the Draw class which you pictured in post 4 you need to edit the specific drawing tool you wanted that draw class for. For example ArrowDown you would need to open the DrawingTools --> ChartMarkers.cs, at the bottom of the file is the partial Draw class for that object. Each object has its own partial class so you wont see that in the visual studio namespace browser like you pictured.


                      Last edited by NinjaTrader_Jesse; 08-09-2022, 10:21 AM.
                      JesseNinjaTrader Customer Service

                      Comment


                        #12
                        Thanks for the support, Jesse.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by MSerag, 12-02-2022, 12:23 AM
                        3 responses
                        15 views
                        0 likes
                        Last Post NinjaTrader_ChrisL  
                        Started by aligator, Yesterday, 07:13 PM
                        1 response
                        15 views
                        0 likes
                        Last Post NinjaTrader_BrandonH  
                        Started by RideMe, Yesterday, 09:30 AM
                        2 responses
                        35 views
                        0 likes
                        Last Post RideMe
                        by RideMe
                         
                        Started by Mindset, 11-23-2022, 09:24 PM
                        5 responses
                        71 views
                        0 likes
                        Last Post NinjaTrader_Jesse  
                        Started by sjmuk1, Yesterday, 04:10 PM
                        3 responses
                        28 views
                        0 likes
                        Last Post NinjaTrader_Emily  
                        Working...
                        X