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

Referencing Indicator Results For Different Bars Objects - Ninjascript Newbie Q.

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

    Referencing Indicator Results For Different Bars Objects - Ninjascript Newbie Q.

    Hi there,

    Just getting up to speed with the NS programming model to create an NS version of a successful multi-timeframe MT4 indicator.

    In MT4, I essentially created a single Indicator harness that is hosted on a chart that is tied to a timeframe. However, the main processing is delegated to a series of classes that are instantiated in the Indicator harness. The purpose of this is to have a "primary" chart timeframe, say 15 minutes. In the Indicator harness, I keep a reference to instances of my processing class that are timeframe-specific, so, the primary timeframe and, a collection of "other" timeframes. The idea is that when I get a signal from the 15-minute processing class instance, I can then reference the "other" timeframe processing instance(s) and see whether their signals are aligned with the signal from the "primary" timeframe instance. In a nutshell, if I get, say, a long signal from a 15-minute instance, before I send a notification of that signal, I check with a higher timeframe to see whether it too, aligns. Therefore, before I take a 15-minute signal, I might want to check that the 30-minute and 60-minute timeframe are in alignment.

    In MT4, I can create multiple non-indicator classes and reference them. In NS, it seems that I cannot do this without creating and referencing external assemblies. However, on reading the NS documentation, it seems that by bringing in multiple Bars objects into a Ninjascript, in my case, just different timeframes of the same instrument, OnBarUpdate will be called for each Bars instance. This sounds very promising indeed but conceptually, I have a couple of "blanks" that I am unable to resolve due to my (current) limited understanding of the NS programming model.

    If I have three Bars objects in a single NS, so, fifteen, thirty and sixty minutes and my primary timeframe is fifteen minutes, I get a signal on the fifteen-minute timeframe, the OnBarUpdate is called and running in the context of that timeframe, is it possible to reach into the "other" Bars context to establish whether they are "aligned" based on my alignment criteria? For example, are the private variables and public properties of the indicator isolated by virtue of the Bars context? If so, is it possible to read the values of the same Public properties associated with a different Bars object in the same indicator?

    I hope I have made myself clear enough! In MT4, my indicator is hosted on a chart that is tied to a timeframe. That fact cannot be changed. So, when the indicator runs on an M15 chart, I create an instance of the processing class and tell it that it is servicing the M15 timeframe. This is the same for each "other" timeframe. So, in the processing class(es), instead of referencing Series arrays like High[], Low[], Close[], the classes always use the functions, iHigh(timeframe,shift), iLow(timeframe,shift), iClose(timeframe,shift) and so on. Therefore, from a primary indicator chart of M15, I can still "know" exactly what is happening in the "other" instances by referencing the processing object like so:-

    PrimaryTracker.TrendDirection();
    OtherTracker01.TrendDirection();
    OtherTracker02.TrendDirection();

    where PrimaryTracker, OtherTracker01 and OtherTracker02 are instances of the same TrendTracker class, that are each constructed with a config object that contains specific values like timeFrame, timeFrameType and noRender, the latter of which ensures that trendlines are drawn on the current host primary chart (M15) but the other Tracker instances don't attempt to render their "other" timeframe-specific trendlines on the primary M15 chart.

    I am asking this question because I realise that there will be NS-specific and optimised ways of "skinning the same cat" without trying to do it in exactly the same way as I did when using MT4's C++-like class-based approach. I'd appreciate any help/advice/comments on what I have asked and am perfectly happy to provide any additional information that may be required.

    With kindest regards and thanks,

    --G


    #2
    Hello Geester,

    Thank you for the question.

    While I can't comment on how MT4 works, I can say that you will be able to use multi-timeframe scripts in NinjaScript which it sounds like this is what you are talking about here. You will very likely need to redevelop your script as these platforms will not work in the same way, so I can't offer much for comparison here in contrast to your description. You will likely find the best help in the help guide and the reference samples along with the following page:



    This is one of the best resources at getting started with multi-series development. This lays out how the script processes along with providing samples on how to access the various series data. This is a required read if you plan to do anything multi-series, without this information it is very difficult to proceed in this kind of development.


    In MT4, I can create multiple non-indicator classes and reference them. In NS, it seems that I cannot do this without creating and referencing external assemblies.
    NinjaScript is just C# language, so you can create classes or do basically any other C# language feature in NinjaScript. You are likely looking for a "partial class" which is a C# design pattern. If you are trying to make a commonly shared class of methods this is generally how that is accomplished. You can also create a class which you need to call the "new" keyword and create an instance of, its really up to your design goals. You would not need to make a whole other assembly for this use case, you just need to create a C# partial class or a class in a file for this type of sharing. This is something you can learn about in external C# education resources. C# namespaces play a large role in NinjaScript so I would highly suggest learning about this subject first if you plan to do complex structures or share code as that will be part of the requirement.


    However, on reading the NS documentation, it seems that by bringing in multiple Bars objects into a Ninjascript, in my case, just different timeframes of the same instrument, OnBarUpdate will be called for each Bars instance.
    Yes, if you look through the first link I provided in this post, that explains this better. Each additional series will call OnBarUpdate and will have its own BarsInProgress index to denote what called OnBarUpdate. The other variables used during this time will associate with the series which called OnBarUdpate however you can look at other series data as well.

    If I have three Bars objects in a single NS, so, fifteen, thirty and sixty minutes and my primary timeframe is fifteen minutes, I get a signal on the fifteen-minute timeframe, the OnBarUpdate is called and running in the context of that timeframe, is it possible to reach into the "other" Bars context to establish whether they are "aligned" based on my alignment criteria?
    Yes please see the first link in this post for developing with multiple instruments or timeframes. This is explained in the samples in a few ways. Most cases you can use the Plural naming of a series, for example, you have Close and Closes which represent the Close of the called series, or the Close of a specific series:

    Code:
    Close[0] // currently called series
    Closes[1][0] // Current bars value of the second series
    My best suggestion surrounding finding differences between platforms would to review the Debugging your NinjaScript code guide we have to learn to utilize the Output window. It sounds like you have a good grasp of MT4 so comparing outputs will likely be the best way to fully understand the differences between what you are now doing and how NinjaScript works. You can also see the other reference samples we have or use the NinnjaScript editor to view how existing scripts work which is helpful in learning.


    I look forward to being of further assistance.









    JesseNinjaTrader Customer Service

    Comment


      #3
      Originally posted by Geester View Post
      Hi there,

      Just getting up to speed with the NS programming model to create an NS version of a successful multi-timeframe MT4 indicator.

      In MT4, I essentially created a single Indicator harness that is hosted on a chart that is tied to a timeframe. However, the main processing is delegated to a series of classes that are instantiated in the Indicator harness. The purpose of this is to have a "primary" chart timeframe, say 15 minutes. In the Indicator harness, I keep a reference to instances of my processing class that are timeframe-specific, so, the primary timeframe and, a collection of "other" timeframes. The idea is that when I get a signal from the 15-minute processing class instance, I can then reference the "other" timeframe processing instance(s) and see whether their signals are aligned with the signal from the "primary" timeframe instance. In a nutshell, if I get, say, a long signal from a 15-minute instance, before I send a notification of that signal, I check with a higher timeframe to see whether it too, aligns. Therefore, before I take a 15-minute signal, I might want to check that the 30-minute and 60-minute timeframe are in alignment.

      In MT4, I can create multiple non-indicator classes and reference them. In NS, it seems that I cannot do this without creating and referencing external assemblies. However, on reading the NS documentation, it seems that by bringing in multiple Bars objects into a Ninjascript, in my case, just different timeframes of the same instrument, OnBarUpdate will be called for each Bars instance. This sounds very promising indeed but conceptually, I have a couple of "blanks" that I am unable to resolve due to my (current) limited understanding of the NS programming model.

      If I have three Bars objects in a single NS, so, fifteen, thirty and sixty minutes and my primary timeframe is fifteen minutes, I get a signal on the fifteen-minute timeframe, the OnBarUpdate is called and running in the context of that timeframe, is it possible to reach into the "other" Bars context to establish whether they are "aligned" based on my alignment criteria? For example, are the private variables and public properties of the indicator isolated by virtue of the Bars context? If so, is it possible to read the values of the same Public properties associated with a different Bars object in the same indicator?

      I hope I have made myself clear enough! In MT4, my indicator is hosted on a chart that is tied to a timeframe. That fact cannot be changed. So, when the indicator runs on an M15 chart, I create an instance of the processing class and tell it that it is servicing the M15 timeframe. This is the same for each "other" timeframe. So, in the processing class(es), instead of referencing Series arrays like High[], Low[], Close[], the classes always use the functions, iHigh(timeframe,shift), iLow(timeframe,shift), iClose(timeframe,shift) and so on. Therefore, from a primary indicator chart of M15, I can still "know" exactly what is happening in the "other" instances by referencing the processing object like so:-

      PrimaryTracker.TrendDirection();
      OtherTracker01.TrendDirection();
      OtherTracker02.TrendDirection();

      where PrimaryTracker, OtherTracker01 and OtherTracker02 are instances of the same TrendTracker class, that are each constructed with a config object that contains specific values like timeFrame, timeFrameType and noRender, the latter of which ensures that trendlines are drawn on the current host primary chart (M15) but the other Tracker instances don't attempt to render their "other" timeframe-specific trendlines on the primary M15 chart.

      I am asking this question because I realise that there will be NS-specific and optimised ways of "skinning the same cat" without trying to do it in exactly the same way as I did when using MT4's C++-like class-based approach. I'd appreciate any help/advice/comments on what I have asked and am perfectly happy to provide any additional information that may be required.

      With kindest regards and thanks,

      --G
      Everything that you want is extensively addressed in the literature.

      ref: https://ninjatrader.com/support/help...nstruments.htm

      Comment


        #4
        Hi Jesse,

        Thank you so much for your timely and detailed reply.

        If I can create C# classes of my own, that will be of great benefit. I am familiar with the use of C# namespaces as I developed extensively in C# for many years. I just didn't know whether NS imposed restrictions on "regular" C# programming. Is there a specific mandatory place in the NS indicator/project hierarchy where class definitions should be located?

        Partial classes are fine but they are either form part of the current instance or, as your documentation suggests, they are public static class methods, so whilst useful, they fulfil a different use-case.

        Does the NS Editor support debugging features like stepping through code, breakpoints etc?

        Thank you for the reference to the articles. I will fully digest them before going any further with my development.

        I look forward to hearing from you!

        Regards,

        -G

        Comment


          #5
          Thanks for the heads-up koganam - I'll be sure to check out the resource you have linked to!

          Cheers,

          --G

          Comment


            #6
            Hello Geester,

            is there a specific mandatory place in the NS indicator/project hierarchy where class definitions should be located?
            The Documents\NinjaTrader 8\bin\Custom\AddOns folder is what would be suggested for most utility files. You can use the NinjaScript editor + button -> New Addon to generate a file with the standard using statements easily. The namespace along with code can be removed from the addon leaving a blank .cs file with the using statements ready for use.


            Partial classes are fine but they are either form part of the current instance or, as your documentation suggests, they are public static class methods, so whilst useful, they fulfil a different use-case.
            The help guide sample is formed in a strange way in my opinion and is not the best example of a partial class use with NinjaScript. Static is not needed with partial classes and nor is the inheritance shown. In terms of C# partial classes in NinjaScript, this is usually the suggestion if you needed to share something between two common types such as two indicators, this may include something like a method. You can also create standard classes or reference external libraries, it's really up to you how you want to design your C# structure. Because the help guide sample is formed strangely, Here is a more simplistic example of both a partial class and class which could be used in a separate .cs file:

            Code:
            namespace NinjaTrader.NinjaScript.Indicators
            {
                // usually what is suggested as this holds context, for example calling ReturnClose in an indicator
                // double myValue = ReturnClose();
            
                public partial class Indicator {
                    public double ReturnClose()
                    {
                        return Close[0];    
                    }
                }
            }
            Code:
            namespace NinjaTrader.NinjaScript.MyNamespaceForUtilities
            {
                // normal class, would require an instance:
                // MyMethods my = new MyMethods();
                // double myValue = my.ReturnClose(Close);
            
                 public class MyMethods{
                    public double ReturnClose(ISeries<double> close)
                    {
                        return close[0];    
                    }
                }
            }
            Does the NS Editor support debugging features like stepping through code, breakpoints etc?
            Not specifically in the editor, you would need to use Visual Studio and enable debug mode then attach to the NinjaTrader process: https://ninjatrader.com/support/help...=visual+studio

            This also entails you have clicked the Visual Studio button in the NinjaScript editor to open the custom project. If you just have VS open or open a single .cs file in VS it won't be able to reach breakpoints or do other code features.

            I look forward to being of further assistance.



            JesseNinjaTrader Customer Service

            Comment


              #7
              Hey Jesse,

              It's like being in a different world asking a question in here as opposed to mql5.com! Thank you so much for your responses and clear commitment to helping out. It's so refreshing!

              Kindest regards,

              --G

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by judysamnt7, 03-13-2023, 09:11 AM
              4 responses
              57 views
              0 likes
              Last Post DynamicTest  
              Started by ScottWalsh, Today, 06:52 PM
              4 responses
              36 views
              0 likes
              Last Post ScottWalsh  
              Started by olisav57, Today, 07:39 PM
              0 responses
              7 views
              0 likes
              Last Post olisav57  
              Started by trilliantrader, Today, 03:01 PM
              2 responses
              19 views
              0 likes
              Last Post helpwanted  
              Started by cre8able, Today, 07:24 PM
              0 responses
              9 views
              0 likes
              Last Post cre8able  
              Working...
              X