Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Enter on Last Bar and Exit on Bid Bar

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

    Enter on Last Bar and Exit on Bid Bar

    Hello,

    I am trying to get my entries to happen on 5 minute Last bars and my exits to happen (and plot) on 1 tick Bid bars. The purpose of this is so I can see exactly which Bid tick caused my exit to happen.

    I followed NT7's instructions on how to do multi time frame entries/exits (shown here http://ninjatrader.com/support/forum...ead.php?t=5787) the best I could
    but for some reason I can only get the exits to happen (and plot) on the 5 minute last bars and not the 1 tick Bid bars.

    The code I wrote to test this out is below (Its a simple MACD strategy). Any idea what I did wrong?

    (FYI: You'll see I did not add anything within the code to plot the bars on the chart. Instead I just manually added a 5 min Last bar series and 1 tick Bid bar series to the chart before adding the strategy to the chart and running it in replay mode).

    Also, a zip file of the code is attached in case anyone prefers importing it into NT7 to view.

    Thanks!

    Code:
    #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
    
    namespace NinjaTrader.Strategy
    {
    
        [Description("Enter the description of your strategy here")]
        public class MACDCROSS : Strategy
        {
            #region Variables
            #endregion
    
            protected override void Initialize()
            {
    			// Add a 5 minute Bars object to the strategy
    			Add("ES 03-16",PeriodType.Minute, 5, MarketDataType.Last);
    			
    			// Add a 1 tick Bid bars to the strategy
    			Add("ES 03-16",PeriodType.Tick, 1, MarketDataType.Bid);
    			
                CalculateOnBarClose = true;
            }
    
            protected override void OnBarUpdate()
            {
    			if (BarsInProgress == 0)
                {
                    if (CrossBelow(MACD(12, 26, 9).Avg, MACD(12, 26, 9), 1))
                    {
                        EnterLong(1, "");
                    }
    			}	
    			if (BarsInProgress == 1)
    			{
             
                    if (Position.GetProfitLoss(Close[0], PerformanceUnit.Points) >= 2)
                    {
                         ExitLong("", "");
                    }
       
                    if (Position.GetProfitLoss(Close[0], PerformanceUnit.Points) <= -2)
                    {
                    ExitLong("", "");
                    }
    			}
            }
    
            #region Properties
            #endregion
        }
    }
    Attached Files

    #2
    Hello Matheyas5,

    The order will plot on the chart at the price the order was executed at whether or not this is within the bars.

    If you place an order while BarsInProgress == 1, that order will be submitted while that series is processing. If BarsInProgress 1 is a tick series then OnBarUpdate will trigger on every bid tick.

    How do you know that the order is not being submitted after a bid tick is received? Do you have prints in your code that print the time when the order is submitted and this is not the time of that tick?

    Are you expecting a second set of bars to appear on chart by adding the data series? This won't happen. Adding a dataseries allows the script to use the series but this will not cause another set of bars to appear on the chart.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the response Chelsea.

      How do you know that the order is not being submitted after a bid tick is received? Do you have prints in your code that print the time when the order is submitted and this is not the time of that tick?

      If you look in the "time" column in my attached screenshot you will see the exits times are all in 5 minute intervals (which means the exits are only happening on 5 minute bars).

      If the exits were happening as soon as the bid tick was received, the Exit times in the screenshot would be at random times (such as 5:43:22, 1:47:48 Etc) between the 5 minute intervals.
      Attached Files

      Comment


        #4
        Hello Matheyas5,

        I can see that the exits are not happening exactly at 5 minutes but are happening a random amount of seconds after 5 minutes.

        5:15:05 - this is 5 seconds after 5 minutes
        3:45:01 - 1 second after 5 minutes
        2:50:01 - 1 second after 5 minutes

        There is no possible way to have these fills a few seconds after the primary bar close if they are filling on the 5 minute primary series with historical data. They will fill at the exact time of the bar close if this is historical. So this proves that the orders are filling on the secondary series.

        This could happen if the conditions that check bars in progress are nested. However, in the code you have posted, this does not appear to be the case.

        Are you certain that the code you have posted is the code you are running?
        Can you provide an export of the strategy?

        To export a NinjaTrader 7 NinjaScript do the following:
        1. Click File -> Utilities -> Export NinjaScript
        2. Enter a unique name for the file in the value for 'File name:'
        3. Select the strategy from the objects list on the left -> click the right facing arrow ">" to add the strategy to the export
        4. Click the 'Export' button -> click 'yes' to add any referenced indicators to the export -> click OK to clear the export location message


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


        Below is a link to the help guide on Exporting NinjaScripts.
        http://www.ninjatrader.com/support/h...nt7/export.htm
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hi Chelsea,

          The exported ninjascript zip file is attached to post #1 of this thread.

          As a test to make sure I am running the right code, I just downloaded the zip file from post #1, imported it into ninjatrader and re-ran it (in Replay mode). The same problem still came up.


          Also, I did an additional test via manually adding 1 tick bid bars (shown in green squares) and 5 minute Last bars (show in red bars) as you will see in the attached screenshot 1.jpg. Notice that the arrow pointed to in blue shows a Bid tick happened at price 1923.00 before the exit happened at price 1922.25. The exit should have happened at price 1923 if the strategy was monitoring every bar.
          (FYI: Attached screenshot 2.jpg shows this 1922.25 did happen at time 9:40:am, which means there is no sort of graphical error in the chart in screenshot 1).


          Also, I am running the strategy in replay mode at 100x. could the 100x speed be causing the issue?
          Attached Files

          Comment


            #6
            Hello Matheyas5,

            I didn't notice at first that you have added a 5 minute series (BarsInProgress 1) and 1 tick bid series (BarsInProgress 2). Yet you are only allowing for the exit to happen when the 5 minute series is processing. The primary series of the chart is BarsInProgress 0, the added 5 minute series is BarsInProgress 1, and the 1 tick bid series is BarsInProgress 2.

            // Add a 5 minute Bars object to the strategy <- This will be BarsInProgress 1
            Add("ES 03-16",PeriodType.Minute, 5, MarketDataType.Last);

            // Add a 1 tick Bid bars to the strategy <- This will be BarsInProgress 2
            Add("ES 03-16",PeriodType.Tick, 1, MarketDataType.Bid);

            if (BarsInProgress == 1)
            {
            if (Position.GetProfitLoss(Close[0], PerformanceUnit.Points) >= 2)
            {
            ExitLong("", "");
            }


            You are only allowing the strategy to exit when the added 5 minute series is processing. Thus your orders will exit at 5 minute intervals.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              That explains it.

              In my original code shown below I had assumed Ninjatrader would consider the 5 minute Last bars were the primary series of the chart (AKA "BarsInProgress 0") and the 1 Tick bid bars as BarsInProgress 1.
              I didn't realize Ninjatrader would condsider the 5 minute last bars as "BarsInProgress 1" and the 1 Tick Bid bars as "BarsInProgress 2"

              Code:
                      protected override void Initialize()
                      {
              			// Add a 5 minute Bars object to the strategy
              			Add("ES 03-16",PeriodType.Minute, 5, MarketDataType.Last);
              			
              			// Add a 1 tick Bid bars to the strategy
              			Add("ES 03-16",PeriodType.Tick, 1, MarketDataType.Bid);
              Anyway, as shown below, I changed my original code from "if(BarsInProgress == 0)" and "if(BarsInProgress == 1)" to "if(BarsInProgress == 1)" and "if(BarsInProgress == 2)".

              It now works perfectly.

              protected override void OnBarUpdate()
              {
              if (BarsInProgress == 1)
              {
              if (CrossBelow(MACD(12, 26, 9).Avg, MACD(12, 26, 9), 1))
              {
              EnterLong(1, "");
              }
              }
              if (BarsInProgress == 2)
              {

              if (Position.GetProfitLoss(Close[0], PerformanceUnit.Points) >= 2)
              {
              ExitLong("", "");
              }

              if (Position.GetProfitLoss(Close[0], PerformanceUnit.Points) <= -2)
              {
              ExitLong("", "");
              }
              }
              However, this brings me to a related question.
              I manually added 1 tick Bid bars to show up on the chart before I ran this strategy in Replay mode. I can see in the execution Report the exits are happening at the right time/price, however the exits are visually plotting on the chart's primary data series (which I made as 6 minute bars for the sake of example) and not on the 1 tick bid bars.

              I figured the exits would plot on the 1 tick Bid bars because the 1 Tick bid bars are where my code is sending the exit orders.

              Is there anyway in NT7 to get these exits to plot on the 1 tick bid bars instead of the primary data series' bars? If its not possible in NT7, is it possible in NT8?

              Thanks
              Last edited by Matheyas5; 02-19-2016, 07:11 PM.

              Comment


                #8
                Hello Matheyas5,

                Calling Add() adds a series and increases the BarsArray length and BarsInProgress even if multiple added series are the same.

                The help guide does not say that calling Add() will be ignored if the added series matches an existing series.

                A script running on a chart is only attached to the primary series that the script is enabled on. Any executions will be plotted on the primary bars and will appear on the bar that was open when the order was filled and will show the marker at whatever price the order is filled at.

                Executions will not appear on any other data series that has been manually added to the chart as the script will not be aware that another series is even on the chart.

                So make your primary chart series a tick series and you would be able to see the fills on tick bars.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Same situation in NT8?

                  Comment


                    #10
                    Hello Matheyas5,

                    Yes, the behavior is the same in NinjaTrader 8.

                    You can test this by using AddDataSeries() and then printing BarsArray.Count to see how many series are added to the script.
                    Chelsea B.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by suroot, 04-10-2017, 02:18 AM
                    5 responses
                    3,021 views
                    0 likes
                    Last Post NinjaTrader_Gaby  
                    Started by Stanfillirenfro, Today, 07:23 AM
                    1 response
                    6 views
                    0 likes
                    Last Post NinjaTrader_Gaby  
                    Started by cmtjoancolmenero, Yesterday, 03:58 PM
                    2 responses
                    22 views
                    0 likes
                    Last Post cmtjoancolmenero  
                    Started by olisav57, Yesterday, 07:39 PM
                    1 response
                    9 views
                    0 likes
                    Last Post NinjaTrader_ChelseaB  
                    Started by cocoescala, 10-12-2018, 11:02 PM
                    7 responses
                    944 views
                    0 likes
                    Last Post Jquiroz1975  
                    Working...
                    X