I didn't use my own Indicator() in my Strategy(), I added a print() at the beginning of the OnBarUpdate() in order to verify OnBarUpdate() is called when each bar is updated. Is there anything I have missed? Thanks.
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
How to trigger OnBarUpdate() to be called in Strategy()?
Collapse
X
-
How to trigger OnBarUpdate() to be called in Strategy()?
I had no problem to let OnBarUpdate() be called in Indicator(), but it looks like it is not called in Strategy(). Why?
I didn't use my own Indicator() in my Strategy(), I added a print() at the beginning of the OnBarUpdate() in order to verify OnBarUpdate() is called when each bar is updated. Is there anything I have missed? Thanks.Tags: None
-
Hi localappleseed, thanks for your post.
You can call the Update() method in any other method in your script to force the OnBarUpdate method to be called. OnBarUpdate should automatically be called for every bar on the chart once you enable the strategy (only if Calculate = OnBarClose). Can you give a test script that is not doing this?
I look forward to hearing from you.
-
Hi, Chris, thanks for quick reply.
Here is the short script to verify OnBarUpdate() to be called.
namespace NinjaTrader.NinjaScript.Strategies
{
public class SampleLuke : Strategy
{
private string atmStrategyId = string.Empty;
private string orderId = string.Empty;
private bool isAtmStrategyCreated = false;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = NinjaTrader.Custom.Resource.NinjaScriptStrategyDes criptionSampleATMStrategy;
Name = NinjaTrader.Custom.Resource.NinjaScriptStrategyNam eSampleATMStrategy;
// This strategy has been designed to take advantage of performance gains in Strategy Analyzer optimizations
// See the Help Guide for additional information
IsInstantiatedOnEachOptimizationIteration = false;
}
}
protected override void OnBarUpdate()
{
// Compares the primary bar's low price to the 5-minute bar's low price
Print("11111");
}
protected override void OnMarketDepth(MarketDepthEventArgs marketDepthUpdate)
{
// Print some data to the Output window
Print("The m22222");
}
}
}
Comment
-
Here is how I use Strategy().
From a active Chart, right click to select Strategies -> In Strategies Window, select Calculate = On Bar Close -> Nothing printed from Output Window.
Comment
-
Hi, Chris,
I tried two methods you suggested, so far no luck.
1) I added Update() in this method:
protected override void OnMarketDepth(MarketDepthEventArgs marketDepthUpdate)
{
Print("The m22222");
Update();
}
2) I tried to set Calculate = OnBarClose, (this is done only in GUI setup), also I tried to initialize it using code "CalculateOnBarClose=false",
None of them are working. CalculateOnBarClose is not defined in NT8. I saw it was used in NT7.
Thanks.
Comment
-
I am sure the data feed is working, I tried to use Indicator(0 which works as expected. It has output printed.
Comment
-
I compiled and applied your Test Strategy, still not working. So it looks like I didn't configure correctly.
Comment
-
-
Hello, I am trying to do something like this too. I want to force OnBarUpdate() to be called in my Strategy.
I have a button on the chart, and in the OnButtonClick(), I addded a call to
Update();
But I have a log first line in OnBarUpdate() and it does not print after clicking the button.
My feed is currently connected. I want to be able to have my button do something even on the weekend with no ticks, and that something happens in OnBarUpdate(). I tried to call it directly from OnButtonClick(), but that is a different thread and does not seem to work, so I need to do something in that function that will cause OnBarUpdate() to be called.
Last edited by DerkWehler; 12-01-2024, 12:03 AM.
Comment
-
I don't think you will ever get OnBarUpdate() to execute when noOriginally posted by DerkWehler View PostI want to be able to have my button do something even on the weekend with no ticks, and that something happens in OnBarUpdate(). I tried to call it directly from OnButtonClick(), but that is a different thread and does not seem to work, so I need to do something in that function that will cause OnBarUpdate() to be called.
bars have closed, ie, meaning no data series require updating.
You want OnBarUpdate() to run with State.Realtime on weekends
with no ticks? That is absolutely something that should not happen.
Note that calling Update() does not always 'force' OnBarUpdate()
to run either. Re-read the documentation very carefully.
If you want to execute something from your OnButtonClick(), then
you need to solve your 'runs in a different thread' problem. There
are NinjaScript examples that exist to help you with that. Solving
that issue sounds like your best solution.
Just my 2˘.

Comment
-
Your link was just to a google search. I looked at several, but they are not about my dilemma. Do you know of any specific examples that should how to make something happen OnButtonClick which lies in the normal program thread?
Comment
-
The proper way to perform asynchronous work would be to use a dispatcher,
such as Dispatcher.InvokeAsync(), as mentioned on the page here.
I'm suggesting you try that approach inside your OnButtonClick().
Something like this,
Code:private void OnButtonClick(object sender, RoutedEventArgs rea) { Dispatcher.InvokeAsync((() => { .... your code goes in here .... })); }
Comment
-
Hello DerkWehler,
If the logic block contains any code using a series, call TriggerCustomEvent() first to ensure all series are updated.
Join the official NinjaScript Developer Community for comprehensive resources, documentation, and community support. Build custom indicators and automated strategies for the NinjaTrader platforms with our extensive guides and APIs.
Chelsea B.NinjaTrader Customer Service
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by NullPointStrategies, Today, 05:17 AM
|
0 responses
39 views
0 likes
|
Last Post
|
||
|
Started by argusthome, 03-08-2026, 10:06 AM
|
0 responses
124 views
0 likes
|
Last Post
by argusthome
03-08-2026, 10:06 AM
|
||
|
Started by NabilKhattabi, 03-06-2026, 11:18 AM
|
0 responses
64 views
0 likes
|
Last Post
|
||
|
Started by Deep42, 03-06-2026, 12:28 AM
|
0 responses
41 views
0 likes
|
Last Post
by Deep42
03-06-2026, 12:28 AM
|
||
|
Started by TheRealMorford, 03-05-2026, 06:15 PM
|
0 responses
46 views
0 likes
|
Last Post
|

Comment