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

Help on Multi Instrument Strategy

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

    Help on Multi Instrument Strategy

    Hi all, trying to do a very simple strategy where data from instrument 2 gets used as condition for entry on instrument 1. But cant seem to get it to work.

    Strategy: I am on an MSFT chart and want to go long if 1) close price of MSFT is > 20 SMA and 2) second instrument (^VIX) closing price is < 50 SMA of the second instrument (VIX).

    My Code is as follows but not sure where the error is or how to fix it even after I look at the error I get when trying to compile ( CS0021 error).
    Help would be appreciated!

    protected override void Initialize()
    {
    // Add Instrument ^VIX daily Bars object to the strategy
    Add("^VIX", PeriodType.Day, 1);
    CalculateOnBarClose = true;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    // Condition set 1: chart instrument close > chart instrument 20 SMA and close instrument 2 (VIX) is below 50 SMA instrument 2 1 period ago.
    if (Close[0] > SMA(20)[0]
    && Close[1][0] < SMA(BarsArray[1],50)[1] )
    {
    EnterLong(DefaultQuantity, "");
    }

    #2
    Hello alfonso,

    Thank you for your post.

    You're running into trouble because you're using multiple data series, but you're trying to access the price data incorrectly for a multi-series script. Since there's more than one series, to get the correct Close value, we have to specify both the series and the bar index using Closes instead of Close, like below:

    Code:
                if (Closes[0][0] > SMA(20)[0] && Closes[1][0] < SMA(BarsArray[1],50)[1])
    I would also recommend adding a current bar check to make sure you don't run into errors from trying to process on the added data series. Add this at the beginning of OnBarUpdate() to avoid that:

    Code:
    If (BarsInProgress != 0)
         return;
    For more information, I suggest checking out the section of this page of our help guide titled "Accessing the Price Data in a Multi-Bars NinjaScript".



    Please let us know if we may be of further assistance to you.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Hello Kate,
      Thank you for the fast reply. the above did not seem to work for me, however found a work around that seems to use the multi instrument part fine.
      However, the issue that appears now is that when I run the strategy, it seems to enter positions in the second instrument (^VIX) which is not intended as I only want to be long the main instrument.
      Any ideas why its doing that?

      the code I am using is as follows:

      protected override void OnBarUpdate()
      {


      // Condition set 1: entry
      if (Close[0] > SMA(20)[0]
      && EMA(BarsArray[1],2)[0] < SMA(BarsArray[1],20)[1])
      {
      EnterLong(DefaultQuantity, "");
      }


      // Condition exit 1: exit
      if (Close[0] < SMA(20)[0])
      {
      ExitLong("", "");
      }
      }


      Appreciate any guidance as I am a bit puzzled as to why it enters VIX positions...

      Comment


        #4
        sorry forgot the first part of the code. which is

        protected override void Initialize()
        {
        // Add an MSFT 1 minute Bars object to the strategy
        Add("^VIX", PeriodType.Day, 1);
        CalculateOnBarClose = true;
        }

        Comment


          #5
          Hi Kate,
          Managed to fix it already looking at multiinstrument link you sent above.
          Thanks.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by CortexZenUSA, Today, 12:53 AM
          0 responses
          1 view
          0 likes
          Last Post CortexZenUSA  
          Started by CortexZenUSA, Today, 12:46 AM
          0 responses
          1 view
          0 likes
          Last Post CortexZenUSA  
          Started by usazencortex, Today, 12:43 AM
          0 responses
          5 views
          0 likes
          Last Post usazencortex  
          Started by sidlercom80, 10-28-2023, 08:49 AM
          168 responses
          2,265 views
          0 likes
          Last Post sidlercom80  
          Started by Barry Milan, Yesterday, 10:35 PM
          3 responses
          11 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Working...
          X