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

OnEachTick or OnPriceChange

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

    OnEachTick or OnPriceChange

    Hi guys,

    I'm building a simple strategy in NinjaTrader 8. I like to read each tick or price change but it's not working well. The event (OnBarUpdate) is triggered on the close bar.
    I attached a pic.

    Thank you

    #2
    Hello JET_IN,

    Thank you for your post.

    How are you testing the strategy? In the Strategy Analyzer, running it on live data, or using the Playback Connection? If you're running it in the Strategy Analyzer or over Historical Data, this would be expected.

    You should expect that a strategy running real-time (live brokerage account, live market simulation, Market Replay etc...) will produce different results than the performance results generated during a backtest using historical data. This difference may be more easily seen on certain Bars types (e.g. Point and Figure) than others due to their inherent nature in bar formation.

    During a backtest in the Strategy Analyzer you can select conservative or liberal fill algorithms which will produce different results. Fills are determined based on 4 data points, OHLC of a bar since that is the only information that is known during a backtest and there will be no intra-bar data. This means actions cannot happen intra-bar, fills cannot happen intra-bar. All prices and actions come from and occur when the bar closes as this is all the information that is known.

    Because of this, OnBarUpdate will only update 'On bar close' as it does not have the intra-bar information necessary for 'On price change' or 'On each tick'.

    Also, here is a link to the differences on real-time vs backtest (historical).



    Adding intra-bar granularity can help with this.

    Intra-bar granularity adds a second data series such as a 1 tick series so that the strategy has finer granularity in the historical data in between the OHLC of the primary series. This allows for more accurate trades by supplying the correct price at the correct time for the order to fill with.

    In NinjaTrader 8, there have been two new enhancements so that programmers do not have to manually add this secondary series and code the script to for high accuracy fills (Order Fill Resolution) and for intra-bar actions (TickReplay).

    Here is a link to our forums that goes into depth on using Order Fill Resolution and Tick Replay to ensure your backtests are as close to real time results as possible:

    Citizens of the NinjaTrader Community, A common question we hear from clients is 'why are results from backtest different from real-time or from market replay?'. Live orders are filled on an exchange with a trading partner on an agreed upon price based on market dynamics. Backtest orders are not using these market dynamics.


    High Fill Order Resolution and TickReplay cannot be used together. If it is necessary to have both, it is still possible to add intra-bar granularity to a script in the code itself for order fill accuracy and use TickReplay to update indicators with Calculate set to OnPriceChange or OnEachTick historically.

    Please let us know if we may be of further assistance to you.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Hello Kate,

      Excuse me, I didn't write this in my last message. It's a playback connection and I'm not running a strategy analyzer.
      In my case, I have got a chart and I add a strategy. I hope to get tick by tick in the event (OnBarUpdate) but it's working with close bar.

      Thanks

      Comment


        #4
        Hello JET_IN,

        Thank you for your reply.

        In the Playback Connection, are you using Historical Data, or Market Replay data? If you are using Historical, I would expect OnBarUpdate to be run on bar close as this is essentially the same as using the Strategy Analyzer. You would need to be using Market Replay data for the strategy to be able to process On Each Tick or On Price Change. I also note that in your original screenshot you've set it to be OnPriceChange, which would not update unless the price changes; not on each tick.

        Thanks in advance; I look forward to assisting you further.
        Kate W.NinjaTrader Customer Service

        Comment


          #5
          Hello Kate,

          Yes, I'm checking with EachTick or PriceChange but it's always a Market Replay Data. It's a NinjaTrader 8.0.19.1 64-bit

          Thank you very much

          Comment


            #6
            The event (OnDataPoint) for a dataseries is working well with Tick by Tick but it's not working with strategies.

            Comment


              #7
              Hello JET_IN,

              Thank you for your replies.

              First, I see that you're using an older version of NinjaTrader 8. To update NinjaTrader, please follow the steps below:
              • First, copy your license key from NinjaTrader under Help> License Key then exit NinjaTrader
              • Click on the link: http://ninjatrader.com/PlatformDirect
              • Enter your license key and press Submit
              • Select 'NinjaTrader 8'
              • Select 'Download'
              • Critical: Before running the installer, ensure NinjaTrader is closed.
              Would you be able to provide at least a simplified version of the strategy so I may assess the behavior on my end? Any code unnecessary to reproduce the behavior may be omitted.

              Thanks in advance; I look forward to assisting you further.
              Kate W.NinjaTrader Customer Service

              Comment


                #8
                Okay, I have updated successufully with my license key. I write you the code.


                namespace NinjaTrader.NinjaScript.Strategies
                {
                public class N3 : Strategy
                {
                protected override void OnStateChange()
                {
                if (State == State.SetDefaults)
                {
                Name = "N3";
                Calculate = Calculate.OnEachTick;
                EntriesPerDirection = 1;
                EntryHandling = EntryHandling.AllEntries;
                IsExitOnSessionCloseStrategy = true;
                ExitOnSessionCloseSeconds = 30;
                IsFillLimitOnTouch = false;
                MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
                OrderFillResolution = OrderFillResolution.High;
                Slippage = 0;
                StartBehavior = StartBehavior.WaitUntilFlat;
                TimeInForce = TimeInForce.Gtc;
                TraceOrders = false;
                RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
                StopTargetHandling = StopTargetHandling.PerEntryExecution;
                BarsRequiredToTrade = 5;
                // Disable this property for performance gains in Strategy Analyzer optimizations
                // See the Help Guide for additional information
                IsInstantiatedOnEachOptimizationIteration = true;
                }
                else if (State == State.Configure)
                {
                Calculate = Calculate.OnEachTick;
                }
                else if (State == State.Historical)
                {
                Calculate = Calculate.OnEachTick;
                }
                }

                protected override void OnBarUpdate()
                {
                ----->>>if (Close[0] < Open[0]) <<<----------- Break Point & read last Close[0] (It's working with bar close, not tick by tick)
                {

                }
                else if (Close[0] > Open[0])
                {

                }
                }
                }
                }

                Comment


                  #9
                  //protected override void OnBarUpdate()
                  //{
                  // 1st Line ----->>>if (High[0] != Low[0]) <<<----------- Break Point & read last Close[0] (It's working with bar close, not tick by tick)
                  {//

                  Comment


                    #10
                    Hello JET_IN,

                    Thank you for your reply.

                    I actually missed something in your last reply - you're trying to monitor in OnDataPoint()?

                    The method OnDataPoint() exists for BarsTypes, but isn't a method used in strategies. What are you wanting to monitor for, so we can figure out where your logic should go?

                    Thanks in advance; I look forward to assisting you further.
                    Kate W.NinjaTrader Customer Service

                    Comment


                      #11
                      Hello Kate,

                      Excuse me for the inconvenience. I'm checking with Market Replay the next three events. All of them with 50 ticks (Data Series). I'm in debug mode and debugging with Visual Studio.

                      Example 1: BarsType

                      //protected override void OnStateChange()
                      //{
                      //if (State == State.SetDefaults)
                      //{
                      Name = Custom.Resource.NinjaScriptBarsTypeTick;
                      BarsPeriod = new BarsPeriod { BarsPeriodType = BarsPeriodType.Tick };
                      BuiltFrom = BarsPeriodType.Tick;
                      DaysToLoad = 3;
                      IsIntraday = true;
                      IsTimeBased = false;
                      //}
                      //else if (State == State.Configure)
                      //{
                      Name = string.Format(Core.Globals.GeneralOptions.CurrentC ulture, Custom.Resource.DataBarsTypeTick, BarsPeriod.Value, (BarsPeriod.MarketDataType != MarketDataType.Last ? string.Format(" - {0}", Core.Globals.ToLocalizedObject(BarsPeriod.MarketDa taType, Core.Globals.GeneralOptions.CurrentUICulture)) : string.Empty));

                      Properties.Remove(Properties.Find("BaseBarsPeriodT ype", true));
                      Properties.Remove(Properties.Find("BaseBarsPeriodV alue", true));
                      Properties.Remove(Properties.Find("PointAndFigureP riceType", true));
                      Properties.Remove(Properties.Find("ReversalType", true));
                      Properties.Remove(Properties.Find("Value2", true));
                      //}
                      //}
                      //protected override void OnDataPoint(Bars bars, double open, double high, double low, double close, DateTime time, long volume, bool isBar, double bid, double ask)

                      Comment


                        #12
                        Hello Kate,

                        Excuse me for the inconvenience. I'm checking with Market Replay the next three events. All of them with 50 ticks (Data Series). I'm in debug mode and debugging with Visual Studio.

                        Example 1: BarsType

                        //protected override void OnStateChange()
                        //{
                        //if (State == State.SetDefaults)
                        //{
                        Name = Custom.Resource.NinjaScriptBarsTypeTick;
                        BarsPeriod = new BarsPeriod { BarsPeriodType = BarsPeriodType.Tick };
                        BuiltFrom = BarsPeriodType.Tick;
                        DaysToLoad = 3;
                        IsIntraday = true;
                        IsTimeBased = false;
                        //}
                        //else if (State == State.Configure)
                        //{
                        Name = string.Format(Core.Globals.GeneralOptions.CurrentC ulture, Custom.Resource.DataBarsTypeTick, BarsPeriod.Value, (BarsPeriod.MarketDataType != MarketDataType.Last ? string.Format(" - {0}", Core.Globals.ToLocalizedObject(BarsPeriod.MarketDa taType, Core.Globals.GeneralOptions.CurrentUICulture)) : string.Empty));

                        Properties.Remove(Properties.Find("BaseBarsPeriodT ype", true));
                        Properties.Remove(Properties.Find("BaseBarsPeriodV alue", true));
                        Properties.Remove(Properties.Find("PointAndFigureP riceType", true));
                        Properties.Remove(Properties.Find("ReversalType", true));
                        Properties.Remove(Properties.Find("Value2", true));
                        //}
                        //}

                        //protected override void OnDataPoint(Bars bars, double open, double high, double low, double close, DateTime time, long volume, bool isBar, double bid, double ask)

                        -->> Each event is Tick by Tick


                        Example 2: Strategies

                        //protected override void OnStateChange()
                        //{
                        //if (State == State.SetDefaults)
                        //{
                        Name = "N3";
                        Calculate = Calculate.OnEachTick;
                        EntriesPerDirection = 1;
                        EntryHandling = EntryHandling.AllEntries;
                        IsExitOnSessionCloseStrategy = true;
                        ExitOnSessionCloseSeconds = 30;
                        IsFillLimitOnTouch = false;
                        MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
                        OrderFillResolution = OrderFillResolution.High;
                        Slippage = 0;
                        StartBehavior = StartBehavior.WaitUntilFlat;
                        TimeInForce = TimeInForce.Gtc;
                        TraceOrders = false;
                        RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
                        StopTargetHandling = StopTargetHandling.PerEntryExecution;
                        BarsRequiredToTrade = 5;
                        // Disable this property for performance gains in Strategy Analyzer optimizations
                        // See the Help Guide for additional information
                        IsInstantiatedOnEachOptimizationIteration = true;
                        //}
                        //else if (State == State.Configure)
                        //{
                        Calculate = Calculate.OnPriceChange;
                        //}
                        //else if (State == State.Historical)
                        //{
                        Calculate = Calculate.OnPriceChange;
                        //}
                        //}

                        //protected override void OnBarUpdate()
                        //{

                        -->> Each event is Tick by Tick

                        //}

                        Comment


                          #13
                          Hello Kate,

                          Excuse me for the inconvenience. I'm checking with Market Replay the next two events. All of them with 50 ticks (Data Series). I'm in debug mode and debugging with Visual Studio.

                          Example 1: BarsType

                          //protected override void OnStateChange()
                          //{
                          //if (State == State.SetDefaults)
                          //{
                          Name = Custom.Resource.NinjaScriptBarsTypeTick;
                          BarsPeriod = new BarsPeriod { BarsPeriodType = BarsPeriodType.Tick };
                          BuiltFrom = BarsPeriodType.Tick;
                          DaysToLoad = 3;
                          IsIntraday = true;
                          IsTimeBased = false;
                          //}
                          //else if (State == State.Configure)
                          //{
                          Name = string.Format(Core.Globals.GeneralOptions.CurrentC ulture, Custom.Resource.DataBarsTypeTick, BarsPeriod.Value, (BarsPeriod.MarketDataType != MarketDataType.Last ? string.Format(" - {0}", Core.Globals.ToLocalizedObject(BarsPeriod.MarketDa taType, Core.Globals.GeneralOptions.CurrentUICulture)) : string.Empty));

                          Properties.Remove(Properties.Find("BaseBarsPeriodT ype", true));
                          Properties.Remove(Properties.Find("BaseBarsPeriodV alue", true));
                          Properties.Remove(Properties.Find("PointAndFigureP riceType", true));
                          Properties.Remove(Properties.Find("ReversalType", true));
                          Properties.Remove(Properties.Find("Value2", true));
                          //}
                          //}

                          //protected override void OnDataPoint(Bars bars, double open, double high, double low, double close, DateTime time, long volume, bool isBar, double bid, double ask)

                          -->> Each event is Tick by Tick


                          Example 2: Strategies

                          //protected override void OnStateChange()
                          //{
                          //if (State == State.SetDefaults)
                          //{
                          Name = "N3";
                          Calculate = Calculate.OnEachTick;
                          EntriesPerDirection = 1;
                          EntryHandling = EntryHandling.AllEntries;
                          IsExitOnSessionCloseStrategy = true;
                          ExitOnSessionCloseSeconds = 30;
                          IsFillLimitOnTouch = false;
                          MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
                          OrderFillResolution = OrderFillResolution.High;
                          Slippage = 0;
                          StartBehavior = StartBehavior.WaitUntilFlat;
                          TimeInForce = TimeInForce.Gtc;
                          TraceOrders = false;
                          RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
                          StopTargetHandling = StopTargetHandling.PerEntryExecution;
                          BarsRequiredToTrade = 5;
                          // Disable this property for performance gains in Strategy Analyzer optimizations
                          // See the Help Guide for additional information
                          IsInstantiatedOnEachOptimizationIteration = true;
                          //}
                          //else if (State == State.Configure)
                          //{
                          //Calculate = Calculate.OnPriceChange;
                          //}
                          //else if (State == State.Historical)
                          //{
                          //Calculate = Calculate.OnPriceChange;
                          //}
                          //}

                          //protected override void OnBarUpdate()
                          //{

                          -->> Each event is bar close.

                          //}

                          Where is the problem?

                          Thank you

                          Comment


                            #14
                            Hello Kate,

                            Excuse me for the inconvenience. I'm checking with Market Replay the next two events. All of them with 50 ticks (Data Series). I'm in debug mode and debugging with Visual Studio.

                            Example 1: BarsType

                            //protected override void OnStateChange()
                            //{
                            //if (State == State.SetDefaults)
                            //{
                            Name = Custom.Resource.NinjaScriptBarsTypeTick;
                            BarsPeriod = new BarsPeriod { BarsPeriodType = BarsPeriodType.Tick };
                            BuiltFrom = BarsPeriodType.Tick;
                            DaysToLoad = 3;
                            IsIntraday = true;
                            IsTimeBased = false;
                            //}
                            //else if (State == State.Configure)
                            //{
                            Name = string.Format(Core.Globals.GeneralOptions.CurrentC ulture, Custom.Resource.DataBarsTypeTick, BarsPeriod.Value, (BarsPeriod.MarketDataType != MarketDataType.Last ? string.Format(" - {0}", Core.Globals.ToLocalizedObject(BarsPeriod.MarketDa taType, Core.Globals.GeneralOptions.CurrentUICulture)) : string.Empty));

                            Properties.Remove(Properties.Find("BaseBarsPeriodT ype", true));
                            Properties.Remove(Properties.Find("BaseBarsPeriodV alue", true));
                            Properties.Remove(Properties.Find("PointAndFigureP riceType", true));
                            Properties.Remove(Properties.Find("ReversalType", true));
                            Properties.Remove(Properties.Find("Value2", true));
                            //}
                            //}

                            //protected override void OnDataPoint(Bars bars, double open, double high, double low, double close, DateTime time, long volume, bool isBar, double bid, double ask)

                            -->> Each event is Tick by Tick


                            Example 2: Strategies

                            ///protected override void OnStateChange()
                            //{
                            //if (State == State.SetDefaults)
                            //{
                            Name = "N3";
                            Calculate = Calculate.OnEachTick;
                            //}
                            //else if (State == State.Configure)
                            //{
                            //Calculate = Calculate.OnPriceChange;
                            //}
                            //}

                            //protected override void OnBarUpdate()
                            //{

                            -->> Each event is bar close.

                            //}

                            Where is the problem?

                            Thank you

                            Comment


                              #15
                              Example 2: Strategies

                              ///protected override void OnStateChange()
                              //{
                              //if (State == State.SetDefaults)
                              //{
                              Name = "N3";
                              Calculate = Calculate.OnEachTick;
                              //}
                              //else if (State == State.Configure)
                              //{
                              //Calculate = Calculate.OnEachTick;
                              //}
                              //}

                              //protected override void OnBarUpdate()
                              //{

                              -->> Each event is bar close.

                              //}

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by michi08, 10-05-2018, 09:31 AM
                              2 responses
                              739 views
                              0 likes
                              Last Post Denver_Wayne  
                              Started by sightcareclickhere, Today, 01:55 PM
                              0 responses
                              1 view
                              0 likes
                              Last Post sightcareclickhere  
                              Started by Mindset, 05-06-2023, 09:03 PM
                              9 responses
                              258 views
                              0 likes
                              Last Post ender_wiggum  
                              Started by Mizzouman1, Today, 07:35 AM
                              4 responses
                              18 views
                              0 likes
                              Last Post Mizzouman1  
                              Started by philmg, Today, 01:17 PM
                              1 response
                              8 views
                              0 likes
                              Last Post NinjaTrader_ChristopherJ  
                              Working...
                              X