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

How to share code between Indicator and Strategy

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

    How to share code between Indicator and Strategy

    Hi,

    In my coding I have several functions (eg SaveChart) and constants (eg Enums) that I would like to use in both Indicators and Strategies. I've tried putting them in either of the UserDefinedMethods.cs in \Indicator or \Strategy folder and adding "using NinjaTrader.Indicator; using NinjaTrader.Strategy;" in my Indicators and Strategies but this doesn't seem to work.

    To bypass the problem I replicate my code in both \Indicator and \Strategy UserDefinedMethods.cs , but having to do everything double creates a risk of missing changes and is against the "Single Point of Definition" principle I like to maintain in my coding.

    What I think I'm really looking for is some kind of old fashioned %include that would insert my common code in both the UserDefinedMethods.cs - but I have no clue how to achieve this (BTW: did I already mention I'm not a C# expert ).

    I'm aware this is not the support NT provides, but maybe other developers have encountered a similar issue and can help me forward.

    Thanks,
    Hans

    #2
    just create a namespace of your own, like

    Code:
    namespace MyNamespace
    {
        public enum MyEnum
        {
            Hello,
            World
        }
    
    }
    declare the same in the indicator or stategy like,

    Code:
    using MyNamespace;

    Comment


      #3
      Thanks bukkan - tried it & works

      Next steps are variables and functions

      Depending on the instrument type I'm setting 4 variables with particular timings for that market (opening range, trading range). The function is called during Initialize(). Something like this: (I live in Belgium, so the timings are CET)

      Code:
      switch ( Instrument.MasterInstrument.Name )
      {
      	case "ES"	: { my_sORB = "15:30"; my_sORE = "15:59"; my_sTRB = "14:30"; my_sTRE = "22:00"; break; }
      	case "FDAX"	: { my_sORB = "07:50"; my_sORE = "08:29"; my_sTRB = "08:00"; my_sTRE = "19:00"; break; }
      }
      
      ... and then later one ...
      
      my_dtORB = DateTime.Parse( my_sORB );		// return value
      my_dtORE = DateTime.Parse( my_sORE );		// return value
      my_dtTRB = DateTime.Parse( my_sTRB );		// return value
      my_dtTRE = DateTime.Parse( my_sTRE );		// return value

      For variables I've been trying with public static variables, however I can't use them directly in my indicators. Being global I would expect to be able to access them via "my_dtORB", but seems I need all vars to be referenced as "MyCommonCode.MyVariables.my_dtORB".
      An alternative way is like this, but is cumbersome just to have 4 variables in my indicators and strategies and I would need to copy this in all indicators, something I'm trying to avoiding.

      Code:
      public DateTime  MyORB
      {get { return MyCommonCode.MyVariables.my_dtORB; } set { my_dtORB = value; } }

      For my functions I also have to prefix them with the name of the public class I put them in ( "MyUtils.fnGetTradingHours()" ). For functions I rarely use this is not a big problem, however for a rounding function or a display function this clutters my code a bit too much to my liking.

      Again thanks for any help from the group.

      Comment


        #4
        Unfortunately, that is one of the things about OOP, and actually it is a good thing, because it removes ambiguity. If you really do not want to reference the entities by their fully qualified path name, you can always use derived or inherited classes. I actually find those harder to debug, because their logic is just harder to keep track of.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by FrazMann, Today, 11:21 AM
        0 responses
        3 views
        0 likes
        Last Post FrazMann  
        Started by geddyisodin, Yesterday, 05:20 AM
        8 responses
        51 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Started by cmtjoancolmenero, Yesterday, 03:58 PM
        10 responses
        36 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by DayTradingDEMON, Today, 09:28 AM
        4 responses
        24 views
        0 likes
        Last Post DayTradingDEMON  
        Started by George21, Today, 10:07 AM
        1 response
        19 views
        0 likes
        Last Post NinjaTrader_ChristopherJ  
        Working...
        X