Calculating Penetration of Moving Average by High and Low Prices

07 Mar 2021
0 comment
0 attachment

This code snippet demonstrates how to calculate the number of times the high and low prices of a stock penetrate a moving average over a specified look-back period. This can be useful for identifying potential market trends.


LBPeriod = 63
AvePeriod = 100
LowPen = summation[LBPeriod](low < Average[AvePeriod])
HighPen = summation[LBPeriod](high > Average[AvePeriod])
return LowPen coloured(128,0,0), HighPen coloured(0,128,0)

Explanation of the Code:

  • LBPeriod = 63: This sets the look-back period to 63 days. It defines the number of days over which the calculations will be performed.
  • AvePeriod = 100: This sets the period for the moving average to 100 days. The average price over these 100 days will be used as a benchmark for comparison.
  • LowPen = summation[LBPeriod](low < Average[AvePeriod]): This line calculates the total number of days within the look-back period where the daily low price is below the 100-day moving average.
  • HighPen = summation[LBPeriod](high > Average[AvePeriod]): Similarly, this line calculates the total number of days within the look-back period where the daily high price exceeds the 100-day moving average.
  • return LowPen coloured(128,0,0), HighPen coloured(0,128,0): The function returns two values, LowPen and HighPen, each colored differently for visual distinction in charting software. LowPen is colored in a shade of red, and HighPen in green, making it easier to differentiate the two measures visually.

This code is particularly useful for traders and analysts looking to gauge market sentiment over a given period by observing how often prices break above or below a moving average, which is often considered a significant technical indicator in financial markets.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/average-penetration-filter/#post-73378

Visit Link
What is a Snippet? A snippet is a small, reusable chunk of code designed to solve specific tasks quickly. Think of it as a shortcut that helps you achieve your coding goals without reinventing the wheel. How to Use: Simply copy the snippet and paste it into your project where needed. Don't forget to tweak it to fit your context. Snippets are not just time-savers; they're also learning tools to help you become a more efficient coder.
Vonasi Master
V-oyaging ON A S-mall I-ncome
Author’s Profile

Comments

Search Snippets

Showing some results...
Sorry, no result found!

Snippets Categories

global
35
indicator
133
strategy
171

Recent Snippets

How to Create a Simple MTF Trend Dashboard with EMA and SMA
indicator
This indicator builds a compact multi-timeframe (MTF) dashboard that shows whether price is trading above or below a [...]
How to Display Per-Bar Volume Accumulation in Real Time (Intrabar Updates)
global
This snippet tracks and displays the current bar’s accumulated volume while the bar is still forming, instead of only [...]
Ticks Counter: Count Tick Updates Per Bar on Tick or Time Charts
global
This snippet counts how many tick updates have occurred for the current bar by incrementing a per-bar counter on each [...]
How to Build a Step-Based Trailing Stop That Moves to Break-Even First
strategy
This snippet implements a step trailing stop that advances in fixed increments once price reaches predefined profit [...]
Utilizing Arrays to Track and Compare Indicator Values Within the Same Bar in ProBuilder
indicator
This ProBuilder code snippet demonstrates how to use arrays to compare the values of an indicator (RSI in this case) [...]
Logo Logo
Loading...