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

Help with indicator

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

    Help with indicator

    Hi, im a new user of NT and im trying to learn the 1st steps on how to create an indicator...

    On a website i found this code to create a bar counter indicator but it starts since the 1st bar. How can i change it so it reset to 0 on the 5 minute chart when the market opens?

    namespace NinjaTrader.NinjaScript.Indicators
    {
    public class MyFirstIndicator : Indicator
    {
    #region Indicator Name / Description

    private string iProductVersion = " v1.0.1";
    private string iProductName = "My First Indicator";
    private string iProductDescrption = string.Empty;

    public override string DisplayName { get { return string.Format("{0} {1}", iProductName, iProductVersion); } }

    #endregion

    private double iOffset = 0;
    private int iOffsetTicks = 1;
    [NinjaScriptProperty]
    [Display(Name = "01. Text Offset (ticks)", Description = "Description of input goes here", GroupName = "01. Indicator Parameters", Order = 1)]
    public int OffsetTicks
    {
    get { return iOffsetTicks; }
    set { iOffsetTicks = Math.Max(0, value); }
    }

    protected override void OnStateChange()
    {
    if(State == State.SetDefaults)
    {
    // Name / Description
    Name = string.Format("{0} {1}", iProductName, iProductVersion);
    Description = iProductDescrption;

    // Visuals
    IsOverlay = true;
    IsAutoScale = true;
    DisplayInDataBox = true;
    DrawOnPricePanel = true;
    BarsRequiredToPlot = 1;
    DrawHorizontalGridLines = true;
    DrawVerticalGridLines = true;
    PaintPriceMarkers = true;
    ShowTransparentPlotsInDataBox = false;
    ScaleJustification = ScaleJustification.Right;

    // Misc
    IsChartOnly = false;
    Calculate = Calculate.OnBarClose;
    IsSuspendedWhileInactive = false;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    }
    else if(State == State.DataLoaded)
    {
    iOffset = iOffsetTicks * TickSize;
    }
    }

    protected override void OnBarUpdate()
    {
    if(CurrentBar < 0)
    return;

    if(IsFirstTickOfBar)
    {
    string xCB = CurrentBar.ToString();
    if(CurrentBar % 2 == 0)
    Draw.Text(this, "Even " + xCB, xCB, 0, High[0] + iOffset);
    else
    Draw.Text(this, "Odd " + xCB, xCB, 0, Low[0] - iOffset);
    }
    }
    }
    }

    #2
    Welcome to the forums jsrdaytrader!

    Thanks for your post.

    The script counts using CurrentBar which itself is a count of the number of bars on the chart. If you would like to have this count from the first bar of a session, I suggest to create your own variable to count the bars, and to set that variable back to 1 on Bars.IsFirstBarOfSession. Documentation on using IsFirstBarOfSession is included below.

    Bars.IsFirstBarOfSession - https://ninjatrader.com/support/help...rofsession.htm

    While we cannot offer programming education with our Support Services, I have included a link to a publicly available resource that demonstrates incrementing variables in C#. Our NinjaTrader 7 help guide also has some light programming materials that can be referenced as well as some indicator tutorials which can help you to get acquainted with indicators.

    Incrementing a variable by 1 - https://stackoverflow.com/questions/...y-1-in-c-sharp

    Basic Programming Concepts (NT7) - https://ninjatrader.com/support/help...g_concepts.htm

    Indicator tutorials - https://ninjatrader.com/support/help...ndicators2.htm

    Our Code Breaking Changes page of the NinjaTrader 8 Help Guide goes over implementation differences between NinjaTrader 7 and NinjaTrader 8 so you can apply the concepts you learn in these tutorials in NinjaTrader 8.

    Code Breaking Changes - https://ninjatrader.com/support/help...ng_changes.htm

    Finally, I may also recommend using the Strategy Builder to build logic, and to use the View Code button after each addition to see the NinjaScript syntax generated from the Builder. This can be a great tool for learning NinjaScript syntax.

    Strategy Builder 301 — https://www.youtube.com/watch?v=_KQF2Sv27oE

    Conditions exmaples —https://ninjatrader.com/support/help...on_builder.htm

    Actions examples — https://ninjatrader.com/support/help...us/actions.htm

    Please let us know if you have any questions.
    JimNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by AttiM, 02-14-2024, 05:20 PM
    12 responses
    212 views
    0 likes
    Last Post DrakeiJosh  
    Started by cre8able, 02-11-2023, 05:43 PM
    3 responses
    237 views
    0 likes
    Last Post rhubear
    by rhubear
     
    Started by frslvr, 04-11-2024, 07:26 AM
    8 responses
    116 views
    1 like
    Last Post NinjaTrader_BrandonH  
    Started by stafe, 04-15-2024, 08:34 PM
    10 responses
    47 views
    0 likes
    Last Post stafe
    by stafe
     
    Started by rocketman7, Today, 09:41 AM
    3 responses
    11 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Working...
    X