Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Entering positions on multiple instruments isn't working

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

    Entering positions on multiple instruments isn't working

    Greetings,
    My "Primary Bars" object is a "SPY" 5-minute. Below I have the following code in my strategy:

    if (State == State.Configure)
    {
    AddDataSeries("AMD", BarsPeriodType.Minute, 5);
    }

    OnBarUpdate()
    {
    if (BarsInProgress == 0)
    {
    if (Bars.IsFirstBarOfSession)
    EnterLong();
    }
    if (BarsInProgress == 1)
    {
    if (Bars.IsFirstBarOfSession)
    EnterLong();
    }​
    }

    I'm referencing the "Multi-Time Frame & Instruments" tutorial but for some reason, the strategy enters into the "SPY" position and never enters into the "AMD".

    Thanks

    #2
    You need to use the Unmanaged Approach if you want to open positions on multiple instruments.

    SubmitOrderUnmanaged(int selectedBarsInProgress, OrderAction orderAction, OrderType orderType, int quantity)

    This is just one of the various versions of this method, where selectedBarsInProgress will be 0 in order to open a position on "SPY" and 1 in order to open a position on "AMD".

    Comment


      #3
      Hello finalfantasy2,

      Thank you for your post.

      It's not necessary to use the Unmanaged Approach, you can just use the following syntax to submit the orders to a specific Bars object:

      EnterLong(int barsInProgressIndex, int quantity, string signalName)



      As jg626 mentioned, BarsInProgress index 0 is your primary data series (SPY) and BarsInProgress index 1 is your secondary series (AMD).

      Please let me know if this doesn't guide you in the right direction.

      Comment


        #4
        Thank you NinjaTrader_Gaby and jg626 ​ for your responses.
        I had already tried various combinations of the "Multi-Time Frame & Instruments" examples before I asked the question, but went ahead and tried them again to see if I was missing something:

        if (BarsInProgress == 0)
        {
        if (Bars.IsFirstBarOfSession)
        EnterLong(0, 100, "BUY SPY");

        }
        if (BarsInProgress == 1)
        {
        if (Bars.IsFirstBarOfSession)
        EnterLong(1, 100, "BUY AMD");

        }

        AND:

        if (BarsInProgress == 0)
        {
        if (Bars.IsFirstBarOfSession)
        {
        EnterLong(0, 100, "BUY SPY");
        EnterLong(1, 100, "BUY AMD");
        }

        }

        AND FINALLY:

        if (BarsInProgress == 1)
        {
        if (Bars.IsFirstBarOfSession)
        {
        EnterLong(0, 100, "BUY SPY");
        EnterLong(1, 100, "BUY AMD");
        }

        }

        However, with each of those tests, it only executes on the "SPY" symbol (see "SPY_ONLY.jpg"). However, when I comment out the "EnterLong(0, 100, "BUY SPY");" from any of those tests, it executes on the "AMD" symbol (see "AMD_ONLY.jpg"). So, no matter what I did, trying the examples under the "Multi-Time Frame & Instruments: Entering, Exiting and Retrieving Position Information" just doesn't work for me. However, when I tried jg626's advice of using the Unmanaged approach, it worked perfectly (see "SPY_AMD.jpg"):

        if (State == State.SetDefaults)
        {
        // Use Unmanaged order methods
        IsUnmanaged = true;

        }​

        if (BarsInProgress == 0)
        {
        if (Bars.IsFirstBarOfSession)
        {
        SubmitOrderUnmanaged(0, OrderAction.Buy, NinjaTrader.Cbi.OrderType.Market, 100, 0, 0, null, "BUY SPY");
        SubmitOrderUnmanaged(1, OrderAction.Buy, NinjaTrader.Cbi.OrderType.Market, 100, 0, 0, null, "BUY AMD");​
        }


        }

        ​So thank you both for your guidance. (Also NinjaTrader_Gaby, if you want to explore this further I'd be happy to do so in order to see if the problem is with my local environment or a larger NinjaTrader 8 issue. All in all, I want to help ensure that the documentation and execution environment are consistent)
        Attached Files

        Comment


          #5
          Hello finalfantasy2,

          To understand why the script is behaving as it is, such as placing orders or not placing orders or drawing objects when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.


          In the strategy add prints (outside of any conditions) that print the date time of the bar and all values compared in every condition that places an order.

          The prints should include the time of the bar and should print all values from all variables and all hard coded values in all conditions that must evaluate as true for this action to be triggered. It is very important to include a text label for each value and for each comparison operator in the print to understand what is being compared in the condition sets.

          The debugging print output should clearly show what the condition is, what time the conditions are being compared, all values being compared, and how they are being compared.

          Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

          Further, enable TraceOrders which will let us know if any orders are being ignored and not being submitted when the condition to place the orders is evaluating as true.

          After enabling TraceOrders remove the instance of the strategy from the Configured list in the Strategies window and add a new instance of the strategy from the Available list.


          I am happy to assist you with analyzing the output from the output window.


          Run or backtest the script and when the output from the output window appears save this by right-clicking the output window and selecting Save As... -> give the output file a name and save -> then attach the output text file to your reply.


          Below is a link to a support article that demonstrates using informative prints to understand behavior and includes a link to a video recorded using the Strategy Builder to add prints.

          https://support.ninjatrader.com/s/ar...nd-TraceOrders

          Comment


            #6
            Thank you NinjaTrader_Gaby and jg626 for your responses. I found the root cause of the problem and am a little embarrassed how easy it was to resolve it. All I had to do is to increase the "Entry per direction" option and everything worked perfectly.

            I again want to thank you both.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by NullPointStrategies, Yesterday, 05:17 AM
            0 responses
            59 views
            0 likes
            Last Post NullPointStrategies  
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            134 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            75 views
            0 likes
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            45 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Started by TheRealMorford, 03-05-2026, 06:15 PM
            0 responses
            50 views
            0 likes
            Last Post TheRealMorford  
            Working...
            X