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

Event Counter

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

    Event Counter

    Hi

    Not understanding this
    I want to count for example every time a new low is hit on a tick by tick chart.

    I use the following code and 2 things are happening.
    1. It increments even when there is no new low
    2.I want it to increment by 1 and not the number of times it ticks

    I am sure it is simple by i have tried for quite some time to get this right and simply failed.

    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.DrawingTools;
    #endregion
    
    //This namespace holds Indicators in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Indicators
    {
    public class Test : Indicator
    {
    
    private int counter = 0;
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Indicator here.";
    Name = "Test";
    Calculate = Calculate.OnEachTick;
    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;
    }
    else if (State == State.Configure)
    {
    }
    }
    
    protected override void OnBarUpdate()
    {
    
    if(Bars.Count < 20)
    return;
    if(Low[0] < Low[1])
    TestCounter();
    if(IsFirstTickOfBar)
    
    counter = 0;
    
    }
    
    protected void TestCounter()
    {
    counter = counter +1;
    Draw.TextFixed(this,"FF",counter.ToString(),TextPo sition.BottomLeft);
    
    }
    }
    }
    
    #region NinjaScript generated code. Neither change nor remove.
    
    namespace NinjaTrader.NinjaScript.Indicators
    {
    public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
    {
    private Test[] cacheTest;
    public Test Test()
    {
    return Test(Input);
    }
    
    public Test Test(ISeries<double> input)
    {
    if (cacheTest != null)
    for (int idx = 0; idx < cacheTest.Length; idx++)
    if (cacheTest[idx] != null && cacheTest[idx].EqualsInput(input))
    return cacheTest[idx];
    return CacheIndicator<Test>(new Test(), input, ref cacheTest);
    }
    }
    }
    
    namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
    {
    public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
    {
    public Indicators.Test Test()
    {
    return indicator.Test(Input);
    }
    
    public Indicators.Test Test(ISeries<double> input )
    {
    return indicator.Test(input);
    }
    }
    }
    
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
    {
    public Indicators.Test Test()
    {
    return indicator.Test(Input);
    }
    
    public Indicators.Test Test(ISeries<double> input )
    {
    return indicator.Test(input);
    }
    }
    }
    
    #endregion

    #2
    Mindset Some comments:
    • Just for clarity, is the intent to count each tick within the current bar that is lower than any other tick that has been seen in the current bar?
    • The comparison currently made in the code is between Low[0] (the Low of the current bar) and Low[1] (the Low of the previous bar), which is not, presumably, what you want?
    • It seems you really want the count of each time Low[0] is lower than the previously seen Low[0]
    • Using a tick chart is fine, but if the period of the chart is 1 Tick, every bar is 1 Tick, and you'll never get a meaningful outcome; choose a period and bar type that will have multiple ticks if you want anything meaningful
    With a chart that has the potential to have multiple ticks per bar, perhaps something like this will be useful:
    Code:
    private double lowest = double.MaxValue;  // The current lowest seen. Initialise to max so that the first comparison is always lower.
    
    protected override void OnBarUpdate()
    {
        if (CurrentBar < BarsRequiredToPlot)
            return;
    
        if (IsFirstTickOfBar)
        {
            lowest = double.MaxValue;  // Do this if you want to reset the comparison for each bar
            counter = 0;
        }
        else
        if (Low[0] < lowest)
        {
            lowest = Low[0];
            counter++;
        }
    
        TestCounter();
    }
    
    private void TestCounter()
    {
        Draw.TextFixed(this,"FF",counter.ToString(),TextPosition.BottomLeft);
    }
    Note that as written above, the count will always increment on the first comparison made in each bar because Low[0] will always be lower than double.MaxValue. You may want something else.

    Hope this helps.

    Thanks.
    Multi-Dimensional Managed Trading
    jeronymite
    NinjaTrader Ecosystem Vendor - Mizpah Software

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by cre8able, Yesterday, 01:16 PM
    3 responses
    11 views
    0 likes
    Last Post cre8able  
    Started by ChartTourist, Today, 08:22 AM
    0 responses
    6 views
    0 likes
    Last Post ChartTourist  
    Started by LiamTwine, Today, 08:10 AM
    0 responses
    2 views
    0 likes
    Last Post LiamTwine  
    Started by Balage0922, Today, 07:38 AM
    0 responses
    5 views
    0 likes
    Last Post Balage0922  
    Started by JoMoon2024, Today, 06:56 AM
    0 responses
    6 views
    0 likes
    Last Post JoMoon2024  
    Working...
    X