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 bubblegum, 03-18-2024, 10:41 AM
    4 responses
    51 views
    0 likes
    Last Post NinjaTrader_Gaby  
    Started by kujista, Yesterday, 12:39 AM
    3 responses
    15 views
    0 likes
    Last Post NinjaTrader_Gaby  
    Started by Yasharandom, 05-04-2024, 07:45 AM
    2 responses
    24 views
    0 likes
    Last Post Yasharandom  
    Started by francesjandujar, Today, 05:15 AM
    0 responses
    2 views
    0 likes
    Last Post francesjandujar  
    Started by francesjandujar, Today, 05:14 AM
    0 responses
    0 views
    0 likes
    Last Post francesjandujar  
    Working...
    X