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

Utility functions & code organization

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

    Utility functions & code organization

    Hey gurus,

    As I develop some strategies, I'm accumulating a growing collection of methods that I would like to share with all of my strategies. I wonder if someone can help with ways of doing this without cut-and-paste...

    Can I use polymorphism, so something like:

    public class MySubStrategy : MyParentStrategy

    where I provide default implementations of OnBarUpdate or OnPositionUpdate? Is there some way of templating so that the OnPositionUpdate comes from one source and OnBarUpdate comes from another? I can create helper methods of course, as long as I pass all necessary properties but it's messy.

    And is there some good location to stuff all of the other helper methods and utilities I create? Right now I have a sort of dummy Indicator. It doesn't do anything indicator-esqe, but it's the only way I've found to create a new class which can be instantiated and referenced in Strategies. Feels like a hack. Is there any better way?

    #2
    >> public class MySubStrategy : MyParentStrategy
    We have not tried that and this would be beyond what we provide support for. You would need to try by yourself.

    Comment


      #3
      Can you suggest another method of sharing code between strategies? I'm having the same issue.

      Comment


        #4
        Since NT strategies / indicators / functions are all "partial" classes, they all compile to one project. So, you can basically create an empty indicator, make it a "partial" class, and write "public" functions.

        I have attached the framework with some comments to help.

        I hope this is helpful
        Attached Files
        mrlogik
        NinjaTrader Ecosystem Vendor - Purelogik Trading

        Comment


          #5
          Our UserDefinedMethods is a partial class that does just this as well although mrlogik approach might work better since if you export your custom scripts, this global indicator will be included in the export file were the UserDefinedMethods will not.
          RayNinjaTrader Customer Service

          Comment


            #6
            Hey Ray,

            Thanks, i forgot to mention that. This is the reason I created my own indicator / class to do it; I export to a few computers for trading whenever I create something.
            mrlogik
            NinjaTrader Ecosystem Vendor - Purelogik Trading

            Comment


              #7
              Thanks mrlogik,

              I tried your GV module and it works great for public functions but do I have to create a public static variable to access the global variable examples in your file? or is there another way to use them?

              Comment


                #8
                You have ot make a public static variable within the Gv global indicator.

                For example, with the code I gave you.

                Code:
                public static BData gBData;
                you can then access gBData from any indicator.

                Make sure you don't run into any race conditions with reading / writing out of sequence, or writing from multiple indicators at the same time.
                mrlogik
                NinjaTrader Ecosystem Vendor - Purelogik Trading

                Comment


                  #9
                  I know this is an old thread but it seems very useful. However I seem to be missing something very simple. After defining global functions in the GV indicator, the functions do not seem to be available, including the original GetDateFunction. Intellisense isn't picking it up and there is a "not found" error from the compiler.

                  What am I missing?

                  Using Ninjatrader 6.5.1000.8

                  Thanks!

                  Comment


                    #10
                    You will need to fix the error before you will see anything. Unfortunately this is outside the scope we can offer support for. Maybe a forum member familiar with what is being requested could help you out.
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      That was fast!

                      Oh sorry, I wasn't clear. If I try typing the global function, it doesn't show in intellisense. But if I do go ahead and type it manually anyway, the compiler isn't able to locate the method. Is there anything special I have to add, say in the Using section? For example, I can't just do GetDateFunction(...). Of course, GV.GetDateFunction doesn't work either. How would I call that one from my strategy?

                      Thanks!

                      Comment


                        #12
                        As mentioned, this is outside the scope of what we can support. I have no idea what this "GV" thing you refer to is.

                        Likely you need to do GV().GetDateFunction. Not sure. You may want to contact whoever's code you are duplicating to try and get help.
                        Josh P.NinjaTrader Customer Service

                        Comment


                          #13
                          The GV is in the GlobalShare.zip file above where the example is provided. I'll see if mrlogik can be of assistance though. But thanks for writing. The compiler doesn't complain so that's a step in the right direction. I'll start testing it out now. Thank you!

                          When writing partials like this, is there a way to refresh the intellisense cache?
                          Last edited by michaelangela; 02-04-2009, 05:19 PM. Reason: new to C# and still learning!

                          Comment


                            #14
                            Here is what I did: I created a class to facilitate the use of these Global Variable methods. I'm attaching it to this post.

                            BTW I'm not sure if you're trying to coordinate between two strategies or two different applications. If two strategies, you don't need GV. You can just use a static variable. The GV is useful for having two different processes communicate, like Ninjatrader and Tradestation for example.
                            Attached Files

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by Vulgoth_t_Destroyer, 05-09-2022, 04:45 PM
                            55 responses
                            5,455 views
                            0 likes
                            Last Post BartMan
                            by BartMan
                             
                            Started by DawnTreader, 05-08-2024, 05:58 PM
                            16 responses
                            52 views
                            0 likes
                            Last Post DawnTreader  
                            Started by tradingnasdaqprueba, 05-07-2024, 03:42 AM
                            15 responses
                            61 views
                            0 likes
                            Last Post NinjaTrader_Jesse  
                            Started by kevinenergy, Yesterday, 12:01 PM
                            8 responses
                            28 views
                            0 likes
                            Last Post kevinenergy  
                            Started by nightstalker, Today, 01:32 PM
                            1 response
                            12 views
                            0 likes
                            Last Post NinjaTrader_Zachary  
                            Working...
                            X