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 Mindset, 04-21-2026, 06:46 AM
    0 responses
    89 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by M4ndoo, 04-20-2026, 05:21 PM
    0 responses
    135 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by M4ndoo, 04-19-2026, 05:54 PM
    0 responses
    68 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by cmoran13, 04-16-2026, 01:02 PM
    0 responses
    119 views
    0 likes
    Last Post cmoran13  
    Started by PaulMohn, 04-10-2026, 11:11 AM
    0 responses
    69 views
    0 likes
    Last Post PaulMohn  
    Working...
    X