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

Playback Connection Time Print Bar/Tick Time

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

    Playback Connection Time Print Bar/Tick Time

    I wrote some code that was working over the weekend but is NOT working right now

    Right now it prints the same time over and over and it is a Saturday of all things
    isPlayBack=True CurrentTick Time=4/17/2022 3:05:00 PM


    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 PlaybackVsLive : Indicator
    {
    private DateTime ThisBarTime;
    private DateTime PlaybackBarTime;
    
    private string BarTime;
    private string PlaybackTime;
    
    private bool isPlayBack=false;
    
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Indicator here.";
    Name = "PlaybackVsLive";
    Calculate = Calculate.OnPriceChange;
    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)
    {
    }
    else if (State == State.DataLoaded)
    {
    if (Connection.PlaybackConnection==null)
    {
    Print("--------------------------------------------------------------------------------------------------NOT Playback---------------------------------->");
    isPlayBack=false;
    }
    else
    {
    Print("--------------------------------------------------------------------------------------------------YES Playback==================================>");
    isPlayBack=true;
    }
    }
    }
    
    protected override void OnBarUpdate()
    {
    //Add your custom indicator logic here.
    
    
    if (isPlayBack)
    {
    BarTime = Bars.GetTime(0).ToString();
    }
    else
    {
    BarTime = DateTime.Now.ToString();
    }
    
    Print("isPlayBack="+isPlayBack+" CurrentTick Time="+BarTime);
    }
    }
    }
    
    #region NinjaScript generated code. Neither change nor remove.
    
    namespace NinjaTrader.NinjaScript.Indicators
    {
    public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
    {
    private PlaybackVsLive[] cachePlaybackVsLive;
    public PlaybackVsLive PlaybackVsLive()
    {
    return PlaybackVsLive(Input);
    }
    
    public PlaybackVsLive PlaybackVsLive(ISeries<double> input)
    {
    if (cachePlaybackVsLive != null)
    for (int idx = 0; idx < cachePlaybackVsLive.Length; idx++)
    if (cachePlaybackVsLive[idx] != null && cachePlaybackVsLive[idx].EqualsInput(input))
    return cachePlaybackVsLive[idx];
    return CacheIndicator<PlaybackVsLive>(new PlaybackVsLive(), input, ref cachePlaybackVsLive);
    }
    }
    }
    
    namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
    {
    public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
    {
    public Indicators.PlaybackVsLive PlaybackVsLive()
    {
    return indicator.PlaybackVsLive(Input);
    }
    
    public Indicators.PlaybackVsLive PlaybackVsLive(ISeries<double> input )
    {
    return indicator.PlaybackVsLive(input);
    }
    }
    }
    
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
    {
    public Indicators.PlaybackVsLive PlaybackVsLive()
    {
    return indicator.PlaybackVsLive(Input);
    }
    
    public Indicators.PlaybackVsLive PlaybackVsLive(ISeries<double> input )
    {
    return indicator.PlaybackVsLive(input);
    }
    }
    }
    
    #endregion

    #2
    Hello BartMan,

    Thank you for your post.

    So that I may try to replicate on my end, are you seeing the incorrect behavior when it is used on a regular chart, or when backtesting in Playback? On what instrument are you testing? Please provide the bar type and interval as well.

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

    Comment


      #3
      So that I may try to replicate on my end, are you seeing the incorrect behavior when it is used on a regular chart, or when backtesting in Playback?

      Regular chart code determines if isPlayBack or not

      Code:
      
      private bool isPlayBack=false;
      
      if (Connection.PlaybackConnection==null) { Print("--------------------------------------------------------------------------------------------------NOT Playback---------------------------------->"); isPlayBack=false; } else { Print("--------------------------------------------------------------------------------------------------YES Playback==================================>"); isPlayBack=true;
      On what instrument are you testing?
      • MNQ 06-22
      Please provide the bar type and interval as well.
      • 5Min

      Click image for larger version

Name:	PlayBack.PNG
Views:	169
Size:	68.8 KB
ID:	1199631Click image for larger version

Name:	PlayBack.PNG
Views:	164
Size:	68.8 KB
ID:	1199632
      Attached Files

      Comment


        #4
        Hello BartMan,

        Thank you for your reply.

        I'm not seeing the same on my end on a normal chart when connected to a live data provider - I see it printing as I would expect on a live chart. Please ensure that you are connected to a live data provider and not the playback connection, then open a new 5 minute MNQ chart and apply the indicator - do you see the same using a fresh chart?

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

        Comment


          #5
          Kate, thank you yes indeed it works in live data. My intent purpose of this code is to print the Bar/Tick Time during Playback. As I titled the post "Playback Connection Time Print Bar/Tick Time

          " that is my focus. And it worked on the weekend. I am using the Playback connector to find and resolve the errors in my code. I need the Time of the Tick where My code blows up. I do not want to write a try catch around every block of code but I cannot have a failed stop order and let my code run unprotected.

          The playback connector is one of the utmost useful Ninja features, once I can reliably get the tick time of the events I must handle; it will be even better.

          Comment


            #6
            Hello BartMan,

            Thank you for your reply.

            In looking at this again, you're getting the wrong time in playback because you're using 0 as the index each time, so it will always return the time of the first bar on the left of the chart. GetTime takes an absolute index, not a bars ago index.

            Try the following:

            Code:
            protected override void OnBarUpdate()
            {
            if (isPlayBack)
            {
            BarTime = Bars.GetTime(CurrentBar).ToString();
            }
            else
            {
            BarTime = DateTime.Now.ToString();
            }
            
            Print("isPlayBack="+isPlayBack+" CurrentTick Time="+BarTime);
            }
            Please let us know if we may be of further assistance to you.
            Kate W.NinjaTrader Customer Service

            Comment


              #7
              +1 Kate please mark issue as resolved/complete
              CurrentBar vs 0 resolved the issue!

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by PhillT, Today, 02:16 PM
              2 responses
              3 views
              0 likes
              Last Post PhillT
              by PhillT
               
              Started by Kaledus, Today, 01:29 PM
              3 responses
              9 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Started by frankthearm, Yesterday, 09:08 AM
              14 responses
              47 views
              0 likes
              Last Post NinjaTrader_Clayton  
              Started by gentlebenthebear, Today, 01:30 AM
              2 responses
              14 views
              0 likes
              Last Post gentlebenthebear  
              Started by PaulMohn, Today, 12:36 PM
              2 responses
              17 views
              0 likes
              Last Post PaulMohn  
              Working...
              X