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

    NT hangs when I run a strategy

    Hi. I wrote my own custom strategy and compiled it with no errors. However, when I try to run it on my chart, the whole program hangs on me. What could be the cause of this and can you recommend a way of fixing this? Thanks in advance.

    #2
    Hello mbesha,

    Without seeing the code for this it is hard to say exactly what is happening.
    It sounds like there may be some sort of loop happening in the OnBarUpdate.

    One thing you may check is the output window to see if anything is noted after it is enabled.
    You can access this by going to the Tools menu and clicking Output window.

    If you would like I can take a look at your code if you would like to post it and see if I notice anything that sticks out.

    If you do not want to post your code publicly please send your question into support [at] ninjatrader.com

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

    Comment


      #3
      I have attached the strategy file for your reference and I have checked the loops in the OnBarUpdate() method and they all seem to have exit conditions. I can not read anything on the output window because as I mentioned earlier, the whole program freezes. The chart I am using the strategy on has Tick Value 1 Line Break 15 data series and the account is the Sim101 with a cash value of $1000 and a margin requirement of $100. Please check the code for me and see if you can find what is the root cause of the problem. Thanks.
      Attached Files

      Comment


        #4
        Hello mbesha,

        Thank you for supplying the code that helps in looking into this further.

        From what I can see the first sign of what would cause this would be the "while" loops that are in OnBarUpdate.

        While loops should not be used in OnBarUpdate because while loops will loop until broken so I believe this is starting a loop on the first bar then getting no further because the loop is still running.

        I would recommend changing these to if statements in place of while statements for the OnBarUpdate section specifically.

        After commenting out the while statements I was able to get the strategy to load and NinjaTrader was responsive.

        for the while(TradingDays) because this is a bool this could easily be converted to an if statement, if(TradingDays)

        Because OnBarUpdate is already looping through the data set you would not need to create an additional loop for a boolean condition, you would only need to check if it is true or false while OnBarUpdate is looping.

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

        Comment


          #5
          Thanks for the useful tip. True to your word, when I changed the while loops into if statements, the strategy could get enabled without a hitch. Highly appreciate your kind and quick assistance!!

          Comment


            #6
            New problem with entry orders

            Hi Jesse. I realized that there is a problem with the execution of my entry orders. As you can see from the following code snippet which I have attached for reference, when a variable called numoc is more than one, two entry orders are executed. This is executed when my position is flat (check for the method called WhenFlat() ).

            .....

            else if (numoc > 1 && numoc % 2 == 0)
            {
            if(BOP(1)[0] > 0.5)
            {
            myEntryOrderL = EnterLong(numoc/2,"BL"); //Buy, Low Profit target
            myEntryOrderH = EnterLong(numoc/2,"BH"); //Buy, High Profit target
            }
            else if(BOP(1)[0] < -0.5)
            {
            myEntryOrderL = EnterShort(numoc/2,"SL"); //Sell, Low Profit target
            myEntryOrderH = EnterShort(numoc/2,"SH"); //Sell, High Profit target
            }
            }

            else if (numoc > 1 && numoc % 2 != 0)
            {
            if(BOP(1)[0] > 0.5)
            {
            myEntryOrderL = EnterLong((numoc+1)/2,"BL"); //Buy, Low Profit target
            myEntryOrderH = EnterLong((numoc-1)/2,"BH"); //Buy, High Profit target
            }
            else if(BOP(1)[0] < -0.5)
            {
            myEntryOrderL = EnterShort((numoc+1)/2,"SL"); //Sell, Low Profit target
            myEntryOrderH = EnterShort((numoc-1)/2,"SH"); //Sell, High Profit target
            }
            }

            .....

            For some reason, only myEntryOrderL gets filled but myEntryOrderH doesn't fill when the entry condition is met even though both are in the same if statement. Then after only myEntryOrderL gets filled, the OnBarUpdate method issues an error message when it moves on to WhenLong() or WhenShort(). Could you please tell me why this is so? I have checked the code and the logic seems okay. I have attached my whole strategy for your reference.
            Attached Files

            Comment


              #7
              Hello mbesha,

              I took a look at the updated strategy. Upon initially loading it I only see a single entry for each direction being placed.

              I wanted to check with you have you enabled more than one entry per direction?

              Because you submitting two orders both in the same direction, by default the strategies are set to allow 1 order per direction so only the first entry would go through but the second being the same direction would not be submitted.

              In the strategies properties when you add it to a chart you can specify a quantity for entries per direction.
              Can you check what yours is set to? I have attached an image of where the Entries per direction option is located in your strategies properties.

              Once I enabled the 2 entries per direction I see the additional order being placed in the condition.

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

              Comment


                #8
                Entry orders executing well now

                Thanks a lot for that tip. The entry orders are now executing as expected.

                Comment


                  #9
                  Problem with backtesting

                  Hi Jesse. Thanks for all your help up until now. I have encountered one new problem. I try backtesting my strategy but it doesn't seem to execute trades at all. Is there something I need to modify in the code for the strategy to work in the Strategy Analyzer for backtesting? Please advise. I have attached my latest code for your reference. Thanks in advance.
                  Attached Files

                  Comment


                    #10
                    Wrong file

                    Please ignore the file I attached in my previous message and use this one instead. Thanks.
                    Attached Files

                    Comment


                      #11
                      Hello mbesha,

                      Thank you for the reply,

                      I would be happy to take a look into this today to see if I can locate a reason for this.

                      Could you please tell me what settings you are using when creating the chart and also what you are using for the backtest so that I can try and recreate this on my end?

                      If you could take a screenshot of the dataseries window for the chart showing how your chart is set up and also one of the backtest settings window I would appreciate that.

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

                      Comment


                        #12
                        Here are the settings

                        Thanks, I have attached a screenshot of the settings you asked for.
                        Attached Files

                        Comment


                          #13
                          Hello mbesha,

                          Thank you for the screenshots,

                          I loaded the strategy today but was unable to get it to perform in a chart or backtest using the settings you provided. I tried some other variations to the same outcome.

                          I did notice in the output where you have "Right day to trade?" this never becomes true using the above settings, so that may possibly be something to look into.

                          I also took a look through the code to see if I could see anything specific preventing this from running but with the complexity of the code as it stands I don't believe I would be able to narrow it down or test this in a reasonable amount of time.

                          For this item what I might recommend because you know the code better than I, would be to eliminate any unnecessary portions of the code and to leave the logic in place to simply test the logic and order handling.

                          With just that portion of code remaining it should be quite a bit easier to see what exactly is happening using Print statements.
                          I am sorry I don't have a better answer for you but I do not see anything that sticks out to me other than the print statement you have already set.

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

                          Comment


                            #14
                            Question about how to implement the streamwriter in the strategy

                            As always, many thanks for your ever kind support and useful assistance. I have stumbled on a new problem and I was wondering if you could help me out. I am trying to have the P/L written in a text file using the StreamWriter but it don't seem to be working. Is there something wrong with my code or logic for the StreamWriter? I have attached my latest code for reference. Thanks in advance.
                            Attached Files

                            Comment


                              #15
                              Hello mbesha,

                              Thanks for the question,

                              I have taken a look at your code and I see where you are using the streamwriter. One thing I noticed right away is that you are not closing the streamwriter and opening a new one for subsequent writes, this may be where you are running into a problem.

                              I would like to test the script but I am unable to get it to place a trade, can you please provide me with all the parameters you are using along with the instrument you are using this on so that I can test this?

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

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by trader252, Today, 06:04 PM
                              0 responses
                              5 views
                              0 likes
                              Last Post trader252  
                              Started by tradesmart27, Today, 05:29 PM
                              0 responses
                              5 views
                              0 likes
                              Last Post tradesmart27  
                              Started by nothingtrader, Today, 05:10 PM
                              0 responses
                              9 views
                              0 likes
                              Last Post nothingtrader  
                              Started by SuperDude123, Today, 02:54 PM
                              1 response
                              7 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by mohawkTrader, Today, 02:12 PM
                              2 responses
                              20 views
                              0 likes
                              Last Post mohawkTrader  
                              Working...
                              X