Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Bar Type

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

    Bar Type

    Hello! I hope I'm posting in the right place. I wanted to create a bar type where The trend size is x ticks from the true open + 1 tick before a new bar is formed.

    The reversal size is x ticks from the true open + 1 tick before a reversal bar is formed that is not time based. I tried my hand at coding it for the first time but I ran into a bunch of issues, which I'm sure I'm doing something wrong lol. Here is the code i tried to build.


    region Using declarations
    using System;
    using System.ComponentModel;
    using System.Windows.Media;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.Design;
    #endregion

    namespace NinjaTrader.NinjaScript.BarsTypes
    {
    [Description("Custom Bar Type with specified trend size and reversal size")]
    public class CustomBarType : BarsType
    {
    private int trendSize = 5;
    private int reversalSize = 5;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = "Custom Bar Type with specified trend size and reversal size";
    Name = "CustomBarType";
    BarsPeriod = new BarsPeriod { BarsPeriodType = BarsPeriodType.Custom0 };
    }
    else if (State == State.Configure)
    {
    BarsPeriod.Value = "CustomBarType";
    }
    }

    public override void ApplyOptions(BarsTypeCustom0 barsTypeCustom0)
    {
    base.ApplyOptions(barsTypeCustom0);
    trendSize = barsTypeCustom0.TrendSize;
    reversalSize = barsTypeCustom0.ReversalSize;
    }

    public override void OnBarUpdate()
    {
    if (CurrentBar == 0)
    {
    AddBar(BarsPeriod.Value);
    }
    else
    {
    double open = FirstTickOfBar ? Open[1] : Close[1];
    double high = FirstTickOfBar ? High[0] : Math.Max(High[0], open);
    double low = FirstTickOfBar ? Low[0] : Math.Min(Low[0], open);
    double close = LastTickOfBar ? Close[0] : 0;
    bool isTrend = Math.Abs(high - open) >= trendSize * TickSize && Math.Abs(open - low) < reversalSize * TickSize;
    bool isReversal = Math.Abs(open - close) >= reversalSize * TickSize;
    if (isReversal)
    {
    AddBar(BarsPeriod.Value);
    open = close;
    high = close;
    low = close;
    }
    else if (!isTrend)
    {
    AddBar(BarsPeriod.Value);
    open = High[0];
    high = High[0];
    low = Low[0];
    }
    Open[0] = open;
    High[0] = high;
    Low[0] = low;
    Close[0] = close;
    }
    }

    public override string ToString()
    {
    return string.Format("{0} - TrendSize:{1} ReversalSize:{2}", Name, trendSize, reversalSize);
    }

    [Browsable(false)]
    public override bool IsIntraday
    {
    get { return true; }
    }

    [Description("Trend size in ticks")]
    [GridCategory("Parameters")]
    public int TrendSize
    {
    get { return trendSize; }
    set { trendSize = Math.Max(1, value); }
    }

    [Description("Reversal size in ticks")]
    [GridCategory("Parameters")]
    public int ReversalSize
    {
    get { return reversalSize; }
    set { reversalSize = Math.Max(1, value); }
    }
    }
    }


    I apologize as this is my first time trying to code. These are the errors I receive


    line 13 column 34 it says the type or namespace name "barstype" could not be found are you missing a using directive or an assembly reference?

    line 32 column 43 it says the type or namespace name "BarstypeCustomO" could not be found are you missing a using directive or an assembly reference?

    line 86 column 10 it says the type or namespace name "GridCategory" could not be found are you missing a using directive or an assembly reference?

    line 86 column 10 it says the type or namespace name "GridCategoryAttribute" could not be found are you missing a using directive or an assembly reference?

    line 94 column 10 it says the type or namespace name "GridCategory" could not be found are you missing a using directive or an assembly reference?

    line 94 column 10 it says the type or namespace name "GridCategoryAttribute" could not be found are you missing a using directive or an assembly reference?​

    Any help would be much appreciated!



    #2
    Hello Ken7787,

    The code you provided has quite a few problems relating to code that does not exist in NT8. Were you reviewing a NT7 script or did you use an external tool to generate code for you?

    A barsType is generally not a good place to start NinjaScript development because it is one of the most advanced types. BarsTypes require existing NinjaScript knowledge because there is really no documentation for them and you will need to be well versed in how NinjaScript works to be able to make a brand new bars type. You can make copies of the existing bars types using the NinjaScript editor. That would allow you to modify the existing code but that still requires having quite a good foundational knowledge of how bars are built in the platform.



    Comment


      #3
      It was a review of a code from NT7. Thank you for the response!


      Is there perhaps, somewhere in the forum or elsewhere that would possibly have this bartype available for use?

      Comment


        #4
        Hello Ken7787,

        You can view the existing bars types in NT8 using the NinjaScript editor. You can also view publically uploaded user scripts on the public user app share: https://ninjatraderecosystem.com/user-app-share/

        If you wanted something pre made that matches your goal you would likely need to hire a third party developer to create that or create it yourself.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        578 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        334 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        101 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        553 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        551 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X