Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Using a custom bar type as a data series for an indicator

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

    Using a custom bar type as a data series for an indicator

    Hello,

    I am trying to use a custom bar type "My Bar Type". This bar type is based on Renko and takes in two parameters for the reversal and the trend.

    In the (State == State.Configure) condition I load the data this way:

    AddDataSeries(new BarsPeriod() { BarsPeriodType = (BarsPeriodType)Custom.Logic.BarHelper.GetBarIdByN ame("My Bar Type"), Value = 14, Value2 = 2});

    I wrote a method to get the Id of the Bar Type and I've confirmed with prints that it's using the right Bar Type.

    I'm trying to just plot a simple EMA from this data series. The EMA that is getting printed is 100% wrong. I assume I'm using the AddDataSeries incorrectly.

    Can someone help point out the error?

    Thanks!


    #2
    Hello CopyPasteGhost,

    Thanks for your post.

    We can't confirm from the snippet if your logic is fetching the right BarsPeriodType index. I may also suggest comparing what you are doing with an existing custom bars type and to also test ensuring you have a hard coded BarsPeriodType index entered.

    You could test the UniRenko BarsType with the following code in an indicator and the prints should align with the bars on the UniRenko chart

    Code:
        else if (State == State.Configure)
        {
            AddDataSeries(new BarsPeriod { BarsPeriodType = (BarsPeriodType)2018, BaseBarsPeriodValue = 2, Value = 2, Value2 = 6 });
        }
    }
    
    protected override void OnBarUpdate()
    {
        if (BarsInProgress == 1)
            Print(Close[0]);
    }
    Looking at the UniRenko BarsTypes code, we can see that BaseBarsPeriodValue, Value and Value2, represent Tick Trend, Open Offset, and TickReversal.

    Code:
    SetPropertyName("BaseBarsPeriodValue", "Open Offset");
    SetPropertyName("Value", "Tick Trend");
    SetPropertyName("Value2", "Tick Reversal");
    With this test you can ensure that you are properly adding the custom BarsType to an indicator. Using your custom BarsType would involve ensuring that you are setting the BarsType's properties appropriately when creating the BarsPeriod object.

    UniRenko - https://ninjatraderecosystem.com/use...nko-bartype-8/

    The NinjaTrader Ecosystem website is for educational and informational purposes only and should not be considered a solicitation to buy or sell a futures contract or make any other type of investment decision. The add-ons listed on this website are not to be considered a recommendation and it is the reader's responsibility to evaluate any product, service, or company. NinjaTrader Ecosystem LLC is not responsible for the accuracy or content of any product, service or company linked to on this website.

    I look forward to assisting.

    Comment


      #3
      hi Jim, Can you please show an example code of how to add an SMA of this new dataseries added above from within the same strategy?

      The SMA should have a displacement of 2 bars.

      Thanks.

      Comment


        #4
        Hello NTbrass,

        You can create a private SMA indicator at class level and instantiate it in State.DataLaoded with the added data series.

        To add the indicator to the chart, you can use AddChartIndicator(), but we could not modify visual displacement here, however.

        When referencing the indicator in code, you can use a BarsAgo reference of 2 to reference 2 bars ago.

        AddChartIndicator - https://ninjatrader.com/support/help...tindicator.htm

        Code:
        private SMA SMA1;
        protected override void OnStateChange()
        {
            if (State == State.SetDefaults)
            {
                Name                                        = "Test";
            }
            else if (State == State.Configure)
            {
                AddDataSeries(new BarsPeriod { BarsPeriodType = (BarsPeriodType)2018, BaseBarsPeriodValue = 2, Value = 2, Value2 = 6 });
            }
            else if (State == State.DataLoaded)
            {
                SMA1 = SMA(BarsArray[1], 14);
            }
        }
        
        protected override void OnBarUpdate()
        {
            if (CurrentBars[1] < 2)
                return;
        
            Print(SMA1[2]);
        }
        I look forward to assisting.

        Comment


          #5
          Code:
          #region Using declarations
          using System;
          using System.Collections.Generic;
          using System.ComponentModel;
          using System.ComponentModel.DataAnnotations;
          using System.Linq;
          using System.Text;
          using System.Threading.Tasks;
          using System.Windows;
          using System.Windows.Input;
          using System.Windows.Media;
          using System.Xml.Serialization;
          using NinjaTrader.Cbi;
          using NinjaTrader.Gui;
          using NinjaTrader.Gui.Chart;
          using NinjaTrader.Gui.SuperDom;
          using NinjaTrader.Gui.Tools;
          using NinjaTrader.Data;
          using NinjaTrader.NinjaScript;
          using NinjaTrader.Core.FloatingPoint;
          using NinjaTrader.NinjaScript.Indicators;
          using NinjaTrader.NinjaScript.DrawingTools;
          #endregion
          
          //This namespace holds Strategies in this folder and is required. Do not change it.
          namespace NinjaTrader.NinjaScript.Strategies
          {
          public class MyCustomStrategy2 : Strategy
          {
          
          private SMA SMA1;
          
          protected override void OnStateChange()
          {
          if (State == State.SetDefaults)
          {
          Description = @"Enter the description for your new custom Strategy here.";
          Name = "MyCustomStrategy2";
          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)
          {
          AddDataSeries(new BarsPeriod { BarsPeriodType = (BarsPeriodType)2018, BaseBarsPeriodValue = 2, Value = 2, Value2 = 6 });
          }
          else if (State == State.DataLoaded)
          {
          SMA1 = SMA(BarsArray[1], 14);
          
          SMA1.Plots[0].Brush = Brushes.Yellow;
          SMA1.Plots[0].Width=3;
          AddChartIndicator(SMA1);
          
          }
          }
          
          protected override void OnBarUpdate()
          {
          //Add your custom strategy logic here.
          
          if (CurrentBars[1] < 2)
          return;
          
          Print(SMA1[2]);
          
          }
          }
          }
          Something seems off with my results of this code.

          For instance the last price of CL 06 20 is 24,63 but this code produces SMA with last value 23.34.

          The SMA value when applied directly to the chart is 24.70

          Plus the results varied from price significantly along the way too.

          Wondering how to fix this. See attached screenshots.
          Attached Files
          Last edited by NTbrass; 05-11-2020, 08:54 AM.

          Comment


            #6
            Hello NTbrass,

            Tick based bars are very dependent on the time they have started. A tick based bar like UniRenko added to one chart may not have started at the same point as the UniRenko data series added in your script.

            The script is adding a UniRenko data series and is using that data series to create an SMA.

            You could add a UniRenko data series to the same chart and make sure that the number of Days To Load is the same. We should then be able to observe consistent prints for what we see with calculated on with the script and what is calculated on the chart.

            Below a demo showing how I have tested this snippet and compared against UniRenko values.

            Demo - https://drive.google.com/file/d/13Fp...w?usp=drivesdk

            We look forward to assisting.

            Comment


              #7
              Thanks Jim for taking the time to setup this demonstration video.

              It explained whats going on.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              663 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              376 views
              1 like
              Last Post Geovanny Suaza  
              Started by Mindset, 02-09-2026, 11:44 AM
              0 responses
              110 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
              0 responses
              575 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              580 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X