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 CarlTrading, 03-31-2026, 09:41 PM
            1 response
            152 views
            1 like
            Last Post NinjaTrader_ChelseaB  
            Started by CarlTrading, 04-01-2026, 02:41 AM
            0 responses
            89 views
            1 like
            Last Post CarlTrading  
            Started by CaptainJack, 03-31-2026, 11:44 PM
            0 responses
            131 views
            2 likes
            Last Post CaptainJack  
            Started by CarlTrading, 03-30-2026, 11:51 AM
            0 responses
            127 views
            1 like
            Last Post CarlTrading  
            Started by CarlTrading, 03-30-2026, 11:48 AM
            0 responses
            107 views
            0 likes
            Last Post CarlTrading  
            Working...
            X