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!
See more
See less

Partner 728x90

Collapse

Multiple Strategy Instances Timing

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    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
    This issue that I'm getting is that after a sell of another symbol has occurred returning it's monies to the account immediately 2-3 other strategy instances attempt to make a buy at the same time. So say $200 has been returned then 3 other instances sees the 200 and attempt to buy but with a combined total of $600. If the account only has say $205 in it then obviously the account doesn't have the total funds needed so I get errors like the one below.
    Order rejected - reason: CASH AVAILABLE: 25.29; CASH NEEDED FOR THIS ORDER AND OTHER PENDING ORDERS: 207.27
    So this leads me to ask how exactly are the strategy instances run?
    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?

    #2
    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?
    We have some information that talks about how strategies are managed, and yes, threads are used. NinjaScripts will generally use their own thread, but the components they use may run in different threads like market data and rendering.

    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?
    The components you assign strategies to receive their data from a BarsRequest and get the data from your provider. If you have multiple connections, you can set preferred data providers to receive the data you wish to have your strategies use. When trades are sent to accounts, they are sent to the appropriate provider in which the account exists.

    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?
    It sounds like the issue is that you are calculating your sharesToBuy and this value is not longer valid when you submit the order.

    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.
    JimNinjaTrader Customer Service

    Comment


      #3
      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>

      Comment


        #4
        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.
        JimNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by r68cervera, Today, 05:29 AM
        0 responses
        3 views
        0 likes
        Last Post r68cervera  
        Started by geddyisodin, Today, 05:20 AM
        0 responses
        6 views
        0 likes
        Last Post geddyisodin  
        Started by JonesJoker, 04-22-2024, 12:23 PM
        6 responses
        35 views
        0 likes
        Last Post JonesJoker  
        Started by GussJ, 03-04-2020, 03:11 PM
        12 responses
        3,241 views
        0 likes
        Last Post Leafcutter  
        Started by AveryFlynn, Today, 04:57 AM
        0 responses
        7 views
        0 likes
        Last Post AveryFlynn  
        Working...
        X