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

Passing indicator or strategy into class

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

    Passing indicator or strategy into class

    I have class called Logger... I want to use this class inside indicator or strategy (ninjatrader objects). This Logger class should write text using Time[0], Close[0], etc - properties which are as in indicator and strategy. Is there a object type which can I pass into Logger constructor, which allows me to use these common properties of Indicator or Strategy?

    Eg:
    Logger(Ninjatrader.Common ntCommon) {
    ntCommon.Print(ntCommon.Tmie[0]):
    }

    ntCommon - can be either NT Indicator or Strategy, which object type can I use?

    #2
    Hello kujista,

    Thank you for the post.

    The base classes for Indicators and Strategies are IndicatorBase and StrategyBase. Those classes will include the common properties and methods of all indicators and strategies. As an alternative, you could make a partial class of the indicator or strategy and do the logging in that partial class.

    Please let me know if I can assist further.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Well.. I understand. Is there a way to define sb variable to convert strategy or indicator into "some other object" which allows me using Methods common for strategy and indicator?

      Code:
      namespace Golemator {
          public class Logger {
              private StreamWriter file;
              private string loggedObject = "";
              private bool logExists = false;
      
              private NinjaTrader.NinjaScript.NinjaScriptBase sb;
      
              // Empty constructor
              public Logger() {}
      
              // Using Logger inside Ninjatrader Strategy
              public Logger(NinjaTrader.NinjaScript.StrategyBase strategy) {
                  sb = strategy;
              }
      
              // Using Logger inside Ninjatrader Indicator
              public Logger(NinjaTrader.NinjaScript.IndicatorBase indicator) {
                  sb = indicator;
              }
      
              // This method should write text into console using sb variable and Print no matter if used in strategy or indicator
              public ShowLogLine() {
                  sb.Print("Logger constructor initiated: " + sb.O);
              }
          }
      }

      Comment


        #4
        Hello kujista,

        Thanks for the reply.

        You could make an Indicator and a Strategy that create a partial class of themselves which will include your logger.

        As an example, say you have a Logger DLL. A reference to this DLL has already been added to NinjaTrader.

        You can make a blank indicator that implements a partial Indicator class like this:

        Code:
         using Logger;
        ...
        namespace NinjaTrader.NinjaScript.Indicators
        {
            public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
            {
                public LoggerHelper MyLogger = new Logger.LoggerHelper();
            }
        }
        Then you can use MyLogger in any Indicator you make after this.

        Both Strategies and Indicators have a built-in Log that you could use as well.

        Please let me know if you have any questions.




        Chris L.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Waxavi, Today, 02:10 AM
        1 response
        16 views
        0 likes
        Last Post NinjaTrader_LuisH  
        Started by Kaledus, Today, 01:29 PM
        5 responses
        13 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by Waxavi, Today, 02:00 AM
        1 response
        12 views
        0 likes
        Last Post NinjaTrader_LuisH  
        Started by alifarahani, Today, 09:40 AM
        5 responses
        23 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by gentlebenthebear, Today, 01:30 AM
        3 responses
        17 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Working...
        X