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

Auto Draw Horizontal Lines (2 different colors) based on CSV or txt file

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

    #16
    Hello fripi,

    Thank you for your screenshot.

    The current errors seem to be a matter of syntax. The method should use .Freeze() and you are missing the parenthesis. The example in the help guide is as follows:
    Code:
    // initiate new solid color brush which has an alpha (transparency) value of 100
    myBrush = new SolidColorBrush(Color.FromArgb(100, 56, 120, 153));
    myBrush.Freeze();
    
    Draw.Line(this, "tag1", true, 10, 1000, 0, 1001, myBrush, DashStyleHelper.Dot, 2);​
    Please add () to your use of Freeze in lines 63 and 64 and this should resolve the errors. The .Freeze() method is a general C# concept and is not specific to NinjaTrader. More information may be found at the following publicly available link:
    Makes a Freezable object unmodifiable and sets its IsFrozen property to true, or tests whether a Freezable object can be made unmodifiable.


    Please let us know if we may be of further assistance.
    Emily C.NinjaTrader Customer Service

    Comment


      #17
      OMG ok sorry missed that, no errors anymore and output changed :
      Code:
      Indicator 'RGBPriceLines': Error on calling 'OnStateChange' method: The calling thread cannot access this object because a different thread owns it.
      ​
      I tried some Print and it stops at
      Code:
      _resColor.Freeze();
                      _resColor2.Freeze();​
      Last edited by fripi; 10-23-2023, 02:58 PM.

      Comment


        #18
        Originally posted by fripi View Post
        OMG ok sorry missed that, no errors anymore and output changed :
        Code:
        Indicator 'RGBPriceLines': Error on calling 'OnStateChange' method: The calling thread cannot access this object because a different thread owns it.
        ​
        I tried some Print and it stops at
        Code:
        _resColor.Freeze();
        _resColor2.Freeze();​
        I see now that the error happens even when using the snippet I previously provided; I apologize for the inconvenience. The following updated snippet should work, and I have also edited my post number 10 previously on this thread to include this updated snippet:
        Code:
        private Brush _myColor;
        private Brush _myColor2;
        ​
        protected override void OnStateChange()
        {
        
        else if (State == State.DataLoaded)
        {
        _myColor = new SolidColorBrush(Color.FromArgb(80, 255, 70, 0));
        _myColor2 = new SolidColorBrush(Color.FromArgb(50, 255, 70, 0));​​
        ​ _myColor.Freeze();
        _myColor2.Freeze();​
        }
        }​
        Thanks again for your time and patience.
        Emily C.NinjaTrader Customer Service

        Comment


          #19
          That works great !
          I just added the reading of a second file to add my support lines, maybe not the most elegant code but it works !

          Now is it possible to trace those lines on all charts of the same instrument ? I read something about isGlobal but when I try to add any more arguments to my Draw.HorizontalLine it says "no overload for method 'HorizontalLine' takes 8 arguments"

          Comment


            #20
            Originally posted by fripi View Post
            That works great !
            I just added the reading of a second file to add my support lines, maybe not the most elegant code but it works !

            Now is it possible to trace those lines on all charts of the same instrument ? I read something about isGlobal but when I try to add any more arguments to my Draw.HorizontalLine it says "no overload for method 'HorizontalLine' takes 8 arguments"
            In order to draw a global horizontal line, you must use the syntax on this page that contains the bool isGlobal:


            Then, if you want to programmatically set the colors still, you will actually need to set the stroke for the object in the same way as the snippet on this page, which saves the line to a variable when it is drawn and then changes the stroke for the myLine object:


            Thank you for your time and patience.
            Emily C.NinjaTrader Customer Service

            Comment


              #21
              Well everything was going really well until I update NT to 8.1.2.0...
              When loading it told me I had to recompile some thing, as I only had 1 personnal code I knew it was this one, and now I got 4 errors, all the same :
              HTML Code:
              Unexpected character " CS1056
              for line 67 and 71, which are :

              Code:
               _resColor2 = new SolidColorBrush(Color.FromArgb(140, 255, 70, 0));​​
               _supColor2 = new SolidColorBrush(Color.FromArgb(140, 30, 144, 255));​​
              ​
              full code part
              Code:
              if (State == State.DataLoaded)
                          {
                              _resColor = new SolidColorBrush(Color.FromArgb(180, 255, 70, 0));
                              _resColor2 = new SolidColorBrush(Color.FromArgb(140, 255, 70, 0));​​
                              _resColor.Freeze();
                              _resColor2.Freeze();
                              _supColor = new SolidColorBrush(Color.FromArgb(180, 30, 144, 255));
                              _supColor2 = new SolidColorBrush(Color.FromArgb(140, 30, 144, 255));​​
                              _supColor.Freeze();
                              _supColor2.Freeze();
                          }​

              Comment


                #22
                Hello fripi,

                Thank you for your reply.

                Do you have any extra spaces before or after the text in each line? Did you copy and paste any of those lines? If so, I suggest deleting and re-typing each line and compile after each line that you re-write to check for errors again. This is a general error and doesn't seem related to any of your logic; it is related to syntax and an unexpected character somewhere in your script. The latest version of NinjaTrader updated the compiler to use Roslyn and can now use C# up to version 8 as noted in the release notes here:


                Please let us know if we may be of further assistance.
                Emily C.NinjaTrader Customer Service

                Comment


                  #23
                  Thank you,

                  by copying the code to sublimeText i discovered invisible characters <0x200b>​​​​​​

                  Comment


                    #24
                    Hi for those interested, this is my last version, it displays levels I have saved in txt files and displays horizontal lines at the given prices.
                    So if I restart or for any reason lose my workspace it's saved elsewhere.

                    To do so it reads 2 documents: "resistances.txt" and "supports.txt" which are placed in "drive:\Documents\NinjaTrader 8"
                    Colors, line width, transparency... are fixed in the code, I don't know yet how to make that configurable, so if you want to change that you'll need to do so in Ninja Script Editor.
                    Actually supports are blue and resistance orange, see screenshot.

                    In the txt file there MUST be a space after the price and one price per line.
                    You can also add the mention (major) after this space if it's an important level, the line will be thicker and and plain, otherwise it's dashed.

                    Attached Files

                    Comment


                      #25
                      Originally posted by fripi View Post
                      Hi for those interested, this is my last version, it displays levels I have saved in txt files and displays horizontal lines at the given prices.
                      So if I restart or for any reason lose my workspace it's saved elsewhere.

                      To do so it reads 2 documents: "resistances.txt" and "supports.txt" which are placed in "drive:\Documents\NinjaTrader 8"
                      Colors, line width, transparency... are fixed in the code, I don't know yet how to make that configurable, so if you want to change that you'll need to do so in Ninja Script Editor.
                      Actually supports are blue and resistance orange, see screenshot.

                      In the txt file there MUST be a space after the price and one price per line.
                      You can also add the mention (major) after this space if it's an important level, the line will be thicker and and plain, otherwise it's dashed.

                      Awesome work!!! Would there be a way to add labels or text per price line?

                      Comment


                        #26
                        Originally posted by JusticeKing View Post

                        Awesome work!!! Would there be a way to add labels or text per price line?
                        I suppose it could be done, but a generic text like the price itself, but I must look into that when I got time, I'm discovering the ninjascript in my free time.
                        I found an indicator which adds the price to drawned lines, maybe if you load it after it could work:
                        This indicator will read the horizontal lines that you have applied to a chart and display the price at which that line sits.


                        I will try to understand how it's done to add the functionality

                        I would like to:
                        • make colors configurable in the indicator properties
                        • also line opacity property
                        • also line width property
                        • checkbox property to add or not the price text above the line
                        • add zones with "region highlight y" so in the txt file if on one line I write : "4400 4405 (major)" it could mark a zone and not just a line

                        Comment


                          #27
                          So I found this other indicator that draws the price tag above or below horizontal lines, I would like to extend it to RegionHighlightY, so I tried this

                          Code:
                          if(thisObject is NinjaTrader.NinjaScript.DrawingTools.RegionHighlightY)
                          {
                          l2 = thisObject as NinjaTrader.NinjaScript.DrawingTools.RegionHighlightY;
                          
                          SharpDX.DirectWrite.TextLayout textLayout2 =
                          new SharpDX.DirectWrite.TextLayout(NinjaTrader.Core.Gl obals.DirectWriteFactory,
                          l2.StartAnchor.Price.ToString(_priceFormat), textFormat2, 400, textFormat1.FontSize);
                          
                          SharpDX.Vector2 lowerTextPoint = new SharpDX.Vector2(ChartPanel.W - textLayout2.Metrics.Width - 5,
                          ChartPanel.Y + ((float)l1.StartAnchor.GetPoint(chartControl, ChartPanel, chartScale).Y)-(float)textLayout2.Metrics.Height);
                          RenderTarget.DrawTextLayout(lowerTextPoint, textLayout2, textBrushDx, SharpDX.Direct2D1.DrawTextOptions.NoSnap);
                          }
                          I got no error but it doesn't do anything, as I copied the code of the HorizontalLine and just changed the type of drawing maybe I miss some arguments as there are 2 Y ?​

                          Another point is that the indicator seems to refresh a lot, and I just want it to do his stuff once at the loading of the chart after another indicator was loaded, so I suppose that I need to change the "onRender" to something else ?

                          Code:
                          protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
                                  {
                                      base.OnRender(chartControl, chartScale);​
                          Last edited by fripi; 11-22-2023, 04:42 AM.

                          Comment


                            #28
                            Originally posted by fripi View Post
                            So I found this other indicator that draws the price tag above or below horizontal lines, I would like to extend it to RegionHighlightY, so I tried this

                            Code:
                            if(thisObject is NinjaTrader.NinjaScript.DrawingTools.RegionHighlightY)
                            {
                            l2 = thisObject as NinjaTrader.NinjaScript.DrawingTools.RegionHighlightY;
                            
                            SharpDX.DirectWrite.TextLayout textLayout2 =
                            new SharpDX.DirectWrite.TextLayout(NinjaTrader.Core.Gl obals.DirectWriteFactory,
                            l2.StartAnchor.Price.ToString(_priceFormat), textFormat2, 400, textFormat1.FontSize);
                            
                            SharpDX.Vector2 lowerTextPoint = new SharpDX.Vector2(ChartPanel.W - textLayout2.Metrics.Width - 5,
                            ChartPanel.Y + ((float)l1.StartAnchor.GetPoint(chartControl, ChartPanel, chartScale).Y)-(float)textLayout2.Metrics.Height);
                            RenderTarget.DrawTextLayout(lowerTextPoint, textLayout2, textBrushDx, SharpDX.Direct2D1.DrawTextOptions.NoSnap);
                            }
                            I got no error but it doesn't do anything, as I copied the code of the HorizontalLine and just changed the type of drawing maybe I miss some arguments as there are 2 Y ?​

                            Another point is that the indicator seems to refresh a lot, and I just want it to do his stuff once at the loading of the chart after another indicator was loaded, so I suppose that I need to change the "onRender" to something else ?

                            Code:
                            protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
                            {
                            base.OnRender(chartControl, chartScale);​
                            You mentioned you got no error but it wasn't doing anything; do you see any errors on the Log tab of the Control Center? If you look in the Drawing Tools menu (right-click chart > hover over Drawing Tools > select Drawing Objects from the bottom of the list) when the indicator is applied, do you see any region highlight y objects on the list?

                            When it comes to OnRender() it is expected that it refreshes frequently. What do you mean by wanting to only do this once when the chart loads? Are you wanting to have one object drawn on the chart that doesn't change and stays in the same place on the chart? For more details on custom rendering and OnRender();I look forward to your reply.
                            Emily C.NinjaTrader Customer Service

                            Comment


                              #29
                              Originally posted by NinjaTrader_Emily View Post

                              You mentioned you got no error but it wasn't doing anything; do you see any errors on the Log tab of the Control Center? If you look in the Drawing Tools menu (right-click chart > hover over Drawing Tools > select Drawing Objects from the bottom of the list) when the indicator is applied, do you see any region highlight y objects on the list?

                              When it comes to OnRender() it is expected that it refreshes frequently. What do you mean by wanting to only do this once when the chart loads? Are you wanting to have one object drawn on the chart that doesn't change and stays in the same place on the chart? For more details on custom rendering and OnRender();I look forward to your reply.
                              I did a Print(l2.StartAnchor.Price) and my output log gives me the first value of all zones, but nothing comes on the display

                              I have a text file where I write down every day the levels and zones that seems important to me, and my indicator loads those levels and draws Horizontal lines or RegionHighlightY, that just needs to be done at the loading 1 time. Orange and blue lines on bellows screenshot.
                              Then I found a the indicator here https://ninjatraderecosystem.com/use...izontal-lines/ and it labels all horizontal lines, but those lines never moves in a session for me.
                              The blue labels right to the lines on my screenshot, but nothing on the zones...
                              So I want to adapt the indicator to also mark the zones and just put the labels once at loading, no need of looping with the onRender

                              Click image for larger version

Name:	Capture d'écran 2023-11-22 185453.png
Views:	79
Size:	42.5 KB
ID:	1279040
                              Attached Files

                              Comment


                                #30
                                Hello fripi,

                                Thank you for your reply.

                                As an alternative to rendering the labeled region in OnRender() within your script, you could consider using the Labeled Lines Drawing Tool script that is also publicly available on our NinjaTrader Ecosystem website:Per the update on 10/08/2019, NinjaScript overloads were added and can be used by calling DrawLL.<insert type of line here>(). The intelliprompt should pop up and guide you through the required arguments for the method signatures. This group of drawing tools does not include a region highlight y tool, though you could use it as a guideline to also add this type of labeled tool if you'd like and then in your script, you could DrawLL.LabeledRegionHighlightY() (for example if you add this) and only call it once since the region does not need to be updated after the indicator initially loads.


                                Please let me know if I can be of further assistance.

                                The NinjaTrader Ecosystem website is for educational and informational purposes only and should not be considered a solicitation to buy or sell a futures contract or make any other type of investment decision. The add-ons listed on this website are not to be considered a recommendation and it is the reader's responsibility to evaluate any product, service, or company. NinjaTrader Ecosystem LLC is not responsible for the accuracy or content of any product, service or company linked to on this website.
                                Emily C.NinjaTrader Customer Service

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by Segwin, 05-07-2018, 02:15 PM
                                14 responses
                                1,789 views
                                0 likes
                                Last Post aligator  
                                Started by Jimmyk, 01-26-2018, 05:19 AM
                                6 responses
                                837 views
                                0 likes
                                Last Post emuns
                                by emuns
                                 
                                Started by jxs_xrj, 01-12-2020, 09:49 AM
                                6 responses
                                3,293 views
                                1 like
                                Last Post jgualdronc  
                                Started by Touch-Ups, Today, 10:36 AM
                                0 responses
                                13 views
                                0 likes
                                Last Post Touch-Ups  
                                Started by geddyisodin, 04-25-2024, 05:20 AM
                                11 responses
                                63 views
                                0 likes
                                Last Post halgo_boulder  
                                Working...
                                X