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

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 Waxavi, Today, 02:10 AM
            0 responses
            3 views
            0 likes
            Last Post Waxavi
            by Waxavi
             
            Started by TradeForge, Today, 02:09 AM
            0 responses
            9 views
            0 likes
            Last Post TradeForge  
            Started by Waxavi, Today, 02:00 AM
            0 responses
            2 views
            0 likes
            Last Post Waxavi
            by Waxavi
             
            Started by elirion, Today, 01:36 AM
            0 responses
            4 views
            0 likes
            Last Post elirion
            by elirion
             
            Started by gentlebenthebear, Today, 01:30 AM
            0 responses
            4 views
            0 likes
            Last Post gentlebenthebear  
            Working...
            X