Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Onbarupdate firing twice

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

    Onbarupdate firing twice

    I'm new to NT and my code is working great except I'm getting two fires with Onbarupdate. It seems to fire within milliseconds of each other at the start or end of a new bar. Does it fire at close and open of a bar?

    I have a send mail command in onbarupdate that i've intended to send one email each hour but it's sending two pretty much at the same time. I've also noticed my counts are double the amount of bars that have passed.

    Thanks for your help!

    protected override void Initialize()
    {
    CalculateOnBarClose = true;
    BarsRequired = 45;
    ClearOutputWindow();
    Add(PeriodType.Minute, 3);

    SetProfitTarget(35 * quantity);

    }

    protected override void OnBarUpdate()
    {

    CalculateHandL();
    if(longPosition > 0)longPosition++;
    if(shortPosition > 0) shortPosition++;


    //Check buy or sell
    if (longPosition >= 1) CheckLongClose();
    else if (longPosition == 0 && shortPosition == 0) CheckLongOpen();

    if (shortPosition >= 1) CheckShortClose();
    else if (longPosition == 0 && shortPosition == 0) CheckShortOpen();


    //Update Performance
    if (ToTime(Time[0]) % 10000 == 0 )
    {
    SendMail("removed for post");
    }
    }

    #2
    Hello aw777, and thank you for your question.

    You are getting two fires by design, due to this line being in your Initialize routine :
    Code:
    [FONT=Courier New] Add(PeriodType.Minute, 3);[/FONT]
    If you would like to only process your primary data series (the one you specify in your indicator preferences, you will need to start off your OnBarUpdate like this

    Code:
    [FONT=Courier New]protected override void OnBarUpdate()
    {
    [B]    if (BarsInProgress == 1)
        {
            return;
        }[/B][/FONT]
    If on the other hand you would like to process the data series you manually added in your initialize section during OnBarUpdate, you can use

    Code:
    [FONT=Courier New]protected override void OnBarUpdate()
    {
        if (BarsInProgress == [B]0[/B])
        {
            return;
        }[/FONT]
    If your code is just going to work on the primary data series, you can remove the Add(...) line entirely.

    Please let us know if there are any other ways we can help.
    Last edited by NinjaTrader_JessicaP; 10-21-2016, 06:01 AM.
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Thanks, that quickly solved the problem!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      579 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      334 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      101 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      554 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      551 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X