• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

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 zweistein, 11-14-2017, 03:24 PM
        2 responses
        1,476 views
        0 likes
        Last Post woodie1
        by woodie1
         
        Started by Newtrader101, Today, 04:50 AM
        3 responses
        18 views
        0 likes
        Last Post NinjaTrader_ChrisL  
        Started by utilizator, Today, 06:31 AM
        3 responses
        13 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by digibob, Today, 01:13 AM
        3 responses
        14 views
        0 likes
        Last Post digibob
        by digibob
         
        Started by calebsandfort, Yesterday, 05:16 PM
        2 responses
        11 views
        0 likes
        Last Post NinjaTrader_PaulH  
        Working...
        X