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.
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
last tick price?
Collapse
X
-
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.Tags: None
-
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.
Code:[FONT=Courier New]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]); } }[/FONT]Jessica P.NinjaTrader Customer Service
-
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
-
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.
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.So is there a way to reference last price (regardless of if bar is still open)?
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.And again, what does Close[-1] refer to in real time?
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 Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
558 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
324 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
101 views
0 likes
|
Last Post
by Mindset
02-09-2026, 11:44 AM
|
||
|
Started by Geovanny Suaza, 02-02-2026, 12:30 PM
|
0 responses
545 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
547 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment