• 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

    Where are Position methods?

    I'm trying to maintain a utility of functions that I can share across all strategy development rather than duplicating the same code in ever strategy. Things like Position.MarketPosition, etc.

    Where is this in the Ninjascript code base? I've tried many differing 'using' statements trying to locate this and am not finding it.

    Is there a better way to find this? Would be great to have this documented in the documentation for each of these values.
    Last edited by RandyT; 07-19-2018, 02:00 PM.

    #2
    Hello RandyT,

    Thank you for the post.

    The objects and properties like Position.MarketPosition are known as inherited properties which are already available for any strategy. If you look in the help guide strategy section, you can find all the inherited properties for a strategy.

    Position.MarketPosition comes from the base object or : Strategy which your script inherits from. These properties are by default shared among all strategies but the Position.MarketPosition value is a virtual position meaning it only relates to each individual strategy. Strategies by nature do not share values with each other so that the account reporting makes sense when you look at it. This is to keep what each strategy did seperate.

    If you wanted to make custom shared methods or properties, you could use a partial class for that.

    You can create a new strategy, and then delete the code that was generated and replace it with the code below:

    Code:
    namespace NinjaTrader.NinjaScript.Strategies
    {
    	public partial class Strategy
    	{
    		public void MyMethod(double inValue)
    		{
    			
    		}
    	}
    }
    This allows you to define methods or properties at the base level or Strategy which then can be called from any strategy like the following:

    Code:
    protected override void OnBarUpdate()
    {
    	MyMethod(Close[0]);
    }
    Because the partial class is not your strategy specifically, you need to pass in any inherited values you want to access like the Position:

    Code:
    namespace NinjaTrader.NinjaScript.Strategies
    {
    	public partial class Strategy
    	{
    		public void MyMethod(Position position)
    		{
    			if(position.MarketPosition == MarketPosition.Flat)
                            { }
    		}
    	}
    }
    Code:
    protected override void OnBarUpdate()
    {
    	MyMethod(Position);
    }

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

    Comment


      #3
      Jesse,

      Thanks for the quick response.

      I don't have a lot of C# experience, so pardon the hand holding here as I get up to speed.

      Question is if this is the right thing for me to be doing, or .... I've already started with this in an AddOn which I am calling from Strategies. Perhaps it is better to have this in Strategies folder (or subfolder in my case) than in AddOns?

      I'm trying to do some very basic things like the following.

      Code:
      bool sessionPre {
      	get { return(ToTime(Time[0]) >= 43000 && ToTime(Time[0]) < 73000); }
      }
      I want to use these basic tests in the Strategy like so:

      Code:
      if (Test.sessionPre) {
         do something
      }
      Is this the right path, or am I getting myself in trouble here?

      Comment


        #4
        After trying this approach, I am getting the following error for every strategy in the subfolder that I created this "Test" strategy/utility file.

        Code:
        OnStateChange(): no suitable method found to override
        Getting that message for every protected method? in these strategies.

        Comment


          #5
          Hello RandyT,

          I would suggest to just make the partial class as that is generally the easiest and requires no additional objects like your example where you use Test.sessionPre.

          What you provided works right away in a partial class, you would just need to make the property public so that strategies can see it.


          Code:
          namespace NinjaTrader.NinjaScript.Strategies
          {
          	public partial class Strategy
          	{
          		public bool SessionPre {
          			get { return(ToTime(Time[0]) >= 43000 && ToTime(Time[0]) < 73000); }
          		}
          	}
          }
          Code:
          protected override void OnBarUpdate()
          {
          	Print(SessionPre);
          }
          The partial class is generally placed inside of the folder for the type that it is part of, so in the strategy folder in this case.

          The approach you are using now with the addon will also work, you just need to be more specific and point directly to that object which contains your methods or properties in addition to either having an instance of that object or making a static property/method.

          You do need to be careful when making methods or properties like this that access data directly (Time[0]). if you were to try and use this property when data was not loaded, for example in OnStateChange it would fail but may lead to confusion unless you go investigate the method or property to see why. The error would simply report the strategy failed in OnStateChange and may not report anything to do with your method or property.



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

          Comment


            #6
            Hello RandyT,

            Regarding the error, I would either suggest reverting what you have done, or I would need more details on what specifically you did before you had the error show up.

            The samples I have provided so far both compile and have worked during tests when following the steps provided.

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

            Comment


              #7
              Not sure what else I can provide.

              That error is given for:
              OnStateChange()
              OnBarUpdate()
              OnOrderUpdate()

              Referenced in every strategy in that subfolder.

              Comment


                #8
                And to be more clear, the error appears when the file that you have suggested I create exists in that folder.

                Comment


                  #9
                  The issue is caused by this file being in a subdirectory. Is there a solution for that?

                  Comment


                    #10
                    Hello RandyT,

                    I would need more details on the specific steps you followed before you received the error. You noted a subfolder, but I had not provided any instructions for creating folders only to create a new strategy in the default Strategies folder and then remove the existing syntax. Also, you noted "if the file exists", you would need to use unique names for new files and classes to prevent errors.

                    Can you provide the specific steps you followed including naming and menu clicks? Otherwise, please try reverting the change you made and recompile to go back to a compiled state.

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

                    Comment


                      #11
                      I want to maintain this file that I created following your instructions, along with all other strategies that I develop in their own subfolder of the Strategies folder.

                      How can I do what you suggest, but maintain it in a subfolder?

                      Comment


                        #12
                        I also see another side effect that is not desirable...

                        I now have a checkbox for every public boolean test that I have put in this file in the strategy analyzer as a parameter for the strategy.

                        How do I prevent this?

                        Comment


                          #13
                          Running a strategy in the analyzer now gives an "unhandled exception" and crashes the entire platform.

                          Comment


                            #14
                            Hello RandyT,

                            Thank you for the reply.

                            Can you provide more details on what specific syntax was used and the steps you used to create your test? I would still need the information I requested in post 10 to further help with your questions here. As I had noted it sounds like you followed some different steps than I had outlined so I would be unsure what the problem may be without more detail.

                            Some possible routes to a solution would be to either detail the steps you followed so that we can determine what is different in order to fix it, or you could reverse the changes you have made and go back to a working state. If you detail the steps you followed, I would also need to see the syntax you have used in the strategy and partial class for your tests.

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

                            Comment


                              #15
                              1. Create a subfolder under Strategies
                              2. Create the empty Strategy file in this new subfolder.
                              Last edited by RandyT; 07-20-2018, 06:57 AM.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by faisamtrader, Today, 10:31 AM
                              1 response
                              9 views
                              0 likes
                              Last Post NinjaTrader_Tyler  
                              Started by ChazJ, Today, 09:48 AM
                              1 response
                              3 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by maxie, Today, 10:01 AM
                              1 response
                              6 views
                              0 likes
                              Last Post NinjaTrader_Melissa  
                              Started by Bionian, Today, 09:36 AM
                              1 response
                              4 views
                              0 likes
                              Last Post NinjaTrader_PaulH  
                              Started by tomd, Today, 09:48 AM
                              1 response
                              12 views
                              0 likes
                              Last Post NinjaTrader_Ariana  
                              Working...
                              X