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=5965BertrandNinjaTrader Customer Service
-
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:
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); }
BertrandNinjaTrader Customer Service
Comment
-
Bertrand,
I am expecting it to work realtime. Using the sample code
Code:TriggerCustomEvent(MyCustomHandler, myTimer.Interval);
Code:No overload for method 'TriggerCustomEvent' takes '2' arguments
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...
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...BertrandNinjaTrader Customer Service
Comment
-
OK, I changed my code to:
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 quebequer, Yesterday, 06:51 AM
|
4 responses
30 views
0 likes
|
Last Post
![]()
by quebequer
Today, 01:16 AM
|
||
Started by briendaluy, Yesterday, 11:38 PM
|
0 responses
7 views
0 likes
|
Last Post
![]()
by briendaluy
Yesterday, 11:38 PM
|
||
Started by RicharLoc, Yesterday, 11:31 PM
|
0 responses
8 views
0 likes
|
Last Post
![]()
by RicharLoc
Yesterday, 11:31 PM
|
||
Started by MiCe1999, 04-23-2025, 09:26 PM
|
4 responses
58 views
0 likes
|
Last Post
![]()
by MiCe1999
Yesterday, 09:17 PM
|
||
Started by linneon, Yesterday, 09:04 PM
|
0 responses
12 views
0 likes
|
Last Post
![]()
by linneon
Yesterday, 09:04 PM
|
Comment