Announcement

Collapse
No announcement yet.

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 RobotSyndicate, Today, 04:27 AM
        0 responses
        1 view
        0 likes
        Last Post RobotSyndicate  
        Started by Ray12345, Today, 04:10 AM
        0 responses
        2 views
        0 likes
        Last Post Ray12345  
        Started by Behungr101, Today, 02:16 AM
        1 response
        8 views
        0 likes
        Last Post NinjaTrader_Jason  
        Started by omermirza, Yesterday, 08:37 PM
        2 responses
        12 views
        0 likes
        Last Post NT-Roland  
        Started by aelagha, Today, 01:28 AM
        1 response
        12 views
        0 likes
        Last Post NinjaTrader_Jason  
        Working...
        X