Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Problem with BarsOnClose()

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

    Problem with BarsOnClose()

    Hi,

    I'm trying to integrate some analytics into my strategy.

    If a buy or sell signal has been generated, I would like to the strategy to record the price a few bars after which is stored as an integer "TimeFrame". As I would only like this to be called once, I have used FirstTickOfBar as well.

    Code:
    if FirstTickOfBar && BarsSinceEntry() == TimeFrame
    My problem now is that the above does not seem to work as I have put several prints within and in the level above the if segment and nothing from within the if segment is printing.

    When I modify the code to BarsSinceEntry() => TimeFrame, it seems to work just fine (even though this is not what I want).

    I know I could do a workaround by adding another variable called IsChecked which switches true from within the if segment and is then added as a condition so that it only triggers once. However, I'd prefer a simple solution which doesn't require adding variables, or if I'm using the functions wrongly, would like to learn how I've misused them.

    Thanks!

    #2
    Hello wuilengh,

    Thank you for your note.

    I created a small script to test this and found it to work.

    My script (running on CalculateOnBarClose = false)
    int TimeFrame = 3;
    if (FirstTickOfBar && BarsSinceEntry() == TimeFrame)
    {
    Print("first tick on 3rd bar since entry");
    }

    if (Position.MarketPosition == MarketPosition.Flat)
    {
    EnterLong(DefaultQuantity);
    }

    This does indeed print the string on the 3rd bar after entry.


    For me to continue assisting you I will need to see your code.
    You can copy and paste the code into your reply or attach the .cs file from your (My) Documents\NinjaTrader 7\bin\Custom\Strategy folder.

    If you would like to keep the code private with me, send an email to support[at]ninjatrader[dot]com and in the body of the message put Attn: Chelsea #800408 and be sure to attach your script.


    I look forward to working with you further.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Sorry it does indeed work!

      I put prints all over and discovered that the problem was with my code. Thanks again for your help and apologies for the trouble.

      Comment


        #4
        Hello wuilengh,

        Thanks for the update.
        I'm glad to hear your script is working correctly.

        Please do not hesitate to contact us for any other NinjaTrader inquiries you may have.
        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        669 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        378 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        111 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        575 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        580 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X