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

BarsRequiredToTrade

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

    BarsRequiredToTrade

    I am migrating all my code from NT7- where it worked fine - to NT8. I run the following code in Strategy Analyzer:
    protectedoverridevoidOnStateChange()
    {
    if (State == State.SetDefaults)
    {
    .....
    BarsRequiredToTrade = 5;
    ......
    }

    protected
    overridevoidOnBarUpdate()
    {
    if (Bars.IsFirstBarOfSession)
    {
    if (Open[0] >= (Close[1]+GapSize))
    {
    TradeCount = TradeCount+1;
    }
    }

    And I get the following error message:
    Error on calling 'OnBarUpdate' method on bar 0: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

    Why is the BarsRequiredToTrade not preventing this error? What code do I need to avoid this?

    #2
    Hello,

    Thank you for the post.

    This property specifically controls Trading but would not prevent OnBarUpdate from being called. By definition, this is the number of historical bars required before the strategy starts processing order methods called in the OnBarUpdate() method


    You could use the sample syntax in the page above to check there are enough bars, you could also use the following because you are using 1 BarsAgo or Close[1]:
    Code:
    if(CurrentBar < 1) return;
    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      In NT7 Strategy Analyzer always automatically validated if Current Bar was larger than BarsRequired before a statement was called. This could never lead to a program error.
      Do I understajnd correctly that under NT8 this is no longer automatically validated, but now has to be programmed manually as "if CurrentBar>BarsrequiredToTrade"?

      Comment


        #4
        Hello,

        In NT7 this property equated to preventing the Total script from running before X number of bars. In NT8 this controls specifically Trading but not the entire script. This allows this property to be used while still allowing the script to use the total requested bars.

        Let's say you have a BarsRequiredToTrade as 20 because 20 bars are needed for the calculation to be correct. On bar 15 a condition to trade is found early and EnterLong() is called. This is earlier than the script requires for data to calculate correctly so this call would be ignored because we are not yet beyond the BarsRequiredToTrade.

        Your script will logically proceed from bar 0, only trades would be ignored if they occur before this set number of bars. This means you would need to check for BarsAgo if you use them to ensure you don't hit any errors.

        A very simple example of checking for enough bars for your logic would be to use the CurrentBar:

        if(CurrentBar < 1) return;



        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          If I understand correctly, in NT8 the BarsRequired function no longer exists. NT8 has a completely different function: BarsRequiredToTrade.
          As the BarsRequired function is essential to avoid errors, I should now define a user input variable BarsRequired myself - with the same objective as it had under NT7 - and in each Strategy introduce the stament:
          if(CurrentBar < BarsRequired) return;

          Is this correct? If so, can you launch an NT8 release update request to reimplement BarsRequired?

          Comment


            #6
            Hello,

            You can find notes about NinjaScript changes in the code breaking changes page of the help guide. This item has been documented being changed and you can find more details here:





            This property was renamed so if you want to use the same check as you did in NT7 you would just need to use the new spelling. I don't believe a feature request is needed as the property still exists and it can still be used in the same way, it was only renamed. The difference is how the core uses the property for trades. If you need to check historical data, the concept of waiting until that data is available would still apply. You could use it in an if statement at the top of OnBarUpdate as you have shown to control the waiting period.

            Please let me know if I may be of further assistance.
            JesseNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Jon17, Today, 04:33 PM
            0 responses
            1 view
            0 likes
            Last Post Jon17
            by Jon17
             
            Started by Javierw.ok, Today, 04:12 PM
            0 responses
            6 views
            0 likes
            Last Post Javierw.ok  
            Started by timmbbo, Today, 08:59 AM
            2 responses
            10 views
            0 likes
            Last Post bltdavid  
            Started by alifarahani, Today, 09:40 AM
            6 responses
            41 views
            0 likes
            Last Post alifarahani  
            Started by Waxavi, Today, 02:10 AM
            1 response
            21 views
            0 likes
            Last Post NinjaTrader_LuisH  
            Working...
            X