Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to Dispose?

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

    How to Dispose?

    Hi,
    I read a text file with streamreader, but I do it in a function.
    How can I dispose the streamriter? If I try in OnTerminate I get an error that it doesn't exist.

    Thanks,
    Baruch

    #2
    Hi Baruch, it sounds like you would need to check into the scope your StreamReader / Writer objects.
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Hi Bertrand,
      Now the streamwriter is declared inside the function, if needed I can declare it with the variables.
      The problem I have is that after one backtest, the next backtest doesn't start immediately. It takes few minutes before the start. If I exit NT and reenter the first time is immediate again.
      I believe that the problem is that I don't dispose of the streamwriter. So my question is what and how to do it.
      Baruch

      Comment


        #4
        Hi Baruch, yes I would suggest giving it global scope and then disposing in OnTermination() as seen here in the NT7 sample:

        BertrandNinjaTrader Customer Service

        Comment


          #5
          Hi Bertrand,
          I saw that example. I have more complicated situation. I read several files and I do it from external cs file (functions file, like UserDifinedMethods). How do I do it in there? Do I send the StreamReader to a function by reference? Do I need separate StreamReader for each file? Can I declare it public? Will anything help?

          Baruch

          Comment


            #6
            Hi Baruch, unfortunately we could not support this more general C# scope with multiple files being accessed - you would likely need to work with lock concepts to avoid running into file access conflicts with your code - http://msdn.microsoft.com/en-us/libr...(v=vs.71).aspx
            BertrandNinjaTrader Customer Service

            Comment


              #7
              Originally posted by Baruch View Post
              Hi Bertrand,
              Now the streamwriter is declared inside the function, if needed I can declare it with the variables.
              The problem I have is that after one backtest, the next backtest doesn't start immediately. It takes few minutes before the start. If I exit NT and reenter the first time is immediate again.
              I believe that the problem is that I don't dispose of the streamwriter. So my question is what and how to do it.
              Baruch
              Reading what you write, it seems to me that you could just dispose of it within the function before the function returns. After all. it seems that you have read/written all the data to file before you return anyway.

              Comment


                #8
                Hi Baruch, unfortunately we could not support this more general C# scope with multiple files being accessed - you would likely need to work with lock concepts to avoid running into file access conflicts with your code - http://msdn.microsoft.com/en-us/libr...(v=vs.71).aspx
                Bertrand thanks for the link, but I don't have a problem with locking. My problem is that after first backtest run, next runs don't start immediately.
                Reading what you write, it seems to me that you could just dispose of it within the function before the function returns. After all. it seems that you have read/written all the data to file before you return anyway.
                Thanks Koganam, I can't exactly do it. I think is time for me to explain what exactly I'm after with the files.
                I read the Instruments file 3 times on start of my strategies. I take from it :
                1. Default session times and Time Zone.
                2. Next RollOver.
                I can do it with 2 runs, but not in 1, because first I find the default session for an instrument and then go to previous point in the file to retrieve the times.
                May be you or NT staff can spare me this procedure and tell me how I can get this data without reading Instruments.txt. After all Ninja does it already, why should I do it for my own.

                Second file I read just once is a personal data file. For now it only has email address, but in the future who knows. Any way if I want to send emails from strategies how can I know the address? Although NT does it somehow.
                Streamreader for those file I can dispose immediately.
                Two more files I read once a day, so the streamreader for them has to stay open until OnTerminate.

                Bertrand this is for you:
                Apparently my problem is not in not disposing streamreader.
                I tried to narrow it down, and came to a strange findings. I don't understand it and thats why I read that I need to dispose.
                What is happening is that I use 3 orders: EntryOrder, StopOrder and ProfitOrder.
                After I enter in a position I issue Stop and Profit orders in OnExecution.
                If I remark a stop order (two lines: 1. for longs 2. for shorts) it runs like it should - Starts immediately each time. The same is true if instead I remark the two Profit orders and leave the stop orders.
                So what kind of black magic is there in Ninja?

                Baruch

                Comment


                  #9
                  Hi Baruch, thanks for the clarification - have you looked into this method here?



                  This would leave you then with only one read for the rollover I believe, as there would be unfortunately no supported techniques for reading it as set.
                  BertrandNinjaTrader Customer Service

                  Comment


                    #10
                    Thanks Bertrand,
                    This gives me only TimeZone. Because I don't want the current session's name, but the instrument DEFAULT session name.
                    I'm a day trader, as most people here, so what I need is to know what time is overnight. So if its "6E" and even if I use "Default 24/7" template I want to know the default instrument template to know that the end of day (for margin purposes) is 16:00 CT. The same for "ES" which is 15:15 CT.
                    But the biggest problem is the delay in backtesting. What is the answer to it?

                    Baruch

                    Comment


                      #11
                      Originally posted by Baruch View Post
                      ...
                      What is happening is that I use 3 orders: EntryOrder, StopOrder and ProfitOrder.
                      After I enter in a position I issue Stop and Profit orders in OnExecution.
                      If I remark a stop order (two lines: 1. for longs 2. for shorts) it runs like it should - Starts immediately each time. The same is true if instead I remark the two Profit orders and leave the stop orders.
                      So what kind of black magic is there in Ninja?

                      Baruch
                      Are those Stop and Profit orders properly tied as OCO? You might be leaving open orders if not. I guess you could always try unconditionally closing all open orders in OnTerminate(), or on any Execution that takes you from "in market" to flat?

                      Comment


                        #12
                        Are those Stop and Profit orders properly tied as OCO?
                        Thanks Koganam for trying to help me.
                        I use managed approach and what I know is that you cannot tie the orders.
                        Also I use a NT example of how to do it and nullify the orders as in the example.

                        Baruch

                        Comment


                          #13
                          I found the problem.
                          When I enter a trade I draw a dot for stoploss and a dot for profit on each bar until the position is closed. In backtest its a lot of dots. Apparently Ninja has difficulty disposing all those draw objects.
                          So how the F... do I dispose them???

                          Baruch

                          Comment


                            #14
                            p.s.
                            I have this code (drawdot) a very long time. From NT6.5 through all NT7 releases and never had a problem until now. I think its a new "Feature".

                            Comment


                              #15
                              Attached is a strategy which demonstrates the problem,
                              Run it in backtest on any instrument, 1 Min TF and at least 6 month.
                              On a first run it starts immediately. After it finishes, click run again. On a first glance it will look as if you didn't click "Run", nothing happens, but if you click now inside the main window you will see that its in a wait mode (Spinning wheel) and only after several minutes the window changes to " Backtest run...".

                              Baruch,
                              p.s. Its my best strategy, hope its not too complicated. LOL.
                              Attached Files

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by f.saeidi, Today, 05:56 AM
                              1 response
                              2 views
                              0 likes
                              Last Post Jltarrau  
                              Started by Jltarrau, Today, 05:57 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post Jltarrau  
                              Started by Stanfillirenfro, Yesterday, 09:19 AM
                              7 responses
                              51 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by TraderCro, 04-12-2024, 11:36 AM
                              4 responses
                              70 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Mindset, Yesterday, 02:04 AM
                              1 response
                              15 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Working...
                              X