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 text next to horizontal line

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

    Draw text next to horizontal line

    Hi,

    I created a basic indicator to draw lines from the open and have attempted to draw text next to the lines but I am having trouble getting it to compile.

    Code:
    #region Using declarations
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Gui.Chart;
    #endregion
    
    // This namespace holds all indicators and is required. Do not change it.
    namespace NinjaTrader.Indicator
    {
        /// <summary>
        /// Enter the description of your new custom indicator here
        /// </summary>
        [Description("Enter the description of your new custom indicator here")]
        public class MyCustomIndicator1 : Indicator
        {
            #region Variables
            // Wizard generated variables
                private int myInput0 = 1; // Default setting for MyInput0
                private Font    largeFont = new Font("Arial", 12);     //This variable holds the large font
            // User defined variables (add any user defined variables below)
            #endregion
    
            /// <summary>
            /// This method is used to configure the indicator and is called once before any bar data is loaded.
            /// </summary>
            protected override void Initialize()
            {
                Add(new Plot(new Pen(Color.Green, 2), PlotStyle.Square, "Up1"));
                Add(new Plot(new Pen(Color.Red, 2), PlotStyle.Square, "Dn1"));
                Add(new Plot(new Pen(Color.Green, 2), PlotStyle.Square, "Up2"));
                Add(new Plot(new Pen(Color.Red, 2), PlotStyle.Square, "Dn2"));
                Plots[0].Pen.DashStyle = DashStyle.Dash;
                Plots[1].Pen.DashStyle = DashStyle.Dash;
                Plots[2].Pen.DashStyle = DashStyle.Dash;
                Plots[3].Pen.DashStyle = DashStyle.Dash;
        
                CalculateOnBarClose    = false;
                Overlay                = true;
                PriceTypeSupported    = false;
                AutoScale           = false;
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
                // Use this method for calculating your indicator values. Assign a value to each
                // plot below by replacing 'Close[0]' with your own formula.
                
                Up1.Set(CurrentDayOHL().CurrentOpen[0] + 200 * TickSize);
                Dn1.Set(CurrentDayOHL().CurrentOpen[0] - 200 * TickSize);
                Up2.Set(CurrentDayOHL().CurrentOpen[0] + 300 * TickSize);
                Dn2.Set(CurrentDayOHL().CurrentOpen[0] - 300 * TickSize);
                
    [B][COLOR=Red]            DrawText("Up1", true, "Up1", Up1 ,1, Color.Black, largeFont, StringAlignment.Near, Color.Transparent, Color.Transparent, 0);[/COLOR][/B]
                
            }
    
            #region Properties
            [Browsable(false)]    // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
            [XmlIgnore()]        // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
            public DataSeries Up1
            {
                get { return Values[0]; }
            }
    
             [Browsable(false)]    // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
            [XmlIgnore()]        // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
            public DataSeries Dn1
            {
                get { return Values[1]; }
            }
            
             [Browsable(false)]    // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
            [XmlIgnore()]        // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
            public DataSeries Up2
            {
                get { return Values[2]; }
            }
            
             [Browsable(false)]    // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
            [XmlIgnore()]        // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
            public DataSeries Dn2
            {
                get { return Values[3]; }
            }
            
            #endregion 
        }
    }
    Appreciate the help,
    suprsnipes

    #2
    Just looking at it, it looks like some mix up and something missing.

    DrawText(string tag, bool autoScale, string text, int barsAgo, double y, double yPixelOffset, Color textColor, etc.

    Is Up1 supposed to be the y position? If so you need to use Up1[0]. First you need a barsAgo position and I assume you want text to the right of the current bar. Then maybe a 1 pixel offset to help center the text vertically. Try this:



    DrawText("Up1", true, "Up1", 0, Up1[0] ,1, Color.Black, largeFont, StringAlignment.Near, Color.Transparent, Color.Transparent, 0);
    eDanny
    NinjaTrader Ecosystem Vendor - Integrity Traders

    Comment


      #3
      Thanks alot eDanny, your suggested code works brilliantly.

      I attempted the draw text after reading some posts on the subject on this forum. I wasn't quite sure where I went wrong and didn't expect a reply so soon. Really appreciate it.

      Originally posted by eDanny View Post
      Just looking at it, it looks like some mix up and something missing.

      DrawText(string tag, bool autoScale, string text, int barsAgo, double y, double yPixelOffset, Color textColor, etc.

      Is Up1 supposed to be the y position? If so you need to use Up1[0]. First you need a barsAgo position and I assume you want text to the right of the current bar. Then maybe a 1 pixel offset to help center the text vertically. Try this:



      DrawText("Up1", true, "Up1", 0, Up1[0] ,1, Color.Black, largeFont, StringAlignment.Near, Color.Transparent, Color.Transparent, 0);

      Regards,
      suprsnipes

      Comment


        #4
        Can I ask a further question. I intend to use the DrawText feature on a number of lines and it appears as though it squashes the chart somewhat if there are a few of them (even though I have autoscale set to false).

        Is there a way around this?

        Comment


          #5
          suprsnipes, can you please post a screenshot of the issue you see? Please also check into all set Y Axis values in your calls.
          BertrandNinjaTrader Customer Service

          Comment


            #6
            Hi Bertrand,

            Please find the screen shots as requested;

            It appears as though all of the horizontal lines that have a draw text comment beside them have to be displayed within 1 page as NT won't allow me to 'zoom'.

            For example I can't hold down the left mouse button on the price axis to show only the closest items (as per screen shot 1). The best I could do is shown is screen shot 2 and that's using the horizontal scroll bar stretching the time series.

            Originally posted by NinjaTrader_Bertrand View Post
            suprsnipes, can you please post a screenshot of the issue you see? Please also check into all set Y Axis values in your calls.
            I am not quite sure what you mean by your section of the above quote (highlighted in red).

            I appreciate you help,
            suprsnipes
            Last edited by suprsnipes; 03-23-2010, 10:52 PM.

            Comment


              #7
              Since you're using plots for those lines - is autoScale for the overall indicator generating those disabled as well?
              BertrandNinjaTrader Customer Service

              Comment


                #8
                Originally posted by NinjaTrader_Bertrand View Post
                Since you're using plots for those lines - is autoScale for the overall indicator generating those disabled as well?
                Yes it is set to false.

                Comment


                  #9
                  Ok, thanks for confirming - anyway to test this for you on my end?
                  BertrandNinjaTrader Customer Service

                  Comment


                    #10
                    All your DrawText calls set autoScale to true, please set it to false and then retry - works for me here as expected.
                    BertrandNinjaTrader Customer Service

                    Comment


                      #11
                      No problems, sorry Bertand. With regards to me answering yes to your question I thought that since I had set AutoScale to false in the Initialize section this was enough, obviously not.

                      Appreciate your help.

                      Comment


                        #12
                        You're welcome, correct this will work for your Plots, but not the Draw method calls where you set this explicitly to a hard coded value (i.e. true / false).
                        BertrandNinjaTrader Customer Service

                        Comment


                          #13
                          I am trying to use this expanded DrawText method. My code blows up with CS0103 The name 'largeFont' does not exist in the current context. It refers me back to the System.Drawing declaration. Can you help me define largeFont?

                          DrawText("Up1", true, "Up1", 0, Up1[0] ,1, Color.Black, largeFont, StringAlignment.Near, Color.Transparent, Color.Transparent, 0);

                          Comment


                            #14
                            Hi maxsys,

                            You need to add the following line of code within the variables section

                            Code:
                            private Font    largeFont = new Font("Arial", 12);     //This variable holds the large font

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by JonesJoker, 04-22-2024, 12:23 PM
                            8 responses
                            41 views
                            0 likes
                            Last Post JonesJoker  
                            Started by timko, Today, 06:45 AM
                            0 responses
                            3 views
                            0 likes
                            Last Post timko
                            by timko
                             
                            Started by Waxavi, 04-19-2024, 02:10 AM
                            2 responses
                            37 views
                            0 likes
                            Last Post poeds
                            by poeds
                             
                            Started by chbruno, Yesterday, 04:10 PM
                            1 response
                            44 views
                            0 likes
                            Last Post NinjaTrader_Gaby  
                            Started by Max238, Today, 01:28 AM
                            1 response
                            25 views
                            0 likes
                            Last Post CactusMan  
                            Working...
                            X