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

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 judysamnt7, 03-13-2023, 09:11 AM
      4 responses
      59 views
      0 likes
      Last Post DynamicTest  
      Started by ScottWalsh, Today, 06:52 PM
      4 responses
      36 views
      0 likes
      Last Post ScottWalsh  
      Started by olisav57, Today, 07:39 PM
      0 responses
      7 views
      0 likes
      Last Post olisav57  
      Started by trilliantrader, Today, 03:01 PM
      2 responses
      21 views
      0 likes
      Last Post helpwanted  
      Started by cre8able, Today, 07:24 PM
      0 responses
      10 views
      0 likes
      Last Post cre8able  
      Working...
      X