Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Adding bars to a Bars series at run time

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

    Adding bars to a Bars series at run time

    I want to hydrate a Bars object in a strategy at runtime (OnBarUpdate), to feed into an indicator.
    I tried:
    Code:
    class MyStrategy {
       void OnStateChange() {
          if (State == State.Configure) {
                      AddDataSeries(null, new BarsPeriod { BarsPeriodType = BarsPeriodType.Day, Value = 1 }, 0, "Default 24 x 7", false); //my empty bars object   
          }
       }
       void OnBarChange(){
           if (BarsInProgress == 0){
               Bars myBars = BarsArray[1]; 
               //logic to add bars 
               myBars.Add( ...); 
               myBars.Update( ... );
    
               //use in indicator
               SMA sma = SMA(myBars, 14);
    
               //more logic ...
           }
       }
    }
    however I have a problem because it seems a Read lock is held on Bars preventing me from adding bars to myBars in onBarChange.
    Also it appears myBars.CurrentBar increases for each OnBarChange, even if I don't manually add any bars to myBars.

    What is the best practice to generate an in-memory array at runtime, like a Bars object, that can be used by various indicators?
    note: I don't know the final count of generated bars ahead of time.

    Thank you!

    #2
    Welcome to the forums purpledawn777, or rather, thanks for your first post!

    There is not a supported means to dynamically add data to drive indicators at run time. All data that should be processed by indicators should be added in State.Configure and should be hardcoded. (Using null for the instrument name is permissible.)

    We also would not be able to populate a Series on our own (add/fill our own slots) as they are synchronized to data series.

    Technically, you could use AddOn code to create a BarsRequest, but this will not return a Series that can be used easily with indicators. You would need to adapt the specific indicators so they may use the bars returned from the BarsRequest. I.E. re-implement the indicators to use the BarsRequest data instead of using Series. This would go beyond what we could provide assistance with at the support level, however.

    BarsRequest - https://ninjatrader.com/support/help...arsrequest.htm

    Please let us know if you have any questions.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Today, 05:17 AM
    0 responses
    50 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    126 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    69 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    42 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    46 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X