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

Using bars from the past to initialize Indicator/Strategy before onBarUpdate

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

    Using bars from the past to initialize Indicator/Strategy before onBarUpdate

    I'm struggling with a seemingly simple thing.
    I want to code this indicator:

    Step:
    1) Initialization
    - a) take 200 bars from the past and process SMA for it. ( eg. SMA mySma = SMA(barsFromThePast, 200) )
    - b) derive some stats from SMA processed in 1a) (eg. minimal/maximal difference)

    2) onBarUpdate
    - a) use stats from 1b) to draw proper information.
    In my case I'll have SMA difference range and will divide into several levels (strong downtrend = -2, weak downtrend = -1, sideways = 0 , weak uptrend = 1, strong uptrend = 2)
    The output of the indicator will be a SMA difference step function which will oscillate between -2...2 values


    The question is: How to get historical SMA values in point 1a) before the indicator starts to onBarUpdate?

    It’s possible to do this:

    protected override void OnBarUpdate()
    { {
    if(CurrentBar < 200) return;
    ...


    E.g. To wait until 200 bar elapse and everything is computed, however it would take 200 bars just to start indicator plotting the correct values! This solution is not acceptable. If I had a 1D timeframe strategy I would wait 200 days just to start working my indicator correctly???


    Same goes to strategies that process some stats from history (eg. 200 or 1000 bars back) before they even start onBarUpdate calls.
    How to implement such initializations?

    Thank you!

    #2
    Hello WADex,

    Welcome to the NinjaTrader forums!

    The data supplied as the input series to an indicator is the same data being processed by the strategy (and the chart if applied to a chart).

    This means if you want the indicator to have processed 200 bars, you would need to wait until CurrentBar is 200 before calling the indicator as you have suggested, and add more days of data to the chart Data Series if necessary.

    https://ninjatrader.com/support/help...currentbar.htm

    if (CurrentBar < 200)
    return;

    Print(string.Format("{0} | SMA(200)[0]: {1}", Time[0], SMA(200)[0] ));

    OR

    if (CurrentBar > 200)
    {
    Print(string.Format("{0} | SMA(200)[0]: {1}", Time[0], SMA(200)[0] ));
    }

    The SMA indicator will already be processing values from the first bar. Your strategy will simply be delaying any action until 200 bars elapsed, but the indicator will have processed all 200 bars.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi!
      Thank you for explanation. This is what i know. However, sine my strategy is dependent on correct signals from indicator, that means the strategy will work correctly after 200 bars? If i have strategy on daily timeframe, it means the strategy is functional after 200 days!?
      Or is the solution to set BarsRequiredToTrade = 200 ? What does is mean when i set this on strategy? Once BarsRequiredToTrade is set this way, the strategy is fully operational once enabled?
      Thank you!

      Comment


        #4
        Hello WADex,

        When you say 'This is what I know', what specifically are you saying you know?

        Yes, the indicator will have processed the 200 bars. It doesn't have the code to return. The strategy will not process the 200 bars, the indicator will.

        The strategy would begin evaluating the code below the return after 200 bars (if the bars are day bars then 200 days) of historical data has been processed. The indicator however, would have started setting values at bar 0.

        Having BarsRequiredToTrade would not prevent the strategy logic from evaluating but would ignore any orders that were submitted before CurrentBar is 200.

        This would be basically the same thing. Either way the indicator would still process from bar 0. The strategy would be delayed in either performing logic with the return, or ignoring orders that are submitted with BarsRequiredToTrade.
        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by rocketman7, Today, 01:00 AM
        1 response
        9 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by wzgy0920, 04-23-2024, 09:53 PM
        3 responses
        76 views
        0 likes
        Last Post NinjaTrader_BrandonH  
        Started by JonesJoker, 04-22-2024, 12:23 PM
        9 responses
        46 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by timko, Today, 06:45 AM
        1 response
        7 views
        0 likes
        Last Post gaz0001
        by gaz0001
         
        Started by Waxavi, 04-19-2024, 02:10 AM
        3 responses
        41 views
        0 likes
        Last Post gaz0001
        by gaz0001
         
        Working...
        X