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 popecapllc, 08-09-2023, 07:42 PM
        8 responses
        1,351 views
        0 likes
        Last Post Johng22
        by Johng22
         
        Started by ETFVoyageur, 04-30-2024, 02:04 PM
        11 responses
        101 views
        0 likes
        Last Post ETFVoyageur  
        Started by bubblegum, 03-18-2024, 10:41 AM
        3 responses
        46 views
        0 likes
        Last Post vjsworld  
        Started by JamesK1, Today, 02:48 PM
        1 response
        13 views
        0 likes
        Last Post JamesK1
        by JamesK1
         
        Started by llanqui, Today, 03:51 PM
        0 responses
        13 views
        0 likes
        Last Post llanqui
        by llanqui
         
        Working...
        X