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

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.
    JimNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Segwin, 05-07-2018, 02:15 PM
    14 responses
    1,788 views
    0 likes
    Last Post aligator  
    Started by Jimmyk, 01-26-2018, 05:19 AM
    6 responses
    837 views
    0 likes
    Last Post emuns
    by emuns
     
    Started by jxs_xrj, 01-12-2020, 09:49 AM
    6 responses
    3,293 views
    1 like
    Last Post jgualdronc  
    Started by Touch-Ups, Today, 10:36 AM
    0 responses
    12 views
    0 likes
    Last Post Touch-Ups  
    Started by geddyisodin, 04-25-2024, 05:20 AM
    11 responses
    62 views
    0 likes
    Last Post halgo_boulder  
    Working...
    X