Calculating Price Proximity to a 200-Day Simple Moving Average

01 Jul 2019
0 comment
0 attachment

This code snippet demonstrates how to calculate the proximity of the current closing price to the 200-day Simple Moving Average (SMA200) in terms of percentage. This can be useful for identifying how close the price is to this moving average, which is often used to determine long-term market trends.


percent = 3
ratio = close/average[200]
percent = percent/100
test = ratio>=1-percent and ratio<=1+percent
screener[test](ratio)

Explanation of the Code:

  • percent = 3: This line sets the proximity threshold to 3%. This means we are interested in cases where the price is within 3% of the SMA200.
  • ratio = close/average[200]: Here, the code calculates the ratio of the current closing price (close) to the 200-day Simple Moving Average (average[200]). This ratio helps in comparing the current price directly to the SMA200.
  • percent = percent/100: Converts the percentage from a whole number to a decimal for calculation purposes. For example, 3 becomes 0.03.
  • test = ratio>=1-percent and ratio<=1+percent: This line creates a logical test to check if the ratio is within the specified percentage range (±3% in this case) around 1 (which represents exact equality with SMA200).
  • screener[test](ratio): This function call uses the test condition to filter or screen data. If the condition is true, it outputs the ratio, indicating how close the price is to the SMA200 within the specified proximity.

This snippet is particularly useful for traders or analysts who want to monitor stocks or securities that are trading close to their 200-day moving average, a common indicator of long-term trends.

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.
Nicolas Master
I created ProRealCode because I believe in the power of shared knowledge. I spend my time coding new tools and helping members solve complex problems. If you are stuck on a code or need a fresh perspective on a strategy, I am always willing to help. Welcome to the community!
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...