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 aa731, Today, 02:54 AM
      0 responses
      4 views
      0 likes
      Last Post aa731
      by aa731
       
      Started by thanajo, 05-04-2021, 02:11 AM
      3 responses
      469 views
      0 likes
      Last Post tradingnasdaqprueba  
      Started by Christopher_R, Today, 12:29 AM
      0 responses
      10 views
      0 likes
      Last Post Christopher_R  
      Started by sidlercom80, 10-28-2023, 08:49 AM
      166 responses
      2,237 views
      0 likes
      Last Post sidlercom80  
      Started by thread, Yesterday, 11:58 PM
      0 responses
      4 views
      0 likes
      Last Post thread
      by thread
       
      Working...
      X