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

BarType without Gaps (Prior bar close at new bar's open)

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

    BarType without Gaps (Prior bar close at new bar's open)

    I'm trying to make a BarType that doesn't have gaps. If the next bar is supposed to open with a gap. I would like to extend the close of the current bar to the open of the new bar, before opening the new bar.

    For clarity, I want the previous bar to close at the next bar's open - not the next bar to open at the prior bar's close.

    I've tried modifying the OnDataPoint in the MinuteBarsType class as follows, but it doesn't look right based on what I'm trying to do.

    Can anyone help? I've been stuck at this for a few days.

    Thanks.



    Code:
    protected override void OnDataPoint(Bars bars, double open, double high, double low, double close, DateTime time, long volume, bool isBar, double bid, double ask)
    {
    if (SessionIterator == null)
    SessionIterator = new SessionIterator(bars);
    
    var isNewSession = SessionIterator.IsNewSession(time: time, includesEndTimeStamp: isBar);
    if (isNewSession)
    {
    SessionIterator.GetNextSession(timeLocal: time, includesEndTimeStamp: isBar);
    }
    
    if (bars.Count == 0 || (bars.IsResetOnNewTradingDay && isNewSession))
    AddBar(bars, open, high, low, close, TimeToBarTime(bars, time, isBar), volume);
    else if (!isBar && time < bars.LastBarTime)
    UpdateBar(bars, high, low, close, bars.LastBarTime, volume);
    else if (isBar && time <= bars.LastBarTime)
    UpdateBar(bars, high, low, close, bars.LastBarTime, volume);
    else
    {
    time = TimeToBarTime(bars, time, isBar);
    
    UpdateBar(bars, high, low, close, bars.LastBarTime, volume);
    
    var priorClose = bars.GetClose(bars.Count - 1);
    
    AddBar(bars, priorClose, high, low, close, time, volume);
    }
    }

    #2
    Hello frantic,

    Thanks for your post.

    UpdateBar will update the current bar and AddBar will open a new bar.

    I would suggest calling UpdateBar to update the previous bar using the open that is processing in OnDataPoint and fed to the AddBar method to accomplish your goal. You would need to reference the previous bar values in your UpdateBar call.

    Testing the below appears to do what you are looking for.

    Code:
    protected override void OnDataPoint(Bars bars, double open, double high, double low, double close, DateTime time, long volume, bool isBar, double bid, double ask)
    {
        if (SessionIterator == null)
            SessionIterator = new SessionIterator(bars);
    
        if (bars.Count == 0)
            AddBar(bars, open, high, low, close, TimeToBarTime(bars, time, isBar), volume);
        else if (!isBar && time < bars.LastBarTime)
            UpdateBar(bars, high, low, close, bars.LastBarTime, volume);
        else if (isBar && time <= bars.LastBarTime)
            UpdateBar(bars, high, low, close, bars.LastBarTime, volume);
        else
        {
            UpdateBar(bars, bars.GetHigh(bars.Count-1), bars.GetLow(bars.Count-1), open, time, bars.GetVolume(bars.Count-1));
            time        = TimeToBarTime(bars, time, isBar);
            AddBar(bars, open, high, low, close, time, volume);
        }
    }
    Please let me know if I can be of further assistance.
    JimNinjaTrader Customer Service

    Comment


      #3
      Thanks Jim, that does exactly what I needed. Really appreciate your time.

      I notice you're calling UpdateBar() with and setting the close to the open parameter. This makes sense because I want to update the bar to the new open, but, it also doesn't make sense because I've not opened a new bar yet by calling AddBar. Maybe I'm not understanding what the Parameters to OnDataPoint represent. Can you enlighten me?

      Comment


        #4
        Hello frantic,

        OnDataPoint iterates for the incoming data and the parameters all represent data from that incoming data point. When AddBar is called, we create a new bar (this closes the last bar.) Logically this occurs when isBar is true (we are receiving an update letting us know that the iterating bar is a minute bar.) So we call UpdateBar to update the last bar we were working on with the new open from this OnDataPoint iteration since we have not yet created a new bar, and then we call AddBar to open the new bar from the Same OnDataPoint iteration.

        I look forward to being of further assistance.
        JimNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by WeyldFalcon, 08-07-2020, 06:13 AM
        11 responses
        1,422 views
        0 likes
        Last Post jculp
        by jculp
         
        Started by RubenCazorla, Today, 09:07 AM
        0 responses
        4 views
        0 likes
        Last Post RubenCazorla  
        Started by BarzTrading, Today, 07:25 AM
        2 responses
        29 views
        1 like
        Last Post BarzTrading  
        Started by devatechnologies, 04-14-2024, 02:58 PM
        3 responses
        21 views
        0 likes
        Last Post NinjaTrader_BrandonH  
        Started by tkaboris, Today, 08:01 AM
        0 responses
        6 views
        0 likes
        Last Post tkaboris  
        Working...
        X