Is it possible to run my strategy every x seconds instead of OnBarUpdate(), OnExecution() or whatever else? If so, how would I code it?
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Run script every x seconds
Collapse
X
-
Welcome to our forums, you would need to work with a custom timer event then - http://www.ninjatrader-support2.com/...ead.php?t=5965
-
Hello again,
I'm trying to implement Timer in my strategy, but it doesn't work. How do I know? There's print instruction in the code, but no output is visible. Let me show you the important parts of my strategy:
As you can see, the code is very simple, therefore I must miss something basic here. I would be very grateful for your help.Code:using System.Windows.Forms; namespace NinjaTrader.Strategy { public class st : Strategy { private System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer(); protected override void Initialize() { now = DateTime.Now; } protected override void OnBarUpdate() { // Initiate our Timer object with an interval of 1000ms (1 second) myTimer.Tick += new EventHandler(TimerEventProcessor); myTimer.Interval = 1000; myTimer.Start(); } private void TimerEventProcessor(Object myObject, EventArgs myEventArgs) { /* Important to use the TriggerCustomEvent() to ensure that NinjaScript indexes and pointers are correctly set. Do not process your code here. Process your code in the MyCustomHandler method. */ TriggerCustomEvent(MyCustomHandler, 0, myTimer.Interval); } private void MyCustomHandler(object state) { // Informs us of when the event was triggered and of our Timer settings Print("\tTime: " + DateTime.Now); Print("\tTimer Interval: " + state.ToString() + "ms"); public override void Dispose() { // Make sure you include base.Dispose() whenever you override the Dispose() method base.Dispose(); // Cleans up our Timer object myTimer.Dispose(); } } }Last edited by alkamid; 02-23-2010, 06:46 AM.
Comment
-
alkamid, any errors in the log tab? Are you expecting this to work in backtesting or realtime use?
Have you tried for example with this call as in our reference sample?
Code:private void TimerEventProcessor(Object myObject, EventArgs myEventArgs) { /* Important to use the TriggerCustomEvent() to ensure that NinjaScript indexes and pointers are correctly set. Do not process your code here. Process your code in the MyCustomHandler method. */ TriggerCustomEvent(MyCustomHandler, myTimer.Interval); }
Comment
-
Bertrand,
I am expecting it to work realtime. Using the sample code
produces error:Code:TriggerCustomEvent(MyCustomHandler, myTimer.Interval);
In the log I found:Code:No overload for method 'TriggerCustomEvent' takes '2' arguments
Why is that? I thought the OnBarUpdate() function should only start the timer once and then let it work until the strategy is turned off. Instead, the strategy seems to execute myTimer.Start() many times on the first bar update.Code:Error on triggering custom event for strategy 'nt': More than 100 subsequent user events
Last edited by alkamid; 02-25-2010, 02:16 AM.
Comment
-
Hi alkamid, thanks for the strategy you had the correct overload in place, it needs to be the 3 parameter one...
You also want to initiate your timer on the first OnBarUpdate() bar, like in the sample ... i.e. -Code:TriggerCustomEvent(CustomEvent customEvent, int barsInProgress, object state)
if (CurrentBar == 0)
{
initiate timer
}
This should take care of the too many events in the log entry...
Comment
-
OK, I changed my code to:
Now there's no error in logs but I can't see any output either. Is there anything particular I have to put in Initialize() to make it work?Code:protected override void OnBarUpdate() { if(CurrentBar == 0) { // Initiate our Timer object with an interval of 1000ms (1 second) myTimer.Tick += new EventHandler(TimerEventProcessor); myTimer.Interval = 10000; myTimer.Start(); } } private void TimerEventProcessor(Object myObject, EventArgs myEventArgs) { /* Important to use the TriggerCustomEvent() to ensure that NinjaScript indexes and pointers are correctly set. Do not process your code here. Process your code in the MyCustomHandler method. */ TriggerCustomEvent(MyCustomHandler, BarsInProgress, myTimer.Interval); }
Comment
-
alkamid,
Here is the deal. Since you are using a strategy and not an indicator you cannot use if (CurrentBar == 0) since the strategy doesn't process anything until BarsRequired is met. Instead you should do
if (CurrentBar == 20) or if (CurrentBar == BarsRequired). Default BarsRequired is 20.Josh P.NinjaTrader Customer Service
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by argusthome, 03-08-2026, 10:06 AM
|
0 responses
111 views
0 likes
|
Last Post
by argusthome
03-08-2026, 10:06 AM
|
||
|
Started by NabilKhattabi, 03-06-2026, 11:18 AM
|
0 responses
60 views
0 likes
|
Last Post
|
||
|
Started by Deep42, 03-06-2026, 12:28 AM
|
0 responses
38 views
0 likes
|
Last Post
by Deep42
03-06-2026, 12:28 AM
|
||
|
Started by TheRealMorford, 03-05-2026, 06:15 PM
|
0 responses
43 views
0 likes
|
Last Post
|
||
|
Started by Mindset, 02-28-2026, 06:16 AM
|
0 responses
79 views
0 likes
|
Last Post
by Mindset
02-28-2026, 06:16 AM
|

Comment