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

Historical vs Live and IsFirstTickOfBar logic

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

    Historical vs Live and IsFirstTickOfBar logic

    Hello,

    I understand how the IsFirstTickOfBar works on live data...

    My plan is to use "Calculate on each tick" in every indicator, but really calculate only on the first tick of the bar in order to get previous close...

    Like "Close[1]"...

    However, how will historical bars be treated when I have condition "IsFirstTickOfBar" but current state is historical?

    Do I need to write a new logic in order to treat historical bars correctly?

    All I really want is to calculate on BarClose, but being able to run indicator "OnEachTick" in live data (and OnBarClose when historical state or running through strategy analyzer which is same).

    Thanks

    #2
    I think I found answer to my question based on testing and following article:



    Seems that the best way to separate both real-time and historical logic is by putting two if statements - one for real time (then I need to use "Close[1]"), and one for historical (then I will need to use "Close[0]"), and so IsFirstTickOfBar will always be "TRUE" when state is historical...

    Please confirm if I am correct, and if that is the best practice to separate both logics... Disadvantage - is I need to pretty much "Duplicate" code... unless maybe I use some offset of "1" and will set based on the current state, and so if state is "RealTime" - then I will need to add "1" offset, if state is "HIstorical" - then add a zero offset...

    protected override void OnBarUpdate()
    {
    //if (State != State.Realtime)
    // return;

    ++counter;

    if (State == State.Realtime && IsFirstTickOfBar)
    {
    Draw.Dot(this, "dot" + CurrentBar.ToString() + counter.ToString(), true, 0,
    High[0] + counter * TickSize, Brushes.Orange);
    Print(string.Format("Time:{0}, High:{1}, Low:{2}", Time[1], High[1], Low[1]));
    }

    if (State == State.Historical && IsFirstTickOfBar)
    {
    Draw.Dot(this, "dot" + CurrentBar.ToString() + counter.ToString(), true, 0,
    High[0] + counter * TickSize, Brushes.Orange);
    Print(string.Format("Time:{0}, High:{1}, Low:{2}", Time[0], High[0], Low[0]));
    }

    if (IsFirstTickOfBar)
    counter = 0;

    }

    Thanks

    Comment


      #3
      Hello music_p13,

      In historical data TickReplay must be enabled for Calculate to work with .OnPriceChange or .OnEachTick.

      With TickReplay disabled, IsFirstTickOfBar will always be true in historical data.

      With TickReplay enabled and Calculate set to .OnPriceChange or .OnEachTick, IsFirstTickOfBar will only be true on the first tick of a bar.

      Below is a link to a forum post with details and links to the help guide.
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        Hello music_p13,

        Unfortunately, Renko bars do not work with TickReplay.

        From the help guide:
        "Note: The system bar types "Line Break" and "Renko" cannot be used with Tick Replay and as a result, the Tick Replay option will be disabled when configured with those bar types. There may be other 3rd party bar types which may also disable Tick Replay by design. If you are a developer, please see the property IsRemoveLastBarSupported for more information"


        This is because RemoveLastBar() is used with this script.
        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by bmartz, 03-12-2024, 06:12 AM
        5 responses
        32 views
        0 likes
        Last Post NinjaTrader_Zachary  
        Started by Aviram Y, Today, 05:29 AM
        4 responses
        13 views
        0 likes
        Last Post Aviram Y  
        Started by algospoke, 04-17-2024, 06:40 PM
        3 responses
        28 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by gentlebenthebear, Today, 01:30 AM
        1 response
        8 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by cls71, Today, 04:45 AM
        1 response
        8 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Working...
        X