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

Show Custom Indicator on Chart, linked to Strategy

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

    Show Custom Indicator on Chart, linked to Strategy

    Hi-

    I have an indicator that passes on a OnBNarUpdate call to a custom class I have written. That class then fires various events, and I draw to the chart, using Draw.Text(() for example.

    In my strategy, I create this indicator (or try to) and call AddChartIndicator(), but I never see the indicator. I cannot work out how to solve this. To get this to work I needed ot make indicator public (defined in Strategy.cs), so that I could create my indicator:

    indicator.ARWalkerIndicator()

    But the indicator doesnt have any plots, so I am wondering if this is partly the reason for not being able to see it draw.

    Any ideas?

    Thanks,

    Nick.

    #2
    Hello ptpatrader,

    I cannot work out how to solve this. To get this to work I needed ot make indicator public (defined in Strategy.cs), so that I could create my indicator:
    It sounds like you have a fairly strange way of calling the indicator here, in general you should never edit @Stratey.cs, that file should be left as is to allow the remainder of the indicator system to work correctly. If the indicator was not able to be called that could be due to how the indicator was designed or also what code you were trying to use.

    Can you provide a sample of how you are currently trying to call the indicator and add it?

    If you work best by example the concept you are describing can be seen in the SampleMACrossOver strategy. That strategy adds two SMA indicators visually and also uses them in its code. That is the correct way to AddChartIndicator and make sure you have the instance of the indicator to be used. If you did it any other way that is not likely going to work.

    In your specific case I would suggest to make a more simplified sample for testing, just exclude your custom class for now. You can add a Draw. method directly in OnBarUpdate in the indicator to make sure your Strategy calling the indicator and adding is correct. After that you can make sure the class works. That just avoids any oddities that may be happening if the structure you used is not aligned with the SampleMACrossOver approach.

    If you otherwise have a specific code sample of the indicator and strategy that you can attach that would additionally help to explain the question.




    I look forward to being of further assistance.

    JesseNinjaTrader Customer Service

    Comment


      #3
      Hi Jesse-

      Understood on the access modifier change in Strategy - that was to test a theory, which failed.

      The issue is actually simple:

      1. Considering Domain Driven Design, I have a strategy taxonomy of my own, which I need to instantiate inside the indicator - just thinking about the indicator.
      2. The indicator is the shim between Ninja and my model code. So I have inside the indicator, a CustomSignal class that I call update on, from the indicator side of the relationship.
      3. This is separation of concerns of ninja indicator code, and my own model code which I use outside of Ninja too.
      4. The indicator calls Update on the signal, and essentially it tells the indicator what it needs to paint - which is done calling Draw.Text(). So the indicator doesnt have a plot per se, which is likely not a good way to do the indicator, but thats how it works right now.
      5. Strategy creates indicator, indicator creates the proprietary signal class.
      6. The indicator in its OnBarUpdate will update the signal class, and then draw text.
      7. Its not clear how to do this in the strategy.

      I managed to resolve an earlier issue, and now I can call MyCustomIndicator() from within the Strategy class. But when I AddChartIndicator(MyCustomIndicator()), I do not see it plotting on the chart. Thats essentially my problem.

      Thanks,

      Nick.

      Comment


        #4
        Hello ptpatrader,

        Are you using the indicator from OnBarUpdate at all? You would need to follow the SampleMACrossOver sample in how it adds the indicator and uses it. If you are doing something else you may not be running the indicators historical processing or may see it not work. If you can provide a sample of the logic used that would be helpful to better understand what may be happening.

        This can also relate to how you programmed your indicator, I always suggest starting simple. This would be a good situation to call a System indicator and make sure you get that working before you move on to more complex tests. The other information about your custom class will just get in the way at this point if you don't have the fundamental part of calling the indicator working. If you can add the SMA, see it and use it that confirms the strategy part is right and you can then focus on the indicator.


        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Hi-

          So the indicator, when added to a chart as an indicator, works perfectly. In its OnBarUpdate it delegates the call, calling update on its internal model/domain class that has the signal code in it. It is during this sequence of events that the indicator will decide to draw something, using Draw.Text.

          In the strategy OnBarUpdate, I do not do anyting with the indicator - I just want the indicator to show up on the chart - when I call AddChartIndicator, is that basically the same as adding the indicator via the Indicators dialog in the chart? If so, I would expect to see stuff being drawn. I am doing what the State.DataLoaded code does in that sample strategy. the only difference is as I mention, the fact I do not have a plot. I am assuming the framework itself will call OnBarUpdate on the indicator that I added via AddChartIndicator?

          Thanks again,

          Nick.

          Comment


            #6
            Hello ptpatrader,
            In the strategy OnBarUpdate, I do not do anyting with the indicator
            This is what I was commenting on, you need to use it for it to calculate historically. The SampleMACrossOver can be considered the baseline example of how to call your indicator. You will need to call the indicator in some way from OnBarUpdate to make sure its Historical processing happens, this is documented in the following page: https://ninjatrader.com/support/help...htsub=addchart

            Tip: If you are adding an indicator which is dependent on the correct State of the indicator, you will need to ensure that you are also calling the indicator from the strategy in OnBarUpdate(), otherwise your indicator will only process in State.RealTime for performance optimizations.




            when I call AddChartIndicator, is that basically the same as adding the indicator via the Indicators dialog in the chart?
            Yes and No, AddChartIndicator is hosting the indicator so there can be other requirements in how you use it but the overall purpose is to display it.


            . the only difference is as I mention, the fact I do not have a plot.
            This can also cause no calculation, you will generally need to call Update on the indicator from the strategies OnBarUpdate or add a Transparent plot and just set a value like the Close[0] to it. The purpose of that is because of how you call the indciator which invokes processing:

            Code:
            double dummyValue = MyIndicator(parameters)[0]; // this causes calculation to happen, will cause an error without a plot
            
            alternatively this may work with no plot: 
            
            MyIndicator(parameters).Update(); // this causes calculation to happen, will cause an error without a plot
            If you are using AddChartIndicator your code will look different, you would have a variable stored of the indciator being used, so it may look like:

            Code:
            double dummyValue = myIndicator[0]
            or
            myIndicator.Update();

            I am assuming the framework itself will call OnBarUpdate on the indicator that I added via AddChartIndicator?
            There are performance optimizations for NinjaScript code so in this case no. As noted you need to make sure to tell the platform you want to calculate that indicator by using it from OnBarUpdate.





            I look forward to being of further assistance.


            JesseNinjaTrader Customer Service

            Comment


              #7
              Jesse, this is a very helpful response, thank you.

              I will digest this and figure things out.

              Appreciated,

              Nick.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by AveryFlynn, Today, 04:57 AM
              1 response
              10 views
              0 likes
              Last Post NinjaTrader_Erick  
              Started by Max238, Today, 01:28 AM
              4 responses
              37 views
              0 likes
              Last Post Max238
              by Max238
               
              Started by r68cervera, Today, 05:29 AM
              1 response
              7 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by geddyisodin, Today, 05:20 AM
              1 response
              11 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Started by timko, Today, 06:45 AM
              2 responses
              14 views
              0 likes
              Last Post NinjaTrader_ChristopherJ  
              Working...
              X