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

Unhandled exception error

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

    Unhandled exception error

    I am trying to convert my Ver 7 strategy to ver 8.

    What I have done so far compiles but when run gets the attached error message:

    This novice could use some help. I have attached my code as well.
    Attached Files

    #2
    Hello Pete77,

    Is the script giving this error as-is, or do we need to uncomment the block of code where the position is long?

    I've run this script as-is and it has placed historical trades without any error.

    The uncomment code seems to be NT7 code that has not yet been changed to NT8 code.


    As a heads up, you have a series of nested if statements that when all true will trigger a print and no other action. The limit order is independent of those if statements and will be triggered on every bar. (This is because the print is after the if statement and not within the action block of curly braces)

    Below I am providing a link to a post with helpful information about getting started creating NinjaScript Strategies and Addons.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the help. I have cleaned up the code but what seems to be quite simple is not working. I have attached the code again and the two error messages, The Unhandled exception message crashes the Ninja program when I enable the strategy. Can you see what is going on? Thanks
      Attached Files
      Last edited by Pete77; 10-18-2017, 11:33 AM.

      Comment


        #4
        Hello Pete77,

        There does not appear to be any changes to the logic in the recent script you have posted. This appears to be exactly the same code as your earlier post.

        Are you running this code as-is or are you changing the script before posting it?

        If you are running the script as-is, I am not showing any objects that would be null with this logic.

        However, the script does call indexes of previous bars without any check that the bar exists.

        if(Close[1] < Open[1] && Close[0] > Open[0])

        If you call Close[1] on the bar 0, this will cause an error. If CurrentBar is 0, then there is no bar 1 bar ago. Calling the previous bar doesn't exist and this will cause an error.

        Adding a check that there is at least 1 historical bar would prevent the indexing error.
        if (CurrentBar < 1)
        return;

        Any code after this line would not be evaluated unless there are at least 2 bars on the chart.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          This code snippet works fine for me on Ninja 7:

          if (CurrentBar < 1)
          return;
          if (Position.MarketPosition == MarketPosition.Flat
          &&Close[1] < Open[1] && Close[0] > Open[0]
          &&CrossAbove(Close, High[1], 2))

          But I am still having trouble getting it to work on Ninja 8.

          For example using the strategy builder when I try to get Close[1] < Open[1] ( to indicate a red bar 1 bar back), I get "Default input[1] < Open[1]" as result.

          What am I missing here?

          Thanks

          Comment


            #6
            Hello Pete77,

            What is the actual code that was generated?

            (Click view code)

            By default the Input is the Close series.

            So Input[0] would be the same as Close[0].

            Are you getting an error with a strategy you have created with the Strategy Builder?

            If so, what is the error?

            What version of NinjaTrader 8 are you using? (Help -> About)


            Are you no longer using the script that you were asking about earlier?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Good to know that Input is the close series.
              Following is code made by Strategy buider:

              protected override void OnBarUpdate()
              {
              if (CurrentBars[0] < 2)
              return;

              // Set 1
              if ((Position.MarketPosition == MarketPosition.Flat)
              && (Close[1] < Open[1])
              && (Close[0] > Open[0])
              && (CrossAbove(Close, High, 2)))
              {
              Print(High[1].ToString());
              }

              }

              This time there is no error message but to test it does not print High[1] value when green bar crosses above red bar high.

              The condition CrossAbove should read &&(CrossAbove(Close, High[1], 2))) but I can't seem to get this result with strategy builder and don't like to unlock code yet. What would you suggest?

              I am using version 8.0.9.0 64-bit (Standard)

              Comment


                #8
                Hello Pete77,

                You are requiring that the close the less than the open on the previous bar and then the close be greater than the open on the current bar, and the close be less than the high on the previous bar and the close be greater than the high on the current bar.

                This is a lot of conditions.

                If you want to know why this is not triggering as true, use prints to debug and understand behavior.

                Prints are in the actions section of the builder.

                Below is a link to a post with a video that demonstrates using prints.


                Add prints and view the output in the output window. Post the output and we can help analyze what is not evaluating as true in the condition.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  No I am not creating all those conditions. To explain, if the current bar is green and the previous bar is red, and price crosses above the high of the previous (red) then (for a test) print the value of the red bar high. It does not print this value so the three conditions are not met. I suspect the CrossAbove method is not done correctly but can't seem to get it right with the strategy builder. I see by the link you sent there is way to make a copy of the strategy builder that can be unlocked. I will try to do that.

                  Comment


                    #10
                    Hello Pete77,

                    In my post #8 I am detailing to you what the code you have posted says.

                    Code:
                    if ((Position.MarketPosition == MarketPosition.Flat)
                    && (Close[1] < Open[1])
                    && (Close[0] > Open[0])
                    && (CrossAbove(Close, High, 2)))
                    {
                    Print(High[1].ToString());
                    }
                    You are requiring that the close the less than the open on the previous bar and then the close be greater than the open on the current bar, and the close be less than the high on the previous bar and the close be greater than the high on the current bar.
                    When you mention:
                    Originally posted by Pete77 View Post
                    No I am not creating all those conditions.
                    If you are not using this code, what is the code you are actually using?


                    Whatever code you are using, add prints for every variable in the conditions. This will let you know why the condition is true or false.

                    You do not have to unlock the script.

                    Add prints using the Strategy Builder.

                    The link I have provided demonstrates with a video using the Strategy Builder to add prints.

                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Sorry, that is the code that the strategy builder generates but not my intention. My intention is the condition that Close crosses above the high of one bar back (the red bar) but so far I have not been able to generate this condition with the strategy builder.

                      If I type in High[1] for value in CrossAbove in an unlocked copy of the strategy it compiles but stops the Ninja 8 program from working when enabled. Such as: CrossAbove(Close, High[1], 2). This did work fine in Ninja 7. I will have to find some other way to create this condition.
                      Last edited by Pete77; 10-24-2017, 02:48 PM.

                      Comment


                        #12
                        Hello Pete77,

                        I'm happy to demonstrate how to create the code of your desired logic in the strategy builder.
                        However, I am not clear on a detail:
                        Originally posted by Pete77 View Post
                        Close crosses above the high of one bar back (the red bar)
                        Are you wanting a condition that checks when the close price of the current bar has crossed above the price of the high one bar ago?
                        Code:
                        if (Close[1] < High[1] && Close[0] > High[1])
                        Are you wanting to check when price of the close has crossed above the high but offset one bar ago?
                        Code:
                        if (Close[2] < High[2] && Close[1] > High[1])
                        Are you wanting to only offset the high?
                        Code:
                        if (Close[1] < High[2] && Close[0] > High[1])

                        With the condition you have provided in post #11:
                        Originally posted by Pete77 View Post
                        CrossAbove(Close, High[1], 2)
                        This would trigger when the close crosses the specific double supplied, which is the high of the previous bar, and this can happen within two bars.
                        Psuedo-code:
                        'If the close of two bars ago is less than the high of the previous bar and the close of one bar ago is greater than the high of one bar ago or the close of one bar ago is less than the high of one bar ago and the close of the current bar is greater than the high of one bar ago.'

                        An explicit way of writing this is:
                        Code:
                        if ((Close[2] < High[1] && Close[1] > High[1]) || (Close[1] < High[1] && Close[0] > High[1]))
                        {
                        // execute code
                        }
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Thank you very much for the help. I have not explained myself very well.

                          Your first question: "Are you wanting a condition that checks when the close price of the current bar has crossed above the price of the high one bar ago?"

                          YES, provided that the current bar is green and 1 bar ago is red.

                          The code I have tried for (that works in Ninja 7 but not for me in Ninja 8):

                          if (Position.MarketPosition == MarketPosition.Flat
                          &&Close[1] < Open[1] //establishes 1 bar ago must be red
                          && Close[0] > Open[0] //and current bar must be green
                          &&CrossAbove(Close, High[1], 2) // high of current green bar closes above high of previous red bar.

                          This last condition I cannot make in the strategy builder and if I type it in an unlocked strategy version it compiles but it crashes the program when enabled. Any suggestion would be appreciated.

                          Comment


                            #14
                            Hello Pete77,

                            If you save the High of the previous bar to a variable you can reference that variable in the CrossAbove().


                            Also, I am not able to produce any crashing when using the code you have added by hand (given that a current bar check is added).


                            Is the test script I've attached also causing crashing on your end?
                            Attached Files
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              Yes, the test program crashed on enabling.

                              Something wrong here? My Ninja 8 program or my windows7?

                              I will make a variable for High[1] as you suggest.

                              Thanks very much.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by DJ888, 04-16-2024, 06:09 PM
                              6 responses
                              18 views
                              0 likes
                              Last Post DJ888
                              by DJ888
                               
                              Started by Jon17, Today, 04:33 PM
                              0 responses
                              1 view
                              0 likes
                              Last Post Jon17
                              by Jon17
                               
                              Started by Javierw.ok, Today, 04:12 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post Javierw.ok  
                              Started by timmbbo, Today, 08:59 AM
                              2 responses
                              10 views
                              0 likes
                              Last Post bltdavid  
                              Started by alifarahani, Today, 09:40 AM
                              6 responses
                              41 views
                              0 likes
                              Last Post alifarahani  
                              Working...
                              X