Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Comparing a bar to an average

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

    Comparing a bar to an average

    I'm trying to do the following calculation:
    1. Calculate the size of the bar divided by the speed of the bar
    2. Take a 20 period SMA of that calculation
    3. Compare the current bar to the SMA
    4. If the current bar is greater than the SMA, change the bar's color to blue.


    Here's what I have:


    double ticks = Range()[0] / TickSize;
    TimeSpan duration;
    duration = Time[0] - Time[1];
    double span = duration.TotalSeconds;
    double speed = ticks / span;
    speedseries.Set(speed);
    avespeedseries.Set(SMA(speedseries,20)[0]);

    if(speed > avespeedseries[0])
    {
    BarColor = Color.Blue;
    CandleOutlineColor = Color.Black;
    }

    Unfortunately, this doesn't do anything. I can even change to greater-than sign to less-than, so that's telling me there's something wrong with the code.

    Any help would be appreciated.

    #2
    Originally posted by tiger45 View Post
    I'm trying to do the following calculation:
    1. Calculate the size of the bar divided by the speed of the bar
    2. Take a 20 period SMA of that calculation
    3. Compare the current bar to the SMA
    4. If the current bar is greater than the SMA, change the bar's color to blue.


    Here's what I have:


    double ticks = Range()[0] / TickSize;
    TimeSpan duration;
    duration = Time[0] - Time[1];
    double span = duration.TotalSeconds;
    double speed = ticks / span;
    speedseries.Set(speed);
    avespeedseries.Set(SMA(speedseries,20)[0]);

    if(speed > avespeedseries[0])
    {
    BarColor = Color.Blue;
    CandleOutlineColor = Color.Black;
    }

    Unfortunately, this doesn't do anything. I can even change to greater-than sign to less-than, so that's telling me there's something wrong with the code.

    Any help would be appreciated.
    What is the error in your log?

    Comment


      #3
      If you are not getting an error, try to add the following lines to OnBarUpdate to allow for your 20MA calculation:

      If (CurrentBar < 21)

      return;



      Originally posted by tiger45 View Post
      I'm trying to do the following calculation:
      1. Calculate the size of the bar divided by the speed of the bar
      2. Take a 20 period SMA of that calculation
      3. Compare the current bar to the SMA
      4. If the current bar is greater than the SMA, change the bar's color to blue.


      Here's what I have:


      double ticks = Range()[0] / TickSize;
      TimeSpan duration;
      duration = Time[0] - Time[1];
      double span = duration.TotalSeconds;
      double speed = ticks / span;
      speedseries.Set(speed);
      avespeedseries.Set(SMA(speedseries,20)[0]);

      if(speed > avespeedseries[0])
      {
      BarColor = Color.Blue;
      CandleOutlineColor = Color.Black;
      }

      Unfortunately, this doesn't do anything. I can even change to greater-than sign to less-than, so that's telling me there's something wrong with the code.

      Any help would be appreciated.

      Comment


        #4
        Looks like two errors:

        Error on calling 'OnBarUpdate' method for indicator 'fastbars' on bar 20: Object reference not set to an instance of an object.

        Error on calling 'OnBarUpdate' method for indicator 'fastbars' on bar 0: Bar index needs to be greater/equal 0

        Comment


          #5
          Originally posted by aligator View Post
          If you are not getting an error, try to add the following lines to OnBarUpdate to allow for your 20MA calculation:

          If (CurrentBar < 21)

          return;
          That was it!! Thank you very much!

          Comment


            #6
            Originally posted by tiger45 View Post
            That was it!! Thank you very much!
            @tiger45,

            Would you mind sharing the indicator or at least a screenshot of what it looks like?

            Thanks.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            600 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            347 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            103 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            558 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            558 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X