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

NT hangs when I run a strategy

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

    #16
    Thanks for the quick response

    I have attached a screenshot of the parameters I am using and have also attached my latest code. Please use this latest one to do your testing.

    I actually included a method called OnTermination() at the very end of my code to close the streamwriter but maybe I haven't used it in the right way. Please take a look and let me know what you think. Many thanks.
    Attached Files

    Comment


      #17
      Hello mbesha,

      Instead of trying to make your strategy produce trades to test this, I will provide a test script that shows how to use the stream writer correctly.

      The issue you are running into is most likely that you are not closing the writer between writes. What this means is that the first time it writes (creates the file) it is not closed so the file is locked, being locked your script can not edit the file.

      So what I have put in the script is simple, it writes the current bar to a new line in the txt file using your path.

      What I do is close the writer after each write that way the next loop through it will have access.

      In OnTermination, I check if there are any open streams, if so close them to unlock the file. Next I dispose the stream writer before exiting.

      Please let me know if I may be of additional assistance.
      Attached Files
      JesseNinjaTrader Customer Service

      Comment


        #18
        Thanks a lot Jesse. It's so good working with you. I will try to modify the code of my strategy accordingly and will get back to you if there's any lingering problem. Highly appreciate it.

        Comment


          #19
          Question as to why code is executing as expected

          Hi Jesse. I have a quick question for you. I am writing my own custom strategy and I don't know why the code at the very beginning of my OnBarUpdate() method isn't being executed (lines 82 - 93). If you refer to my code which has been attached, the code I am talking about is called CheckBars(). Feel free to modify the time condition for the code to test it. I have tried all I can to make the CheckBars() work but it doesn't seem to even though the strategy is working fine. I have attached a screenshot of the data settings which I am using.

          I have also noticed that for lines 198 - 200, the values for the day and date that written using the streamwriter are for the previous day rather than the current day. Why is this so even though the code explicitly states Time[0] ?

          Please advise and thanks for the many times you've helped me out before.
          Attached Files

          Comment


            #20
            Hello mbesha,

            Thank you for the question.

            I looked the strategy over, CheckBars method is working, I'm using all the same settings as you have pictured.

            The only thing I have changed was the time in your check so that I could test it this morning

            Code:
            if(ToTime(Time[0]) >= 140000 && ToTime(Time[0]) < 150000)
            Changed to

            Code:
            if(ToTime(Time[0]) >= 060000 && ToTime(Time[0]) < 150000)
            This allowed me to run it now in the morning to test it.

            I placed a print statement at the top of the CheckBars method and ran it, then worked my way down to the end of the method with the print statement, running it each time I had moved it to check that there were no hold ups.

            The print worked all the way through, were you testing this outside your time window in the script? Otherwise I see the method is being used.

            Please let me know if I may be of additional assistance.
            JesseNinjaTrader Customer Service

            Comment


              #21
              Hi Jesse. Thanks for checking it out. It still doesn't seem to be working the way I expect it to even when I use within my time window.

              I have a couple of questions I'd like to ask. My intention is to have the for loop run until the first bar in the session is reached. To achieve this I tried using Bars.BarsSinceSession but I am not too sure if this also works with historical data. Does it work with historical data? I want the CheckBars() method to work even with historical data, for example, on Sunday, it should be able to use Friday's data to calculate in readiness for the market open on Monday. Is it possible to use Bars.FirstBarOfSession to achieve my goal? Or is there something like Bar[i] (i being a variable) that I can use to do the same?
              Please let me know.

              Comment


                #22
                Hello mbesha,

                For your question regarding Bars.BarsSinceSession.

                BarsSinceSession simply gets the amount of bars from the first bar after the last session break and subtracts that from your current bar to come out with the amount of bars since the session. This does work historically.

                I am not sure of exactly the results you are expecting but currently it seems that you have used this correct.
                You are setting your variable I to the amount of bars in between your current bar and the session start.
                Then reducing the count by 1 for every iteration until you have reached 0, each iteration is used as a bars ago value which is based on the bars count between the current bar and the session start.

                If the results are not correct I would advise to just take the for loop and create a test strategy or indicator to test just that loop. This would help so that you can print all data and not really worry about what else is going on in the script. you could also change your order logic that is in the loop to DrawDot or something to verify the conditions are happening when you think they should.


                Please let me know if I may be of additional assistance.
                JesseNinjaTrader Customer Service

                Comment


                  #23
                  How to check for local PC time

                  Hi Jesse. Thanks for your reply. I do agree with you that from the logic the code should be executing without a hitch but this is not the case in real time execution. I actually tried printing out each execution of the for loop as you suggested and that revealed that indeed there is a problem.

                  I have a question with regards to how to check the local PC time. I wrote a condition stating that

                  if(BarsInProgress == 1)
                  {
                  if((ToTime(Time[0]) >= 070000 && ToTime(Time[0]) <= 235959) || (ToTime(Time[0]) >= 000000 && ToTime(Time[0]) <= 061500))
                  TradeTime = true;
                  else
                  TradeTime = false;
                  }

                  For your reference, I have attached my most recent code. These are lines 81 to 87. I enabled this strategy at around 6:30 am local PC time and hence I was expecting the variable TradeTime to be set at false. However, it was set at true. On closer investigation, I noticed that the last bar in the previous trading session (the trading session had already ended) was around 5:58 am as you can see from the screenshot and this time falls into the TradeTime = true condition. It seems that (ToTime(Time[0]) doesn't actually use PC time but rather the time stamp of the last bar. Is this assumption true? If that is the case, how do I get local PC time? It is crucial for the proper operation of my strategy. Please let me know. Thanks.

                  PS: The settings for the data series the same as last time
                  Attached Files

                  Comment


                    #24
                    Hello mbesha,

                    You are correct the Time[0] will return the timestamp from the bar.

                    Time[0] is returning a DateTime object so it can be replaced with DateTime.Now to return the current PC time instead.

                    DateTime is an object of time which has methods that are built into it for different time manipulations. Getting the current time, or subtracting or adding time from a date and so on.

                    Here is a link for more information on DateTIme in C# http://msdn.microsoft.com/en-us/libr...v=vs.110).aspx

                    Please try and use the DateTime.Now and see if that corrects the results for your time condition.

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

                    Comment


                      #25
                      Thanks for your input. Could you please tell me how exactly to code for DateTime.Now instead of Time[0]? For example, would this code "Condition 2 <= ToTime(DateTime.Now) >= Condition 1" for the time and "DateTime.Now.DayOfWeek == DayOfWeek.Saturday" for the day work? Please let me know.

                      Comment


                        #26
                        Hello,

                        If you are trying to use DateTime.Now instead of Time[0] you would just need to replace Time[0] with DateTime.Now

                        Time[barsAgo] does exactly the same thing as DateTime.Now except that Time[barsAgo] you tell it how many bars ago to get the date from. Either way Time[0] and DateTime.Now return a DateTime object.

                        You can then use ToTime to convert the DateTime into a integer value for easy comparison.

                        Here is a example where I switched the two:

                        Code:
                        if((ToTime(DateTime.Now) >= 070000 && ToTime(DateTime.Now) <= 235959) || (ToTime(DateTime.Now) >= 000000 && ToTime(DateTime.Now) <= 061500))
                        Now this would work to compare your local DateTime to an integer value, this still would work in coordination to the OnBarUpdate so this would only be checked each bar or each tick depending on your CalculateOnBarClose setting.

                        Please let me know if I may be of additional assistance.
                        JesseNinjaTrader Customer Service

                        Comment


                          #27
                          Question about the way the chart is plotting

                          Hi Jesse. I have a question for you regarding the way the bars are being plotted on the chart. As you can see from the screenshot I have attached, the chart is for the M6E 09-14 series and this is today's data. I have also attached a screenshot of the chart properties for your reference. Since it is a line break chart, I do not expect to see bars with zero range being plotted but that is what I find on the chart. Could you please tell me why this is happening? Thanks as always for your help and assistance.
                          Attached Files

                          Comment


                            #28
                            Hello mbesha,
                            For this could you please write me at platformsupport @ ninjatrader.com so that I can look into what you are seeing a little more.

                            Please reference the link to this forum thread in the email so that It can get to me.

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

                            Comment


                              #29
                              Error message when trying to enable strategy

                              Hi Jesse

                              Thanks as always for your help. I have encountered a problem when trying to enable my strategy. I keep getting an error message saying "Object reference not set to an instance of an object" despite the fact that the strategy code compiled without any problems. I have tried checking my code over again and again but I still can't pinpoint where the problem is. Can you please help me figure this out? I have attached my strategy file and a screenshot of the data settings for the primary data. Thanks.
                              Attached Files

                              Comment


                                #30
                                Hello,

                                I wanted to ask have you defined any custom DataSeries in your script?

                                Generally the error you are getting is related to a variable that was defined but given a value so it is not set to anything essentially.

                                Being that this is happening on startup it is most likely that there is a data series that needs to be corrected or that there is a null value variable.

                                The most common is when you create a data series like this

                                Code:
                                DataSeries test;

                                but forget to add in your Initialize()

                                Code:
                                test = new DataSeries(this);

                                That is just a example as other types of objects could do this.

                                If you would like I can take a look at the code if you can not locate it.

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

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by benmarkal, Yesterday, 12:52 PM
                                3 responses
                                22 views
                                0 likes
                                Last Post NinjaTrader_Gaby  
                                Started by helpwanted, Today, 03:06 AM
                                1 response
                                18 views
                                0 likes
                                Last Post sarafuenonly123  
                                Started by Brevo, Today, 01:45 AM
                                0 responses
                                11 views
                                0 likes
                                Last Post Brevo
                                by Brevo
                                 
                                Started by aussugardefender, Today, 01:07 AM
                                0 responses
                                6 views
                                0 likes
                                Last Post aussugardefender  
                                Started by pvincent, 06-23-2022, 12:53 PM
                                14 responses
                                244 views
                                0 likes
                                Last Post Nyman
                                by Nyman
                                 
                                Working...
                                X