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
Multiple Strategy Instances Timing
Collapse
X
-
Hello antrux,
Thanks for the reply.
You might want to consider creating an AddOn to handle your buying power calculations as opposed to writing to and from a file.
Our colleague Chelsea has a sample on our forums that may be of use to you. You can find this thread here: http://ninjatrader.com/support/forum...d.php?p=457104
A direct link to the AddOn as well: http://ninjatrader.com/support/forum...5&d=1461098445
This would be faster and would not have the same implications as having 450 instances that are trying to read from and write to the same file.
Please let me know if I may be of further assistance.
-
Well thank you Jim for the information however that does get a little too involved for what simple things I'm doing. Kinetick is already set to my Live and Historical preferred data provider and given that even if I used OnAccountItemUpdate then it would be the same code run across 450 symbols.
When one item sells and the funds are returned to the account in IB then OnAccountItemUpdate will trigger across all strategy instances and to me at least I could still see the same thing happening then. Maybe if I wrote another strategy to do the main account balance calcs then write it to a file each minute and IF the buy conditions are true of a running strategy then right before Order placement, lock that file, read the value, subtract (shares * avgPrice), rewrite that value to the file, flush, then unlock that file then maybe I could see it helping. However, I also see that greatly slowing things down with all the extra writing and etc not to mention how to handle the situation when the file is locked yet another instance wants access to it, ie. does it error out and stop or does it keep trying and etc.
So I moved the main account balance lines nearly 40 code lines closer to the actual EnterLong statement so this should shave off a few nanoseconds. Although IB is still an issue as during the order transmit if it doesn't subtract some value from buying power/cash balance then NT will still see that there is money available until the order is executed at the exchange, sent back to IB with the avgPrice info, and IB update it's values so NT may then see it. So in the end I'm not sure there is a full proof way around it other than setting the Affinity setting for NT8 to 1 so it only uses a single CPU Core and defeat all the great performance gains that it gave us.
As of now it's all just a simple error as IB will reject it so no harm no foul I guess. It's just that those pop ups interfere with other AutoHotKey scripts as I'm not sure yet how to detect if an error message has popped up or not yet. Just like when NT pushes a notification/popup saying a new version is out that always stops my auto startup routine and it has to be remotely managed too. I'll just be happy that so far this code makes any money at all. <grin>
Leave a comment:
-
Hello antrux,
Thanks for opening the thread.
So this leads me to ask how exactly are the strategy instances run?
Do they run on separate threads from each other?
In depth explanations on multi-threading considerations and the lifecycle of NinjaScripts can be referenced from the help guide here:
Multi-threading considerations - http://ninjatrader.com/support/helpG...-threading.htm
Understanding NinjaScript LifeCycle - http://ninjatrader.com/support/helpG...fecycle_of.htm
I've heard that they simply run when data comes in so can data come in from Kinetick simultaneously across symbols or is it read sequentially?
Please see the section below for configuring NinjaTrader for preferred data feeds while having multiple connections.
Configuring Multiple Connections - http://ninjatrader.com/support/helpG...onnections.htm
If any of the strategies can run at the same time do you have any suggestions as to where I should be looking to try and prevent these errors?
Without the full code I am unable to provide much input for your other than speculation. I would suggest to work on the code used to calculate sharesToBuy. This should be recalculated before you submit an order so the value is correct.
I would also suggest to utilize OnAccountItemUpdate() to update variables that hold these values so you can recalculate your sharesToBuy with current criteria.
OnAccountItemUpdate() - http://ninjatrader.com/support/helpG...itemupdate.htm
Please let me know if you have any additional questions.
Leave a comment:
-
Multiple Strategy Instances Timing
Currently I'm running 1 strategy across 450 symbols so 450 instances in the Strategies Tab. The strategy calculates it's own SharesToBuy with the following code.
Code:currentCash = Account.Get(AccountItem.BuyingPower, Currency.UsDollar); currentCash = currentCash - accountMin; // Subtracts accountMin dollars from the accounts value to ensure enough money to cover broker fees. spendingCash = currentCash * (percentPerTrade * 0.01); // Stores X% of currentCash value as spendingCash if (spendingCash > perTradeLimit) spendingCash = perTradeLimit; // If spendingCash is more than perTradeLimit then limit spending to perTradeLimit. sharesToBuy = Convert.ToInt32(Math.Floor(spendingCash / Close[0])); //Calculates how many whole shares it can buy with spendingCash. EnterLong(sharesToBuy, ""); // Buy at Market sharesToBuy
Order rejected - reason: CASH AVAILABLE: 25.29; CASH NEEDED FOR THIS ORDER AND OTHER PENDING ORDERS: 207.27
Do they run on separate threads from each other?
I've heard that they simply run when data comes in so can data come in from Kinetick simultaneously across symbols or is it read sequentially?
If any of the strategies can run at the same time do you have any suggestions as to where I should be looking to try and prevent these errors?Tags: None
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by bmartz, Today, 09:30 AM
|
2 responses
11 views
0 likes
|
Last Post
by bltdavid
Today, 11:24 AM
|
||
Started by f.saeidi, Today, 11:02 AM
|
1 response
2 views
0 likes
|
Last Post Today, 11:08 AM | ||
Started by geotrades1, Today, 10:02 AM
|
4 responses
12 views
0 likes
|
Last Post
by geotrades1
Today, 11:08 AM
|
||
Started by rajendrasubedi2023, Today, 09:50 AM
|
3 responses
16 views
0 likes
|
Last Post Today, 10:57 AM | ||
Started by lorem, Today, 09:18 AM
|
2 responses
11 views
0 likes
|
Last Post Today, 10:55 AM |
Leave a comment: