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

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

    Hello, was wondering if anyone here had ever successfully built an indicator to automatically draw horizontal lines on NT charts based on price levels entered on a txt or CSV file?

    For example:
    Blue Lines Yellow Lines
    4410.4 4309.75
    4409.7 4567.25
    4405.25 4375.50

    #2
    Hi, thanks for posting.

    This was discussed in the past here:


    I also found this example on the app share site, but I have not tested it myself:
    I originally developed this indicator for NT7 and now have converted to NT8. It simply plots lines on chart at price lines specified in a text file. A few times NT7 would drop a line I added through Draw Tools, or I would accidentally delete a line. So, I created this indicator that plotted my […]


    Kind regards,
    -ChrisL

    Disclaimer:
    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.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChrisL View Post
      Hi, thanks for posting.

      I also found this example on the app share site, but I have not tested it myself:
      https://ninjatraderecosystem.com/use...rgbpricelines/

      Kind regards,
      -ChrisL
      Years after I come on this post, it's exactly what I search, an indicator that places lines based on a CSV or txt file.
      But I can't make it work, I created "ES-Zones.txt" in the correct path, must the price be 4400, 4400,00 or 4400.00
      But then the tab separation isn't maybe the best, akso it talks about 5 params, but 2 aren't used,...
      I'm lost, someone who knows how code works can help me with it ?
      I would like to place lines, configure price, color, width and opacity
      Last edited by fripi; 10-19-2023, 08:25 AM.

      Comment


        #4
        Originally posted by fripi View Post

        Years after I come on this post, it's exactly what I search, an indicator that places lines based on a CSV or txt file.
        But I can't make it work, I created "ES-Zones.txt" in the correct path but then the tab separation isn't maybe the best, akso it talks about 5 params, but 2 aren't used,...
        I'm lost, someone who knows how code works can help me with it ?
        Hello fripi,

        Thank you for your note.

        We do have two reference samples regarding using StreamWriter to write to a text file and using StreamReader to read text from a text file:You may be able to go off of these examples to figure out how to plot indicator lines based on text read from a file. If you are getting any error messages, please send a screenshot of the errors so we may better assist you.
        • To send a screenshot with Windows 10 or newer I would recommend using the Windows Snipping Tool.
        • Alternatively to send a screenshot press Alt + PRINT SCREEN to take a screenshot of the selected window. Then go to Start--> Accessories--> Paint, and press CTRL + V to paste the image. Lastly, save it as a jpeg file and send the file as an attachment.
        ​Otherwise, this forum thread is open for members of the forum community to assist you if you prefer more hands-on assistance or you can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like our NinjaTrader Ecosystem team to follow up with you with a list of affiliate consultants who would be happy to create this script or any others at your request or provide one-on-one educational services.

        Please feel free to reach out with any additional questions or concerns.
        Emily C.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Emily View Post

          Hello fripi,

          Thank you for your note.

          We do have two reference samples regarding using StreamWriter to write to a text file and using StreamReader to read text from a text file:
          Thank you,

          I tried to modify a code, but I'm not a coder so it's complex, and nothing happens, don't have any error message, but no line appears.
          My final goal would be to load 2 different tx files and apply different colors to the lines depending on the file they're from, also on some lines a string could indicate a major line, then I make it bigger.


          Code:
          public class RGBPriceLines : Indicator
          {
          private int _row = 0; // count of number of rows read from file
          private Brush _myColor = new SolidColorBrush(Color.FromArgb(80, 255, 70, 0));
          private Brush _myColor2 = new SolidColorBrush(Color.FromArgb(50, 255, 70, 0));
          private double _prices = 0;
          private int _width = 0;
          private bool _linesDrawn = false;
          private int _maxFields = 2; // number of fields in input record
          
          
          
          protected override void OnStateChange()
          {
          if (State == State.SetDefaults)
          {
          Description = @"Displays lines at specifed price levels with color and width options";
          Name = "RGBPriceLines";
          Calculate = Calculate.OnBarClose;
          IsOverlay = true;
          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;
          
          _fileSuffix = "resistances.txt";
          _path = NinjaTrader.Core.Globals.UserDataDir;
          
          
          }
          else if (State == State.Configure)
          {
          }
          }
          
          protected override void OnBarUpdate()
          {
          
          if (!_linesDrawn){
          
          _linesDrawn = true;
          
          string t;
          string _file_name=_path +"/"+ _fileSuffix;
          
          StreamReader myStreamReader = new StreamReader(_file_name);
          
          while (( t = myStreamReader.ReadLine()) != null)
          {
          string[] splitted = new String[_maxFields];
          splitted = t.Split(' ');
          
          if (splitted.Length == 2)
          {
          _row++; // increment record count
          double.TryParse(splitted[0], out _prices); // parse price from first tab of file record
          if (_prices == 0) Draw.TextFixed(this,"tag"+_row,"Price field not numberic at row: "+_row, TextPosition.BottomLeft);
          if(splitted[1]=="(major)"){
          Draw.HorizontalLine(this,"tag"+_row, _prices, _myColor, DashStyleHelper.Solid, 4, _linesDrawn); // Draw desired line
          }else{
          Draw.HorizontalLine(this,"tag"+_row, _prices, _myColor2, DashStyleHelper.Solid, 2, _linesDrawn); // Draw desired line
          }
          }
          }
          myStreamReader.Close();
          }
          }​
          Attached Files

          Comment


            #6
            Hello fripi,

            Thank you for your reply.

            I suggest adding Print() statements to your script to understand which parts of your logic are being evaluated as well as what values are being used in each of your variables from drawing the lines. For more information on using prints to debug your scripts:


            Otherwise, as previously mentioned, ​the thread is open for members of the forum community to assist you.

            If you prefer more hands-on assistance, you can contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like our NinjaTrader Ecosystem team to follow up with you with a list of affiliate consultants who would be happy to create this script or any others at your request or provide one-on-one educational services.

            Please let me know if I may be of further assistance.
            Emily C.NinjaTrader Customer Service

            Comment


              #7
              Well rookie coder just discovered that I had an error, now I must discover what to do with it before I learn the Print thing

              Code:
              4259
              Indicator 'RGBPriceLines': Error on calling 'OnBarUpdate' method on bar 0: The calling thread cannot access this object because a different thread owns it.

              Comment


                #8
                Originally posted by fripi View Post
                Well rookie coder just discovered that I had an error, now I must discover what to do with it before I learn the Print thing

                Code:
                4259
                Indicator 'RGBPriceLines': Error on calling 'OnBarUpdate' method on bar 0: The calling thread cannot access this object because a different thread owns it.
                Print statements will help you to identify which line in OnBarUpdate() is causing this error. To narrow down the potential culprit, add a unique print statement (such as incrementing numbers or print the line number where you added the statement) every couple of lines throughout OnBarUpdate(). Next, open the NinjaScript Output window. Then, run your script and review the output prior to the error being thrown. The last print statement to show up is a clue that the line (or lines) of code causing the error are below that last print statement shown in your logic. This process is mentioned on the page that I shared as well as in the following guide on debugging:


                The error itself has to do with multi-threading and trying to access an object that is on a different thread. Once you narrow down the line of code causing the error, you should consider the information from the following page to identify a way to resolve it:


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

                Comment


                  #9
                  Following your recomendation I think I found out that the problem is linked to the brushes

                  Code:
                  private Brush _myColor = new SolidColorBrush(Color.FromArgb(80, 255, 70, 0));
                  private Brush _myColor2 = new SolidColorBrush(Color.FromArgb(50, 255, 70, 0));​
                  which are called here

                  Code:
                  if(splitted[1]=="(major)"){
                  Draw.HorizontalLine(this,"tag"+_row, _prices, _myColor, DashStyleHelper.Solid, 4, _linesDrawn); // Draw desired line
                  }else{
                  Draw.HorizontalLine(this,"tag"+_row, _prices, _myColor2, DashStyleHelper.Solid, 2, _linesDrawn); // Draw desired line
                  }​
                  I tried it differently following some info I found on the forum but got errors...
                  I tried to add _myColor.freeze; just after the brush definition, doesn't work, tried to declare variables and then later in the code give values... more errors, I'm so lost
                  must be easy for people here who can code, anyone ?

                  Comment


                    #10
                    Hello fripi,

                    Thank you for your reply.

                    Yes, this does seem that it is likely caused by your custom brushes. Calling .Freeze() on the brushes is the right thing to do; you mentioned this does not work. What if you freeze the brushes in State.DataLoaded so they are frozen before attempting to use them elsewhere in your script? We have more information about working with brushes, including the section "Understanding custom brushes," on the following page:
                    https://ninjatrader.com/support/help...th_brushes.htm

                    Here is an example of where you could try to freeze them in OnStateChange() when State is State.DataLoaded:
                    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();​
                    }
                    }​


                    Thank you for your time and patience.
                    Last edited by NinjaTrader_Emily; 10-23-2023, 03:22 PM.
                    Emily C.NinjaTrader Customer Service

                    Comment


                      #11
                      just adding
                      Code:
                      else if (State == State.DataLoaded){}

                      already gives me
                      Code:
                      'NinjaTrader.NinjaScript.State' does not contain a definition for 'Dataloaded' and no extension method 'Dataloaded' accepting a first argument of type 'NinjaTrader.NinjaScript.State' could be found (are you missing a using directive or an assembly reference?)    CS1061
                      And adding the freeze lines gives 2 extra errors
                      Code:
                      Only assignment, call, increment, decrement, and new object expressions can be used as a statement.
                      I must drive you crazy, I become crazy myself, but I search what all that means.

                      Thank you for your time

                      Comment


                        #12
                        Originally posted by fripi View Post
                        just adding
                        Code:
                        else if (State == State.DataLoaded){}

                        already gives me
                        Code:
                        'NinjaTrader.NinjaScript.State' does not contain a definition for 'Dataloaded' and no extension method 'Dataloaded' accepting a first argument of type 'NinjaTrader.NinjaScript.State' could be found (are you missing a using directive or an assembly reference?) CS1061
                        And adding the freeze lines gives 2 extra errors
                        Code:
                        Only assignment, call, increment, decrement, and new object expressions can be used as a statement.
                        I must drive you crazy, I become crazy myself, but I search what all that means.

                        Thank you for your time
                        I see the error message explicitly mentions "Dataloaded" with a capital 'D' and lowercase 'l' for loaded:
                        does not contain a definition for 'Dataloaded'
                        With that in mind, capitalization does matter and you will need to specifically use State.DataLoaded in your logic, as the message is correct that Dataloaded does not exist due to the lowercase 'l' in loaded.

                        No worries about feeling like you drive me crazy! I am glad to help and appreciate your patience and dedication to working through this. That is part of programming; sometimes you solve one thing only to lead to another error or unexpected behavior. There is definitely a feeling of accomplishment once everything is polished and in working order, and with more practice and experience it can get easier and more routine if this is something that you're interested in doing more of beyond this script that you are working on.

                        Please feel free to reach out with any additional questions or concerns.
                        Emily C.NinjaTrader Customer Service

                        Comment


                          #13
                          Here's a screenshot of my code, it uses the good capitalization
                          Click image for larger version

