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

Indicator that Plots a given value

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

    Indicator that Plots a given value

    Hi,


    Since IsOverlay isn't dynamic, I am trying to develop an indicator that takes a double value in a method ("UpdatePlot") and plots the value. This way, I figure I can plot the things that I want with an Overlay using "AddPlot", and the things I want in a separate window using this indicator called "PlotValue". I come up with the following code for my indicator:

    Code:
    namespace NinjaTrader.NinjaScript.Indicators
    {
        public class PlotValue : Indicator
        {
            private Series<double>        plotValue;
            
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Plots the value given to it in an extra Method UpdatePlot";
                    Name                                        = "PlotValue";
                    Calculate                                    = Calculate.OnBarClose;
                    IsOverlay                                    = false;
                    DisplayInDataBox                            = true;
                    DrawOnPricePanel                            = true;
                    DrawHorizontalGridLines                        = true;
                    DrawVerticalGridLines                        = true;
                    PaintPriceMarkers                            = true;
                    ScaleJustification                            = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                    //Disable this property if your indicator requires custom values that cumulate with each new market data event.
                    //See Help Guide for additional information.
                    IsSuspendedWhileInactive                    = true;
                    AddPlot(Brushes.Orange, "Value");
                }
                else if (State == State.Configure)
                {
                }
                else if (State == State.DataLoaded)
                {
                    plotValue    = new Series<double>(this);
                }
            }
    
            public void UpdatePlot(double input){
                plotValue[0] = input;
            }
            
            protected override void OnBarUpdate()
            {
                Value[0] = plotValue[0];
            }
    
            #region Properties
    
            [Browsable(false)]
            [XmlIgnore]
            public Series<double> Value
            {
                get { return Values[0]; }
            }
            #endregion
    
        }
    }​

    I try to test this indicator in a strategy "TestStrategy" using Close[0] as my argument input:

    Code:
    namespace NinjaTrader.NinjaScript.Strategies
    {
        public class TestStrategy : Strategy
        {
            private PlotValue PlotValue1;
            
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Enter the description for your new custom Strategy here.";
                    Name                                        = "TestStrategy";
                    Calculate                                    = Calculate.OnBarClose;
                    EntriesPerDirection                            = 1;
                    EntryHandling                                = EntryHandling.AllEntries;
                    IsExitOnSessionCloseStrategy                = true;
                    ExitOnSessionCloseSeconds                    = 30;
                    IsFillLimitOnTouch                            = false;
                    MaximumBarsLookBack                            = MaximumBarsLookBack.TwoHundredFiftySix;
                    OrderFillResolution                            = OrderFillResolution.Standard;
                    Slippage                                    = 0;
                    StartBehavior                                = StartBehavior.WaitUntilFlat;
                    TimeInForce                                    = TimeInForce.Gtc;
                    TraceOrders                                    = false;
                    RealtimeErrorHandling                        = RealtimeErrorHandling.StopCancelClose;
                    StopTargetHandling                            = StopTargetHandling.PerEntryExecution;
                    BarsRequiredToTrade                            = 20;
                    // Disable this property for performance gains in Strategy Analyzer optimizations
                    // See the Help Guide for additional information
                    IsInstantiatedOnEachOptimizationIteration    = true;
                }
                else if (State == State.Configure)
                {
                }
                else if (State == State.DataLoaded)
                {    
                    PlotValue1 = PlotValue();
                    AddChartIndicator(PlotValue1);
                }
            }
    
            protected override void OnBarUpdate()
            {
                PlotValue1.UpdatePlot(Close[0]);
            }
        }
    }​
    Now this is where it gets tricky for me. I inspect the results in a chart:

    Click image for larger version

Name:	image.png
Views:	107
Size:	56.3 KB
ID:	1287184
    My indicator seems to work, but only for the last couple of bars. "Earlier" I seem to get the same data, repeated cyclically.


    I wonder if anyone has any clue why this might happen? What are some steps I can take to solve it? Is there a better way for me to do this?


    Sincerely,
    Axel


    #2
    Hello axelbaws,

    Rather than having the strategy set a value in the indicator it would be best to have the indicator pull a value from the strategy and plot it. You can see an example of that concept in the following link:

    JesseNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Jesse View Post
      Hello axelbaws,

      Rather than having the strategy set a value in the indicator it would be best to have the indicator pull a value from the strategy and plot it. You can see an example of that concept in the following link:

      https://ninjatrader.com/support/help..._a_ninjasc.htm
      Hi Jesse,


      That is definitely a better way to do what I was trying to! Thank you very much for the quick reply and the great example - exactly what I was looking for!


      Have a nice weekend,
      Axel

      Comment


        #4
        Originally posted by NinjaTrader_Jesse View Post
        Hello axelbaws,

        Rather than having the strategy set a value in the indicator it would be best to have the indicator pull a value from the strategy and plot it. You can see an example of that concept in the following link:
        Here is a small update. Earlier today I implement the indicator as per the example provided. I run into the same problem as with my own indicator.

        After much frustration I realise that the problem is me setting "Maximum bars look back" to 256. I change it to "Infinite". After this both indicators works as they should!


        In the end I go for my own indicator as it is Strategy agnostic.
        Last edited by axelbaws; 01-21-2024, 05:08 AM.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by pibrew, Today, 06:37 AM
        0 responses
        4 views
        0 likes
        Last Post pibrew
        by pibrew
         
        Started by rbeckmann05, Yesterday, 06:48 PM
        1 response
        14 views
        0 likes
        Last Post bltdavid  
        Started by llanqui, Today, 03:53 AM
        0 responses
        6 views
        0 likes
        Last Post llanqui
        by llanqui
         
        Started by burtoninlondon, Today, 12:38 AM
        0 responses
        11 views
        0 likes
        Last Post burtoninlondon  
        Started by AaronKoRn, Yesterday, 09:49 PM
        0 responses
        16 views
        0 likes
        Last Post AaronKoRn  
        Working...
        X