• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Indicator based on an earlier period, rather than current period.

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

    Indicator based on an earlier period, rather than current period.

    I am trying to create an indicator that averages the volume of the first few bars of the day.

    So even though it's 11 AM or 2PM, I want this indicator to still display what the SMA of volume was from the first 10 or so bars, using tick charting.

    Can anyone help?

    Thanks!

    #2
    Hello jasonbradh,

    Thank you for writing in.

    I have created an example below that will plot the SMA of volume over the first 10 bars of a new session. It will continue to plot that last value until the next session occurs again.

    Code:
    private int startBar = 0;
    private int endBar = 0;
    private double lastSMAValue = 0;
    
    protected override void OnBarUpdate()
    {
            // check if the current bar the indicator is evaluating over the first bar of a new session
    	if (Bars.FirstBarOfSession)
    	{
                    // set startBar to CurrentBar and set endBar to CurrentBar + 9, which would be nine bars ahead
                    // CurrentBar is the index of the bar the script is evaluating over
    		startBar = CurrentBar;
    		endBar = CurrentBar + 9;
    	}
    	
            // plot the volume SMA if CurrentBar is more than or equal to startBar and CurrentBar is less than or equal to endBar
    	if (CurrentBar >= startBar && CurrentBar <= endBar)
    	{
    		Value.Set(SMA(Volume, 14)[0]);
                    // set lastSMAValue to the current value of the volume SMA
    		lastSMAValue = SMA(Volume, 14)[0];
    	}
    	
            // if CurrentBar is more than endBar, this means the script is evaluating a bar outside of our range we have set
            // we'll set our plot to lastSMAValue while outside of this range
    	if (CurrentBar > endBar)
    		Value.Set(lastSMAValue);
    }
    Please, let me know if you have any questions on this sample.
    Zachary G.NinjaTrader Customer Service

    Comment


      #3
      It works beautifully. Thank you so much!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by [email protected], Yesterday, 07:45 AM
      1 response
      7 views
      0 likes
      Last Post koganam
      by koganam
       
      Started by tgn55, Today, 07:22 AM
      0 responses
      2 views
      0 likes
      Last Post tgn55
      by tgn55
       
      Started by U0000999, Today, 05:17 AM
      0 responses
      4 views
      0 likes
      Last Post U0000999  
      Started by Prataturry89, Today, 03:40 AM
      0 responses
      5 views
      0 likes
      Last Post Prataturry89  
      Started by UltraFastKetoBoost28, Today, 02:43 AM
      0 responses
      6 views
      0 likes
      Last Post UltraFastKetoBoost28  
      Working...
      X