Name:	Capture d'écran 2023-10-23 191958.png
Views:	205
Size:	2.5 KB
ID:	1274341
                          I searched for the version given in the error code with lowercase l and it's nowhere to be found, so that's an error in the error message :-D

                          Comment


                            #14
                            Hello fripi,

                            Thank you for your patience.

                            Please provide a screenshot of the full error message, as well as the section of code it takes you to when you double-click the error message.

                            I look forward to your reply.
                            Emily C.NinjaTrader Customer Service

                            Comment


                              #15
                              I rebooted everything and it seems that error isn't there anymore :

                              Click image for larger version

Name:	Capture d'écran 2023-10-23 223602.png
Views:	207
Size:	98.5 KB
ID:	1274384

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by fx.practic, 10-15-2013, 12:53 AM
                              5 responses
                              5,404 views
                              0 likes
                              Last Post Bidder
                              by Bidder
                               
                              Started by Shai Samuel, 07-02-2022, 02:46 PM
                              4 responses
                              95 views
                              0 likes
                              Last Post Bidder
                              by Bidder
                               
                              Started by DJ888, Yesterday, 10:57 PM
                              0 responses
                              8 views
                              0 likes
                              Last Post DJ888
                              by DJ888
                               
                              Started by MacDad, 02-25-2024, 11:48 PM
                              7 responses
                              159 views
                              0 likes
                              Last Post loganjarosz123  
                              Started by Belfortbucks, Yesterday, 09:29 PM
                              0 responses
                              8 views
                              0 likes
                              Last Post Belfortbucks  
                              Working...
                              X