• 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

Ninjascript function

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

    Ninjascript function

    Hi

    I have a particular set of rules that I employe in many of my strategies. The inputs are always the same.

    Right now, I’m always copying and pasting the block of my code in new strategies. Is there a way to create it and call it from any strategy? This way if I have to update it:: I can update one script rather than updating the code in each of my strategy.

    For example:

    I want to create a function that does the below:

    Code:
    			if(Position.MarketPosition==MarketPosition.Flat)
    			{
    			total_fundUSD= (AccountSize+SystemPerformance.AllTrades.TradesPerformance.Currency.CumProfit);
    			double dummyQ = (((int)Math.Floor((total_fundUSD*RiskPercent))) * Leverage); 
    			fxToBuy =  ((int)(dummyQ/1000))*1000;
    		//	Print(total_fundUSD);
    			}
    Once the above is defined, I want to call it in any strategy under OnBarUpdate. My question is,

    1) how do I create the above as a standalone function?
    2) where do i call it in the strategy? Ideally, I'd want to define it as a "name of the function" and then just drop it under OnBarUpdate.

    is there an example of this?

    Thanks
    Last edited by staycool3_a; 08-26-2018, 02:38 PM.

    #2
    found this example:

    https://ninjatrader.com/support/foru...104#post457104

    Code:
    namespace NinjaTrader.NinjaScript.Strategies
    {
    	public partial class Strategy
    	{
    		// This double can be accessed from within any strategy with PrintPositionInfo()
    		// or can be accessed from any script using NinjaTrader.NinjaScript.Strategies.PrintPositionInfo()
    		public static void PrintPositionInfo(Position position)
            {
                NinjaTrader.Code.Output.Process(String.Format("{0}: {1} {2} at {3}", position.Instrument, position.Quantity, position.MarketPosition, position.AveragePrice), PrintTo.OutputTab1);
            }
    
    		// inner nested organizing class
    		public class MySharedMethods
    		{
    			// This double can be accessed from within any strategy with MySharedMethods.PrintPositionInfo()
    			// or can be accessed from any script using NinjaTrader.NinjaScript.Strategies.MySharedMethods.PrintPositionInfo()
    			public static void PrintPositionInfo(Position position)
    			{
    				NinjaTrader.Code.Output.Process(String.Format("{0}: {1} {2} at {3}", position.Instrument, position.Quantity, position.MarketPosition, position.AveragePrice), PrintTo.OutputTab1);
    			}
    		}
    	}
    }
    Whats the difference between public static void VS public class VS public partial class?

    I havn't tested the below but what do you think:

    Code:
    namespace NinjaTrader.NinjaScript.Strategies
    {
    	public partial class Strategy
    	{
    		// This double can be accessed from within any strategy PositionInfo()
    		// or can be accessed from any script using NinjaTrader.NinjaScript.Strategies.PositionInfo()
    		public static void PositionInfo(Position position)
            {
    			if(Position.MarketPosition==MarketPosition.Flat)
    			{
    			double total_fundUSD= (AccountSize+SystemPerformance.AllTrades.TradesPerformance.Currency.CumProfit);
    			double dummyQ = (((int)Math.Floor((total_fundUSD*RiskPercent))) * Leverage); 
    			fxToBuy =  ((int)(dummyQ/1000))*1000;
    		//	Print(total_fundUSD);
    			}       
    			}
    }
    }
    then i can just call the above within any strategy as PositionInfo() under OnBarUpdate?? Or do I have to initialize it somewhere else before calling it in OBU? Thank you

    Comment


      #3
      Hello calhawk01,

      A void is a method that doesn't return anything.
      Below is a public link to a 3rd party educational site on void.
      https://www.dotnetperls.com/void

      Public means that the object can be access from outside of its class or namespace.
      Another public link to a 3rd party educational site on public.
      https://www.dotnetperls.com/public

      Static means that there are no instances that get created and the class or method only exists once. (Meaning the new keyword cannot be used)
      Another public link to a 3rd party educational site on static.
      https://www.dotnetperls.com/static

      A class is a structure that contains methods and variables.
      And another public link to a 3rd party educational site on class.
      https://www.dotnetperls.com/class


      A partial class is not the same as a static class. I would highly advise against making any script type (like Indicator, Strategy, or Drawing Tool) an partial class or extending a new class from any of these class types. This is not supported by NinjaTrader to do, is very complex and prone to issues, and doesn't allow you to share information between scripts. It instead allows you to add code to a class in multiple locations.
      Below is a public link to a 3rd party educational site on partial.
      https://www.dotnetperls.com/partial

      You wouldn't want to try and call Positions directly. Instead, you would want to pass the instance of the Strategy as a parameter to a method call in another class, and then access the Position of that strategy instance from the strategy instance parameter variable in the class.
      Below is a public link to an example that demonstrates.
      https://ninjatrader.com/support/foru...686#post492686
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        Originally posted by NinjaTrader_ChelseaB View Post
        Hello calhawk01,

        A void is a method that doesn't return anything.
        Below is a public link to a 3rd party educational site on void.
        https://www.dotnetperls.com/void

        Public means that the object can be access from outside of its class or namespace.
        Another public link to a 3rd party educational site on public.
        https://www.dotnetperls.com/public

        Static means that there are no instances that get created and the class or method only exists once. (Meaning the new keyword cannot be used)
        Another public link to a 3rd party educational site on static.
        https://www.dotnetperls.com/static

        A class is a structure that contains methods and variables.
        And another public link to a 3rd party educational site on class.
        https://www.dotnetperls.com/class


        A partial class is not the same as a static class. I would highly advise against making any script type (like Indicator, Strategy, or Drawing Tool) an partial class or extending a new class from any of these class types. This is not supported by NinjaTrader to do, is very complex and prone to issues, and doesn't allow you to share information between scripts. It instead allows you to add code to a class in multiple locations.
        Below is a public link to a 3rd party educational site on partial.
        https://www.dotnetperls.com/partial

        You wouldn't want to try and call Positions directly. Instead, you would want to pass the instance of the Strategy as a parameter to a method call in another class, and then access the Position of that strategy instance from the strategy instance parameter variable in the class.
        Below is a public link to an example that demonstrates.
        https://ninjatrader.com/support/foru...686#post492686
        Thanks, Chelsea.

        Even though you said I shouldn't use the partial class methond.. i did b/c i couldn't find any other examples lol. Anyways, I have another question:

        Code:
        strategy value is ;53000
        53000
        strategy value is ;53000
        53000
        strategy value is ;53000
        53000
        strategy value is ;53000
        53000
        strategy value is ;53000
        53000
        strategy value is ;53000
        53000
        strategy value is ;53000
        53000
        strategy value is: 53000 is the "fxtobuy" calculation within my current strategy. The other variable right below it (53k) is using the below function:

        Code:
        namespace NinjaTrader.NinjaScript.Strategies
        {
        		public partial class Strategy
        		{
        			
        			// This double can be accessed from within any strategy with MySharedMethods.PrintPositionInfo()
        			// or can be accessed from any script using NinjaTrader.NinjaScript.Strategies.MySharedMethods.PrintPositionInfo()
        			public static void PrintPositionInfo3(SystemPerformance systemperformance)
        			
        			{
        			double AccountSize=5000;
        			double RiskPercent=1;
        			double Leverage = 20;
        			double total_fundUSD= (AccountSize+systemperformance.AllTrades.TradesPerformance.Currency.CumProfit);
        			double dummyQ = (((int)Math.Floor((total_fundUSD*RiskPercent))) * Leverage); 
        			double fxToBuy =  ((int)(dummyQ/1000))*1000;
                    NinjaTrader.Code.Output.Process(String.Format("{0}:", fxToBuy), PrintTo.OutputTab1);
        			}			
        		}
        
        }
        And then I am calling that function inside of my strategy using
        Code:
        			PrintPositionInfo3(SystemPerformance);
        Is this an OK method? For my purposes it's giving the right values. I'm asking b/c you had mentioned that the partial method may cause issues etc.
        //****************************************//
        Another question: How do I create a place for variables? Accountsize/leverage are all variables that I have inside of my strategy. I'd want the function to use the variables that I have in my strategy rather than me having to hard code the variables inside my function
        Last edited by staycool3_a; 08-26-2018, 08:10 PM.

        Comment


          #5
          Hello calhawk01,

          If this works for you its up to you.

          You might have issues with exports and dlls but I wouldn't be able to say for sure.

          You seem to want the strategy instance (which is what I was suggesting.. pass the instance of the strategy to the external addon class instead of trying to make partial class) which would give you all properties of the strategy.

          However, the way you are currently passing the SystemPerformance object you could pass in other variables this way in the method parameters.
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Originally posted by NinjaTrader_ChelseaB View Post
            Hello calhawk01,

            If this works for you its up to you.

            You might have issues with exports and dlls but I wouldn't be able to say for sure.

            You seem to want the strategy instance (which is what I was suggesting.. pass the instance of the strategy to the external addon class instead of trying to make partial class) which would give you all properties of the strategy.

            However, the way you are currently passing the SystemPerformance object you could pass in other variables this way in the method parameters.
            Code:
            			public static void PrintPositionInfo3(SystemPerformance systemperformance double AccountSize, double Leverage double RiskPercent)
            When i do the above, it doesn't compile. Ideally, i'd just want those as a variable so when I'm calling the class in my strategy i can type:

            PrintPositionInfo3(SystemPerformance, AccountSize, Leverage, RiskPercent);
            ..and then the strategy would just match those variables within my strategy defined variables. this is how a function in matlab would work.

            matlab example:

            Code:
            
            	
            	
            	
            
            
            Code:
            function calc_position= get_position( s, k )
            %   s - account size
            %   k - percent
            
            calc_position= s*k;
            and then when i'm calling it in matlab.. i'd just type calc_position(any variable,any variable)
            Last edited by staycool3_a; 08-26-2018, 08:43 PM.

            Comment


              #7
              Hello calhawk01,

              This is custom code that is outside of what is supported by NinjaTrader Support.

              This thread will remain open for any community members that would like to assist.

              You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like our business development follow up with you with a list of affiliate consultants who would be happy to create this script or any others at your request.


              I am still recommending you don't make a partial class and instead make a new addon class (it doesn't have to be static if you don't want) and pass the instead of the strategy as a parameter and access any properties in it you want as demonstrated. Or you could just pass individual items.
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Originally posted by NinjaTrader_ChelseaB View Post
                Hello calhawk01,

                This is custom code that is outside of what is supported by NinjaTrader Support.

                This thread will remain open for any community members that would like to assist.

                You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like our business development follow up with you with a list of affiliate consultants who would be happy to create this script or any others at your request.


                I am still recommending you don't make a partial class and instead make a new addon class (it doesn't have to be static if you don't want) and pass the instead of the strategy as a parameter and access any properties in it you want as demonstrated. Or you could just pass individual items.
                Thanks. Is there an example of a Addon class? I'm using partial b/c i found it as an example in a code you made.. "mysharedmethodaddonexample" I looked around the forums but couldn't find it. All the write-ups in https://ninjatrader.com/support/help...s/?welcome.htm also use partial class

                Comment


                  #9
                  Code:
                  		public partial class Strategy
                  		{
                  			// This double can be accessed from within any strategy with MySharedMethods.PrintPositionInfo()
                  			// or can be accessed from any script using NinjaTrader.NinjaScript.Strategies.MySharedMethods.PrintPositionInfo()
                  	//		public static void PrintPositionInfo3(SystemPerformance systemperformance)
                  	//		{
                  	//		}
                  			
                  			public static void AccountCalcualtion(SystemPerformance systemperformance, double AccountSize, double RiskPercent, double Leverage, double fxToBuy, double total_fundUSD)
                  			{
                  			double CumProfits= (systemperformance.AllTrades.TradesPerformance.Currency.CumProfit);
                  			total_fundUSD= (AccountSize+CumProfits);
                  			double dummyQ = (((int)Math.Floor((total_fundUSD*RiskPercent))) * Leverage); 
                  			fxToBuy =  ((int)(dummyQ/1000))*1000;
                              NinjaTrader.Code.Output.Process(String.Format("{0}: ", fxToBuy), PrintTo.OutputTab1);
                  			}
                  			
                  			
                  		}
                  got it to work the way i wanted it. it's calculating the correct numbers. BUT.... now I just gotta figure out how to assign this value to a variable inside my strategy...

                  ..... side note: i hate programming

                  i tried adding it to my strategy via
                  Code:
                  		private NinjaTrader.NinjaScript.Strategies.Strategy.AccountCalcualtion fxToBuy1;
                  but that didnt work
                  Last edited by staycool3_a; 08-26-2018, 11:52 PM.

                  Comment


                    #10
                    tried

                    Code:
                    			fxToBuy= Strategies.Strategy.AccountCalcualtion(SystemPerformance, AccountSize, RiskPercent, Leverage, fxToBuy1, total_fundUSD);
                    as shown in attached example but i keep getting: "cannot convert void to double"

                    i'm getting the correct printed values within my strategy when i add the user defined:

                    Code:
                    namespace NinjaTrader.NinjaScript.Strategies
                    {
                    		public partial class Strategy
                    		{
                    			// This double can be accessed from within any strategy with MySharedMethods.PrintPositionInfo()
                    			// or can be accessed from any script using NinjaTrader.NinjaScript.Strategies.MySharedMethods.PrintPositionInfo()
                    	//		public static void PrintPositionInfo3(SystemPerformance systemperformance)
                    	//		{
                    	//		}
                    			
                    			public static void AccountCalcualtion(SystemPerformance systemperformance, double AccountSize, double RiskPercent, double Leverage, double fxToBuy1, double total_fundUSD)
                    			{
                    			double CumProfits= (systemperformance.AllTrades.TradesPerformance.Currency.CumProfit);
                    			total_fundUSD= (AccountSize+CumProfits);
                    			double dummyQ = (((int)Math.Floor((total_fundUSD*RiskPercent))) * Leverage); 
                    			fxToBuy1 =  ((int)(dummyQ/1000))*1000;
                               // NinjaTrader.Code.Output.ProcessfxToBuy1), PrintTo.OutputTab1);
                    				
                    			}
                    			
                    			
                    		}
                    
                    }
                    I just can't seem to assign them to my actual variable that i can use within my strategy....
                    Attached Files

                    Comment


                      #11
                      Hello calhawk01,

                      If you are not wanting to pass the strategy instance, use a ref variable as a parameter to the method call. The ref variable will reference the supplied variable from the strategy.
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        Hello calhawk01,

                        I was really thinking we wen't suppose to be advocating using partial classes that extend Strategy or Indicator, but after reviewing and testing.. this is fine.

                        Whatever addon contains the custom partial extension would also need to be exported with the export but it seems to be working alright.

                        The ref should work for what you need.

                        The public variables that are specific to each strategy that has a different name are not part of the Strategy class. They are only part of that specific strategy's name class.

                        If you were to make a partial class of that specific strategy's class name, this wouldn't be flexible to work with all the different strategies.

                        So because of this, instead pass a ref as a parameter to the method call.

                        The ref keyword means send the reference not a copy of the object. So whatever you do the reference in whatever external class is happening to the original object.

                        Below is a public link to a 3rd party educational site on the ref keyword.
                        https://www.dotnetperls.com/ref
                        Chelsea B.NinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by SAITON, Today, 09:03 PM
                        0 responses
                        1 view
                        0 likes
                        Last Post SAITON
                        by SAITON
                         
                        Started by suprsnipes, Today, 08:26 PM
                        0 responses
                        5 views
                        0 likes
                        Last Post suprsnipes  
                        Started by RobertoPaez, Today, 06:47 PM
                        0 responses
                        7 views
                        0 likes
                        Last Post RobertoPaez  
                        Started by catinabag, Today, 06:45 PM
                        0 responses
                        4 views
                        0 likes
                        Last Post catinabag  
                        Started by darkgreen, Today, 03:35 PM
                        0 responses
                        9 views
                        0 likes
                        Last Post darkgreen  
                        Working...
                        X