Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Calculate = OnPriceChange / OnEachTick Error!

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

    Calculate = OnPriceChange / OnEachTick Error!

    Hi NT,
    I am getting absurd results using the below code with Renko Bars NinjaTrader.

    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 MyCustomStrategy : Strategy
        {
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Enter the description for your new custom Strategy here.";
                    Name                                        = "MyCustomStrategy";
                    Calculate                                    = Calculate.OnPriceChange;
    
                    // Disable this property for performance gains in Strategy Analyzer optimizations
                    // See the Help Guide for additional information
    
                }
                else if (State == State.Configure)
                {
                }
            }
    
            protected override void OnBarUpdate()
            {
                if(IsFirstTickOfBar && CurrentBar > 0)
                {
                    if(Close[1] >= Open[1])
                    {
                        Print("Bullish" + " " + Time[1] + " " + CurrentBar);
                    }
                    else
                    {
                        Print("Bearish" + " " + Time[1] + " " + CurrentBar);
                    }
                }
            }
        }
    }
    I am getting wrong result which is duplicate bars. Need Help!

    #2
    Hello dastaan,

    Thank you for your post.

    This would be expected when trying to use IsFirstTickOfBar with bar types that remove the last bar, such as renko bars.



    Since renko bars redraw the open of the bar, IsFirstTickOfBar will return true several times per bar as that open is adjusted.

    If you only want this logic to run once per bar, you should run this OnBarClose instead, as it would then have the full, redrawn bar to refer to.

    Please let us know if we may be of further assistance to you.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by CarlTrading, 03-31-2026, 09:41 PM
    1 response
    80 views
    1 like
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 04-01-2026, 02:41 AM
    0 responses
    41 views
    0 likes
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    64 views
    2 likes
    Last Post CaptainJack  
    Started by CarlTrading, 03-30-2026, 11:51 AM
    0 responses
    66 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 03-30-2026, 11:48 AM
    0 responses
    54 views
    0 likes
    Last Post CarlTrading  
    Working...
    X