• 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

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".

    https://ninjatrader.com/support/help...nstruments.htm

    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 cirzonorkecv, Yesterday, 11:52 PM
          0 responses
          6 views
          0 likes
          Last Post cirzonorkecv  
          Started by rickyblah12, Yesterday, 01:01 PM
          1 response
          14 views
          0 likes
          Last Post Erwin Beckers  
          Started by dadarara, Yesterday, 12:18 PM
          0 responses
          8 views
          0 likes
          Last Post dadarara  
          Started by joemiller, Yesterday, 07:33 AM
          2 responses
          13 views
          0 likes
          Last Post joemiller  
          Started by TheGameOfProbabilities, 07-18-2019, 06:56 AM
          7 responses
          38 views
          0 likes
          Last Post DaveS
          by DaveS
           
          Working...
          X