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

What is wrong in my code using a method to find supports in multi-timeframe strategy?

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

    What is wrong in my code using a method to find supports in multi-timeframe strategy?

    I am trying to write a NinjaScript strategy with multi timeframes, using supports and resistances from all time frames.
    I have written a method to calculate new supports ( similars to swing(5) indicator) in all time frames, and adding them into a collections ( List<>)
    of new supports.
    But appears errors, and I don't understand what is wrong in the code.
    What is wrong in my code?
    Thanks.
    Attached Files

    #2
    Hello Parmenides48,

    Thank you for the post.

    I looked at the attached file but the code is not formed correctly. Did you have a point where the script would compile and you had seen errors, or are you referring to the errors you see in the script now due to the syntax/structure of what was coded?

    I can tell that you are currently missing a curly brace for OnBarUpdate which makes the structure of the file invalid. You also have some other grammatical errors such as using capitalization on an if statement.

    Code:
    If(CurrentsBar[1] < 11 || CurrentsBar[0] < 11)
    should instead be lower case if:

    Code:
    if(....


    There are also private variables which seem to be placed in a method which is not valid. There are quite a few problems surrounding the syntax used so I would be unsure of a specific point in where you should start here. Very likely I would suggest that you start from a fresh empty indicator and slowly add syntax, making sure it compiles while adding it.

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

    Comment


      #3
      Thanks a lot for your fast answer.
      I am going to follow your indications and try to solve my problems-errors related to syntax and structure.

      Comment


        #4
        Hello again,
        I worked in the code, and finally have just only one error, but I don't know how to solve it.
        I send the new code, solving errors, but finally I have one at the end, related to the method that I describe at the end.
        How can I solve this error?
        Thanks.
        Attached Files

        Comment


          #5
          Hello Parmenides48,

          Thank you for the reply.

          It looks like there are still a few items in this script that need to be solved but the structure of the file is now corrected which is a good portion of the battle.

          The first error I see is with the method you created:

          Code:
           
           public void CalculoS (Iseries<double> min, SR sraux)
          Iseries is not a type, this is just not capitalized correctly. ISeries is a type, so this needs to change to ISeries.
          Code:
          public void CalculoS ([B]ISeries[/B]<double> min, SR sraux)
          The next item on line 84 is your Print, Prints don't take two parameters so you need to format the string you supplied before giving it to the print. It looks like you are just adding a blank space at the end:

          Code:
          Print( Soportes[i],"  ");
          should instead be

          Code:
          Print( Soportes[i] + "  ");

          Finally on line 105 you have an if condition that is missing an equals sign, you have one but two would be needed to make a comparison:

          Code:
          if(IsS=true)
          should be

          Code:
          if(IsS == true)
          After that I see that the script compiles. I did not test the script further from this point.

          I look forward to being of further assistance.

          JesseNinjaTrader Customer Service

          Comment


            #6
            Hello,
            I solved the errors you have told me, I compiled correctly, but,
            when I open an NinjaScript Output window and insert that code as strategy in a 5 minutes chart of CL future, what appears in the Output windows is the attached image.
            How can I do to print all the calculated supports in the Output window??
            Attached Files

            Comment


              #7
              Hello Parmenides48,

              This depends, what information are you trying to print here? You are already looping and printing the SR object you made a list of. If you want to print the values of the SR object, you need to specify one of the properties and print that.

              Here is the loop you created:

              Code:
              for(int i=0 ; i<Soportes.Count ;i++)
              
                              Print( Soportes[i] + "  ");
              Soportes[i] is the SR object you made which has precio and IsSoR in it. Are you trying to print one of these values? If so, that would just be:
              Code:
              Soportes[i].IsSoR
              or
              Code:
              Soportes[i].precio
              You can also create a ToString override in your class if you wanted the object ot print in a specific way:

              Code:
              public class SR   //I create this class to hold information for supports or resistances
              {
                  public double precio;
                  public string IsSoR;    
              
                  public override string ToString()
                  {
                      return precio + " " + IsSoR;
                  }
              }
              I look forward to being of further assistance.
              JesseNinjaTrader Customer Service

              Comment


                #8
                I see my error.
                I wanted to print only the price of the object sr.
                So I code Soportes[i].precio.
                But I changed the Print sentence. On every OnBarUpdate() I will print only the last value of the last support found.
                I think that with the last code , there was a mistake.
                On every OnBarUpdate() call, it will print "all" the supports, repeating all them and addding the last new support.
                So, all the supports before the last new one will be written again...
                My new Print sentence is:
                Print( Soportes[0].precio + " "); // so I will print only the last support.
                I added to the code orders (profit, stop loss, and enterLong with a simple condition)

                But, with this new code of strategy (attached file), there is a message of error in the Output window.
                Attached Files

                Comment


                  #9
                  What is wrong with calling OnBarUpdate() from bar 11?
                  I understand that I need 11 historical bars to begin calculations for supports.
                  Then, it is logic to wait to CurrentBar=11?
                  it is correct to code:
                  if (CurrentBars[0] < 11) // calculations for supports begins from having at least 11 bars processed
                  return; ????

                  Comment


                    #10
                    Hello Parmenides48,

                    In this case, you are likely checking an index before there is a valid value in the list creating an indexing error. This just happens to happen on bar 11 due to the specifics surrounding this script.

                    You will very likely need to now isolate which line is specifically throwing the error and then handle that directly.

                    One item is you are using a direct index in the list now instead of a loop, this is one area to explore:
                    Code:
                     
                      Print(Soportes[B][0][/B].precio + "  ");
                    A simple check to make sure the list is valid would be:
                    Code:
                      
                     if (Soportes.Count > 0) {     Print(Soportes[0].precio + "  "); }
                    I see that you are also using this in the condition above this print which would also need to be within the check that there is a value in the list. The loop you had previously would not produce this error because it would not loop if there were no indexes to loop over.

                    I look forward to being of further assistance.

                    ​​​​​​​
                    JesseNinjaTrader Customer Service

                    Comment


                      #11
                      Hello,
                      I put in the code :
                      if ( Soportes.Count>0)
                      Print(Soportes[0].precio + " ") ;

                      And now the message is( see attached image)
                      I don't understand this message.
                      If I wait to the eleven historical bar ( beginning from the left, the older bar in the chart), There aren't enough bars (11) to begin calculations for supports?

                      Attached Files

                      Comment


                        #12
                        Hello Parmenides48,

                        This is the same error as before, you haven't fully handled the problem based on the error.

                        Have you surrounded all use cases with the count check? I noted in the last email you have a few locations where you use the direct index, any place you used it you would need to make sure that there is a value in the list before you try to access it.

                        To understand this better, I would suggest that you comment out your code and find what specific line is causing the error. Comments are two slashes at the start of the line:

                        Code:
                        // if ( Soportes.Count>0) 
                        // Print(Soportes[0].precio + " ") ;
                        I look forward to being of further assistance.
                        JesseNinjaTrader Customer Service

                        Comment


                          #13
                          Yeeesssss!!!!
                          Coding if ( Soportes.Count>0) to check the condition for enterLong, all cases are checked with .Count>0, and finally the output windows show me the prices of the last supports.
                          And trades of this strategy now appear correctly. With the last error, there were no trades with that code!.
                          Thanks a lot .
                          Now I will continue step by step developing mor and more aspects of the full strategy that I want to develop.
                          Attached Files

                          Comment


                            #14
                            Hello,
                            there is a problem. When I try to write in the Output window the three last supports,
                            I see that Soportes[0].precio is the same value as Soportes[1].precio and Soportes[2].precio !!!
                            If I use Soportes.Insert(0,sr), I understand that I put this last support (sr) in first position in the Collection Soportes, and all the other supports in this List increase their index in +1.
                            So, the new support sr have now the cero index, and the last support that had index cero have now index one....
                            Is it correct???
                            Why it doesn´t happen???
                            How do I do to put the new support at cero index and move all other supports increasing their indexes in +1 position???
                            See attached image for output window and the strategy.
                            Attached Files

                            Comment


                              #15
                              Hello Parmenides48,

                              Thank you for the post.

                              Code:
                              [I]I see that Soportes[0].precio is the same value as Soportes[1].precio and Soportes[2].precio !!![/I]
                              Well, I see that you only use one object so that is part of the problem. You would also need to create new SR objects if you expect to append new items into the list. You are appending the same object over and over, so this is a list of the same thing. I am not entirely sure what the overall goal with your method is, but you would likely benefit from simplifying your code to not include the extra method and variables for the time being.

                              For example, you have created:

                              Code:
                              public SR sr= new SR();
                              In your code, you always use only this object and reset the values of this specific object. You should only see the value of this object being printed multiple times based on the code so far. You would need to call new SR() each time you add a row to the list and create a totally new object if you want individual values.

                              I would likely suggest that you remove your sr variable and also the CalculoS method. You could then just use OnBarUpdate and make sure you get the appending to the list logic correct before adding more complication to the script.

                              You can read more about how Lists work in C# and Object-oriented programming by using external C# educational materials. This would not necessarily be something our support would go into but you can find many tutorials using google.

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

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by alifarahani, Today, 09:40 AM
                              4 responses
                              20 views
                              0 likes
                              Last Post alifarahani  
                              Started by gentlebenthebear, Today, 01:30 AM
                              3 responses
                              16 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by PhillT, Today, 02:16 PM
                              2 responses
                              7 views
                              0 likes
                              Last Post PhillT
                              by PhillT
                               
                              Started by Kaledus, Today, 01:29 PM
                              3 responses
                              11 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by frankthearm, Yesterday, 09:08 AM
                              14 responses
                              47 views
                              0 likes
                              Last Post NinjaTrader_Clayton  
                              Working...
                              X