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 NullPointStrategies, Yesterday, 05:17 AM
    0 responses
    54 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    130 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    72 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    44 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    49 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X