• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

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 rontronimous, Today, 05:55 PM
        0 responses
        8 views
        0 likes
        Last Post rontronimous  
        Started by bcgeek, Today, 03:43 PM
        2 responses
        6 views
        0 likes
        Last Post bcgeek
        by bcgeek
         
        Started by Mberg, Today, 04:05 PM
        0 responses
        9 views
        0 likes
        Last Post Mberg
        by Mberg
         
        Started by dkbyond, Today, 03:52 PM
        0 responses
        5 views
        0 likes
        Last Post dkbyond
        by dkbyond
         
        Started by Newtrader101, Today, 03:09 PM
        2 responses
        7 views
        0 likes
        Last Post Newtrader101  
        Working...
        X