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

Change Y-Position of Text object (converting code from NT7)

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

    Change Y-Position of Text object (converting code from NT7)

    Hi,

    I'm in the process of converting code from NT7 to NT8. I've read some of the reference guides, but I'm stuck with this problem.


    In my NT7 code, I have a bunch of text objects as part of `DrawObjects`. At a particular point in the code, I retrieve the object using `DrawObjects["some_text_tag"]` and update the Y position of the object after converting it into IText i.e,

    ```
    IText text = (IText) drawObject;
    text.Y = Low[42]; //some numeric type
    ```

    But unfortunately, this does not seem to be possible in NT8. I don't see any explicit `Y` coordinate objects in the text object in the current NT8 documentation (https://ninjatrader.com/support/helpGuides/nt8/text.htm).

    I do understand that IText (https://ninjatrader.com/support/help.../nt7/itext.htm) is not available in NT8, so I'm using just `Text` object here. Do I have any other alternative if I want to change the position of these objects?

    Edit:
    I seem to have another problem with using Brushes. I used just color in the old `DrawText` method in NT7, but in NT8, as per the documentation, I'm supposed to use a Brush object. I tried to do the following, but get few errors (mentioned below):

    ```
    Draw.Text(this, "text-tag", "BUY", 90, 1000, new SolidBrush(Color.LightGreen));
    ```

    Errors:
    > The best overloaded method match for 'NinjaTrader.NinjaScript.DrawingTools.Draw.Text(Ni njaTrader.NinjaScript.NinjaScriptBase, string, string, int, double, System.Windows.Media.Brush)' has some invalid arguments

    > Argument 6: cannot convert from 'System.Drawing.SolidBrush' to 'System.Windows.Media.Brush



    Any help regarding these issues will be very helpful to me.

    Thanks!
    Last edited by tmmr34; 10-02-2019, 01:31 PM.

    #2
    Welcome to the forums tmmr34!

    Changing the Y coordinate for a Text Drawing Object would be done through the Text's Anchor.Price property.

    For example:
    Code:
    protected override void OnBarUpdate()
    {
        Text myText = Draw.Text(this, "tag1", "Text to draw", 0, High[0] + (5 * TickSize), Brushes.Silver);
        myText.Anchor.Price = 53;
    }
    Please see IDrawingTool documentation for more information on Anchor properties and other generic Drawing Object properties.



    Let me know if there is anything else I can do to help.
    JimNinjaTrader Customer Service

    Comment


      #3
      Thanks for the quick reply, Jim! It compiles without any errors now.

      I edited my OP with another issue I having trouble with. The brush parameter of the `Draw.Text` object. I also tried 'Brushes.<SOME COLOR>` but it gives me this error:

      > Argument 6: cannot convert from 'System.Drawing.Brush' to 'System.Windows.Media.Brush'.

      I reported the errors of other methods I tried in my OP.

      Comment


        #4
        Hello tmmr34,

        You will want to get acquainted more with using Windows Media Brushes in NinjaTrader 8 as this is a pretty significant code breaking change from NinjaTrader 7 NinjaScript.

        Proper syntax could be: new SolidColorBrush(Colors.LightGreen) I would suggest Brushes.LimeGreen however.

        Working with Brushes (NT8 doc) - https://ninjatrader.com/support/help...th_brushes.htm

        Our Code Breaking Changes page can also help when converting scripts to NinjaTrader 8 - https://ninjatrader.com/support/help...ng_changes.htm

        We look forward to assisting!
        JimNinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Jim View Post
          Hello tmmr34,

          You will want to get acquainted more with using Windows Media Brushes in NinjaTrader 8 as this is a pretty significant code breaking change from NinjaTrader 7 NinjaScript.

          Proper syntax could be: new SolidColorBrush(Colors.LightGreen) I would suggest Brushes.LimeGreen however.

          Working with Brushes (NT8 doc) - https://ninjatrader.com/support/help...th_brushes.htm

          Our Code Breaking Changes page can also help when converting scripts to NinjaTrader 8 - https://ninjatrader.com/support/help...ng_changes.htm

          We look forward to assisting!
          Yep. I understand and I tried both. Neither worked.

          Here is my line 1043:
          ```
          Draw.Text(this, "text-tag", "Buy", 10, 1000, Brushes.LimeGreen); //dummy values for offset
          ```
          Here are the errors:
          XYZ.cs The best overloaded method match for 'NinjaTrader.NinjaScript.DrawingTools.Draw.Text(Ni njaTrader.NinjaScript.NinjaScriptBase, string, string, int, double, System.Windows.Media.Brush)' has some invalid arguments CS1502 1043 yyyy
          XYZ.cs Argument 6: cannot convert from 'System.Drawing.Brush' to 'System.Windows.Media.Brush' CS1503 1043 yyyy
          (Note: I changed column num in above error messages because I edited the tag name and text parameter to show you an example)

          -----------------------------------------

          [Please let me know if I should start another thread for this]


          I seem to have another issue in this code migration process. After removing the brush parameter (the only error in my code now) and fully compiling, when I add the parameter to a chart (say Simulated data - AAPL 1 minute chart), it gives me the following popup error:

          `Value property of 'Period' of NinjaScript 'MIN' is 0 and not in valid range between 1 and 2147483647 `

          I found this forum thread: https://ninjatrader.com/support/foru...rom-this-state that also talks about this problem but with a user-defined script. I'm not sure where I should start debugging since this is coming from a default/system script of 'MIN'.


          Once again, thanks for your replies!

          Comment


            #6
            Hello tmmr34,

            Is the error being reported from the same file? You can double click on the error to have the Editor go to the line that throws the error.

            Draw.Text(this, "text-tag", "Buy", 10, 1000, Brushes.LimeGreen);

            The above compiles without error on my end.

            It can be helpful to have a new thread opened for new questions so other users browsing the forum can find related articles more easily. Since you have already asked though, we'll be happy to answer.

            I would suggest first getting a setup together to consistently hit the issue. You can then search for where you use MIN in your code and then add unique prints before each call to see which one is being called with a Period of 0. You can then adjust the code as needed to correct. This could involve using Math.Max(1, YourPeriod) or adjusting the code in another way so it does not call MIN with 0 for the Period.

            We look forward to being of further assistance.
            JimNinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_Jim View Post
              Hello tmmr34,

              Is the error being reported from the same file? You can double click on the error to have the Editor go to the line that throws the error.
              Yes, it's the same file. That's why I included the exact error log. I'm only working on a single file now. Not sure what I can try from my end. I will get back to this issue after I solve the MIN-related errors I'm getting.

              For now, I've decided to step through line by line to check the parameters that go into a MIN call and check if any of them are zero. Has this behavior changed from NT7 to NT8? I do not see anything specific about this in https://ninjatrader.com/support/help...ng_changes.htm.

              I will let you know my results after I've tried some more debugging.

              Comment


                #8
                Hello tmmr34,

                NinjaTrader 7 would force a Period of 1 if a Period of 0 is supplied. Testing the following will show the same results in NT7.

                Code:
                public class MINPeriodTest : Indicator
                {
                    protected override void Initialize()
                    {
                        Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
                        Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot1"));
                        Overlay                = false;
                    }
                
                    protected override void OnBarUpdate()
                    {
                        Plot0.Set(MIN(Close, 0)[0]);
                        Plot1.Set(MIN(Close, 1)[0]);
                    }
                
                    #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 Plot0
                    {
                        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 Plot1
                    {
                        get { return Values[1]; }
                    }
                    #endregion
                }
                For a sanity check, I would suggest excluding that file from compilation temporarily and testing compiling that line of code in a new test indicator. We should expect a compile without errors or would be pointed to another file which would include the error. I would then suggest dropping the file back in, compiling and observing the syntax. If there are still issues, please attach the cs file so I can test on my end.

                I look forward to being of any further assistance.
                JimNinjaTrader Customer Service

                Comment


                  #9
                  Thanks for the testing code, Jim.
                  I will test these tonight and let you know the results.

                  Comment


                    #10
                    Originally posted by NinjaTrader_Jim View Post
                    Hello tmmr34,

                    Is the error being reported from the same file? You can double click on the error to have the Editor go to the line that throws the error.

                    Draw.Text(this, "text-tag", "Buy", 10, 1000, Brushes.LimeGreen);

                    The above compiles without error on my end.

                    It can be helpful to have a new thread opened for new questions so other users browsing the forum can find related articles more easily. Since you have already asked though, we'll be happy to answer.

                    I would suggest first getting a setup together to consistently hit the issue. You can then search for where you use MIN in your code and then add unique prints before each call to see which one is being called with a Period of 0. You can then adjust the code as needed to correct. This could involve using Math.Max(1, YourPeriod) or adjusting the code in another way so it does not call MIN with 0 for the Period.

                    We look forward to being of further assistance.
                    Alright, I've solved the MIN problem by adding Math.Max(...). Now, back to the brush problem.

                    I suspect it has something to do with types. To reiterate, ```Draw.Text(this, "text-tag", "Buy", 10, 1000, Brushes.LimeGreen);``` does not compile for me. I've provided the exact error messages in my post #5 in this thread.

                    Could there be conflicts with declarations?
                    Here are my declarations:

                    ```
                    #region Using declarations
                    using System;
                    using System.ComponentModel;
                    using System.Diagnostics;
                    using System.Drawing;
                    using System.Drawing.Drawing2D;
                    using System.Xml.Serialization;
                    using System.ComponentModel.DataAnnotations;
                    using NinjaTrader.Cbi;
                    using NinjaTrader.Data;
                    using NinjaTrader.Gui.Chart;
                    using System.Collections.Generic;
                    using NinjaTrader.NinjaScript.DrawingTools;
                    #endregion
                    ```


                    Edit: I'm trying to use something like the following:
                    ```
                    private Brush c1 = Brushes.Red;
                    ```
                    Is this wrong? I'm getting a compile error saying 'Brush' could not be found (missing directive/assembly). I wanted to use this variable `c1` as the brush param in `Draw.Text(...)` - this should be possible, right?
                    Last edited by tmmr34; 10-03-2019, 04:07 PM.

                    Comment


                      #11
                      Hello tmmr34,

                      Yes, I would not recommend using the same Using declarations as would be used in NinjaTrader 7. I would suggest creating a new script in NinjaTrader 8 by right clicking on the Indicators folder, selecting New Indicator, and then adding your code as needed. You will not want to copy Using statements in from NinjaTrader 7, and System.Drawing is not used in NinjaTrader 8.

                      Standard using statements that get generated with a new indicator are included below.

                      Code:
                      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;
                      Please let me know if you have any questions.
                      JimNinjaTrader Customer Service

                      Comment


                        #12
                        Originally posted by NinjaTrader_Jim View Post
                        Hello tmmr34,

                        Yes, I would not recommend using the same Using declarations as would be used in NinjaTrader 7. I would suggest creating a new script in NinjaTrader 8 by right clicking on the Indicators folder, selecting New Indicator, and then adding your code as needed. You will not want to copy Using statements in from NinjaTrader 7, and System.Drawing is not used in NinjaTrader 8.

                        Standard using statements that get generated with a new indicator are included below.

                        Code:
                        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;
                        Please let me know if you have any questions.
                        Thanks. This worked. I'm still learning the transition-gotchas. haha!
                        Now, everything is all-clear in the compiler and all the syntaxes work in NT8. But the thing now is that the indicator takes a long time (few minutes) to calculate the values when I add it to the chart. In NT7, it was instantaneous (<1 sec). Pretty much all I did was, copy the NT7 code's `Initialize` to NT8 code's `OnStateChange` and everything in `OnBarUpdate` in the same `OnBarUpdate` in NT8. Are there any optimization steps I'm missing in transition from NT7 to NT8? Because I have not changed any logic in the code - it's all the same NT7 code with the code-breaking changes (NT7->NT8) applied to it.

                        Comment


                          #13
                          Hello tmmr34,

                          OnStateChange will handle things differently than with Initialize/OnStartUp/OnTermination so it will be good to understand the different NinjaScript states in NinjaTrader 8.

                          OnStateChange - https://ninjatrader.com/support/help...tatechange.htm

                          NinjaScript LifeCycle - https://ninjatrader.com/support/help...fecycle_of.htm

                          For optimizing scripts and performance tips, please see our Best Practices documentation.

                          Best Practices - https://ninjatrader.com/support/help..._practices.htm

                          We look forward to being of further assistance.
                          JimNinjaTrader Customer Service

                          Comment


                            #14
                            Originally posted by NinjaTrader_Jim View Post
                            Hello tmmr34,

                            OnStateChange will handle things differently than with Initialize/OnStartUp/OnTermination so it will be good to understand the different NinjaScript states in NinjaTrader 8.

                            OnStateChange - https://ninjatrader.com/support/help...tatechange.htm

                            NinjaScript LifeCycle - https://ninjatrader.com/support/help...fecycle_of.htm

                            For optimizing scripts and performance tips, please see our Best Practices documentation.

                            Best Practices - https://ninjatrader.com/support/help..._practices.htm

                            We look forward to being of further assistance.
                            Thanks! I had to use isfirstickofbar and ChartBars.FromIndex to fix my problems as the code was essentially looping over and over again.
                            Everything is fine now. The NT7 code's output is same as NT8's code output. Thanks again for the assistance, Jim!

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by Max238, Today, 01:28 AM
                            1 response
                            22 views
                            0 likes
                            Last Post CactusMan  
                            Started by giulyko00, Yesterday, 12:03 PM
                            2 responses
                            10 views
                            0 likes
                            Last Post giulyko00  
                            Started by r68cervera, Today, 05:29 AM
                            0 responses
                            4 views
                            0 likes
                            Last Post r68cervera  
                            Started by geddyisodin, Today, 05:20 AM
                            0 responses
                            6 views
                            0 likes
                            Last Post geddyisodin  
                            Started by JonesJoker, 04-22-2024, 12:23 PM
                            6 responses
                            38 views
                            0 likes
                            Last Post JonesJoker  
                            Working...
                            X