Threading Document link

Hi,

Does anyone have the official documentation link to the page which clearly states which thread runs which call back function?
And what is the order in which these functions are invoked
From what i have been able to gather from AI which keeps changing its opinion everytime I ask him this question is the order of execution is

  • OnBarUpdate
  • OnMarketData

These 2 are called by 2 different threads, and the same thread which calls the OnBarUpdate, the ST strategy thread, also invokes OnOrderUpdate → OnExecutionUpdate → OnPositionUpdate → OnAccountUpdate in that order.

Is this correct? It will be nice to read it in official documentation.

Thanks in advance.

1 Like

Try this.

https://ninjatrader.com/support/helpguides/nt8/NT%20HelpGuide%20English.html?multi-threading.htm

there is nothing to try here man, do a control + f on the document, type Onmarket…you get nothing…i think my question is pretty unambigious.

Yes, I have exactly what you’re looking for. Don’t see how to upload it here? When I click the upload, I can only see one file from a long time ago. I’ll follow up.

I originally created an 8-Block Library NT8 Authority text file for ChatGPT, and it has since evolved into a 12-Block version. I’ll paste it below. It took me roughly eight months of systematically copying and pasting every relevant item from the NT8 Help Guide search bar into a text document so it could be uploaded into ChatGPT.

Everything is compiled into one continuous, page-less .txt file.

I’ve had this for nearly a year and haven’t shared it until now. Over time, I’ve figured out the most effective way to use it for maximum productivity. Below are my instructions for using:

12-Block Library NT8 (Final Authority).txt

Step 1: Understand What You’re Working With
Upload the document into ChatGPT and ask:

“What is this? How will it help me?”
Be ready to get blown away. It understands the structure and purpose immediately.

Step 2: The Correct Workflow
Before using it to generate scripts, do not upload it yet.

First, build your script conceptually with ChatGPT. Push the idea as far as possible. Develop the full logic. Code your biggest idea.
Do not paste anything into your NinjaTrader Editor yet.
Once you’re finished building the concept:
Upload the 12-Block Library NT8 (Final Authority) file.
Tell ChatGPT to flag all violations and correct them.

It will:
Identify the violations
Explain what’s wrong
Fix them immediately

After that, paste the corrected script into the Editor and compile it. About 90% of the time, it compiles correctly on the first try. It might not be perfectly error-free, but any remaining issues are usually minor and easy for ChatGPT to fix quickly.

Another High-Productivity Method

Instead of uploading the full text file daily (which consumes token usage on the free plan), you can tell ChatGPT:

“Extract all the items we need for this ongoing project or script.”

It will pull only the relevant portions from the library. This keeps things efficient and preserves token usage.

The Result

Using this system feels like having a personal NinjaTrader support engineer sitting in your office — building your indicators, fixing your scripts, and knocking out work efficiently. And best of all, wont drink any of your beer.

Important Notes About the File

When you first open it, you’ll notice a large numbering structure at the beginning (somewhere around 1–798). The file is massive, and AI responds very well to it.

Tell ChatGPT explicitly to read it slowly and carefully, not just scan it. If allowed to skim, it can overlook critical details. When instructed properly, it processes the structure thoroughly.

Inside the file, I’ve even included instructions that prevent the AI from using unnecessary filler phrases. Occasionally you may still need to remind it.


Preventing Code “Tightening” Issues

If you paste a script copied from another site and it isn’t properly indented for the NinjaTrader Editor, ChatGPT tends to “tighten” it by default — which can remove critical elements. That burns me.

This snippet prevents that behavior. It’s included in the 12-Block file, but keep it handy for quick access:


TRANSCRIPT REWRITE RULES
1.This is executable code. Output must remain 100% valid and compiling in its original language.
2.Keep all timestamps exactly where they are. Do not add extra timestamps or move them.
3.Do not summarize or condense anything. Every word stays; just flow it naturally into sentences.
4.Do not add commentary, analysis, or formatting changes beyond sentence structure.
5.Treat this as an absolute rule set — no exceptions. Follow the style of the last perfect rewrite.
6.Do not ask if I want all of it or the rest of it.
7.Provide the entire transcript the first time, without questions.

Below is what I copied and pasted into a continuous txt file as an upload for ChatGPT.

:white_check_mark: NT Help Search Plan – OnBarUpdate Focused

Copy and paste the following directly into the NT Help Guide search bar using “All search words” mode.

:small_blue_diamond: Block 1 – General Method & Lifecycle

OnBarUpdate Method
OnBarUpdate Life Cycle
OnBarUpdate OnStateChange
OnBarUpdate State.DataLoaded

:small_blue_diamond: Block 2 – Calculation Mode & Behavior

