• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Where are Position methods?

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

    #16
    Hello RandyT,

    Thank you for the post.

    This is a start but is still not enough information for me to complete a test.

    What was the name of the sub folder, and what was the specific name of the strategy you created?

    What syntax went in the file after you had created it, could you copy and paste the strategies code as you have it now or upload the file for review?

    Was the problem immediate when using the backtest, or were there specific settings required to see the problem? Does the crash happen when loading the strategy or sometime during the backtest?

    Once I have the full details surrounding your test, I will test the situation to see if I have the same outcome. That will give me enough information to know why that is happening. Without the full details surrounding what you are doing, I cannot complete a test or provide relevant help.

    Alternatively, you could remove the changes you have made and use the steps I had originally provided without modification or creating subfolders. I can see that using a partial class from the strategies folder as outlined works on my end and I see that it works in the analyzer.

    For properties you do not want to see in the user interface, you would need to use the Browsable attribute to hide them:

    Code:
    [Browsable(false)]
    public bool SessionPre {get;set;}

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

    Comment


      #17
      I've gone through the steps to recreate this and in particular, did not remove the folder name "Sessions" from the generated class declaration. This seems to have solved the problem.

      Regarding the need to hide these in every strategy now with the [Browseable(false)] attribute... Does C# provide a way for me to #include a file with these properties defined?

      In order to hide these, I am having to provide nearly as much code in every strategy as some of these basic helper methods require. Kind of defeats the purpose of centralizing these shared methods. (unless there is some way to do this in the shared code rather than in properties of every strategy)

      Code:
      #region Using declarations
      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.ComponentModel.DataAnnotations;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using System.Windows;
      using System.Windows.Input;
      using System.Windows.Media;
      using System.Xml.Serialization;
      using NinjaTrader.Cbi;
      using NinjaTrader.Gui;
      using NinjaTrader.Gui.Chart;
      using NinjaTrader.Gui.SuperDom;
      using NinjaTrader.Gui.Tools;
      using NinjaTrader.Data;
      using NinjaTrader.NinjaScript;
      using NinjaTrader.Core.FloatingPoint;
      using NinjaTrader.NinjaScript.Indicators;
      using NinjaTrader.NinjaScript.DrawingTools;
      #endregion
      
      //This namespace holds Strategies in this folder and is required. Do not change it. 
      namespace NinjaTrader.NinjaScript.Strategies.Shared
      {
      	public partial class Sessions : Strategy
      	{
      		public bool sessionPre {
      			get { return(ToTime(Time[0]) >= 43000 && ToTime(Time[0]) < 73000); }
      		}
      	}
      }

      Comment


        #18
        Actually, I wrote too soon...

        Even after placing the following "using" statement in the strategy, I still cannot seem to find these helper methods. (renamed the file containing the helper methods to 'mySessions' in case there was conflict with the native 'Sessions" object)

        Code:
        using NinjaTrader.NinjaScript.Strategies.Shared;

        Comment


          #19
          Hello RandyT,

          I believe we should take a step back to the sample I had originally provided again, the syntax you have provided is not the same. When testing what I have provided, you can copy and paste the syntax to prevent errors and so that we are testing the same code.

          You have created a strategy class named Sessions that is a strategy instead of a partial class named Strategy.

          This is what you provided:

          Code:
          public partial class Sessions : Strategy
          {
          This is what I had suggested:

          Code:
          public partial class Strategy // note the name is specifically Strategy and there is no inheritance here or :Strategy
          {
          As noted, I had not suggested using subfolders. For the sake of simplicity lets get one working sample before you move to more advanced uses and subfolders. Understanding the concept of a partial class would be more important in this case than the aesthetics of being in a subfolder.

          The entire partial class definition would look the same as we have been discussing, I have appended the Browsable tag to show where that can go:

          Code:
          namespace NinjaTrader.NinjaScript.Strategies
          {
          	public partial class Strategy
          	{
          		[Browsable(false)]
          		public bool SessionPre {
          			get { return(ToTime(Time[0]) >= 43000 && ToTime(Time[0]) < 73000); }
          		}
          	}
          }

          Then from any strategy, this is called as the following:

          Code:
          Print(SessionPre);
          If you deviate from this structure or these steps by using different names or class structure, you will likely see problems that you would need to figure out as you have not used the same syntax.

          If you did want to use a subfolder, you would need to first create an empty strategy in the subfolder, and then change the namespace to the correct namespace for the partial class which is NinjaTrader.NinjaScript.Strategies. After doing that, the partial class could be added into the strategy which is in the subfolder. In your other scripts that are in the subfolder, the sample from above would then work as expected. I would suggest just leaving the partial class in the main root folder for simplicity.

          I look forward to being of further assistance.
          Last edited by NinjaTrader_Jesse; 07-20-2018, 09:49 AM.
          JesseNinjaTrader Customer Service

          Comment


            #20
            That all works for me when placed in a file in the Strategies folder.

            When trying to move that file to a subfolder, I get the following error:

            Code:
            The name 'sessionPre' does not exist in the current context

            Comment


              #21
              Hello RandyT,

              Thank you for the reply.

              Correct, you would need to use the suggestion I had provided in the last post for creating the class in the folder if that is something you wish to do.

              You are unable to move it and would need to instead use the specific steps I had supplied. You would also need to comment out or remove the sample as you have it now before you create the file in the folder because the overall structure changes when you use the folder.

              I would still suggest using the main folder for the partial class so it matches the namespace where the file resides. Placing the class in the subfolder will make it want to use the subfolders namespace or NinjaTrader.NinjaScript.Strategies.MyFolderName which then breaks the partial class and the concept behind using a partial class. That means if you want that in the folder, you need to manually create the partial class in the folder and also make the namespace change so the partial class is still in the namespace NinjaTrader.NinjaScript.Strategies.

              If you want to specifically use subfolders without the partial class or namespace requirements, an alternative would be to make a normal class and create an instance of it. This is essentially what you were doing and what I had already commented on in post #5. That would also work but is more syntax and you have to create instances of the class.

              For further C# education on Classes, Partial Classes and general structure of C# files I would suggest reviewing further external C# educational information online. Class and Inheritance would be specifically helpful with what you are trying to do. This type of education is generally outside what our support would assist with as this falls on general C# syntax and structure and is not specific to NinjaScript.

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

              Comment


                #22
                Jesse,

                Sorry to be a bit obstinate, but I have done exactly as you describe to create the strategy in a subfolder and it does not work.

                I am successfully doing what I am attempting for Indicators and Addons. I am not able to do what you describe in Strategies. I'd ask that you take the time to get a working solution in a subfolder to confirm that there is not a bug that needs to be addressed.

                I've asked a somewhat related question here: https://ninjatrader.com/support/foru...872#post547872

                Not being able to maintain this development work in source code management. That requires files to live in their own hierarchy and not be mixed with every other indicator/strategy/addon/etc. that gets imported into Custom. I'm attempting to live within the Custom folder with my development work maintained in their own subfolders. There is a very valid use case for this.

                Comment


                  #23
                  Just to be clear, as I wrote in https://ninjatrader.com/support/foru...9&postcount=15

                  I've attempted to do what you describe by creating the subfolder through the Editor interface, create a new strategy through the Editor interface, and create the partial class as you described. Code compiles, but when trying to reference these methods within any strategy results in the other error I reported:

                  Code:
                  The name 'sessionPre' does not exist in the current context

                  Comment


                    #24
                    Hello RandyT,

                    Thank you for the reply.

                    Had you copied and pasted all of what I have provided? you are using lowercase sessionPre where my samples include Uppercase SessionPre which goes with the standard naming scheme in C# for public properties.

                    In my previous tests, which is what the sample syntax has come from, I see that placing a partial class formed like post # 19's sample in the Strategy folder works. The Strategies can see SessionPre. Placing a new file in a sub folder, and changing the namespace as noted also works. When you create the file in the subfolder, it will have the wrong namespace for the partial class which I have also noted. The partial class would look identical in the Strategies folder or in the subfolder.

                    The error you are seeing "The name 'sessionPre' does not exist in the current context" means that the property is not being found, which may relate to the namespace or names you have used. Without seeing the specific syntax you created on your end all I could really recommend is that you revisit the previous posts and ensure you completed the steps in the same way with the same names and structure.

                    Regarding your related question, I have also posted there with my thoughts. Assuming you are able to create the correct structure for your sub folder and classes you could use source control on that folder.


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

                    Comment


                      #25
                      Success.

                      So the key here was to use the exact same file in the subfolder as in the main Strategies folder. Which doesn't make sense to me on the whole Inheritance... topic, but appears to work.

                      Begs the question why creating a file in a subfolder through the editor adds subfolder name and file name to class declarations, unless this is just specific to 'partial's.

                      Thanks for sticking with me on this.

                      Comment


                        #26
                        Hello RandyT,

                        Correct, the naming would need to be exactly like I had tried to highlight in post #19. Partial classed need to be in the same namespace where the class is defined and also retain the same name as the class it is partial of. I would suggest doing some research on C# inheritance and C# partial classes if you would like to learn more about this subject. As noted partial classes are a C# concept and are not specific to NinjaScript or NinjaTrader, this is why none of this is documented and we have no samples for creating a partial class in a subfolder.

                        Creating files in the editor will create the default structure for that type of file, in the case of a strategy it would create a Strategy with the correct namespace for the folder it is in. This is correct for the normal NinjaScript files but because you are doing something else, you would have to remove the default structure that is created.

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

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by psquare, Today, 03:11 PM
                        4 responses
                        16 views
                        0 likes
                        Last Post psquare
                        by psquare
                         
                        Started by ballboy11, Today, 03:20 PM
                        1 response
                        12 views
                        0 likes
                        Last Post NinjaTrader_ChelseaB  
                        Started by tonynt, Today, 02:18 PM
                        1 response
                        10 views
                        0 likes
                        Last Post NinjaTrader_JoshG  
                        Started by denalek2419, Today, 01:46 PM
                        3 responses
                        12 views
                        0 likes
                        Last Post NinjaTrader_ChristopherJ  
                        Started by REI140205, Today, 12:58 PM
                        1 response
                        11 views
                        0 likes
                        Last Post NinjaTrader_Jim  
                        Working...
                        X