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

Rectangle drawing with extended line on right and Price level

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

    #16
    Hi ChrisL,
    thanks for the sample script. It helped a lot.

    I am drawing rectangles with Draw.Rectangle(NinjaScriptBase owner, string tag, bool isAutoScale, int startBarsAgo, double startY, int endBarsAgo, double endY, Brush brush, Brush areaBrush, int areaOpacity) syntex. But problem I am facing is, though the indicator is drawing the rectangles, but all are gone after I refersh or remove the indicator. is there any way to save them so that later those are plotted whenever I add the indicator?

    I thought creatig a list so that I can call them. but it did not work for me. Any advise or quick sample will be really great. thanks in advance.

    Comment


      #17
      Hello asmmbillah,

      Unfortunately, the drawing objects drawn by an indicator are attached to that indicator. If the indicator is removed, the drawing objects are also removed.

      It would be possible to save the bar time / price for each object to a text file and read these from a text file the next time the script is run so those objects are re-drawn by the indicator.

      Below is a link to the StreamWriter/StreamReader examples that demonstrate how to write to a text file.

      Chelsea B.NinjaTrader Customer Service

      Comment


        #18
        Hi, thanks for the ref.

        for streamWrite I can see the below code:
        sw.WriteLine(Time[0] + "," + Open[0] + "," + High[0] + "," + Low[0] + "," + Close[0]); // Append a new line to the file

        But I can see constructors for sw.WriteLine, I am little bit confused which one I should use for multiple drawn Rectangles using Draw.Rectangle(NinjaScriptBase owner, string tag, bool isAutoScale, int startBarsAgo, double startY, int endBarsAgo, double endY, Brush brush, Brush areaBrush, int areaOpacity)

        Your suggestion will be appreciated. thanks in advance.

        Comment


          #19
          Hello asmmbillah,

          Instead of writing the Open[0], High[0], etc...

          Write the values used for the drawing object. Such as the Time[0] for when the object was drawn, and the barsAgo and price used for the startY for each anchor of the drawing object. Write to the file any time a drawing object is drawn (instead of on every bar).
          Chelsea B.NinjaTrader Customer Service

          Comment


            #20
            thanks for your reply. In case if I remove any of the rectangles manually, does it automatically remove the text from the file?
            And for "tag" I am using "tag"+CurrentBar, is it correct to use ""tag"" +CurrentBar +", " or ""tag +CurrentBar"" +", " ?
            Do I need to add date stamp or barindex with the writeLine text additionally to read it properly later with StreamReader?

            thanks in advance.
            Last edited by asmmbillah; 09-09-2019, 11:32 AM.

            Comment


              #21
              Hello asmmbillah,

              You would need to re-write the file..

              Text is not written or removed from a text file unless you write the code to do so.

              If there are modifications, you may choose to loop through the drawing objects to re-write the file.


              For the tag this can be a word with the CurrentBar added. This would allow for multiple objects to appear on the chart instead of an existing object being updated.

              For example:
              Draw.Rectangle(this, "myRectangle" + CurrentBar, true, 10, Low[10], 0, High[0], Brushes.Blue, Brushes.Blue, 100)
              Chelsea B.NinjaTrader Customer Service

              Comment


                #22
                sorry probably I edited the post after you replied.

                Do I need to add date stamp or barindex or instrument with the writeLine text additionally to read it properly and verify, if I open different instrument later, with StreamReader?

                Comment


                  #23
                  Hello asmmbillah,

                  You would definitely want to write the instrument name, bar type, and interval into the text file name to distinguish it from other charts. This is the path string that designates where the file is written.
                  You would likely want a separate text file for each chart the indicator is added to.

                  Something like: NinjaTrader.Core.Globals.UserDataDir + Instrument.FullName + "." + BarsPeriod.Value + " " + BarsPeriod.Id
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #24
                    Thanks for your reply. I am using below for path:

                    path = System.IO.Path.Combine(NinjaTrader.Core.Globals.Us erDataDir, @"templates\ab_Resources\abBoxes"+Instrument.Fu llN ame.ToString()+""+BarsPeriod.Value.ToString()+Bars Period.BarsPeriodType.ToString()+".txt");


                    and even tried below one:

                    path = System.IO.Path.Combine(string.Format(@"{0}\templat es\ab_Resources\abBoxes\{1}\{2}{3}.txt",NinjaTrade r.Core.Globals.UserDataDir, Instrument.FullName, BarsPeriod.Value, BarsPeriod.BarsPeriodType));

                    (in above lines for some wired reason adding some spaces in the middle, but in my code actually there are not wired spaces.)

                    but it continuously showing error saying, "Error on calling 'OnStateChange' method: Object reference not set to an instance of an object"

                    am I missing anything or doing something wrong?
                    I am trying to organise the files under different folders within template and under separate intruments full name.

                    Please correct me, if I am wrong.
                    thanks
                    Last edited by asmmbillah; 09-09-2019, 02:28 PM.

                    Comment


                      #25
                      Hello asmmbillah,

                      What object is the object that is null?

                      (Use prints to check for null)
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #26

                        I think file does not exists. But I am handling the error with in OnBarUpdate()

                        if (!File.Exists(path))
                        {
                        // If file does not exist, let the user know
                        Print("File does not exist.");
                        return;
                        }

                        Do I need to create file first, What is the syntex for that please?

                        Most wired part is, it is only working when I am using

                        path = NinjaTrader.Core.Globals.UserDataDir +"myTestfile.txt";

                        I could not think of any reason why? Any advise from you?

                        thanks
                        Last edited by asmmbillah; 09-09-2019, 03:14 PM.

                        Comment


                          #27
                          Hello asmmbillah,

                          The file doesn't exist until its written. Have you run the script to write the file?

                          StreamWriter shows how to write a file.
                          StreamReader shows how to read a file that exists because its been written.
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #28
                            thanks for your reply. well, the indicator does not show up in indicator list, so there is no opportunity to even write file. it shows the error. what i could find unless i use: path = NinjaTrader.Core.Globals.UserDataDir +"myTestfile.txt"; it does not work. any idea why that might be the case?

                            Comment


                              #29
                              Hello asmmbillah,

                              You'll have to fix the error in OnStateChange() before the indicator will show in the list.

                              What is the null object causing the error?

                              Also, the string for the path is probably not what is causing the error. What do you mean when you say this is not working? What happens when you print that string?
                              Chelsea B.NinjaTrader Customer Service

                              Comment


                                #30
                                I found the problem. I had to create directory first for that extra folders. thanks again.

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by Fitspressorest, Today, 01:38 PM
                                0 responses
                                2 views
                                0 likes
                                Last Post Fitspressorest  
                                Started by Jonker, Today, 01:19 PM
                                0 responses
                                2 views
                                0 likes
                                Last Post Jonker
                                by Jonker
                                 
                                Started by futtrader, Today, 01:16 PM
                                0 responses
                                6 views
                                0 likes
                                Last Post futtrader  
                                Started by Segwin, 05-07-2018, 02:15 PM
                                14 responses
                                1,791 views
                                0 likes
                                Last Post aligator  
                                Started by Jimmyk, 01-26-2018, 05:19 AM
                                6 responses
                                844 views
                                0 likes
                                Last Post emuns
                                by emuns
                                 
                                Working...
                                X