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

Problem in Adding Multi-Instrument

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

    Problem in Adding Multi-Instrument

    Please kindly help. Someone

    Scratching my head for the whole day what I am doing wrong
    I add QQQ as a secondary instrument

    protected override void Initialize()
    CalculateOnBarClose = false;
    Add("QQQ", PeriodType.Day, 1);
    Add("QQQ", PeriodType.Day, 3);

    But when I use the code below in OnBarUpdate()
    Print(Time[0]);
    Print(Opens[1][0]);
    Print(Lows[1][1]);
    Print(Highs[1][1]);

    It returns 1 day old value for Opens[1][0] rather than for current day.

    In back testing, it returns 129.89 as open on march 23, but that was actually open for march 22

    In your expert opinion, what could I be doing wrong? Please kindly give your guidance on how to access the day open value of QQQ (added as a secondary instrument) at the time of market open in ninjascript at 9:30 est



    ////////////////////////////////////////////////////


    the code is below:
    protected override void OnBarUpdate()
    {
    if (Bars.FirstBarOfSession && FirstTickOfBar)
    {
    BarNumber = Bars.BarsSinceSession;
    QQQFirstBarofSession = 0;
    }

    if (BarsInProgress ==1 && Bars.BarsSinceSession >= BarNumber && QQQFirstBarofSession == 0 )

    {
    Print(Time[0]);
    Print(Opens[1][0]);
    Print(Lows[1][1]);
    QQQFirstBarofSession = 1;
    }

    #2
    Hi sanjeev74,

    Can you also include the output from this code along with a screenshot of the chart showing the open for that day?

    I created a video as this code by itself would cause an error and I wanted to explain why I had to modify the code and correct a logic error to be able to run this.
    I am not able to reproduce the behavior.
    Free online storage and sharing with Screencast.com. 2 GB of storage and 2 GB of bandwidth per month for free. We won't compress, alter or take ownership of your content.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks

      Hi NinjaTrader_ChelseaB
      You are awesome!
      Such a fantastic reply, with video.. wow.. thank you so so so much! you are awesome, grateful for your help. Thank you again very much.


      PS-

      - Per your request, please find the code below, To test, i created two long orders (Line 70 and 76 of the code) - I wanted to get orders to execute as soon as market opens, so I set COBC to false

      - the first long order should be executed at 9:30:00, but rather, it seems to be getting executed at 9:31:00 (for AAPL, on March 9th, it was executed at 138.37 in backtest using two entries per direction)

      - the second long order should also be executed at 9:30:00, but it seems to be getting executed at 9:32:00. the condition for this second order to execute is if the SPY is near the low of previous day, or the low of the day before that. (for AAPL, on March 9th, it was executed at 138.58 in backtest using two entries per direction)

      Please kindly help guide me on how to make these both order execute (as soon as market opens)- for example 9:30:00, or at most, seconds after that, instead of waiting till 9:31 or 9:32









      #region Using declarations
      using System;
      using System.ComponentModel;
      using System.Diagnostics;
      using System.Drawing;
      using System.Drawing.Drawing2D;
      using System.Xml.Serialization;
      using NinjaTrader.Cbi;
      using NinjaTrader.Data;
      using NinjaTrader.Indicator;
      using NinjaTrader.Gui.Chart;
      using NinjaTrader.Strategy;
      #endregion

      // This namespace holds all strategies and is required. Do not change it.
      namespace NinjaTrader.Strategy
      {
      /// <summary>
      /// Enter the description of your strategy here
      /// </summary>
      [Description("Enter the description of your strategy here")]
      public class testSPY : Strategy
      {
      #region Variables
      private int SystemCheck;
      #endregion

      /// <summary>
      /// This method is used to configure the strategy and is called once before any strategy method is called.
      /// </summary>
      protected override void Initialize()
      {
      Add(PriorDayOHLC());
      CalculateOnBarClose = false;
      BarsRequired = 0;
      TraceOrders = true;
      Add("SPY", PeriodType.Day, 1);
      Add("SPY", PeriodType.Day, 2);
      Add("SPY", PeriodType.Minute, 1);
      }

      /// <summary>
      /// Called on each bar update event (incoming tick)
      /// </summary>
      protected override void OnBarUpdate()
      {

      if (BarsInProgress != 0)
      return;


      if (Bars.FirstBarOfSession && FirstTickOfBar)
      {
      SystemCheck = 1;

      Print(CurrentDayOHL(BarsArray[3]).CurrentOpen[0]); //Debugging print statements
      Print(Lows[1][0]); //Debugging print statements
      Print(Lows[1][1]); //Debugging print statements

      if ((CurrentDayOHL(BarsArray[3]).CurrentOpen[0] >= (Lows[1][0]-0.15) && CurrentDayOHL(BarsArray[3]).CurrentOpen[0] <= (Lows[1][0]+0.15)) ||
      (CurrentDayOHL(BarsArray[3]).CurrentOpen[0] >= (Lows[1][1]-0.15) && CurrentDayOHL(BarsArray[3]).CurrentOpen[0] <= (Lows[1][1]+0.15)))
      {
      SystemCheck = 0;
      }
      }


      if (ToTime(Time[0])>=093000)
      {
      EnterLong();
      Print(Times[0][0]); //Debugging print statements
      }

      if (ToTime(Time[0])>=093000 && SystemCheck == 0)
      {
      EnterLong();
      Print(Times[0][0]); //Debugging print statements
      }

      }
      #region Properties
      #endregion
      }
      }
      Attached Files
      Last edited by sanjeev74; 03-28-2017, 12:15 PM.

      Comment


        #4
        Cobc

        Just to summarize this question

        Does COBC = false still work if there is a multi-instrument added?

        In example below

        even though COBC = false, and I am trading primary instrument [0] -- the order executes at end of bar at 9:31:00, instead of executing in the middle of bar, for example 9:30:15

        Thanks again

        Comment


          #5
          Hello sanjeev74

          To export a complete NinjaTrader 8 NinjaScript, so this may be easily shared, do the following:
          1. Click Tools -> Export -> NinjaScript...
          2. Click the 'add' link -> check the box(es) for the script(s) you want to include
          3. Click the 'Export' button
          4. Enter a unique name for the file in the value for 'File name:'
          5. Choose a save location -> click Save
          6. Click OK to clear the export location message

          By default your exported file will be in the following location:
          • (My) Documents/NinjaTrader 8/bin/Custom/ExportNinjaScript/<export_file_name.zip>


          Below is a link to the help guide on Exporting NinjaScripts.


          Yes, multi-time frame scripts work fine with CalculateOnBarClose set to false.

          Are you only testing real-time data?

          Do you have 1 tick granularity added to your script?

          CalculateOnBarClose is always true when in historical data or backtest.
          From the help guide:
          "When indicators or strategies are running on historical data, OnBarUpdate() is only called on the close of each historical bar even if this property is set to false. This is due to the fact that with a historical data set, only the OHLCVT of the bar is known and not each tick that made up the bar."


          There is no check for Historical in this script so I am not able to tell if you are asking about real-time or historical data.


          If this is Historical data (which loads on the chart before real-time data starts evaluating) then it is expected that orders would be placed after a bar closes and would appear on the next bar as the order is filled after the next bar has opened.

          If 1 tick intra-bar granularity is added I would expect actions (such as orders being placed) to occur mid-bar.

          If the script is processing real-time data and all historical data has been processed and the Historical bool is false, I would expect actions to occur mid-bar.
          Chelsea B.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by philmg, Today, 01:17 PM
          0 responses
          2 views
          0 likes
          Last Post philmg
          by philmg
           
          Started by cre8able, Today, 01:01 PM
          1 response
          4 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by manitshah915, Today, 12:59 PM
          1 response
          3 views
          0 likes
          Last Post NinjaTrader_Erick  
          Started by ursavent, Today, 12:54 PM
          1 response
          4 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Started by Mizzouman1, Today, 07:35 AM
          3 responses
          17 views
          0 likes
          Last Post NinjaTrader_Gaby  
          Working...
          X