//Run this every 5 minutes
void TimerElapsed(object sender, ElapsedEventArgs e)
{
FetchDataAsync();
}
protected override void OnStateChange()
{
Print(State);
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Indicator here.";
Name = "TestProj";
Calculate = Calculate.OnPriceChange;
IsOverlay = false;
DisplayInDataBox = true;
DrawOnPricePanel = false;
DrawHorizontalGridLines = true;
DrawVerticalGridLines = true;
PaintPriceMarkers = true;
ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
IsSuspendedWhileInactive = false;
AddPlot(new Stroke(Brushes.Black), PlotStyle.Bar, "Plot1");
}
else if (State == State.Configure){
FetchDataAsync(); //fetch the data the first time then timer takes over every 5mins
System.Timers.Timer t = new System.Timers.Timer();
t.Interval = 50000; // Every 5 mins
t.AutoReset = true;
t.Elapsed += new ElapsedEventHandler(TimerElapsed);
t.Start();
}
}
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Fetching Data from Indicator
Collapse
X
-
Fetching Data from Indicator
My Indicator fetches some data from a web resource every 5 minutes. I have created a simple timer that calls an Asynchronous task. My question is, I don't know where to dispose of the timer or write this so it automatically disposes. Here is my Code:
What event can I use to call t.Dispose() when the user removes the indicator. If I don't, the timer keeps running even though the Indicator is removed. Alternatively, is there another way to write this so that the timer is automatically disposed of at the right time?Code:Last edited by swcooke; 01-04-2020, 11:27 PM.Tags: None
-
Hello swcooke,
bltdavid is giving a good suggestion.
Below is a link to an example of timer.
https://ninjatrader.com/support/forum/forum/ninjatrader-8/indicator-development/95391-nt8-sometimes-runs-onstatechange-termination-during-onbarupdate?p=777185#post777185Chelsea B.NinjaTrader Customer Service
- Likes 1
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by CarlTrading, 03-31-2026, 09:41 PM
|
1 response
81 views
1 like
|
Last Post
|
||
|
Started by CarlTrading, 04-01-2026, 02:41 AM
|
0 responses
42 views
0 likes
|
Last Post
by CarlTrading
04-01-2026, 02:41 AM
|
||
|
Started by CaptainJack, 03-31-2026, 11:44 PM
|
0 responses
64 views
2 likes
|
Last Post
by CaptainJack
03-31-2026, 11:44 PM
|
||
|
Started by CarlTrading, 03-30-2026, 11:51 AM
|
0 responses
68 views
0 likes
|
Last Post
by CarlTrading
03-30-2026, 11:51 AM
|
||
|
Started by CarlTrading, 03-30-2026, 11:48 AM
|
0 responses
55 views
0 likes
|
Last Post
by CarlTrading
03-30-2026, 11:48 AM
|

Comment