Thanks
Announcement
Collapse
Looking for a User App or Add-On built by the NinjaTrader community?
Visit NinjaTrader EcoSystem and our free User App Share!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less
Partner 728x90
Collapse
NinjaTrader
Is there a function called after all instruments in a strategy?
Collapse
X
-
Is there a function called after all instruments in a strategy?
Hi, I am looking for a way to do some hand-crafted summary of strategy results. If I run a strategy over an instrument list, it seems that for each instrument, there is a separate strategy object. Is that so? Is there a function (hook) which I can override which is called when all instruments in a list are processed by a strategy?
ThanksTags: None
-
Hi ivannp,
You are correct, when backtesting over an instrument list, each test will be a separate independent instance and they cannot communicate with each other.
Your option is to add all the instruments that you would like to a single script as additional dataseries and then backtest over a single instrument.
It is not supported to do so, but it is possible to add instruments to a script by an instrument list.
Below is some sample code that can help with this. (Again this is unsupported code)
Code:NinjaTrader.Cbi.InstrumentList list = NinjaTrader.Cbi.InstrumentList.GetObject("Custom"); if (list != null) { foreach (Instrument i in list.Instruments) { //Print(i.FullName); Add(i.FullName, PeriodType.Day, 1); } } else { //Log("AddInstrumentsByListExample: There is no instrument list named Custom", LogLevel.Information); }
Chelsea B.NinjaTrader Customer Service
-
Thanks, can you add a request for similar single Start method? The Start should be called prior to running the strategy over any of the instruments and the Stop should be called after all instruments were processed.
Not sure whether these could be part of the Strategy itself (i.e. static methods), but user functions or any other form of "hooks" should suffice.
Thanks again
Comment
-
Hello ivannp,
Again, this would be possible if you add the multiple dataseries to a single script. The OnStartUp() and OnTermination() would do what you are asking.
Also, the summary and statistics would be merged as well like you are asking.
Merging the performance results of multiple backtests into one large summary and statistic report does sound feasible to implement without any further explanation as they can be merged after the data is produced.
However, how would you suggest strategies be structured so that it would be possible to run code before running several backtests and then after as well. Would strategies be broken into three separate files? A pre-backtest .cs file and a post-backtest .cs file?Chelsea B.NinjaTrader Customer Service
Comment
-
I haven't tried yet to add multiple instruments to a single script, but if I go that path, am I not going to lose the ability to simply select a few instruments from the strategy analyzer GUI and firing off the backtest? I also suspect that the execution is going to be running on a single core, while I have the feeling that multiple instruments are run in parallel.
What I am currently working on is a backtest over about 30 continuous future contracts, daily data. Once the run finishes, I'd like to be able to extract trading data (buy/sell orders for the next day). First I tried writing to a file, but that's how I discovered that the various instruments are run in paralell - I'd get random file errors. Everything cleared up once I added a static Mutex to my strategy and serialized the file operations. That seems to work. Wouldn't something like that work for a global start/stop inside NT as well - (a static mutex + variable to sync all threads at the start and to drain them at the end and two static methods in the strategy?) although I am not sure how assemblies, global mutexes and other low level stuff operates in C#.
The problem that I am trying to solve, is that at the end of each instrument, I can store the stats I need into a file, or a database. However, I don't know which is the last thread finishing and I don't think it's easy to figure out without some non-deterministic hacks. If I new for instance how many instruments the strategy is being run for - then a simple atomic counter will tell me which one is the last, but until then - my only choice is to run a separate program at the end to summarize the results. Hopefully in NT 8 at least I will be able to integrate this "final" program better in NT ...
Comment
-
Hi ivannp,
If you script was coded to do it, it would be possible to select any instrument and then make an input to select the instrument list you would like to backtest over. You could have that ignore the primary series if you wanted and just backtest the selected list. Currently, you can choose a single instrument to backtest over or an instrument list to backtest over. It is not possible to individually select multiple instruments to backtest over.
You are correct, when backtesting (and only when backtesting) instances of backtests are run in their own thread in parallel.
Is your suggestion a special method that is run preceeding a backtest and a special method that is run after all of the backtests have completed?Chelsea B.NinjaTrader Customer Service
Comment
-
Originally posted by NinjaTrader_ChelseaB View PostHi ivannp,
Is your suggestion a special method that is run preceeding a backtest and a special method that is run after all of the backtests have completed?
Hmm, I am starting to think that I can actually implement that even now: What if I add a counter, protected by a global mutex to count the threads (each thread increasing the counter in its own StarUp method). I can repeat the process within the termination routine, with the last thread performing the summary tasks. That should actually work. I will give it a try later this week (or next week). Will update the thread again.
Comment
-
Hello ivannp,
I wanted to let you know that your suggestion has been entered into our tracking system and is being tracked with ID #2822.
Thank you for your great suggestion!
Please do not hesitate to contact us for any other NinjaTrader inquiries you may have.Chelsea B.NinjaTrader Customer Service
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by adeelshahzad, Today, 11:49 AM
|
2 responses
3 views
0 likes
|
Last Post
by adeelshahzad
Today, 12:15 PM
|
||
Started by spottysallrite, Today, 11:31 AM
|
1 response
5 views
0 likes
|
Last Post Today, 11:43 AM | ||
Started by johnMoss, 03-04-2024, 01:53 PM
|
2 responses
32 views
0 likes
|
Last Post
by Overnhou
Today, 11:21 AM
|
||
Started by Padan, Today, 02:02 AM
|
5 responses
17 views
0 likes
|
Last Post Today, 11:14 AM | ||
Started by Lele2k24, Yesterday, 11:24 AM
|
8 responses
29 views
0 likes
|
Last Post
by Lele2k24
Today, 11:00 AM
|
Comment