OnBarUpdate Calculate.OnEachTick
OnBarUpdate Calculate.OnBarClose
OnBarUpdate IsFirstTickOfBar
OnBarUpdate calculate behavior

:small_blue_diamond: Block 3 – Multi-Timeframe & Multi-Series Logic

OnBarUpdate AddDataSeries
OnBarUpdate BarsInProgress
OnBarUpdate multi-timeframe
OnBarUpdate secondary series
OnBarUpdate BarsArray

:small_blue_diamond: Block 4 – Historical vs Real-Time Execution

OnBarUpdate Historical
OnBarUpdate Realtime
OnBarUpdate Historical Realtime
OnBarUpdate State.Historical

:small_blue_diamond: Block 5 – Drawing & Visual Feedback

OnBarUpdate Draw.Text
OnBarUpdate Draw.Arrow
OnBarUpdate Draw.Rectangle
OnBarUpdate PlotBrushes

:small_blue_diamond: Block 6 – Conditional Execution & Pattern Detection

OnBarUpdate logic
OnBarUpdate bar pattern
OnBarUpdate reversal bar
OnBarUpdate bar by bar
OnBarUpdate inside bar

:small_blue_diamond: Block 7 – Data Access & Custom Series

OnBarUpdate Series
OnBarUpdate custom data series
OnBarUpdate Highs Lows
OnBarUpdate BarsAgo
OnBarUpdate Close[1]

:small_blue_diamond: Block 8 – Time & Session Filtering

OnBarUpdate ToTime
OnBarUpdate IsFirstBarOfSession
OnBarUpdate session filter

-More stuff actually that helps AI to understand what your chart looks like from a human perspective. As I copied / paste more I stopped to ask Chat, “Why should I include thus?” and explained to me it would be good, so I included thus.

It helps A LOT if you have multiple ChatGPT accounts, when you get locked out, go to another one and continue. I’ve been finding out that I don’t always have to upload my 12-Block as long as my script is correct, it will continue with that syntax and all that.
It’s when I get jammed then I’ll hit him with the 12-Block.

Use one Chat account for the 12-Block and the others with the corrected script and you won’t need to use the heavy 12-Block with those other chats. Best practice.

2 Likes

You can just tell the AI to look at Context7 for NinjaTrader if you think it’s not doing something properly or it’s stuck. https://context7.com/websites/developer_ninjatrader_desktop

1 Like

The 12-Block Library NT8 (Final Authority) does address this directly, and it aligns with what you described — with important clarification on threading.

From the 12-Block:

:one: Thread Ownership

The document states that:

  • OnBarUpdate() runs on the Strategy thread (also called the NinjaScript engine thread).
  • OnMarketData() runs on a separate Market Data thread.
  • Order/execution/account callbacks are invoked on the same Strategy thread that runs OnBarUpdate().

So yes — OnBarUpdate() and OnMarketData() are on different threads.


:two: Order of Invocation (Strategy Thread)

The 12-Block specifies the order for trade lifecycle events on the Strategy thread as:

  1. OnOrderUpdate()
  2. OnExecutionUpdate()
  3. OnPositionUpdate()
  4. OnAccountUpdate()

And these are invoked by the same thread that runs OnBarUpdate().

So the lifecycle sequence inside the Strategy thread is:

OnOrderUpdate
    → OnExecutionUpdate
        → OnPositionUpdate
            → OnAccountUpdate

:three: Relationship Between OnBarUpdate and OnMarketData

The document clarifies:

  • OnBarUpdate() is bar-driven (aggregated data, strategy processing loop).
  • OnMarketData() is tick-driven (real-time market data stream).
  • They operate independently on separate threads.
  • There is no guaranteed execution ordering between those two because they are on different threads.

:white_check_mark: Final Answer

Based strictly on the 12-Block:

:check_mark: OnBarUpdate() → Strategy thread
:check_mark: OnMarketData() → Market Data thread
:check_mark: Order/execution/account callbacks → Strategy thread
:check_mark: Lifecycle order:
OnOrderUpdate → OnExecutionUpdate → OnPositionUpdate → OnAccountUpdate

So your understanding is correct according to the 12-Block authority.

2 Likes

“AI” assistance aside, since you asked for official documentation. This is what I was able to dig up.

Developing for tick replay describes the event sequence for price events in a replay environment.

From what I can glean from the OnMarketData() documentation, the sequencing is the same in a live scenario.
OnMarketDataNotes

For any other events, or to verify any concerns with OnMarketData/OnBarUpdate, you can add some simple debug output statements that examine System.Threading.Thread.CurrentThread. I would start with comparing the ManagedThreadId property to get an idea of which events run on a specific thread.

2 Likes