PineCoders

Averages - PineCoders FAQ

The need to calculate averages (arithmetic mean) comes up here and there in scripts. When you want the average of a value only when a given condition has occurred, you will often not be able to use the standard sma function in Pine because it will average all values in the series. Even if you take care to use your condition to set non-conforming values to zero, this ends up skewing your results with zero values.

This script calculates:
1. The running average of all up volumes,
2. The average of last x occurrences of up volume ,
3. The average up volume occurrences in last y bars.
May 17
Release Notes:
  • Made code for cases 2 and 3 into functions.
  • Added examples of multiple-result functions.
Aug 01
Release Notes: Conversion to v4.
Aug 01
Release Notes: Fixes to comments.
Sep 15
Release Notes: Simplified Case 1 code.
Sep 15
Release Notes: Changes to comments.
Remove from Favorite Scripts Add to Favorite Scripts
http://www.pinecoders.com — Tools and ideas for all Pine coders.
Home Stock Screener Forex Screener Crypto Screener Economic Calendar Shows How It Works Chart Features Pricing House Rules Moderators Website & Broker Solutions Widgets Charting Solutions Lightweight Charting Library Help Center Refer a friend Feature Request Blog & News FAQ Wiki Twitter
Profile Profile Settings Account and Billing Refer a friend My Support Tickets Help Center Ideas Published Followers Following Private Messages Chat Sign Out