Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Extract EnterLong EnterShort into Class

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

    Extract EnterLong EnterShort into Class

    Hi everyone,

    for a better reuse of my own entry strategy I want to extract EnterLong, EnterShort and other functions into an extra class.

    Unfortunately following is not working. Is there a way to get this working?

    Thanks in advance.

    Cheers



    HTML Code:
    public class myStrategy: Strategy
    {
    Ordermanager myOrders = new Ordermanager();
    
    
    protected override void OnBarUpdate()
    {
    
    myOrders.Long()
    
    }
    }
    
    public class Ordermanager : Strategy
    {
    public void Long()
    {
    var order = EnterLong(100, "name");
    }
    }
    Airwave
    NinjaTrader Ecosystem Vendor - Airwave

    #2
    Hello Airwave,

    Thank you for your reply.

    You'd want to create a partial class and put your reusable methods in there. How you have this currently is that you're essentially making 2 separate strategies, and strategies can't really talk to each other - you can't call a strategy from another strategy. I'd check out the section on Partial Classes on this page of our help guide:



    Please see the sample script at the following forum post that demonstrates.

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

    Please let us know if we may be of further assistance to you.

    Comment


      #3
      Hello Kate,

      Thank you, that helped a lot.


      But still have following problems.

      I've created a shared class in the addons folder.
      In the class I have following (here simplified) code:


      namespace NinjaTrader.NinjaScript.Strategies
      {
      public partial class Strategy
      {
      public class Ordermanager
      {
      public void Long() {
      var order = EnterLong(100, "name");
      }
      }
      }
      }

      Unfortunetely I cannot access the EnterLong here?
      Error CS0120 An object reference is required for the non-static field, method, or property 'StrategyBase.EnterLong(int, string)' NinjaTrader.Custom


      Hope you can help me again.

      Cheers
      Airwave
      NinjaTrader Ecosystem Vendor - Airwave

      Comment


        #4
        Hello Airwave,

        Thank you for your reply.

        You may be missing some using declarations in your addon script (you'll need to expand this section at the top of your code to see them). I'm attaching a few example scripts that may be helpful in figuring out what may be missing. Make sure there's one for using NinjaTrader.NinjaScript;.

        Please let us know if we may be of further assistance to you.
        Attached Files

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Yesterday, 05:17 AM
        0 responses
        59 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        134 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        75 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        45 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        50 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X