Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

BarsInProgress skips bar objects

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

    BarsInProgress skips bar objects

    Hello,

    I have encountered a problem with BarsInProgress. Certain bar objects are skipped, seemingly at random. The specific bar objects that are skipped are not consistent.
    The data provider is Gain Capital, although this persists if I just use "Kinetick - End of Day (free)". I'm running for the entire year of 2018 to keep tests consistent. NT version = 8.0.22.2 64-bit.

    First, a sample printout. Notice how it skips 31, 33, 34, 35, 36, 41, 50, 53, and 54.
    As a sanity check, I pulled up the one-minute chart for AUD/USD (Bar Array 31 here). The bar in question definitely exists; O = 0.73382, H = 0.73382, L = 0.73375, C = 0.73375.

    12/4/2018 2:32:00 PM BarsInProgress: 29
    12/4/2018 2:32:00 PM BarsInProgress: 30
    12/4/2018 2:32:00 PM BarsInProgress: 32
    12/4/2018 2:32:00 PM BarsInProgress: 37
    12/4/2018 2:32:00 PM BarsInProgress: 38
    12/4/2018 2:32:00 PM BarsInProgress: 39
    12/4/2018 2:32:00 PM BarsInProgress: 40
    12/4/2018 2:32:00 PM BarsInProgress: 42
    12/4/2018 2:32:00 PM BarsInProgress: 43
    12/4/2018 2:32:00 PM BarsInProgress: 44
    12/4/2018 2:32:00 PM BarsInProgress: 45
    12/4/2018 2:32:00 PM BarsInProgress: 46
    12/4/2018 2:32:00 PM BarsInProgress: 47
    12/4/2018 2:32:00 PM BarsInProgress: 48
    12/4/2018 2:32:00 PM BarsInProgress: 49
    12/4/2018 2:32:00 PM BarsInProgress: 51
    12/4/2018 2:32:00 PM BarsInProgress: 52
    12/4/2018 2:32:00 PM BarsInProgress: 55
    12/4/2018 2:32:00 PM BarsInProgress: 56

    Due to character limits, I'll post the sample code in a response to this.
    Thank you.

    #2
    Test strategy:

    #region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.Indicators;
    using NinjaTrader.NinjaScript.DrawingTools;

    using System.Diagnostics;
    using System.Net.Mail;
    #endregion

    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class A_Test : Strategy
    {
    #region Variables
    //Main Currency Pairs
    private string EURUSD = "EURUSD";
    private string GBPUSD = "GBPUSD";
    private string AUDUSD = "AUDUSD";
    private string NZDUSD = "NZDUSD";
    private string USDCHF = "USDCHF";
    private string USDCAD = "USDCAD";
    private string USDJPY = "USDJPY";
    //Cross Pairs
    private string EURGBP = "EURGBP";
    private string EURAUD = "EURAUD";
    private string EURNZD = "EURNZD";
    private string EURCHF = "EURCHF";
    private string EURCAD = "EURCAD";
    private string EURJPY = "EURJPY";
    private string GBPAUD = "GBPAUD";
    private string GBPNZD = "GBPNZD";
    private string GBPCHF = "GBPCHF";
    private string GBPCAD = "GBPCAD";
    private string GBPJPY = "GBPJPY";
    private string AUDNZD = "AUDNZD";
    private string AUDCHF = "AUDCHF";
    private string AUDCAD = "AUDCAD";
    private string AUDJPY = "AUDJPY";
    private string NZDCHF = "NZDCHF";
    private string NZDCAD = "NZDCAD";
    private string NZDJPY = "NZDJPY";
    private string CHFJPY = "CHFJPY";
    private string CADCHF = "CADCHF";
    private string CADJPY = "CADJPY";

    //Time periods
    private int trendPeriod = 240;
    private int entryPeriod = 1;

    //"Other" variables
    private int minBarsReqdForStratToWork = 2;
    #endregion

    protected override void OnStateChange()
    {
    if (State == State.Configure)
    {
    //Trend period data series
    AddDataSeries(EURUSD, BarsPeriodType.Minute, trendPeriod); //BarsArray[1]
    AddDataSeries(GBPUSD, BarsPeriodType.Minute, trendPeriod); //BarsArray[2]
    AddDataSeries(AUDUSD, BarsPeriodType.Minute, trendPeriod); //BarsArray[3]
    AddDataSeries(NZDUSD, BarsPeriodType.Minute, trendPeriod); //BarsArray[4]
    AddDataSeries(USDCHF, BarsPeriodType.Minute, trendPeriod); //BarsArray[5]
    AddDataSeries(USDCAD, BarsPeriodType.Minute, trendPeriod); //BarsArray[6]
    AddDataSeries(USDJPY, BarsPeriodType.Minute, trendPeriod); //BarsArray[7]

    AddDataSeries(EURGBP, BarsPeriodType.Minute, trendPeriod); //BarsArray[8]
    AddDataSeries(EURAUD, BarsPeriodType.Minute, trendPeriod); //BarsArray[9]
    AddDataSeries(EURNZD, BarsPeriodType.Minute, trendPeriod); //BarsArray[10]
    AddDataSeries(EURCHF, BarsPeriodType.Minute, trendPeriod); //BarsArray[11]
    AddDataSeries(EURCAD, BarsPeriodType.Minute, trendPeriod); //BarsArray[12]
    AddDataSeries(EURJPY, BarsPeriodType.Minute, trendPeriod); //BarsArray[13]

    AddDataSeries(GBPAUD, BarsPeriodType.Minute, trendPeriod); //BarsArray[14]
    AddDataSeries(GBPNZD, BarsPeriodType.Minute, trendPeriod); //BarsArray[15]
    AddDataSeries(GBPCHF, BarsPeriodType.Minute, trendPeriod); //BarsArray[16]
    AddDataSeries(GBPCAD, BarsPeriodType.Minute, trendPeriod); //BarsArray[17]
    AddDataSeries(GBPJPY, BarsPeriodType.Minute, trendPeriod); //BarsArray[18]

    AddDataSeries(AUDNZD, BarsPeriodType.Minute, trendPeriod); //BarsArray[19]
    AddDataSeries(AUDCHF, BarsPeriodType.Minute, trendPeriod); //BarsArray[20]
    AddDataSeries(AUDCAD, BarsPeriodType.Minute, trendPeriod); //BarsArray[21]
    AddDataSeries(AUDJPY, BarsPeriodType.Minute, trendPeriod); //BarsArray[22]

    AddDataSeries(NZDCHF, BarsPeriodType.Minute, trendPeriod); //BarsArray[23]
    AddDataSeries(NZDCAD, BarsPeriodType.Minute, trendPeriod); //BarsArray[24]
    AddDataSeries(NZDJPY, BarsPeriodType.Minute, trendPeriod); //BarsArray[25]

    AddDataSeries(CHFJPY, BarsPeriodType.Minute, trendPeriod); //BarsArray[26]
    AddDataSeries(CADCHF, BarsPeriodType.Minute, trendPeriod); //BarsArray[27]
    AddDataSeries(CADJPY, BarsPeriodType.Minute, trendPeriod); //BarsArray[28]

    //Entry period data series
    AddDataSeries(EURUSD, BarsPeriodType.Minute, entryPeriod); //BarsArray[29]
    AddDataSeries(GBPUSD, BarsPeriodType.Minute, entryPeriod); //BarsArray[30]
    AddDataSeries(AUDUSD, BarsPeriodType.Minute, entryPeriod); //BarsArray[31]
    AddDataSeries(NZDUSD, BarsPeriodType.Minute, entryPeriod); //BarsArray[32]
    AddDataSeries(USDCHF, BarsPeriodType.Minute, entryPeriod); //BarsArray[33]
    AddDataSeries(USDCAD, BarsPeriodType.Minute, entryPeriod); //BarsArray[34]
    AddDataSeries(USDJPY, BarsPeriodType.Minute, entryPeriod); //BarsArray[35]
    AddDataSeries(EURGBP, BarsPeriodType.Minute, entryPeriod); //BarsArray[36]
    AddDataSeries(EURAUD, BarsPeriodType.Minute, entryPeriod); //BarsArray[37]
    AddDataSeries(EURNZD, BarsPeriodType.Minute, entryPeriod); //BarsArray[38]
    AddDataSeries(EURCHF, BarsPeriodType.Minute, entryPeriod); //BarsArray[39]
    AddDataSeries(EURCAD, BarsPeriodType.Minute, entryPeriod); //BarsArray[40]
    AddDataSeries(EURJPY, BarsPeriodType.Minute, entryPeriod); //BarsArray[41]
    AddDataSeries(GBPAUD, BarsPeriodType.Minute, entryPeriod); //BarsArray[42]
    AddDataSeries(GBPNZD, BarsPeriodType.Minute, entryPeriod); //BarsArray[43]
    AddDataSeries(GBPCHF, BarsPeriodType.Minute, entryPeriod); //BarsArray[44]
    AddDataSeries(GBPCAD, BarsPeriodType.Minute, entryPeriod); //BarsArray[45]
    AddDataSeries(GBPJPY, BarsPeriodType.Minute, entryPeriod); //BarsArray[46]
    AddDataSeries(AUDNZD, BarsPeriodType.Minute, entryPeriod); //BarsArray[47]
    AddDataSeries(AUDCHF, BarsPeriodType.Minute, entryPeriod); //BarsArray[48]
    AddDataSeries(AUDCAD, BarsPeriodType.Minute, entryPeriod); //BarsArray[49]
    AddDataSeries(AUDJPY, BarsPeriodType.Minute, entryPeriod); //BarsArray[50]
    AddDataSeries(NZDCHF, BarsPeriodType.Minute, entryPeriod); //BarsArray[51]
    AddDataSeries(NZDCAD, BarsPeriodType.Minute, entryPeriod); //BarsArray[52]
    AddDataSeries(NZDJPY, BarsPeriodType.Minute, entryPeriod); //BarsArray[53]
    AddDataSeries(CHFJPY, BarsPeriodType.Minute, entryPeriod); //BarsArray[54]
    AddDataSeries(CADCHF, BarsPeriodType.Minute, entryPeriod); //BarsArray[55]
    AddDataSeries(CADJPY, BarsPeriodType.Minute, entryPeriod); //BarsArray[56]

    Calculate = Calculate.OnBarClose;
    TraceOrders = false;
    RealtimeErrorHandling = RealtimeErrorHandling.IgnoreAllErrors;
    }
    if (State == State.SetDefaults)
    {
    EntriesPerDirection = 56;
    EntryHandling = EntryHandling.AllEntries;

    ConnectionLossHandling = ConnectionLossHandling.KeepRunning;
    DisconnectDelaySeconds = 10;
    NumberRestartAttempts = 60;
    RestartsWithinMinutes = 60;
    IncludeTradeHistoryInBacktest = true;
    }
    }

    protected override void OnBarUpdate()
    {
    //Set CurrentBars to the minimum amount of time required (in bars) for subsequent variables to be resolved. Brackets denote BarsArray and the number denotes the number of bars needed.
    if (CurrentBars[1] < minBarsReqdForStratToWork || CurrentBars[2] < minBarsReqdForStratToWork || CurrentBars[3] < minBarsReqdForStratToWork || CurrentBars[4] < minBarsReqdForStratToWork || CurrentBars[5] < minBarsReqdForStratToWork || CurrentBars[6] < minBarsReqdForStratToWork || CurrentBars[7] < minBarsReqdForStratToWork || CurrentBars[8] < minBarsReqdForStratToWork || CurrentBars[9] < minBarsReqdForStratToWork || CurrentBars[10] < minBarsReqdForStratToWork || CurrentBars[11] < minBarsReqdForStratToWork || CurrentBars[12] < minBarsReqdForStratToWork || CurrentBars[13] < minBarsReqdForStratToWork || CurrentBars[14] < minBarsReqdForStratToWork || CurrentBars[15] < minBarsReqdForStratToWork || CurrentBars[16] < minBarsReqdForStratToWork || CurrentBars[17] < minBarsReqdForStratToWork || CurrentBars[18] < minBarsReqdForStratToWork || CurrentBars[19] < minBarsReqdForStratToWork || CurrentBars[20] < minBarsReqdForStratToWork || CurrentBars[21] < minBarsReqdForStratToWork || CurrentBars[22] < minBarsReqdForStratToWork || CurrentBars[23] < minBarsReqdForStratToWork || CurrentBars[24] < minBarsReqdForStratToWork || CurrentBars[25] < minBarsReqdForStratToWork || CurrentBars[26] < minBarsReqdForStratToWork || CurrentBars[27] < minBarsReqdForStratToWork || CurrentBars[28] < minBarsReqdForStratToWork) return;

    if (BarsInProgress > 28)//BiP NOT between 1 and 28 (inclusive)
    {
    Print(Time[0] + " BarsInProgress: " + BarsInProgress);
    }//End section: BiP NOT between 1 and 28.
    }
    }
    }

    Thank you.

    Comment


      #3
      Hello UglyHack,

      This may mean that historical data is no loading for those series.

      Are you able to open a chart with that instrument and bar type?

      Do you see bars closing within the date range?
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        Good morning, and thank you for your reply.

        Yes, I was able to open a chart and find that exact bar. The OHLC values for that bar are in the first message above.
        I just tried to edit the code I sent you to print out the Close value for this bar - the idea was to demonstrate that values for the bar could be retrieved, even though BarsInProgress itself wasn't being updated - and the issue changed position. 12/4/18 now works as expected; scroll down a bit, and 12/20/18 has the same error - but with different bar objects! The new printout is below.

        Since I've been trying to code workarounds for a week now, I tried to re-run my base strategy without changing anything. The date on which it starts skipping changed. As of Friday when I wrote the first post, the main strategy failed on 9/11/18. As of now (Monday morning), with zero changes made to the code over the weekend, it fails on 9/18/18 instead.

        I'm open to suggestions here.

        Thank you.

        12/20/2018 2:13:00 AM BarsInProgress: 29
        12/20/2018 2:13:00 AM BarsInProgress: 30
        12/20/2018 2:13:00 AM BarsInProgress: 32
        12/20/2018 2:13:00 AM BarsInProgress: 35
        12/20/2018 2:13:00 AM BarsInProgress: 37
        12/20/2018 2:13:00 AM BarsInProgress: 38
        12/20/2018 2:13:00 AM BarsInProgress: 39
        12/20/2018 2:13:00 AM BarsInProgress: 40
        12/20/2018 2:13:00 AM BarsInProgress: 42
        12/20/2018 2:13:00 AM BarsInProgress: 43
        12/20/2018 2:13:00 AM BarsInProgress: 44
        12/20/2018 2:13:00 AM BarsInProgress: 45
        12/20/2018 2:13:00 AM BarsInProgress: 46
        12/20/2018 2:13:00 AM BarsInProgress: 47
        12/20/2018 2:13:00 AM BarsInProgress: 48
        12/20/2018 2:13:00 AM BarsInProgress: 49
        12/20/2018 2:13:00 AM BarsInProgress: 51
        12/20/2018 2:13:00 AM BarsInProgress: 52
        12/20/2018 2:13:00 AM BarsInProgress: 53
        12/20/2018 2:13:00 AM BarsInProgress: 55
        12/20/2018 2:13:00 AM BarsInProgress: 56

        Comment


          #5
          Hello UglyHack,

          May I have you open a chart to the primary series, and then add the bar series that is BarsInProgress 31.

          Enable the mouse crosshairs and place over that bar to show that bar is on the chart but not appearing in the prints?

          Print the BarsPeriod.BarsPeriodType and Value so we can confirm these are the same.
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Good morning ChelseaB,

            It is said that the definition of insanity is doing the same thing over and over again and expecting different results.
            Well, I don't expect different results each time, but I sure am getting them! That must mean that I'm not insane... yet.

            That said, I followed your recommendation to the letter. Calling up a multi-pane 1-minute chart for EURUSD (BarArray 0) and AUDUSD (BarArray 31) and navigating to the 12/20/2018 2:13:00 AM bar shows that both bars exist. We don't need a screen-cap for this part.

            Then, I added your recommendations to my print command, resulting in this code: Print(Time[0] + " BarsInProgress: " + BarsInProgress + " BarsPeriodType: " + BarsPeriod.BarsPeriodType + " BarsPeriodValue: " + BarsPeriod.Value);

            After that, I ran the exact same backtest: 1/1/2018 - 12/31/2018, same settings, etc. This resulted in the printout included below. Note what suddenly ISN'T missing.

            On a whim, I re-ran the backtest on the actual trading strategy in question. I have not touched this code, nor any code that this strategy invokes, since this thread was started. As of a few minutes ago, it works fine through September (which was a problem before), but skips the month of April.
            Please don't worry about specifics in this strategy; I only wanted to illustrate that results are changing even when inputs stay consistent, and that this holds true across different strategies.

            Where do we go from here?

            Thank you for your help.

            12/20/2018 2:13:00 AM BarsInProgress: 29 BarsPeriodType: Minute BarsPeriodValue: 1
            12/20/2018 2:13:00 AM BarsInProgress: 30 BarsPeriodType: Minute BarsPeriodValue: 1
            12/20/2018 2:13:00 AM BarsInProgress: 31 BarsPeriodType: Minute BarsPeriodValue: 1
            12/20/2018 2:13:00 AM BarsInProgress: 32 BarsPeriodType: Minute BarsPeriodValue: 1
            12/20/2018 2:13:00 AM BarsInProgress: 33 BarsPeriodType: Minute BarsPeriodValue: 1
            12/20/2018 2:13:00 AM BarsInProgress: 34 BarsPeriodType: Minute BarsPeriodValue: 1
            12/20/2018 2:13:00 AM BarsInProgress: 35 BarsPeriodType: Minute BarsPeriodValue: 1
            12/20/2018 2:13:00 AM BarsInProgress: 36 BarsPeriodType: Minute BarsPeriodValue: 1
            12/20/2018 2:13:00 AM BarsInProgress: 37 BarsPeriodType: Minute BarsPeriodValue: 1
            12/20/2018 2:13:00 AM BarsInProgress: 38 BarsPeriodType: Minute BarsPeriodValue: 1
            12/20/2018 2:13:00 AM BarsInProgress: 39 BarsPeriodType: Minute BarsPeriodValue: 1
            12/20/2018 2:13:00 AM BarsInProgress: 40 BarsPeriodType: Minute BarsPeriodValue: 1
            12/20/2018 2:13:00 AM BarsInProgress: 41 BarsPeriodType: Minute BarsPeriodValue: 1
            12/20/2018 2:13:00 AM BarsInProgress: 42 BarsPeriodType: Minute BarsPeriodValue: 1
            12/20/2018 2:13:00 AM BarsInProgress: 43 BarsPeriodType: Minute BarsPeriodValue: 1
            12/20/2018 2:13:00 AM BarsInProgress: 44 BarsPeriodType: Minute BarsPeriodValue: 1
            12/20/2018 2:13:00 AM BarsInProgress: 45 BarsPeriodType: Minute BarsPeriodValue: 1
            12/20/2018 2:13:00 AM BarsInProgress: 46 BarsPeriodType: Minute BarsPeriodValue: 1
            12/20/2018 2:13:00 AM BarsInProgress: 47 BarsPeriodType: Minute BarsPeriodValue: 1
            12/20/2018 2:13:00 AM BarsInProgress: 48 BarsPeriodType: Minute BarsPeriodValue: 1
            12/20/2018 2:13:00 AM BarsInProgress: 49 BarsPeriodType: Minute BarsPeriodValue: 1
            12/20/2018 2:13:00 AM BarsInProgress: 50 BarsPeriodType: Minute BarsPeriodValue: 1
            12/20/2018 2:13:00 AM BarsInProgress: 51 BarsPeriodType: Minute BarsPeriodValue: 1
            12/20/2018 2:13:00 AM BarsInProgress: 52 BarsPeriodType: Minute BarsPeriodValue: 1
            12/20/2018 2:13:00 AM BarsInProgress: 53 BarsPeriodType: Minute BarsPeriodValue: 1
            12/20/2018 2:13:00 AM BarsInProgress: 54 BarsPeriodType: Minute BarsPeriodValue: 1
            12/20/2018 2:13:00 AM BarsInProgress: 55 BarsPeriodType: Minute BarsPeriodValue: 1
            12/20/2018 2:13:00 AM BarsInProgress: 56 BarsPeriodType: Minute BarsPeriodValue: 1

            Comment


              #7
              Originally posted by UglyHack View Post
              Where do we go from here?
              I would recommend you run Strategy Analyzer only when you are not connected.
              This will prevent the download of historical data (missing or otherwise) and perhaps
              lead to better consistency in results.

              However, this requires you to pro-actively think about downloading any needed
              historical data yourself -- which you do via Tools -> Historical Data -> Load tab.

              I use this approach exactly because it surfaces historical data in my mind, requires
              me to make sure I have it, and to download it manually (and often) to guarantee it.

              So, what next?
              Well, I would certainly reload all historical data (which could take awhile, but one
              big massive initial re-download is a good idea) -- then, I would change my habits
              and reload the most immediate 10 days once or twice a day, and then backtest
              only when disconnected.

              With so many data series being loaded, this might lead to some better sanity in
              controlling the current state of all your historical data.

              Just my 2˘.

              Comment


                #8
                Hello UglyHack,

                The output would only change if the data changes or the code changes.

                To confirm the script is not generating any random numbers is this correct?

                Are you able to reproduce this behavior of the data changing?
                Chelsea B.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Lumbeezl, Today, 01:20 AM
                1 response
                7 views
                0 likes
                Last Post NinjaTrader_Jason  
                Started by oz187, Yesterday, 11:31 PM
                1 response
                11 views
                0 likes
                Last Post NinjaTrader_Jason  
                Started by ofiramitai, Today, 01:59 AM
                0 responses
                8 views
                0 likes
                Last Post ofiramitai  
                Started by gravityflyer, Yesterday, 09:51 PM
                1 response
                10 views
                0 likes
                Last Post NinjaTrader_Jason  
                Started by SmartArtsStudio, Yesterday, 09:41 PM
                1 response
                13 views
                0 likes
                Last Post NinjaTrader_Jason  
                Working...
                X