Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Writing line location to text file

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

    Writing line location to text file

    Hi, I would like to draw lines on a chart and have the start and end point written to a text file.
    I understand I can use the stream writer but I'm looking for help on where to get the start and end data from when drawing objects.

    Thanks.

    #2
    Hello brucelevy,

    I'm assuming you are trying to loop through manually drawn objects and not objects that have been placed with the script, is this correct?

    For example, this would print the price (.Y value) of a horizontal line:
    Code:
    foreach(IDrawObject draw in DrawObjects)
    {
    	if (draw.DrawType == DrawType.HorizontalLine)
    	{
    		IHorizontalLine hLine;
    		hLine = (IHorizontalLine) draw;
    		if (hLine != null)
    			Print(hLine.Y);
    	}
    }
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Yes I want to get the manually drawn objects. I am testing it now and it does not appear to be printing anything to the output window when drawing a horizontal line.

      Thanks.

      Comment


        #4
        Hello brucelevy,

        In what method have you added this code?

        Is the script being run before or after you have drawn an object on the chart?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Sorry I am not sure what you mean. I inserted the code as is into an indicator. Opened the output window, applied the indicator and drew on the chart to see if anything appeared in the output window.

          Comment


            #6
            Hello brucelevy,

            Initialize(), OnStartUp(), OnBarUpdate(), OnMarketData(), OnOrderUpdate(), are all methods in which you may have placed this code.

            In other words, I am trying to ensure there is a proper order of actions happening.

            If you run the script before placing the drawing object, then it will not be able to find the drawing object because it ran before you drew it.

            If you have the code in OnBarUpdate, then the print would appear after the object is drawn and bar is closed.

            Below I am including a video to demonstrate this code will print the y value of a horizontal line. (no sound, just quick demonstration)
            World's leading screen capture + recorder from Snagit + Screencast by Techsmith. Capture, edit and share professional-quality content seamlessly.


            Attached is the script I've tested in the video.
            Attached Files
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Thanks, great video.

              Is it possible to provide an example of how to get hLine.Y to print into the streamwriter text file?

              I have tried using the following

              foreach(IDrawObject draw in DrawObjects)
              {
              if (draw.DrawType == DrawType.HorizontalLine)
              {
              IHorizontalLine hLine;
              hLine = (IHorizontalLine) draw;
              if (hLine == null)
              return;
              Print(hLine.Y);
              sw.WriteLine(hLine.Y);

              }

              Comment


                #8
                Hello brucelevy,

                While we do have an already created example of using the StreamWriter (SampleStreamWriter), which I am linking below, I will have a newer member of our support create this example to help him gain experience.

                We appreciate your patience.

                SampleStreamWriter reference sample - http://ninjatrader.com/support/forum...ead.php?t=3475
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Hello Brucelevy,

                  I’ve created a script which combines the indicator Chelsea put together which prints the Y value of your horizontal line to the output window, and instead writes that value inside a text file stored in the NinjaTrader 7 folder called MyTextFile.txt”. If the file does not exist, it’ll create it.

                  The Horizontal Line script found here,


                  The NinjaTrader Streamwriter reference example,


                  Please let us know if you need further assistance.
                  Attached Files
                  Alan P.NinjaTrader Customer Service

                  Comment


                    #10
                    Thanks Alan I see the changes you made, nice job..

                    Would it be similar when writing trend lines?

                    I am just getting a message that says "An object reference is required for the non -static StartY and EndY

                    if (draw.DrawType == DrawType.Line)
                    {
                    ILine line;
                    // IHorizontalLine hLine;
                    line = (ILine) draw;
                    if (line != null)
                    {
                    try
                    {
                    if (CurrentBar == 0)
                    sw = File.AppendText(path); // If file at 'path' doesn't exist it will create the file. If it does exist it will append the file.
                    sw.WriteLine(ILine.StartY);// This is the output of all lines.
                    sw.WriteLine(ILine.EndY);// This is the output of all lines.

                    }
                    //

                    //
                    catch (Exception e)
                    {
                    // Outputs the error to the log
                    Log("You cannot write and read from the same file at the same time. Please remove SampleStreamReader.", NinjaTrader.Cbi.LogLevel.Error);
                    throw;
                    }
                    }

                    }
                    Last edited by brucelevy; 11-09-2016, 10:28 PM.

                    Comment


                      #11
                      Hello brucelevy,

                      Currently you are trying to print the type's StartY and not an object's StartY.

                      ILine is the type of object. The line object is an ILine object.

                      The line variable (lower case) is the actual object. This variable's type is an ILine object.

                      line.StartY would work. ILine.StartY will not.
                      Chelsea B.NinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                      0 responses
                      571 views
                      0 likes
                      Last Post Geovanny Suaza  
                      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                      0 responses
                      330 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by Mindset, 02-09-2026, 11:44 AM
                      0 responses
                      101 views
                      0 likes
                      Last Post Mindset
                      by Mindset
                       
                      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                      0 responses
                      548 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by RFrosty, 01-28-2026, 06:49 PM
                      0 responses
                      549 views
                      1 like
                      Last Post RFrosty
                      by RFrosty
                       
                      Working...
                      X