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

Read and plot line data from text file

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

    Read and plot line data from text file

    Hi I am drawing lines from my chart into a text file with streamwriter. I am now trying to read the lines from the text file to plot on another users chart.


    How the data is output to text file:
    (Instrument.FullName+", "+line.EndBarsAgo+", "+line.StartY+", "+line.StartBarsAgo+", "+line.EndY+" ###");

    Text file example:
    6B 12-16, -904, 1.21805790881045, -620, 1.21455946327216 ###

    How would I go about plotting them into the chart?
    Last edited by brucelevy; 11-15-2016, 03:47 PM.

    #2
    Hello,

    Thank you for the question.

    What specifically is the problem when trying to read the data?

    I am not able to tell by the description on what you have tried already, if you are unsure how to read at all, we have a sample that demonstrates this concept here: http://ninjatrader.com/support/forum...ead.php?t=3476

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Hi Jesse,

      I have a related question similar to this.

      What I want to do is make a script that saves the price level of about 200 horizontal lines out to a text file and then can also re-create them again.

      So far, I've referenced and used the code from your previous post:


      And it seems to work SOMETIMES. What I mean by that is I can test it by creating a few horizontal lines and it will print out the price anchors but if I save and recompile the code, it will no longer work until I double click one of the lines to 'refresh' it or close and reopen NT8.

      Is this because I've placed the code in the State.Dataloaded method? Is there a better place for it if I want to enable and disable to import and export a few times per session?

      Can you also broadly summarize the relationships between the following since there's a lot that changed in NT8?: Like, why did your previous post search for IChartObjects in ChartObjects instead of HorizontalLines in DrawObjects?

      - IChartObject
      - ChartObject
      - DrawObjects
      - DrawingTools
      - IDrawingTool
      - Anchors

      As you can tell, I'm pretty much a newbie when it comes to coding so your help and patience is greatly appreciated!

      Comment


        #4
        Hello,

        Thank you for the reply.

        Do you have a sample of the code you are currently trying? I couldn't really say if it is the state being used or not but if I could review what you are trying I could look further into it.

        Regarding the objects, the sample in the other post uses IChartObject because that is the type of item in the collection being used. The Collection ChartControl.ChartObjects has a type of Collection<IChartObject>, this means this collection can hold any type of object that inherits from IChartObject. This is followed by checks to know what type of object that IChartObject actually is.

        The various items you have listed all have specific purposes in the platform, you would mainly need to understand from the sample that the Collection contains multiple different types of objects and there is a check in place to know what object is being iterated.

        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Hi Jesse,

          Thanks for the quick reply!

          The descriptions of DrawObjects and ChartObjects and ChartControl are confusing to me because I'm not sure how they relate in regards to creating and editing 'stuff' on the charts.

          DrawObjects Definition:
          A collection holding all of the drawn chart objects for the primary bar series. The draw objects can be manually drawn or script generated objects.

          ChartObjects Definition:
          A collection of objects configured on the chart panel.
          Property Value:An IList of Gui.NinjaScript.IChartObject instances containing references to the objects configured on the panel

          ChartControl Definition:
          The ChartControl class provides access to a wide range of properties and methods related to the location of objects on a chart and other chart-related properties. The ChartControl object provides information related to the entire hosting grid of the chart, which overlap with the ChartPanel, ChartScale and ChartBars.

          So, should I always search for and edit things on a chart within ChartControl, DrawObjects, or in the IChartObject collection?

          As for the code, here it is:


          I create a few horizontal lines on the ES, attach the strategy to the chart, run simulated, enable the strategy and it creates the file with the price anchors.

          But when I stop the strategy, open the strategy in the editor, create some new blank lines, save and compile, when I start the strategy, it still shows the correct count of objects but not the price anchors until I restart NT8. Should the code not be running under State==Data.Loaded? Maybe OnRender()?

          And again, thank you very much for your help!

          Comment


            #6
            Hello,

            The difference between these items is they are used for different purposes. DrawObjects is a collection of only DrawObjects where as the ChartObjects is a collection of all objects on the chart. This could include other things that are not DrawObjects.

            ChartControl is an actual control, this is the charts main controller per-say. This contains many useful methods for controlling the chart. You can see this page for more details: http://ninjatrader.com/support/helpG...n-us/chart.htm (click chart control to see its properties, see other items like ChartPanel as well)

            I tried the sample and also see it has problems, for what you are trying I would likely suggest to instead use serialization and an object to make things easy. I have made a small sample showing saving unique objects to file and then reloading along with a test.

            To run the sample,
            1. draw a Line object on the chart.
            2. Apply the strategy and enable it.
            3. Disable the strategy and delete the line
            4. Enable the strategy once again.


            The result should be a new red line in place of the deleted line, this is a simple example but demonstrates saving and loading. You could expand this further by adding more properties to the MyObject class like the Tag, Brush or Instrument. You could add further conditions upon loading to draw depending on what is being loaded etc.

            Also when compiling changes it can be expected that some items don't get reloaded property in the script depending on what was changed. This is usually specific to items in OnStateChange or early on in the script. I suggest to always remove the strategy and re add it to ensure all changes are made after compiling.


            I look forward to being of further assistance.
            Attached Files
            JesseNinjaTrader Customer Service

            Comment


              #7
              Hi Jesse,

              I want to sincerely thank you for your help. The speed, quality, and thoroughness of your replies are fantastic.

              Since I've only started to learn programming last month, I see that I should study System.Type, Lamda, and now Serialization. It will take a bit to digest your example but I had some questions regarding it.

              1. In Line 54: myObjects = new List<MyObject>();
              Why did you initialize the List<MyObject>() in State.SetDefaults?

              2. This is the first time I've learned of serialization and you're right, it seems much better than saving to a text file. In my review of it, msdn docs say that binary serialization can save the public AND private fields/properties so I'm wondering why you chose XML serialization instead. Does NT8 support Binary Serialization?

              3. Can you explain why you used the lamda expression on line 82 and what it does, especially compared to line 83
              82: if (myObjects.Exists(o => o.Anchor1Time == l.StartAnchor.Time && o.Anchor1Price == l.StartAnchor.Price && o.Anchor2Time == l.EndAnchor.Time && o.Anchor2Price == l.EndAnchor.Price && o.ObjectType == l.GetType().FullName)) continue;
              83: myObjects.Add(new MyObject() { Anchor1Price = l.StartAnchor.Price, Anchor1Time = l.StartAnchor.Time, Anchor2Price = l.EndAnchor.Price, Anchor2Time = l.EndAnchor.Time, ObjectType = l.GetType().FullName });

              4. On Line 115: XmlSerializer.FromTypes(new[] { objectType })[0];
              Did you use the FromTypes method in anticipation of me wanting to serialize something more than a single list of MyObjects?

              5. And lastly, what version of .Net does NT8 use?

              Thank you so much!
              Last edited by hillborne; 11-20-2016, 08:52 AM. Reason: Forgot a question....

              Comment


                #8
                Hello,

                1, This is to give the list a value, if you were to try and add to the list later without having first created the list you would get an error. SetDefaults is a suggested place to create instances if needed.

                2. This was an easy example to provide, how you accomplish the end goal is up to you and there are many ways to do that. I had found these serialization methods using google quite some time ago and were fairly simple examples of serialization/deserialization. You could use XML or binary, or simply a text file. The logic that is most easy for you to understand and use would be preferred unless the performance is greatly reduced, in that case you could likely try something new or different and compare results.

                3. I would suggest to comment out this region of code out and then re try the example, these samples are generally intended for you to explore with and change however needed to better understand the concept. The result of removing this syntax should be that existing items are re added to the list so you would end up with duplicated lines. Because there are many properties in a DrawingObject, this could be one way to check if the object is identical. You could also use a Tag or any other means that suits your end goal.

                4. The XmlSerializer does not contain a method for a single type, instead FromTypes takes an Array of types. Per the msdn example it is possible to use multiple types but this sample specifically only accounts for the sample class provided in the script.
                Serializes and deserializes objects into and from XML documents. The XmlSerializer enables you to control how objects are encoded into XML.



                5. NT8 uses .net 4.5, this is a good question to ask as many C# samples you find online may be specific to the framework version and window system being used. MSDN is a good example as you can change the framework version in the toolbar to change the samples. Please note with items you locate if they are intended for .net 4.5 or less, and also if it is something related to the user interface make note if the syntax is for "Winforms" or "WPF" this will be specifically important for GUI items like customizing the chart or using Addons.

                I look forward to being of further assistance.
                JesseNinjaTrader Customer Service

                Comment


                  #9
                  Hi Jesse,

                  Yes, you've given me some terrific examples of techniques that will be very useful and I'm beginning to understand what you've made in your code.

                  To clarify my previous question #1 about initializing the List in State.SetDefaults: I was confused where the proper place to declare/initialize variables is after reviewing the OnState() documentation here: http://ninjatrader.com/support/helpG...tatechange.htm

                  It says that 'custom resources' should be declared under State.Configure but I'm not sure what is meant by that, especially with the Tip section at the end. Is State.Configure where you put your own variables and classes?

                  I promise that this will be the last question for at least a few days! Thank you!

                  Comment


                    #10
                    Hello,

                    In this case the list would only need to be declared before it is used, because there is no public property for the list it really wouldn't matter as much where this is defined so long as it is before the use. This sample was more for simplicity so the additional states have been excluded and the initializing of the list was added to SetDefaults.The reasoning for this note is that SetDefaults gets called multiple times.

                    The note in the help guide would include items you see as a resource, generally a resource would be things like adding subscriptions to event handlers, items that should only occur once the script is actually added to the chart, or other items you have to manage carefully.

                    Default values for public properties should still be defined in SetDefaults as the default value of the property should be available when the script is being configured in the GUI. This would really only be the case if the property is a browsable property the user may edit, otherwise no value would be defined when the user goes to edit the property.

                    Depending on the logic or syntax being used would specifically delegate what State is needed, NinjaScript specific syntax will generally show an example with the correct state for its use but general use syntax from C# will require you to think through what would be the best state.

                    I look forward to being of further assistance.
                    JesseNinjaTrader Customer Service

                    Comment


                      #11
                      This is very good. Exactly what i am looking for, well, almost.

                      I prefer to execute similar code but as an indicator plotting AddLine() instead of HorizontalLine() - try not to use drawings (if possible) - from multiple price levels drawn by the user. On a related note I found this in the forum . . . https://paul-ninjatrader.tinytake.co...Ml8xMTEwNjQ4MQ. I envision running the "IsDrawnByUser" in OnBarUpdate but keeping the read/write in State.Historical. I assume the "MyObject" class would remain outside the comparable "Indicator" class. Also I assume the "Serialize" / "DeSerialize" would likewise operate outside of OnBarUpdate but inside the "Indicator" class.

                      Please advise if it is feasible to turn this into an indicator. If so, please comment on my thoughts above and elaborate on any other notable similarities/differences.

                      Thanks in advance for your support.

                      Comment


                        #12
                        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

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by funk10101, Today, 09:43 PM
                        0 responses
                        3 views
                        0 likes
                        Last Post funk10101  
                        Started by pkefal, 04-11-2024, 07:39 AM
                        11 responses
                        36 views
                        0 likes
                        Last Post jeronymite  
                        Started by bill2023, Yesterday, 08:51 AM
                        8 responses
                        44 views
                        0 likes
                        Last Post bill2023  
                        Started by yertle, Today, 08:38 AM
                        6 responses
                        26 views
                        0 likes
                        Last Post ryjoga
                        by ryjoga
                         
                        Started by algospoke, Yesterday, 06:40 PM
                        2 responses
                        24 views
                        0 likes
                        Last Post algospoke  
                        Working...
                        X