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

Indicator calculates OnBarClose, Strategy OnEachTick how to get value of previous bar

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

    Indicator calculates OnBarClose, Strategy OnEachTick how to get value of previous bar

    Hi there,
    I read so many posts last night and today and I finally gave up and want to ask for a bit of help.

    I have a strategy that Calculates OnEachTick and inside of it placed a custom indicator that Calculates OnBarClose.

    AddChartIndicator(ATRExponential(20));

    double ATR = ATRExponential(20)[0];

    This leads to the indicator calculating OnEachTick as well, and I cannot access the previous bar value which is more stable, using this:
    double ATR = ATRExponential(20)[1];
    Click image for larger version

Name:	chart.png
Views:	72
Size:	34.2 KB
ID:	1278492

    #2
    Hello Tradosophy,

    I wanted to clarify the specific problem here. The code you used that has [1] bars ago would be correct to get the last closed bars value. When you use OnEachTick in the strategy the indicator will inherit the Calculate setting. In your image the indicator on the bottom would be the one using OnEachTick because it now calculated for every bar, when using OnBarClose the rightmost building bar is excluded.

    One way to verify you are accessing the correct bars datta would be to use a print:

    Code:
    Print(Time[1] + " " + ATR );

    JesseNinjaTrader Customer Service

    Comment


      #3
      Hi Jesse,
      I should have made it more clear.

      You are correct the indicator in my strategy is calculated OnEachTick, and can be seen at the bottom. I don't need this.

      The one at the top is the same indicator on its own calculated OnEachBar showing a value of 2.14.

      How can I get the previous bar value of 2.14 in my strategy which updates on every tick?

      Comment


        #4
        Hello Tradosophy,

        The indicator on the top showing 2.14 is the last closed bar for that indicator when it uses OnBarClose processing. In the bottom indicator 1 bars ago would represent that value.

        When you use OnEachTick in realtime the [0] bars ago value represents the rightmost bar and its current value. [1] bars ago would represent the last closed bar which would be the same as the indicator using OnbarClose and its right most value.

        I would suggest using the Print that I provided so you can see the last closed bars timestamp and then you could use the mouse to hover over that bar and check the indicators price. \

        For historical bars you can use the [0] bars ago value for the indicator because historical bars are calculated OnBarClose.

        JesseNinjaTrader Customer Service

        Comment


          #5
          When I add Print(Time[1] + " " + ATR ); I cannot enable the Strategy in Playback, it stops working.

          The same thing happens when I use double ATR = ATRExponential(20)[1];

          Comment


            #6
            Hello Tradosophy,

            Did you add that after your CurrentBar check?

            To use more than 1 bars ago you need a condition like the following at the beginning of OnBarUpdate.

            Code:
            protected override void OnBarUpdate()
            {
                if(CurrentBar < 1) return;
            JesseNinjaTrader Customer Service

            Comment


              #7
              Thanks, I will have a look!

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by rhyminkevin, Today, 04:58 PM
              3 responses
              46 views
              0 likes
              Last Post Anfedport  
              Started by iceman2018, Today, 05:07 PM
              0 responses
              5 views
              0 likes
              Last Post iceman2018  
              Started by lightsun47, Today, 03:51 PM
              0 responses
              7 views
              0 likes
              Last Post lightsun47  
              Started by 00nevest, Today, 02:27 PM
              1 response
              14 views
              0 likes
              Last Post 00nevest  
              Started by futtrader, 04-21-2024, 01:50 AM
              4 responses
              50 views
              0 likes
              Last Post futtrader  
              Working...
              X