How do I program, test, and run this in Ninjascript so I can test consolidated results and execute a large volume of entrys and exits?
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Large number of stocks
Collapse
X
-
Large number of stocks
I want to develop and run a strategy that evaluates entry criteria for a large volume of stocks (e.g. all S&P500 stocks) and places an order for all stocks that meet entry criteria and subsequently monitors exit criteria.
How do I program, test, and run this in Ninjascript so I can test consolidated results and execute a large volume of entrys and exits?Tags: None
-
Hello PaulZ,
Thank you for writing in. You would need to use a multi instrument/time frame strategy. For more information on this please make sure to read our help guide article in-depth here: http://ninjatrader.com/support/helpG...lightsub=multi
For each instrument that you add, OnBarUpdate() gets called an additional time (with Calculate On Bar Close set to true) or multiple more times (with Calculate On Bar Close set to false) per bar.
To begin you have the list of instruments you want to use. You need to add these using Add() in the Initialilze() method.
Then you need to determine what the entry criteria is and add that in the OnBarUpdate() method. If there are going to be different entries/exits per instrument you need to add a BarsInProgress check. For example: if(BarsInProgress == 0)
Then you need to add the exit criteria. This is usually done in either the OnBarUpdate() method or the OnExecution() method depending on how you want the strategy to work.
Once you have begun developing this, if you run into any issues please let me know and I can assist further. Also, if you have any questions about anything I have referred to in my post, please see the alphabetical NinjaScript help guide page here: http://ninjatrader.com/support/helpG..._reference.htm
Thank you in advance.Michael M.NinjaTrader Quality Assurance
-
Thanks for your response. I followed your guidance to program a multi instrument strategy with CalculateOnBarClose=true.
I programmed the following under OnBarUpdate():
if (Positions[BarsInProgress].MarketPosition==MarketPosition.Flat)
{
....some entry logic....
EnterLong();
}
I expected the strategy to take a long position for each relevant instrument where no position is open yet, irrespective of positions in other instruments. However, the strategy only goes long if no other position is open in my portfolio. What is the flaw in my logic/code?
Comment
-
Hello PaulZ,
Because you are working with multiple data series, you will want to use the correct overload for EnterLong():
http://ninjatrader.com/support/helpG.../enterlong.htmCode:EnterLong(int barsInProgressIndex, int quantity, string signalName)
You will also want to make sure your EntriesPerDirection variable is set to allow the correct number of entries at the same time. Here is a working example for reference:
Please let me know if I may be of further assistance.Code:protected override void Initialize() { CalculateOnBarClose = true; Add("TF 09-15", PeriodType.Second, 5); Add("NQ 09-15", PeriodType.Second, 5); EntriesPerDirection = 3; } protected override void OnBarUpdate() { if(Historical) return; if(Positions[BarsInProgress].MarketPosition == MarketPosition.Flat) { EnterLong(BarsInProgress, DefaultQuantity, BarsInProgress.ToString()); } }Michael M.NinjaTrader Quality Assurance
Comment
-
Thanks for your response. I corrected my code accordingly.
My next issue is the following: when backtesting multiple instruments with historical data sets of different lengths, my backtest strategy only triggers when each instrument has a Bar.
For example: Kinetick only gives Visa (V) data as of 2008 and more historical data for other Dow stocks. When I try to backtest a strategy with AXP, BA and V from 2000-2010, I only get trades starting in 2008, while AXP and BA data is available to trigger entries in earlier years. I would expect to see AXP and BA trades for 2000-2007, but these do not happen.
Can I change a setting so that the strategy starts executing trades for historical dates where at least one of the instruments has historical data to initiate a trade?
Comment
-
Hello PaulZ,
Unfortunately there is no way to affect this behavior. This is because calculations are performed between the data series' being backtested against, so there would be some potentially incorrect results if there were periods where one or more data series did not have the same data available as the other.
The best solution to this it to test the data series' that have the same amount of data together, and then compare that against a separate backtest for the data series that has less data. You can open multiple strategy analyzer windows, so it is not difficult to do this.
Please let me know if I may be of further assistance.Michael M.NinjaTrader Quality Assurance
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
582 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
338 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
103 views
0 likes
|
Last Post
by Mindset
02-09-2026, 11:44 AM
|
||
|
Started by Geovanny Suaza, 02-02-2026, 12:30 PM
|
0 responses
554 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
552 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment