Announcement

Collapse
No announcement yet.

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:	171
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 );

    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.

        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;

            Comment


              #7
              Thanks, I will have a look!

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by NullPointStrategies, Today, 05:17 AM
              0 responses
              50 views
              0 likes
              Last Post NullPointStrategies  
              Started by argusthome, 03-08-2026, 10:06 AM
              0 responses
              126 views
              0 likes
              Last Post argusthome  
              Started by NabilKhattabi, 03-06-2026, 11:18 AM
              0 responses
              69 views
              0 likes
              Last Post NabilKhattabi  
              Started by Deep42, 03-06-2026, 12:28 AM
              0 responses
              42 views
              0 likes
              Last Post Deep42
              by Deep42
               
              Started by TheRealMorford, 03-05-2026, 06:15 PM
              0 responses
              46 views
              0 likes
              Last Post TheRealMorford  
              Working...
              X