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 with firsts bars

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

    Problem with firsts bars

    Hi, im working with a second data series:

    else if (State == State.Configure)
    {

    AddDataSeries(BarsPeriodType.Tick, 1);
    }


    Everything works normally in the strategy loading the chart with Range 100, 30 days.
    The problem arises when I load a chart with lets say 200 bars.
    In this condition, no Draw appears on the chart, no trades, and the values disappear from the Output Window.
    I noticed that the code with problems seems to be this, but I cannot understand what the error may be. I would appreciate any possible help.


    (.....)

    protected override void OnBarUpdate()
    {
    if (CurrentBar < BarsRequiredToTrade)
    {
    return;
    }

    if (Bars.IsFirstBarOfSession)
    {
    currentPnL = 0;
    }

    if((Calculate == Calculate.OnEachTick && BarsInProgress != 2) || (Calculate == Calculate.OnBarClose && BarsInProgress != 0))
    return;

    if (CurrentBars[0] < 1 || CurrentBars[1] < 0)
    return;

    #2
    Hi rocker84, thanks for posting.

    It's possible your data feed reached the tick data limit. Note the start and end bar timestamps of the primary series when you load 200 bars, then try loading up a 1 tick chart of the same date range to see how much tick data is being returned. Different data providers have different historical tick data limits. E.g. Kinetic will let you load 180 days of tick data, but during Mon-Fri 9:30am ET to 4:30pm ET you will have access to 8 days of historical tick data.

    I look forward to assisting.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChrisL View Post
      Hi rocker84, thanks for posting.

      It's possible your data feed reached the tick data limit. Note the start and end bar timestamps of the primary series when you load 200 bars, then try loading up a 1 tick chart of the same date range to see how much tick data is being returned. Different data providers have different historical tick data limits. E.g. Kinetic will let you load 180 days of tick data, but during Mon-Fri 9:30am ET to 4:30pm ET you will have access to 8 days of historical tick data.

      I look forward to assisting.

      I don't think that's it, I have a lot of time loaded in db / tick and 100 bars is less than 30 days.

      Could it be that it happens because the first tick does not coincide with a first bar? If it is loaded in "bars" that condition changes. If "days" are loaded, the first bar always coincides with the first bar calculated in ticks. Could it be something about that?

      Comment


        #4
        With this simple code it generates the same error

        The problem seems to be here with x bars loaded:

        if (CurrentBars[0] < BarsRequiredToTrade || CurrentBars[1] < BarsRequiredToTrade)
        return;
        Attached Files
        Last edited by rocker84; 10-01-2021, 12:40 PM.

        Comment


          #5
          HI Rocker84,

          The strategy is adding one series and you are checking for BarsInProgress != 2. The script you posted will only have BarsArray[0] and BarsAray[1], so it is returning on every bar because of this line.

          Kind regards,
          -ChrisL
          Chris L.NinjaTrader Customer Service

          Comment


            #6
            Originally posted by NinjaTrader_ChrisL View Post
            HI Rocker84,

            The strategy is adding one series and you are checking for BarsInProgress != 2. The script you posted will only have BarsArray[0] and BarsAray[1], so it is returning on every bar because of this line.

            Kind regards,
            -ChrisL
            Deleting this line is giving me the same error

            if((Calculate == Calculate.OnEachTick && BarsInProgress != 2) || (Calculate == Calculate.OnBarClose && BarsInProgress != 0))
            return;

            The line with the problem is

            if (CurrentBars[0] < BarsRequiredToTrade || CurrentBars[1] < BarsRequiredToTrade)
            return;

            which came from this example:

            Comment


              #7
              Hi rocker84,

              What is the value of CurrentBars[1] when you print it out? Please confirm there is actually data coming from the second series when the bars to load is loaded to 200 bars. The script is also not filtering the 1 tick series and the primary series. It would either need to submit the EnterLong/ExitLong order on the second series or run this logic on the secondary series. E.g.

              Code:
              if(BarsInPrgress == 0) //run the logic on the close of every primary bar, submitting to the 1 tick series for backtesting 
                          {
                              // Set 1
                              if (CrossAbove(MACD1.Diff, 0, 1))
                              {
                                  EnterLong(1, 1, "LongEntry");
                              }
              
                              // Set 2
                              if (CrossBelow(MACD1.Diff, 0, 1))
                              {
                                  ExitLong(1, 1, "LongExit", "Lo ngEntry");
                              }
                          }
              
              or 
              
              if(BarsInPrgress == 1) //run the logic on every tick, still submitting the ordedr to the one tick series. 
                          {
                              // Set 1
                              if (CrossAbove(MACD1.Diff, 0, 1))
                              {
                                  EnterLong(1, 1, "LongEntry");
                              }
              
                              // Set 2
                              if (CrossBelow(MACD1.Diff, 0, 1))
                              {
                                  ExitLong(1, 1, "LongExit", "LongEntry");
                              }
                          }
              Chris L.NinjaTrader Customer Service

              Comment


                #8
                Originally posted by NinjaTrader_ChrisL View Post
                Hi rocker84,

                What is the value of CurrentBars[1] when you print it out? Please confirm there is actually data coming from the second series when the bars to load is loaded to 200 bars. The script is also not filtering the 1 tick series and the primary series. It would either need to submit the EnterLong/ExitLong order on the second series or run this logic on the secondary series. E.g.

                Code:
                if(BarsInPrgress == 0) //run the logic on the close of every primary bar, submitting to the 1 tick series for backtesting
                {
                // Set 1
                if (CrossAbove(MACD1.Diff, 0, 1))
                {
                EnterLong(1, 1, "LongEntry");
                }
                
                // Set 2
                if (CrossBelow(MACD1.Diff, 0, 1))
                {
                ExitLong(1, 1, "LongExit", "Lo ngEntry");
                }
                }
                
                or
                
                if(BarsInPrgress == 1) //run the logic on every tick, still submitting the ordedr to the one tick series.
                {
                // Set 1
                if (CrossAbove(MACD1.Diff, 0, 1))
                {
                EnterLong(1, 1, "LongEntry");
                }
                
                // Set 2
                if (CrossBelow(MACD1.Diff, 0, 1))
                {
                ExitLong(1, 1, "LongExit", "LongEntry");
                }
                }
                Ok, Let's forget my code for a minute. Please try this code provided by Ninjatrader on the first post.


                Originally posted by NinjaTrader_ChelseaB View Post
                Citizens of the NinjaTrader Community,

                A common question we hear from clients is 'why are results from backtest different from real-time or from market replay?'.

                The main reason is due to a lack of intra-bar granularity.
                (......)
                .
                https://ninjatrader.com/support/foru...ay-performance


                If I run it with tick replay, loading 10 days in "Range 100" everything happens normal.

                But if I load the chart with "200 bars" "Range 100". Only one trade appears at the end of the chart.
                Same with 500 bars

                I attach the screenshots.
                Attached Files

                Comment


                  #9
                  So, the problem is that the chart is also loading 200 bars in 1 tick, which should be about a minute. There is nothing wrong with the codes, but should it load that way? I think the most logical thing (or at least that a popup warning or something appears) is that it loads the same time range in the main series and in all series.

                  Comment


                    #10
                    Hi rocker84, thanks for your reply.

                    If you select to load 200 bars in the Data Series menu it will load the same number of each series. Load the data based on Days setting instead of Bars. There is also an overload of AddDataSeries where you can specify the number of bars:

                    AddDataSeries(string instrumentName, BarsPeriod barsPeriod, int barsToLoad, string tradingHoursName, bool? isResetOnNewTradingDay)

                    Kind regards,
                    -ChrisL
                    Chris L.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by TradeSaber, Today, 07:18 AM
                    0 responses
                    4 views
                    0 likes
                    Last Post TradeSaber  
                    Started by PaulMohn, Today, 05:00 AM
                    0 responses
                    9 views
                    0 likes
                    Last Post PaulMohn  
                    Started by ZenCortexAuCost, Today, 04:24 AM
                    0 responses
                    6 views
                    0 likes
                    Last Post ZenCortexAuCost  
                    Started by ZenCortexAuCost, Today, 04:22 AM
                    0 responses
                    3 views
                    0 likes
                    Last Post ZenCortexAuCost  
                    Started by SantoshXX, Today, 03:09 AM
                    0 responses
                    17 views
                    0 likes
                    Last Post SantoshXX  
                    Working...
                    X