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

Change Calculation from OnBarClose to OnEachTick and still recognize bar is close

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

    Change Calculation from OnBarClose to OnEachTick and still recognize bar is close

    Hi,

    I have developed a strategy and using the following code to take a position and the Calculation is OnBarClose.

    if(Close[0] > Open[0])
    EnterLong();
    else if(Close[0] < Open[0])
    EnterShort();

    Is there a way to change the Calculation to OnEachTick and have exact same functionality(I mean detect when bar is closed).



    #2
    Hello bosajin,

    Thanks for your post.

    When using Calculate.OnBarClose your code is executed once when the currently forming bar closes. The just closed bar is referenced as [0], This means Close[0] would refer to the close price of the just-closed bar. The new currently forming bar is unknown to your script until the bar closes and the new closed bar becomes [0].

    When using Calculate.OnEachTick, your code is operated on every incoming tick of the currently forming bar. This means that the currently forming bar is referenced as [0], the just-closed bar would be [1] (so you can see that the references shift). Close[0] in this case would represent the current right edge price of the currently forming bar.

    You can use the system bool of Bars.IsFirstTickOfBar as a means to perform once per bar processing. for example:

    if (Bars.IsFirstTickOfBar)
    {
    if(Close[1] > Open[1]) // Using [1] here because we are operating on the first tick of the new bar which would be [0]
    EnterLong();
    else if(Close[1] < Open[1])
    EnterShort();
    }


    Here is a link to another example of coding to use both oneachtick and once per bar processing: https://ninjatrader.com/support/help...either_cal.htm
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Thanks Paul for swift response .
      1) How can I access to price of a bar before the just-closed bar?
      2) is there any delay between when a bar is close and another bar is opened?(I would make sure the IsFirstTickOfBar is fully trustable to get same result as OnBarClose)

      Comment


        #4
        Hello bosajin,

        Thanks for your reply.

        The barsAgo value is in sequence, [0] is the current bar, [1] the bar before that, [2] the bar before that, etc. etc.

        Calculate.OnEachTick is an event-driven process, in this case as a tick comes in, that is the event that drives the process.

        Paul H.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by elirion, Yesterday, 09:32 PM
        0 responses
        7 views
        0 likes
        Last Post elirion
        by elirion
         
        Started by cre8able, Yesterday, 09:15 PM
        1 response
        8 views
        0 likes
        Last Post bltdavid  
        Started by cummish, Yesterday, 08:43 PM
        0 responses
        15 views
        0 likes
        Last Post cummish
        by cummish
         
        Started by Option Whisperer, Yesterday, 07:58 PM
        4 responses
        21 views
        0 likes
        Last Post Option Whisperer  
        Started by ETFVoyageur, 05-07-2024, 07:05 PM
        13 responses
        87 views
        0 likes
        Last Post ETFVoyageur  
        Working...
        X