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

last tick price?

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

    last tick price?

    When CalculateOnBarClose is set to true, is it possible to still reference the last tick price in the code?

    Could that be accomplished by Close[-1]? In historical tests Close[-1] gives the price of the next bar, but I suspect it may be what I'm looking for in real time.

    #2
    Hello YD777, and thank you for your question.

    The situation you are in, where your strategy only needs to update on a bar close but needs access to last tick prices, is ideal for the multi time frame approach. I am including some documentation on this approach and an example. Please let us know if there are any other ways we can help.

    http://ninjatrader.com/support/helpG...nstruments.htm

    Code:
    protected override void Initialize()
    {
        Add(PeriodType.Tick, 1);
    }
    
    protected override void OnBarUpdate()
    {
    
        // Checks to ensure all Bars objects contain enough bars before beginning
        if (CurrentBars[0] <= BarsRequired || CurrentBars[1] <= BarsRequired)
        {
            return;
        }
     
        if (BarsInProgress == 0)
        {
            Print("The closing price of the previous tick is " + Closes[1][1]);
        }
    }
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Hi Jessica, My situation is more complicated than that. It's an indicator and I don't always need last tick price, just in some conditions. I'm also trying to learn Ninjascript.

      So is there a way to reference last price (regardless of if bar is still open)? And again, what does Close[-1] refer to in real time?

      Comment


        #4
        Hello YD777, and thank you for your further questions.

        The short answer to both your questions, is that bars in Ninja are ordered most-recent-to-least. Close[100] means the closing price 100 bars ago, Close[0] means the current closing price.

        Since you mentioned trying to learn scripting, I can provide a more complete answer.

        So is there a way to reference last price (regardless of if bar is still open)?
        Yes. Close[1] will be the closing price of the previous bar. If you need the previous tick price, however, you will need a tick series to draw from, building off the example I provided. I am happy to further clarify the example and what I mean if you have any further questions.

        And again, what does Close[-1] refer to in real time?
        Conceptually, since bars are ordered most-to-least recent, with Close[0] referring to the most recent close price, Close[-1] would refer to the closing price of the currently building bar. While this would exist in historical data, because this refers to a future event in live data, providing this would make Ninja inconsistent between historical and live environments and likely lead to programming errors.

        Literally speaking, you will get a compile time error if you attempt to write this into code. The explanation is a bit complex, but in the interest of completeness, variables in computer programming languages like C# are easy ways to refer to locations in your computer's memory. In older languages like C, you could actually print memory addresses from variable handles, so "Close" might be a way to refer to memory address 12345678. When we say something like Close[3] , we mean "find the memory address of Close and increase it by the storage size of 3 numbers". Building off our example, if it took 4 bytes to store a number, then this would be address 12345690. Close[-1] then, would be 1 number before close in memory (address 12345674). Memory in a programming language has to be reserved ahead of time so that nothing else can use it. If, say, Close could have 4 numbers in it, then you would only be allowed to use addresses 12345678 through 12345694 through the word "Close", and the memory located at Close[-1], 12345674, is not memory that is allowed to be used through the Close handle.

        What this means is that we can not provide Close[-1], both for practical and conceptual reasons.
        Jessica P.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by stafe, Today, 12:34 PM
        2 responses
        20 views
        0 likes
        Last Post stafe
        by stafe
         
        Started by asmmbillah, Today, 11:18 AM
        1 response
        18 views
        0 likes
        Last Post NinjaTrader_ChrisL  
        Started by nickse2003, Today, 09:56 AM
        1 response
        28 views
        0 likes
        Last Post NinjaTrader_ChristopherJ  
        Started by ATMtrader0001, Today, 10:08 AM
        5 responses
        39 views
        0 likes
        Last Post ATMtrader0001  
        Started by WHICKED, Today, 09:47 AM
        1 response
        26 views
        0 likes
        Last Post NinjaTrader_Spencer  
        Working...
        X