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

Accessing user defined method from custom indicator

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

    Accessing user defined method from custom indicator

    I have a user defined method that I can access from any strategy, but I cannot access that same user defined method from a custom indicator that I have created. For context the method I've coded up is streamwriter, with parameters for the file name and input string (both type string). Note that I have created the user defined method exactly as per the NT7 manual:
    HTML Code:
    https://ninjatrader.com/support/helpGuides/nt7/?user_defined_methods.htm
    and as I say it works perfectly with any strategy (both live strategies or backtesting strategies).

    When I first tried to call the method in my indicator it failed to compile with the error message 'name does not exist in the current context.' Next I tried:
    Code:
    NinjaTrader.Strategy.Strategy.MyMethod(myfile,mystring);
    but that failed to compile with 'an object reference is required for the non-static field, method or property.' So that led me to try creating a variable sw as follows:
    Code:
    NinjaTrader.Strategy.Strategy.StrmWrt<string,string> sw = new NinjaTrader.Strategy.Strategy.MyMethod<string,string>();
    but that failed to compile with '...is a method but is used like a type.'

    Help much appreciated. Thanks.

    #2
    Hello newuser,

    Yes that is correct, a UserDefinedMethod is just a C# partial class of the specific type (Indicator/Strategy). You can research partial classes in MSDN documentation if you wanted to learn more on that subject.

    The fastest solution here is to just copy your custom code in the same fashion into a UserDefinedMethod file for the indicator as well. You would have to update both Strategy/Indicator UserDefinedMethod files if that code needed changes but overall that's the simplest/fastest fix. You could do other more complex C# items like creating a custom C# class in a custom namespace to contain your methods and then from either partial class create a instances of your custom class to use when needed. For sake of simplicity I won't provide a sample of that but you can research C# classes in MSDN or other public C# resources for some examples on making objects in C#.

    Keep in mind the UserDefinedMethod file can be considered a sample file, that exists on each installation. If you plan to export your scripts it would not be suggested to use that file for that use case. You can instead copy your code into each strategy/indicator directly so UserDefinedMethod is not something which needs included with your export. If you import and replace UserDefinedMethods on another machine which is using that it may cause errors.

    JesseNinjaTrader Customer Service

    Comment


      #3
      Thanks for the reply. I realized my mistake...can't believe I didn't see it (clearly I glossed over the manual) but for some reason I hadn't registered that there's a separate 'user defined methods' file in the indicators folder specifically for custom indicators. Copied my streamwriter method across and it's working just fine thanks.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by funk10101, Today, 12:02 AM
      0 responses
      3 views
      0 likes
      Last Post funk10101  
      Started by gravdigaz6, Yesterday, 11:40 PM
      1 response
      7 views
      0 likes
      Last Post NinjaTrader_Manfred  
      Started by MarianApalaghiei, Yesterday, 10:49 PM
      3 responses
      10 views
      0 likes
      Last Post NinjaTrader_Manfred  
      Started by XXtrader, Yesterday, 11:30 PM
      0 responses
      4 views
      0 likes
      Last Post XXtrader  
      Started by love2code2trade, 04-17-2024, 01:45 PM
      4 responses
      28 views
      0 likes
      Last Post love2code2trade  
      Working...
      X