• 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

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.
    https://www.screencast.com/t/1fxiUexL
    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.
          http://ninjatrader.com/support/helpG...-us/export.htm

          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."
          http://ninjatrader.com/support/helpG...onbarclose.htm

          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.
          http://ninjatrader.com/support/helpG...historical.htm

          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 superkolleech, Today, 12:46 AM
          1 response
          11 views
          0 likes
          Last Post MojoJojo  
          Started by davidwade, Today, 02:51 AM
          0 responses
          3 views
          0 likes
          Last Post davidwade  
          Started by aviat72, 04-26-2020, 09:41 PM
          10 responses
          92 views
          0 likes
          Last Post kissandfly  
          Started by healthooa, Today, 01:28 AM
          0 responses
          8 views
          0 likes
          Last Post healthooa  
          Started by harjinderjit, Yesterday, 08:24 PM
          0 responses
          20 views
          0 likes
          Last Post harjinderjit  
          Working...
          X