Point and Figure (PnF) CCIThis is live and non-repainting Point and Figure Chart Commodity Channel Index - CCI tool. The script has it’s own P&F engine and not using integrated function of Trading View.
Point and Figure method is over 150 years old. It consist of columns that represent filtered price movements. Time is not a factor on P&F chart but as you can see with this script P&F chart created on time chart.
P&F chart provide several advantages, some of them are filtering insignificant price movements and noise, focusing on important price movements and making support/resistance levels much easier to identify.
Commodity Channel Index – CCI was developed by Donalt Lambert. CCI can be used to identify overbought or oversold, a new trend or warn of extreme conditions. CCI measures the difference between a security's price change and its average price change. High positive readings indicate that prices are well above their average, which is a show of strength. Low negative readings indicate that prices are well below their average, which is a show of weakness.
The Formula for the Commodity Channel Index ( CCI ) Is:
CCI = (Typical Price – L-period SMA of TP) / (0.015 * Mean Deviation)
Mean Deviation = (SumOf 1->L ( |TP – MA| )) / L
L = Length
TP = Typical Price
If you are new to Point & Figure Chart then you better get some information about it before using this tool. There are very good web sites and books. Please PM me if you need help about resources.
Options in the Script
Box size is one of the most important part of Point and Figure Charting. Chart price movement sensitivity is determined by the Point and Figure scale. Large box sizes see little movement across a specific price region, small box sizes see greater price movement on P&F chart. There are four different box scaling with this tool: Traditional, Percentage, Dynamic (ATR), or User-Defined
4 different methods for Box size can be used in this tool.
User Defined: The box size is set by user. A larger box size will result in more filtered price movements and fewer reversals. A smaller box size will result in less filtered price movements and more reversals.
ATR: Box size is dynamically calculated by using ATR, default period is 20.
Percentage: uses box sizes that are a fixed percentage of the stock's price. If percentage is 1 and stock’s price is $100 then box size will be $1
Traditional: uses a predefined table of price ranges to determine what the box size should be.
Price Range Box Size
Under 0.25 0.0625
0.25 to 1.00 0.125
1.00 to 5.00 0.25
5.00 to 20.00 0.50
20.00 to 100 1.0
100 to 200 2.0
200 to 500 4.0
500 to 1000 5.0
1000 to 25000 50.0
25000 and up 500.0
Default value is “ATR”, you may use one of these scaling method that suits your trading strategy.
If ATR or Percentage is chosen then there is rounding algorithm according to mintick value of the security. For example if mintick value is 0.001 and box size (ATR/Percentage) is 0.00124 then box size becomes 0.001.
And also while using dynamic box size (ATR or Percentage), box size changes only when closing price changed.
Reversal : It is the number of boxes required to change from a column of Xs to a column of Os or from a column of Os to a column of Xs. Default value is 3 (most used). For example if you choose reversal = 2 then you get the chart similar to Renko chart.
Source: Closing price or High-Low prices can be chosen as data source for P&F charting.
Upper Band : as default, Upper band is 100
Lower Band : as default, Lower band is -100
There are alerts when P&F CCI moves above Upper Band or moves below Lower Band.
Tìm kiếm tập lệnh với "美股标普500指数基金中国"
Point and Figure (PnF) Bollinger BandsThis is live and non-repainting Point and Figure Chart Bollinger Bands tool. The script has it’s own P&F engine and not using integrated function of Trading View.
Point and Figure method is over 150 years old. It consist of columns that represent filtered price movements. Time is not a factor on P&F chart but as you can see with this script P&F chart created on time chart.
P&F chart provide several advantages, some of them are filtering insignificant price movements and noise, focusing on important price movements and making support/resistance levels much easier to identify.
P&F Bollinger Bands is calculated and shown by using its own P&F engine. Because of Point and Figure Chart Moving averages are already smoothed, better to use smaller moving average periods, 5 or 10 etc. This period can be chosen by prives movements and characteristics. You can see the consolidation areas and with P&F Breakout signals it’s possible to see the direction. Narrowing bands indicate a consolidation and narrowing does not provide a direction clue. You must look for the next P&F signal to establish direction. But beware of the ‘head fake’. This occurs when prices break a band, then suddenly reverse and move the other way (Trap).
An example for Head Fake:
If you are new to Point & Figure Chart then you better get some information about it before using this tool. There are very good web sites and books. Please PM me if you need help about resources.
Options in the Script
Box size is one of the most important part of Point and Figure Charting. Chart price movement sensitivity is determined by the Point and Figure scale. Large box sizes see little movement across a specific price region, small box sizes see greater price movement on P&F chart. There are four different box scaling with this tool: Traditional, Percentage, Dynamic (ATR), or User-Defined
4 different methods for Box size can be used in this tool.
User Defined: The box size is set by user. A larger box size will result in more filtered price movements and fewer reversals. A smaller box size will result in less filtered price movements and more reversals.
ATR: Box size is dynamically calculated by using ATR, default period is 20.
Percentage: uses box sizes that are a fixed percentage of the stock's price. If percentage is 1 and stock’s price is $100 then box size will be $1
Traditional: uses a predefined table of price ranges to determine what the box size should be.
Price Range Box Size
Under 0.25 0.0625
0.25 to 1.00 0.125
1.00 to 5.00 0.25
5.00 to 20.00 0.50
20.00 to 100 1.0
100 to 200 2.0
200 to 500 4.0
500 to 1000 5.0
1000 to 25000 50.0
25000 and up 500.0
Default value is “ATR”, you may use one of these scaling method that suits your trading strategy.
If ATR or Percentage is chosen then there is rounding algorithm according to mintick value of the security. For example if mintick value is 0.001 and box size (ATR/Percentage) is 0.00124 then box size becomes 0.001.
And also while using dynamic box size (ATR or Percentage), box size changes only when closing price changed.
Reversal : It is the number of boxes required to change from a column of Xs to a column of Os or from a column of Os to a column of Xs. Default value is 3 (most used). For example if you choose reversal = 2 then you get the chart similar to Renko chart.
Source: Closing price or High-Low prices can be chosen as data source for P&F charting.
Options P&F Bollimger Bands:
Length: Base Moving Average Length, default value is 5
StdDev: Standart Deviation, default value ise 2. (Standart deviation is calculated by the engine)
MA Source: Moving averages on P&F charts are based on the average price of each column. Bar chart moving averages are based on each close price. Average price means “(ClosePrice + OpenPrice) / 2”. You can choose Close Price or Average Price as source. Default is Average Price.
Point and Figure (PnF) RSIThis is live and non-repainting Point and Figure Chart RSI tool. The script has it’s own P&F engine and not using integrated function of Trading View.
Point and Figure method is over 150 years old. It consist of columns that represent filtered price movements. Time is not a factor on P&F chart but as you can see with this script P&F chart created on time chart.
P&F chart provide several advantages, some of them are filtering insignificant price movements and noise, focusing on important price movements and making support/resistance levels much easier to identify.
P&F RSI is calculated and shown by using its own P&F engine.
If you are new to Point & Figure Chart then you better get some information about it before using this tool. There are very good web sites and books. Please PM me if you need help about resources.
Options in the Script
Box size is one of the most important part of Point and Figure Charting. Chart price movement sensitivity is determined by the Point and Figure scale. Large box sizes see little movement across a specific price region, small box sizes see greater price movement on P&F chart. There are four different box scaling with this tool: Traditional, Percentage, Dynamic (ATR), or User-Defined
4 different methods for Box size can be used in this tool.
User Defined: The box size is set by user. A larger box size will result in more filtered price movements and fewer reversals. A smaller box size will result in less filtered price movements and more reversals.
ATR: Box size is dynamically calculated by using ATR, default period is 20.
Percentage: uses box sizes that are a fixed percentage of the stock's price. If percentage is 1 and stock’s price is $100 then box size will be $1
Traditional: uses a predefined table of price ranges to determine what the box size should be.
Price Range Box Size
Under 0.25 0.0625
0.25 to 1.00 0.125
1.00 to 5.00 0.25
5.00 to 20.00 0.50
20.00 to 100 1.0
100 to 200 2.0
200 to 500 4.0
500 to 1000 5.0
1000 to 25000 50.0
25000 and up 500.0
Default value is “ATR”, you may use one of these scaling method that suits your trading strategy.
If ATR or Percentage is chosen then there is rounding algorithm according to mintick value of the security. For example if mintick value is 0.001 and box size (ATR/Percentage) is 0.00124 then box size becomes 0.001.
And also while using dynamic box size (ATR or Percentage), box size changes only when closing price changed.
Reversal : It is the number of boxes required to change from a column of Xs to a column of Os or from a column of Os to a column of Xs. Default value is 3 (most used). For example if you choose reversal = 2 then you get the chart similar to Renko chart.
Source: Closing price or High-Low prices can be chosen as data source for P&F charting.
you can use PNF type RSI or RENKO type RSI.
What is the difference between them?
While calculating PNF type RSI, the script checks last X/O column's closing price but when using RENKO type RSI the scipt calculates RSI on every price changes according to number of boxes. and also with RENKO type RSI, calculation is made for each boxes on price changes.
Important note if you use this PNF script with reversal = 2 then you get RENKO chart. So, with this RENKO chart better to use RENKO type RSI ;)
Point and Figure (PnF) ChartThis is live and non-repainting Point and Figure Charting tool. The tool has it’s own P&F engine and not using integrated function of Trading View.
Point and Figure method is over 150 years old. It consist of columns that represent filtered price movements. Time is not a factor on P&F chart but as you can see with this script P&F chart created on time chart.
P&F chart provide several advantages, some of them are filtering insignificant price movements and noise, focusing on important price movements and making support/resistance levels much easier to identify.
If you are new to Point & Figure Chart then you better get some information about it before using this tool. There are very good web sites and books. Please PM me if you need help about resources.
Options in the Script
Box size is one of the most important part of Point and Figure Charting. Chart price movement sensitivity is determined by the Point and Figure scale. Large box sizes see little movement across a specific price region, small box sizes see greater price movement on P&F chart. There are four different box scaling with this tool: Traditional, Percentage, Dynamic (ATR), or User-Defined
4 different methods for Box size can be used in this tool.
User Defined: The box size is set by user. A larger box size will result in more filtered price movements and fewer reversals. A smaller box size will result in less filtered price movements and more reversals.
ATR: Box size is dynamically calculated by using ATR, default period is 20.
Percentage: uses box sizes that are a fixed percentage of the stock's price. If percentage is 1 and stock’s price is $100 then box size will be $1
Traditional: uses a predefined table of price ranges to determine what the box size should be.
Price Range Box Size
Under 0.25 0.0625
0.25 to 1.00 0.125
1.00 to 5.00 0.25
5.00 to 20.00 0.50
20.00 to 100 1.0
100 to 200 2.0
200 to 500 4.0
500 to 1000 5.0
1000 to 25000 50.0
25000 and up 500.0
Default value is “ATR”, you may use one of these scaling method that suits your trading strategy.
If ATR or Percentage is chosen then there is rounding algorithm according to mintick value of the security. For example if mintick value is 0.001 and box size (ATR/Percentage) is 0.00124 then box size becomes 0.001.
And also while using dynamic box size (ATR or Percentage), box size changes only when closing price changed.
Reversal : It is the number of boxes required to change from a column of Xs to a column of Os or from a column of Os to a column of Xs. Default value is 3 (most used). For example if you choose reversal = 2 then you get the chart similar to Renko chart.
Source: Closing price or High-Low prices can be chosen as data source for P&F charting.
Chart Style: There are 3 options for chart style: “Candle”, “Area” or “Don’t show”.
As Area:
As Candle:
X/O Column Style: it can show all columns from opening price or only last Xs/Os.
Color Theme: different themes exist => Green/Red, Yellow/Blue, White/Yellow, Orange/Blue, Lime/Red, Blue/Red
Show Breakouts is the option to show Breakouts
This tool detects & shows following Breakouts:
Triple Top/Bottom,
Triple Top Ascending,
Triple Bottom Descending,
Simple Buy/Sell (Double Top/Bottom),
Simple Buy With Rising Bottom,
Simple Sell With Declining Top
Catapult bullish/bearish
Show Horizontal Count Targets: Finds the congestion or consolidation pattern and if there is breakout then it calculates the Target by using Horizontal Count method (based on the width of congestion pattern). It shows how many column exist on congestion area. There is no guarantee that prices will reach the target.
Show Vertical Count Targets: When Triple Top/Bottom Breakouts occured the script calculates the target by using Vertical Count Method (based on the length of the column). There is no guarantee that prices will reach the target.
For both methods there is auto target cancellation if price goes below congestion bottom or above congestion top.
trend is calculated by EMA of closing price of the P&F
Whipsaw protection:
Last options are “Show info panel” and Labeling Offset. Script shows current box size, reversal, and recommanded minimum and maximum box size. And also it shows the price level to reverse the column (Xs <-> Os) and the price level to add at least 1 more box to column. This is the option to put these labels 10, 20, 30, 50 or 100 bars away from the last bar. Labeling content and color change according to X/O column.
do not hesitate to comment.
Candlesticks ANN for Stock Markets TF : 1WHello, this script consists of training candlesticks with Artificial Neural Networks (ANN).
In addition to the first series, candlesticks' bodies and wicks were also introduced as training inputs.
The inputs are individually trained to find the relationship between the subsequent historical value of all candlestick values 1.(High,Low,Close,Open)
The outputs are adapted to the current values with a simple forecast code.
Once the OHLC value is found, the exponential moving averages of 5 and 20 periods are used.
Reminder : OHLC = (Open + High + Close + Low ) / 4
First version :
Script is designed for S&P 500 Indices,Funds,ETFs, especially S&P 500 Stocks,and for all liquid Stocks all around the World.
NOTE: This script is only suitable for 1W time-frame for Stocks.
The average training error rates are less than 5 per thousand for each candlestick variable. (Average Error < 0.005 )
I've just finished it and haven't tested it in detail.
So let's use it carefully as a supporter.
Best regards !
TNZ - Index above MA Use this indicator to filter stock selection based on the relevant index value being above the selected simple moving average.
For example, only buying the S+P 500 stock if the S+P 500 index value is above the 10 period moving average.
The time frame used is that displayed
Macroeconomic Artificial Neural Networks
This script was created by training 20 selected macroeconomic data to construct artificial neural networks on the S&P 500 index.
No technical analysis data were used.
The average error rate is 0.01.
In this respect, there is a strong relationship between the index and macroeconomic data.
Although it affects the whole world,I personally recommend using it under the following conditions: S&P 500 and related ETFs in 1W time-frame (TF = 1W SPX500USD, SP1!, SPY, SPX etc. )
Macroeconomic Parameters
Effective Federal Funds Rate (FEDFUNDS)
Initial Claims (ICSA)
Civilian Unemployment Rate (UNRATE)
10 Year Treasury Constant Maturity Rate (DGS10)
Gross Domestic Product , 1 Decimal (GDP)
Trade Weighted US Dollar Index : Major Currencies (DTWEXM)
Consumer Price Index For All Urban Consumers (CPIAUCSL)
M1 Money Stock (M1)
M2 Money Stock (M2)
2 - Year Treasury Constant Maturity Rate (DGS2)
30 Year Treasury Constant Maturity Rate (DGS30)
Industrial Production Index (INDPRO)
5-Year Treasury Constant Maturity Rate (FRED : DGS5)
Light Weight Vehicle Sales: Autos and Light Trucks (ALTSALES)
Civilian Employment Population Ratio (EMRATIO)
Capacity Utilization (TOTAL INDUSTRY) (TCU)
Average (Mean) Duration Of Unemployment (UEMPMEAN)
Manufacturing Employment Index (MAN_EMPL)
Manufacturers' New Orders (NEWORDER)
ISM Manufacturing Index (MAN : PMI)
Artificial Neural Network (ANN) Training Details :
Learning cycles: 16231
AutoSave cycles: 100
Grid
Input columns: 19
Output columns: 1
Excluded columns: 0
Training example rows: 998
Validating example rows: 0
Querying example rows: 0
Excluded example rows: 0
Duplicated example rows: 0
Network
Input nodes connected: 19
Hidden layer 1 nodes: 2
Hidden layer 2 nodes: 0
Hidden layer 3 nodes: 0
Output nodes: 1
Controls
Learning rate: 0.1000
Momentum: 0.8000 (Optimized)
Target error: 0.0100
Training error: 0.010000
NOTE : Alerts added . The red histogram represents the bear market and the green histogram represents the bull market.
Bars subject to region changes are shown as background colors. (Teal = Bull , Maroon = Bear Market )
I hope it will be useful in your studies and analysis, regards.
Damped Sine Wave Weighted FilterIntroduction
Remember that we can make filters by using convolution, that is summing the product between the input and the filter coefficients, the set of filter coefficients is sometime denoted "kernel", those coefficients can be a same value (simple moving average), a linear function (linearly weighted moving average), a gaussian function (gaussian filter), a polynomial function (lsma of degree p with p = order of the polynomial), you can make many types of kernels, note however that it is easy to fall into the redundancy trap.
Today a low-lag filter who weight the price with a damped sine wave is proposed, the filter characteristics are discussed below.
A Damped Sine Wave
A damped sine wave is a like a sine wave with the difference that the sine wave peak amplitude decay over time.
A damped sine wave
Used Kernel
We use a damped sine wave of period length as kernel.
The coefficients underweight older values which allow the filter to reduce lag.
Step Response
Because the filter has overshoot in the step response we can conclude that there are frequencies amplified in the passband, we could have reached to this conclusion by simply seeing the negative values in the kernel or the "zero-lag" effect on the closing price.
Enough ! We Want To See The Filter !
I should indeed stop bothering you with transient responses but its always good to see how the filter act on simpler signals before seeing it on the closing price. The filter has low-lag and can be used as input for other indicators
Filter with length = 100 as input for the rsi.
The bands trailing stop utility using rolling squared mean average error with length 500 using the filter of length 500 as input.
Approximating A Least Squares Moving Average
A least squares moving average has a linear kernel with certain values under 0, a lsma of length k can be approximated using the proposed filter using period p where p = k + k/4 .
Proposed filter (red) with length = 250 and lsma (blue) with length = 200.
Conclusions
The use of damping in filter design can provide extremely useful filters, in fact the ideal kernel, the sinc function, is also a damped sine wave.
VIX reversion-Buschi
English:
A significant intraday reversion (commonly used: 3 points) on a high (over 20 points) S&P 500 Volatility Index (VIX) can be a sign of a market bottom, because there is the assumption that some of the "big guys" liquidated their options / insurances because the worst is over.
This indicator shows these reversions (3 points as default) when the VIX was over 20 points. The character "R" is then shown directly over the daily column, the VIX need not to be loaded explicitly.
Deutsch:
Eine deutliche Intraday-Umkehr (3 Punkte im Normalfall) bei einem hohen (über 20 Punkte) S&P 500 Volatility Index (VIX) kann ein Zeichen für eine Bodenbildung im Markt sein, weil möglicherweise einige "große Jungs" ihre Optionen / Versicherungen auflösen, weil das schlimmste vorbei ist.
Dieser Indikator zeigt diese Umkehr (Standardwert: 3 Punkte), wenn der VIX vorher über 20 Punkte lag. Der Buchstabe "R" wird dabei direkt über dem Tagesbalken angezeigt, wobei der VIX nicht explizit geladen werden muss.
Relative Price StrengthThe strength of a stock relative to the S&P 500 is key part of most traders decision making process. Hence the default reference security is SPY, the most commonly trades S&P 500 ETF.
Most profitable traders buy stocks that are showing persistence intermediate strength verses the S&P as this has been shown to work. Hence the default period is 63 days or 3 months.
TICK Extremes IndicatorSimple TICK indicator, plots candles and HL2 line
Conditional green/red coloring for highs above 500, 900 and lows above 0, and for lows below -500, -900, and highs above 0
Probably best used for 1 - 5 min timeframes
Always open to suggestions if criteria needs tweaking or if something else would make it more useful or user-friendly!
Market direction and pullback based on S&P 500.A simple indicator based on www.swing-trade-stocks.com The link is also the guide for how to use it.
0 - nothing. If the indicator is showing 0 for a prolonged amount of time, it is likely the market is in "momentum mode" (referred to in the link above).
1 - indicates an uptrend based on SMA and EMA and also a place where a reversal to the upside is likely to occur. You should look only for long trades in the stock market when you see a spike upwards and S&P 500 is showing an obvious uptrend.
-1 - indicates a downtrend based on SMA and EMA and also a place where a reversal to the downside is likely to occur. You should look only for short trades in the stock market when you see a spike upwards and S&P 500 is showing an obvious uptrend.
Net XRP Margin PositionTotal XRP Longs minus XRP Shorts in order to give you the total outstanding XRP margin debt.
ie: If 500,000 XRP has been longed, and 400,000 XRP has been shorted, then 500,000 has been bought, and 400,000 sold, leaving us with 100,000 XRP (net) remaining to be sold to give us an overall neutral margin position.
That isn't to say that the net margin position must move towards zero, but it is a sensible reference point, and historical net values may provide useful insights into the current circumstances.
Net DASH Margin PositionTotal DASH Longs minus DASH Shorts in order to give you the total outstanding DASH margin debt.
ie: If 500,000 DASH has been longed, and 400,000 DASH has been shorted, then 500,000 has been bought, and 400,000 sold, leaving us with 100,000 DASH (net) remaining to be sold to give us an overall neutral margin position.
That isn't to say that the net margin position must move towards zero, but it is a sensible reference point, and historical net values may provide useful insights into the current circumstances.
(Anyone know what category this script should be in?)
Net NEO Margin PositionTotal NEO Longs minus NEO Shorts in order to give you the total outstanding NEO margin debt.
ie: If 500,000 NEO has been longed, and 400,000 NEO has been shorted, then 500,000 has been bought, and 400,000 sold, leaving us with 100,000 NEO (net) remaining to be sold to give us an overall neutral margin position.
That isn't to say that the net margin position must move towards zero, but it is a sensible reference point, and historical net values may provide useful insights into the current circumstances.
(Anyone know what category this script should be in?)
Everyday 0002 _ MAC 1st Trading Hour WalkoverThis is the second strategy for my Everyday project.
Like I wrote the last time - my goal is to create a new strategy everyday
for the rest of 2016 and post it here on TradingView.
I'm a complete beginner so this is my way of learning about coding strategies.
I'll give myself between 15 minutes and 2 hours to complete each creation.
This is basically a repetition of the first strategy I wrote - a Moving Average Crossover,
but I added a tiny thing.
I read that "Statistics have proven that the daily high or low is established within the first hour of trading on more than 70% of the time."
(source: )
My first Moving Average Crossover strategy, tested on VOLVB daily, got stoped out by the volatility
and because of this missed one nice bull run and a very nice bear run.
So I added this single line: if time("60", "1000-1600") regarding when to take exits:
if time("60", "1000-1600")
strategy.exit("Close Long", "Long", profit=2000, loss=500)
strategy.exit("Close Short", "Short", profit=2000, loss=500)
Sweden is UTC+2 so I guess UTC 1000 equals 12.00 in Stockholm. Not sure if this is correct, actually.
Anyway, I hope this means the strategy will only take exits based on price action which occur in the afternoon, when there is a higher probability of a lower volatility.
When I ran the new modified strategy on the same VOLVB daily it didn't get stoped out so easily.
On the other hand I'll have to test this on various stocks .
Reading and learning about how to properly test strategies is on my todo list - all tips on youtube videos or blogs
to read on this topic is very welcome!
Like I said the last time, I'm posting these strategies hoping to learn from the community - so any feedback, advice, or corrections is very much welcome and appreciated!
/pbergden
ADR levels+// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at mozilla.org
// © notprofessorgreen
//@version=5
indicator("ADR levels", shorttitle = 'ADR', overlay=true, max_bars_back=5000, max_lines_count=500)
// Error catching
if (timeframe.in_seconds() >= timeframe.in_seconds('D'))
runtime.error('Timeframe cannot be greater than Daily')
// Inputs
adr_days = input.int(10, title = 'Days', maxval=250, minval = 1)
std_x = input.float(1.0, "Scale Factor")
width = input.int(1, "Line Width")
// ADR line inputs
adr_color = input.color(color.gray, "ADR Color")
adr_style = input.string("solid", "ADR Style", options= )
// Standard deviation inputs
std_dev_0_5 = input.float(0.5, "Std Dev 1 Multiplier", minval=0.1, maxval=5.0)
std_0_5_show = input.bool(true, "Show Std Dev 1", inline="std1")
std_0_5_color = input.color(color.gray, "Std Dev 1 Color", inline="std1")
std_0_5_style = input.string("dotted", "Std Dev 1 Style", options= , inline="std1")
std_dev_1 = input.float(1.0, "Std Dev 2 Multiplier", minval=0.1, maxval=5.0)
std_1_show = input.bool(true, "Show Std Dev 2", inline="std2")
std_1_color = input.color(color.gray, "Std Dev 2 Color", inline="std2")
std_1_style = input.string("dotted", "Std Dev 2 Style", options= , inline="std2")
std_dev_2 = input.float(2.0, "Std Dev 3 Multiplier", minval=0.1, maxval=5.0)
std_2_show = input.bool(true, "Show Std Dev 3", inline="std3")
std_2_color = input.color(color.gray, "Std Dev 3 Color", inline="std3")
std_2_style = input.string("dotted", "Std Dev 3 Style", options= , inline="std3")
// Fibonacci inputs
fib_1_level = input.float(0.3, "Fib Level 1", minval=0, maxval=2.0)
fib_1_show = input.bool(true, "Show Fib 1", inline="fib1")
fib_1_color = input.color(color.blue, "Fib 1 Color", inline="fib1")
fib_1_style = input.string("dashed", "Fib 1 Style", options= , inline="fib1")
fib_2_level = input.float(0.5, "Fib Level 2", minval=0, maxval=2.0)
fib_2_show = input.bool(true, "Show Fib 2", inline="fib2")
fib_2_color = input.color(color.blue, "Fib 2 Color", inline="fib2")
fib_2_style = input.string("dashed", "Fib 2 Style", options= , inline="fib2")
fib_3_level = input.float(0.7, "Fib Level 3", minval=0, maxval=2.0)
fib_3_show = input.bool(true, "Show Fib 3", inline="fib3")
fib_3_color = input.color(color.blue, "Fib 3 Color", inline="fib3")
fib_3_style = input.string("dashed", "Fib 3 Style", options= , inline="fib3")
show_labels = input.bool(true, "Show Labels")
// Stats table inputs
show_stats = input.bool(true, "Show Table")
sample_size = input.bool(true, "Show Sample Sizes")
tbl_loc = input.string('Bottom Right', "Table", options = )
tbl_size = input.string('Tiny', "", options = )
rch_color = input.color(color.rgb(3, 131, 99, 70), "Reached ")
csd_color = input.color(color.rgb(127, 1, 185, 70), "Closed Through ")
// Function to convert style string to line style
get_line_style(string style) =>
switch style
"solid" => line.style_solid
"dashed" => line.style_dashed
"dotted" => line.style_dotted
// Variables
reset = session.islastbar_regular
var float track_highs = 0.00
var float track_lows = 0.00
var float today_adr = 0.00
var adrs = array.new_float(adr_days, 0.00)
var line adr_pos = na
var line adr_neg = na
var line fib_1_pos = na
var line fib_1_neg = na
var line fib_2_pos = na
var line fib_2_neg = na
var line fib_3_pos = na
var line fib_3_neg = na
var line std_0_5_pos = na
var line std_0_5_neg = na
var line std_1_pos = na
var line std_1_neg = na
var line std_2_pos = na
var line std_2_neg = na
var label fib_1_pos_lbl = na
var label fib_1_neg_lbl = na
var label fib_2_pos_lbl = na
var label fib_2_neg_lbl = na
var label fib_3_pos_lbl = na
var label fib_3_neg_lbl = na
var label adr_pos_lbl = na
var label adr_neg_lbl = na
var label std_0_5_pos_lbl = na
var label std_0_5_neg_lbl = na
var label std_1_pos_lbl = na
var label std_1_neg_lbl = na
var label std_2_pos_lbl = na
var label std_2_neg_lbl = na
// ADR calculation
track_highs := reset ? high : math.max(high, track_highs )
track_lows := reset ? low : math.min(low, track_lows )
if reset
array.unshift(adrs, math.round_to_mintick(track_highs - track_lows ))
if array.size(adrs) > adr_days
array.pop(adrs)
today_adr := math.round_to_mintick(array.avg(adrs))
// Delete previous lines and labels
line.delete(adr_pos )
line.delete(adr_neg )
line.delete(fib_1_pos )
line.delete(fib_1_neg )
line.delete(fib_2_pos )
line.delete(fib_2_neg )
line.delete(fib_3_pos )
line.delete(fib_3_neg )
line.delete(std_0_5_pos )
line.delete(std_0_5_neg )
line.delete(std_1_pos )
line.delete(std_1_neg )
line.delete(std_2_pos )
line.delete(std_2_neg )
label.delete(fib_1_pos_lbl )
label.delete(fib_1_neg_lbl )
label.delete(fib_2_pos_lbl )
label.delete(fib_2_neg_lbl )
label.delete(fib_3_pos_lbl )
label.delete(fib_3_neg_lbl )
label.delete(adr_pos_lbl )
label.delete(adr_neg_lbl )
label.delete(std_0_5_pos_lbl )
label.delete(std_0_5_neg_lbl )
label.delete(std_1_pos_lbl )
label.delete(std_1_neg_lbl )
label.delete(std_2_pos_lbl )
label.delete(std_2_neg_lbl )
// Draw ADR lines
adr_pos := line.new(bar_index, open + today_adr, bar_index+50, open + today_adr,
width=width, color=adr_color, style=get_line_style(adr_style))
adr_neg := line.new(bar_index, open - today_adr, bar_index+50, open - today_adr,
width=width, color=adr_color, style=get_line_style(adr_style))
// Draw ADR labels
if show_labels
adr_pos_lbl := label.new(bar_index+50, open + today_adr, "ADR High (" + str.tostring(adr_days) + "D)",
xloc=xloc.bar_index, textalign=text.align_left, textcolor=adr_color, color=color.new(color.blue, 90), style=label.style_none)
adr_neg_lbl := label.new(bar_index+50, open - today_adr, "ADR Low (" + str.tostring(adr_days) + "D)",
xloc=xloc.bar_index, textalign=text.align_left, textcolor=adr_color, color=color.new(color.red, 90), style=label.style_none)
// Calculate deviations
var float half_dev = na
var float one_dev = na
var float two_dev = na
half_dev := today_adr * std_dev_0_5
one_dev := today_adr * std_dev_1
two_dev := today_adr * std_dev_2
// Draw standard deviation lines (with show/hide options)
if std_0_5_show
std_0_5_pos := line.new(bar_index, (open + today_adr) + half_dev, bar_index+50, (open + today_adr) + half_dev,
width=width, color=std_0_5_color, style=get_line_style(std_0_5_style))
std_0_5_neg := line.new(bar_index, (open - today_adr) - half_dev, bar_index+50, (open - today_adr) - half_dev,
width=width, color=std_0_5_color, style=get_line_style(std_0_5_style))
if show_labels
std_0_5_pos_lbl := label.new(bar_index+50, (open + today_adr) + half_dev, "Std " + str.tostring(std_dev_0_5),
xloc=xloc.bar_index, textalign=text.align_left, textcolor=std_0_5_color, color=color.new(#000000,100), style=label.style_none)
std_0_5_neg_lbl := label.new(bar_index+50, (open - today_adr) - half_dev, "Std -" + str.tostring(std_dev_0_5),
xloc=xloc.bar_index, textalign=text.align_left, textcolor=std_0_5_color, color=color.new(#000000,100), style=label.style_none)
if std_1_show
std_1_pos := line.new(bar_index, (open + today_adr) + one_dev, bar_index+50, (open + today_adr) + one_dev,
width=width, color=std_1_color, style=get_line_style(std_1_style))
std_1_neg := line.new(bar_index, (open - today_adr) - one_dev, bar_index+50, (open - today_adr) - one_dev,
width=width, color=std_1_color, style=get_line_style(std_1_style))
if show_labels
std_1_pos_lbl := label.new(bar_index+50, (open + today_adr) + one_dev, "Std " + str.tostring(std_dev_1),
xloc=xloc.bar_index, textalign=text.align_left, textcolor=std_1_color, color=color.new(#000000,100), style=label.style_none)
std_1_neg_lbl := label.new(bar_index+50, (open - today_adr) - one_dev, "Std -" + str.tostring(std_dev_1),
xloc=xloc.bar_index, textalign=text.align_left, textcolor=std_1_color, color=color.new(#000000,100), style=label.style_none)
if std_2_show
std_2_pos := line.new(bar_index, (open + today_adr) + two_dev, bar_index+50, (open + today_adr) + two_dev,
width=width, color=std_2_color, style=get_line_style(std_2_style))
std_2_neg := line.new(bar_index, (open - today_adr) - two_dev, bar_index+50, (open - today_adr) - two_dev,
width=width, color=std_2_color, style=get_line_style(std_2_style))
if show_labels
std_2_pos_lbl := label.new(bar_index+50, (open + today_adr) + two_dev, "Std " + str.tostring(std_dev_2),
xloc=xloc.bar_index, textalign=text.align_left, textcolor=std_2_color, color=color.new(#000000,100), style=label.style_none)
std_2_neg_lbl := label.new(bar_index+50, (open - today_adr) - two_dev, "Std -" + str.tostring(std_dev_2),
xloc=xloc.bar_index, textalign=text.align_left, textcolor=std_2_color, color=color.new(#000000,100), style=label.style_none)
// Draw Fibonacci lines
if fib_1_show
fib_1_pos := line.new(bar_index, open + today_adr * fib_1_level, bar_index+50, open + today_adr * fib_1_level,
width=width, color=fib_1_color, style=get_line_style(fib_1_style))
fib_1_neg := line.new(bar_index, open - today_adr * fib_1_level, bar_index+50, open - today_adr * fib_1_level,
width=width, color=fib_1_color, style=get_line_style(fib_1_style))
if show_labels
fib_1_pos_lbl := label.new(bar_index+50, open + today_adr * fib_1_level, "Fib " + str.tostring(fib_1_level),
xloc=xloc.bar_index, textalign=text.align_left, textcolor=fib_1_color, color=color.new(#000000,100), style=label.style_none)
fib_1_neg_lbl := label.new(bar_index+50, open - today_adr * fib_1_level, "Fib -" + str.tostring(fib_1_level),
xloc=xloc.bar_index, textalign=text.align_left, textcolor=fib_1_color, color=color.new(#000000,100), style=label.style_none)
if fib_2_show
fib_2_pos := line.new(bar_index, open + today_adr * fib_2_level, bar_index+50, open + today_adr * fib_2_level,
width=width, color=fib_2_color, style=get_line_style(fib_2_style))
fib_2_neg := line.new(bar_index, open - today_adr * fib_2_level, bar_index+50, open - today_adr * fib_2_level,
width=width, color=fib_2_color, style=get_line_style(fib_2_style))
if show_labels
fib_2_pos_lbl := label.new(bar_index+50, open + today_adr * fib_2_level, "Fib " + str.tostring(fib_2_level),
xloc=xloc.bar_index, textalign=text.align_left, textcolor=fib_2_color, color=color.new(#000000,100), style=label.style_none)
fib_2_neg_lbl := label.new(bar_index+50, open - today_adr * fib_2_level, "Fib -" + str.tostring(fib_2_level),
xloc=xloc.bar_index, textalign=text.align_left, textcolor=fib_2_color, color=color.new(#000000,100), style=label.style_none)
if fib_3_show
fib_3_pos := line.new(bar_index, open + today_adr * fib_3_level, bar_index+50, open + today_adr * fib_3_level,
width=width, color=fib_3_color, style=get_line_style(fib_3_style))
fib_3_neg := line.new(bar_index, open - today_adr * fib_3_level, bar_index+50, open - today_adr * fib_3_level,
width=width, color=fib_3_color, style=get_line_style(fib_3_style))
if show_labels
fib_3_pos_lbl := label.new(bar_index+50, open + today_adr * fib_3_level, "Fib " + str.tostring(fib_3_level),
xloc=xloc.bar_index, textalign=text.align_left, textcolor=fib_3_color, color=color.new(#000000,100), style=label.style_none)
fib_3_neg_lbl := label.new(bar_index+50, open - today_adr * fib_3_level, "Fib -" + str.tostring(fib_3_level),
xloc=xloc.bar_index, textalign=text.align_left, textcolor=fib_3_color, color=color.new(#000000,100), style=label.style_none)
else
today_adr := today_adr
line.set_x2(adr_pos, bar_index+50)
line.set_x2(adr_neg, bar_index+50)
if show_labels
label.set_x(adr_pos_lbl, bar_index+50)
label.set_x(adr_neg_lbl, bar_index+50)
if std_0_5_show
line.set_x2(std_0_5_pos, bar_index+50)
line.set_x2(std_0_5_neg, bar_index+50)
if show_labels
label.set_x(std_0_5_pos_lbl, bar_index+50)
label.set_x(std_0_5_neg_lbl, bar_index+50)
if std_1_show
line.set_x2(std_1_pos, bar_index+50)
line.set_x2(std_1_neg, bar_index+50)
if show_labels
label.set_x(std_1_pos_lbl, bar_index+50)
label.set_x(std_1_neg_lbl, bar_index+50)
if std_2_show
line.set_x2(std_2_pos, bar_index+50)
line.set_x2(std_2_neg, bar_index+50)
if show_labels
label.set_x(std_2_pos_lbl, bar_index+50)
label.set_x(std_2_neg_lbl, bar_index+50)
if fib_1_show
line.set_x2(fib_1_pos, bar_index+50)
line.set_x2(fib_1_neg, bar_index+50)
if show_labels
label.set_x(fib_1_pos_lbl, bar_index+50)
label.set_x(fib_1_neg_lbl, bar_index+50)
if fib_2_show
line.set_x2(fib_2_pos, bar_index+50)
line.set_x2(fib_2_neg, bar_index+50)
if show_labels
label.set_x(fib_2_pos_lbl, bar_index+50)
label.set_x(fib_2_neg_lbl, bar_index+50)
if fib_3_show
line.set_x2(fib_3_pos, bar_index+50)
line.set_x2(fib_3_neg, bar_index+50)
if show_labels
label.set_x(fib_3_pos_lbl, bar_index+50)
label.set_x(fib_3_neg_lbl, bar_index+50)
// Stats calculation
var float d_hi = high
var float d_lo = low
var float d_open = open
var float d_range = array.new_float()
var float adr_val = na
var float d_adr_hi = na
var float d_adr_lo = na
type adr_stats
int hit_adr_hi = 0
int hit_adr_lo = 0
int hit_adr_both = 0
int thru_adr_hi = 0
int thru_adr_lo = 0
int hit_fib_1_hi = 0
int hit_fib_1_lo = 0
int hit_fib_2_hi = 0
int hit_fib_2_lo = 0
int hit_fib_3_hi = 0
int hit_fib_3_lo = 0
int hit_std_0_5_hi = 0
int hit_std_0_5_lo = 0
int hit_std_1_hi = 0
int hit_std_1_lo = 0
int hit_std_2_hi = 0
int hit_std_2_lo = 0
int d_count = 0
var adr_sun = adr_stats.new()
var adr_mon = adr_stats.new()
var adr_tue = adr_stats.new()
var adr_wed = adr_stats.new()
var adr_thu = adr_stats.new()
var adr_fri = adr_stats.new()
var adr_sat = adr_stats.new()
if timeframe.change("D")
x = adr_mon
dow = dayofweek(time , "America/New_York")
if dow == dayofweek.tuesday
x := adr_tue
else if dow == dayofweek.wednesday
x := adr_wed
else if dow == dayofweek.thursday
x := adr_thu
else if dow == dayofweek.friday
x := adr_fri
else if dow == dayofweek.saturday
x := adr_sat
else if dow == dayofweek.sunday
x := adr_sun
if not na(adr_val)
x.d_count += 1
if d_hi > d_adr_hi
x.hit_adr_hi += 1
if d_lo < d_adr_lo
x.hit_adr_lo += 1
if d_hi > d_adr_hi and d_lo < d_adr_lo
x.hit_adr_both += 1
if close > d_adr_hi
x.thru_adr_hi += 1
if close < d_adr_lo
x.thru_adr_lo += 1
if fib_1_show
if d_hi > d_open + (adr_val * fib_1_level)
x.hit_fib_1_hi += 1
if d_lo < d_open - (adr_val * fib_1_level)
x.hit_fib_1_lo += 1
if fib_2_show
if d_hi > d_open + (adr_val * fib_2_level)
x.hit_fib_2_hi += 1
if d_lo < d_open - (adr_val * fib_2_level)
x.hit_fib_2_lo += 1
if fib_3_show
if d_hi > d_open + (adr_val * fib_3_level)
x.hit_fib_3_hi += 1
if d_lo < d_open - (adr_val * fib_3_level)
x.hit_fib_3_lo += 1
if std_0_5_show
if d_hi > d_adr_hi + (adr_val * std_dev_0_5)
x.hit_std_0_5_hi += 1
if d_lo < d_adr_lo - (adr_val * std_dev_0_5)
x.hit_std_0_5_lo += 1
if std_1_show
if d_hi > d_adr_hi + (adr_val * std_dev_1)
x.hit_std_1_hi += 1
if d_lo < d_adr_lo - (adr_val * std_dev_1)
x.hit_std_1_lo += 1
if std_2_show
if d_hi > d_adr_hi + (adr_val * std_dev_2)
x.hit_std_2_hi += 1
if d_lo < d_adr_lo - (adr_val * std_dev_2)
x.hit_std_2_lo += 1
if timeframe.change("D")
d_open := open
array.unshift(d_range, d_hi - d_lo)
if array.size(d_range) > adr_days
array.pop(d_range)
if array.size(d_range) == adr_days
adr_val := array.avg(d_range)
d_adr_hi := open + (adr_val*std_x)/2
d_adr_lo := open - (adr_val*std_x)/2
d_hi := high
d_lo := low
else
d_hi := math.max(high, d_hi)
d_lo := math.min(low, d_lo)
// Table functions
get_table_pos(pos) =>
switch pos
"Bottom Center" => position.bottom_center
"Bottom Left" => position.bottom_left
"Bottom Right" => position.bottom_right
"Middle Center" => position.middle_center
"Middle Left" => position.middle_left
"Middle Right" => position.middle_right
"Top Center" => position.top_center
"Top Left" => position.top_left
"Top Right" => position.top_right
var _loc = get_table_pos(tbl_loc)
get_table_size(size) =>
switch size
'Tiny' => size.tiny
'Small' => size.small
'Normal' => size.normal
'Large' => size.large
'Huge' => size.huge
'Auto' => size.auto
var _size = get_table_size(tbl_size)
fmt_sample(s, float pct, int count) =>
str.format("{0,number,percent}", pct) + (sample_size ? " ("+str.tostring(count)+")" : "")
// Draw table
if barstate.islast and show_stats
var tbl = table.new(_loc, 100, 100, chart.bg_color, chart.fg_color, 2, chart.fg_color, 1)
// Column headers (days + empty first cell)
table.cell(tbl, 0, 0, "Level", text_size = _size)
table.cell(tbl, 1, 0, "Mon", bgcolor = rch_color, text_size = _size)
table.cell(tbl, 2, 0, "Tue", bgcolor = rch_color, text_size = _size)
table.cell(tbl, 3, 0, "Wed", bgcolor = rch_color, text_size = _size)
table.cell(tbl, 4, 0, "Thu", bgcolor = rch_color, text_size = _size)
table.cell(tbl, 5, 0, "Fri", bgcolor = rch_color, text_size = _size)
// Row headers and data
var row = 1
table.cell(tbl, 0, row, "ADR High", text_size = _size)
table.cell(tbl, 1, row, fmt_sample(adr_mon.d_count, adr_mon.hit_adr_hi / adr_mon.d_count, adr_mon.hit_adr_hi), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 2, row, fmt_sample(adr_tue.d_count, adr_tue.hit_adr_hi / adr_tue.d_count, adr_tue.hit_adr_hi), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 3, row, fmt_sample(adr_wed.d_count, adr_wed.hit_adr_hi / adr_wed.d_count, adr_wed.hit_adr_hi), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 4, row, fmt_sample(adr_thu.d_count, adr_thu.hit_adr_hi / adr_thu.d_count, adr_thu.hit_adr_hi), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 5, row, fmt_sample(adr_fri.d_count, adr_fri.hit_adr_hi / adr_fri.d_count, adr_fri.hit_adr_hi), bgcolor = rch_color, text_size = _size)
row := row + 1
table.cell(tbl, 0, row, "ADR Low", text_size = _size)
table.cell(tbl, 1, row, fmt_sample(adr_mon.d_count, adr_mon.hit_adr_lo / adr_mon.d_count, adr_mon.hit_adr_lo), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 2, row, fmt_sample(adr_tue.d_count, adr_tue.hit_adr_lo / adr_tue.d_count, adr_tue.hit_adr_lo), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 3, row, fmt_sample(adr_wed.d_count, adr_wed.hit_adr_lo / adr_wed.d_count, adr_wed.hit_adr_lo), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 4, row, fmt_sample(adr_thu.d_count, adr_thu.hit_adr_lo / adr_thu.d_count, adr_thu.hit_adr_lo), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 5, row, fmt_sample(adr_fri.d_count, adr_fri.hit_adr_lo / adr_fri.d_count, adr_fri.hit_adr_lo), bgcolor = rch_color, text_size = _size)
row := row + 1
table.cell(tbl, 0, row, "ADR High (Close)", text_size = _size)
table.cell(tbl, 1, row, fmt_sample(adr_mon.d_count, adr_mon.thru_adr_hi / adr_mon.d_count, adr_mon.thru_adr_hi), bgcolor = csd_color, text_size = _size)
table.cell(tbl, 2, row, fmt_sample(adr_tue.d_count, adr_tue.thru_adr_hi / adr_tue.d_count, adr_tue.thru_adr_hi), bgcolor = csd_color, text_size = _size)
table.cell(tbl, 3, row, fmt_sample(adr_wed.d_count, adr_wed.thru_adr_hi / adr_wed.d_count, adr_wed.thru_adr_hi), bgcolor = csd_color, text_size = _size)
table.cell(tbl, 4, row, fmt_sample(adr_thu.d_count, adr_thu.thru_adr_hi / adr_thu.d_count, adr_thu.thru_adr_hi), bgcolor = csd_color, text_size = _size)
table.cell(tbl, 5, row, fmt_sample(adr_fri.d_count, adr_fri.thru_adr_hi / adr_fri.d_count, adr_fri.thru_adr_hi), bgcolor = csd_color, text_size = _size)
row := row + 1
table.cell(tbl, 0, row, "ADR Low (Close)", text_size = _size)
table.cell(tbl, 1, row, fmt_sample(adr_mon.d_count, adr_mon.thru_adr_lo / adr_mon.d_count, adr_mon.thru_adr_lo), bgcolor = csd_color, text_size = _size)
table.cell(tbl, 2, row, fmt_sample(adr_tue.d_count, adr_tue.thru_adr_lo / adr_tue.d_count, adr_tue.thru_adr_lo), bgcolor = csd_color, text_size = _size)
table.cell(tbl, 3, row, fmt_sample(adr_wed.d_count, adr_wed.thru_adr_lo / adr_wed.d_count, adr_wed.thru_adr_lo), bgcolor = csd_color, text_size = _size)
table.cell(tbl, 4, row, fmt_sample(adr_thu.d_count, adr_thu.thru_adr_lo / adr_thu.d_count, adr_thu.thru_adr_lo), bgcolor = csd_color, text_size = _size)
table.cell(tbl, 5, row, fmt_sample(adr_fri.d_count, adr_fri.thru_adr_lo / adr_fri.d_count, adr_fri.thru_adr_lo), bgcolor = csd_color, text_size = _size)
row := row + 1
if fib_1_show
table.cell(tbl, 0, row, "Fib " + str.tostring(fib_1_level), text_size = _size)
table.cell(tbl, 1, row, fmt_sample(adr_mon.d_count, adr_mon.hit_fib_1_hi / adr_mon.d_count, adr_mon.hit_fib_1_hi), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 2, row, fmt_sample(adr_tue.d_count, adr_tue.hit_fib_1_hi / adr_tue.d_count, adr_tue.hit_fib_1_hi), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 3, row, fmt_sample(adr_wed.d_count, adr_wed.hit_fib_1_hi / adr_wed.d_count, adr_wed.hit_fib_1_hi), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 4, row, fmt_sample(adr_thu.d_count, adr_thu.hit_fib_1_hi / adr_thu.d_count, adr_thu.hit_fib_1_hi), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 5, row, fmt_sample(adr_fri.d_count, adr_fri.hit_fib_1_hi / adr_fri.d_count, adr_fri.hit_fib_1_hi), bgcolor = rch_color, text_size = _size)
row := row + 1
table.cell(tbl, 0, row, "Fib -" + str.tostring(fib_1_level), text_size = _size)
table.cell(tbl, 1, row, fmt_sample(adr_mon.d_count, adr_mon.hit_fib_1_lo / adr_mon.d_count, adr_mon.hit_fib_1_lo), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 2, row, fmt_sample(adr_tue.d_count, adr_tue.hit_fib_1_lo / adr_tue.d_count, adr_tue.hit_fib_1_lo), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 3, row, fmt_sample(adr_wed.d_count, adr_wed.hit_fib_1_lo / adr_wed.d_count, adr_wed.hit_fib_1_lo), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 4, row, fmt_sample(adr_thu.d_count, adr_thu.hit_fib_1_lo / adr_thu.d_count, adr_thu.hit_fib_1_lo), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 5, row, fmt_sample(adr_fri.d_count, adr_fri.hit_fib_1_lo / adr_fri.d_count, adr_fri.hit_fib_1_lo), bgcolor = rch_color, text_size = _size)
row := row + 1
if fib_2_show
table.cell(tbl, 0, row, "Fib " + str.tostring(fib_2_level), text_size = _size)
table.cell(tbl, 1, row, fmt_sample(adr_mon.d_count, adr_mon.hit_fib_2_hi / adr_mon.d_count, adr_mon.hit_fib_2_hi), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 2, row, fmt_sample(adr_tue.d_count, adr_tue.hit_fib_2_hi / adr_tue.d_count, adr_tue.hit_fib_2_hi), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 3, row, fmt_sample(adr_wed.d_count, adr_wed.hit_fib_2_hi / adr_wed.d_count, adr_wed.hit_fib_2_hi), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 4, row, fmt_sample(adr_thu.d_count, adr_thu.hit_fib_2_hi / adr_thu.d_count, adr_thu.hit_fib_2_hi), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 5, row, fmt_sample(adr_fri.d_count, adr_fri.hit_fib_2_hi / adr_fri.d_count, adr_fri.hit_fib_2_hi), bgcolor = rch_color, text_size = _size)
row := row + 1
table.cell(tbl, 0, row, "Fib -" + str.tostring(fib_2_level), text_size = _size)
table.cell(tbl, 1, row, fmt_sample(adr_mon.d_count, adr_mon.hit_fib_2_lo / adr_mon.d_count, adr_mon.hit_fib_2_lo), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 2, row, fmt_sample(adr_tue.d_count, adr_tue.hit_fib_2_lo / adr_tue.d_count, adr_tue.hit_fib_2_lo), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 3, row, fmt_sample(adr_wed.d_count, adr_wed.hit_fib_2_lo / adr_wed.d_count, adr_wed.hit_fib_2_lo), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 4, row, fmt_sample(adr_thu.d_count, adr_thu.hit_fib_2_lo / adr_thu.d_count, adr_thu.hit_fib_2_lo), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 5, row, fmt_sample(adr_fri.d_count, adr_fri.hit_fib_2_lo / adr_fri.d_count, adr_fri.hit_fib_2_lo), bgcolor = rch_color, text_size = _size)
row := row + 1
if fib_3_show
table.cell(tbl, 0, row, "Fib " + str.tostring(fib_3_level), text_size = _size)
table.cell(tbl, 1, row, fmt_sample(adr_mon.d_count, adr_mon.hit_fib_3_hi / adr_mon.d_count, adr_mon.hit_fib_3_hi), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 2, row, fmt_sample(adr_tue.d_count, adr_tue.hit_fib_3_hi / adr_tue.d_count, adr_tue.hit_fib_3_hi), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 3, row, fmt_sample(adr_wed.d_count, adr_wed.hit_fib_3_hi / adr_wed.d_count, adr_wed.hit_fib_3_hi), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 4, row, fmt_sample(adr_thu.d_count, adr_thu.hit_fib_3_hi / adr_thu.d_count, adr_thu.hit_fib_3_hi), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 5, row, fmt_sample(adr_fri.d_count, adr_fri.hit_fib_3_hi / adr_fri.d_count, adr_fri.hit_fib_3_hi), bgcolor = rch_color, text_size = _size)
row := row + 1
table.cell(tbl, 0, row, "Fib -" + str.tostring(fib_3_level), text_size = _size)
table.cell(tbl, 1, row, fmt_sample(adr_mon.d_count, adr_mon.hit_fib_3_lo / adr_mon.d_count, adr_mon.hit_fib_3_lo), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 2, row, fmt_sample(adr_tue.d_count, adr_tue.hit_fib_3_lo / adr_tue.d_count, adr_tue.hit_fib_3_lo), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 3, row, fmt_sample(adr_wed.d_count, adr_wed.hit_fib_3_lo / adr_wed.d_count, adr_wed.hit_fib_3_lo), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 4, row, fmt_sample(adr_thu.d_count, adr_thu.hit_fib_3_lo / adr_thu.d_count, adr_thu.hit_fib_3_lo), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 5, row, fmt_sample(adr_fri.d_count, adr_fri.hit_fib_3_lo / adr_fri.d_count, adr_fri.hit_fib_3_lo), bgcolor = rch_color, text_size = _size)
row := row + 1
if std_0_5_show
table.cell(tbl, 0, row, "Std " + str.tostring(std_dev_0_5), text_size = _size)
table.cell(tbl, 1, row, fmt_sample(adr_mon.d_count, adr_mon.hit_std_0_5_hi / adr_mon.d_count, adr_mon.hit_std_0_5_hi), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 2, row, fmt_sample(adr_tue.d_count, adr_tue.hit_std_0_5_hi / adr_tue.d_count, adr_tue.hit_std_0_5_hi), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 3, row, fmt_sample(adr_wed.d_count, adr_wed.hit_std_0_5_hi / adr_wed.d_count, adr_wed.hit_std_0_5_hi), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 4, row, fmt_sample(adr_thu.d_count, adr_thu.hit_std_0_5_hi / adr_thu.d_count, adr_thu.hit_std_0_5_hi), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 5, row, fmt_sample(adr_fri.d_count, adr_fri.hit_std_0_5_hi / adr_fri.d_count, adr_fri.hit_std_0_5_hi), bgcolor = rch_color, text_size = _size)
row := row + 1
table.cell(tbl, 0, row, "Std -" + str.tostring(std_dev_0_5), text_size = _size)
table.cell(tbl, 1, row, fmt_sample(adr_mon.d_count, adr_mon.hit_std_0_5_lo / adr_mon.d_count, adr_mon.hit_std_0_5_lo), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 2, row, fmt_sample(adr_tue.d_count, adr_tue.hit_std_0_5_lo / adr_tue.d_count, adr_tue.hit_std_0_5_lo), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 3, row, fmt_sample(adr_wed.d_count, adr_wed.hit_std_0_5_lo / adr_wed.d_count, adr_wed.hit_std_0_5_lo), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 4, row, fmt_sample(adr_thu.d_count, adr_thu.hit_std_0_5_lo / adr_thu.d_count, adr_thu.hit_std_0_5_lo), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 5, row, fmt_sample(adr_fri.d_count, adr_fri.hit_std_0_5_lo / adr_fri.d_count, adr_fri.hit_std_0_5_lo), bgcolor = rch_color, text_size = _size)
row := row + 1
if std_1_show
table.cell(tbl, 0, row, "Std " + str.tostring(std_dev_1), text_size = _size)
table.cell(tbl, 1, row, fmt_sample(adr_mon.d_count, adr_mon.hit_std_1_hi / adr_mon.d_count, adr_mon.hit_std_1_hi), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 2, row, fmt_sample(adr_tue.d_count, adr_tue.hit_std_1_hi / adr_tue.d_count, adr_tue.hit_std_1_hi), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 3, row, fmt_sample(adr_wed.d_count, adr_wed.hit_std_1_hi / adr_wed.d_count, adr_wed.hit_std_1_hi), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 4, row, fmt_sample(adr_thu.d_count, adr_thu.hit_std_1_hi / adr_thu.d_count, adr_thu.hit_std_1_hi), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 5, row, fmt_sample(adr_fri.d_count, adr_fri.hit_std_1_hi / adr_fri.d_count, adr_fri.hit_std_1_hi), bgcolor = rch_color, text_size = _size)
row := row + 1
table.cell(tbl, 0, row, "Std -" + str.tostring(std_dev_1), text_size = _size)
table.cell(tbl, 1, row, fmt_sample(adr_mon.d_count, adr_mon.hit_std_1_lo / adr_mon.d_count, adr_mon.hit_std_1_lo), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 2, row, fmt_sample(adr_tue.d_count, adr_tue.hit_std_1_lo / adr_tue.d_count, adr_tue.hit_std_1_lo), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 3, row, fmt_sample(adr_wed.d_count, adr_wed.hit_std_1_lo / adr_wed.d_count, adr_wed.hit_std_1_lo), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 4, row, fmt_sample(adr_thu.d_count, adr_thu.hit_std_1_lo / adr_thu.d_count, adr_thu.hit_std_1_lo), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 5, row, fmt_sample(adr_fri.d_count, adr_fri.hit_std_1_lo / adr_fri.d_count, adr_fri.hit_std_1_lo), bgcolor = rch_color, text_size = _size)
row := row + 1
if std_2_show
table.cell(tbl, 0, row, "Std " + str.tostring(std_dev_2), text_size = _size)
table.cell(tbl, 1, row, fmt_sample(adr_mon.d_count, adr_mon.hit_std_2_hi / adr_mon.d_count, adr_mon.hit_std_2_hi), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 2, row, fmt_sample(adr_tue.d_count, adr_tue.hit_std_2_hi / adr_tue.d_count, adr_tue.hit_std_2_hi), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 3, row, fmt_sample(adr_wed.d_count, adr_wed.hit_std_2_hi / adr_wed.d_count, adr_wed.hit_std_2_hi), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 4, row, fmt_sample(adr_thu.d_count, adr_thu.hit_std_2_hi / adr_thu.d_count, adr_thu.hit_std_2_hi), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 5, row, fmt_sample(adr_fri.d_count, adr_fri.hit_std_2_hi / adr_fri.d_count, adr_fri.hit_std_2_hi), bgcolor = rch_color, text_size = _size)
row := row + 1
table.cell(tbl, 0, row, "Std -" + str.tostring(std_dev_2), text_size = _size)
table.cell(tbl, 1, row, fmt_sample(adr_mon.d_count, adr_mon.hit_std_2_lo / adr_mon.d_count, adr_mon.hit_std_2_lo), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 2, row, fmt_sample(adr_tue.d_count, adr_tue.hit_std_2_lo / adr_tue.d_count, adr_tue.hit_std_2_lo), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 3, row, fmt_sample(adr_wed.d_count, adr_wed.hit_std_2_lo / adr_wed.d_count, adr_wed.hit_std_2_lo), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 4, row, fmt_sample(adr_thu.d_count, adr_thu.hit_std_2_lo / adr_thu.d_count, adr_thu.hit_std_2_lo), bgcolor = rch_color, text_size = _size)
table.cell(tbl, 5, row, fmt_sample(adr_fri.d_count, adr_fri.hit_std_2_lo / adr_fri.d_count, adr_fri.hit_std_2_lo), bgcolor = rch_color, text_size = _size)
Anchored ATH Drawdown LevelsThe Anchored ATH Drawdown Levels plots horizontal lines from a chosen anchor price (ATH), showing potential pullback zones at set percentage drops below it.
This indicator's use lies in its anchored ATH framework, which rapidly visualizes precise drawdown levels as dynamic levels of interest or price targets enabling traders to anticipate pullback depths and potential reversal levels without manual calculations.
Pick "True ATH" for the all-time high or "Period ATH" for anchored highs reset weekly, monthly, or quarterly. Lines stretch right for a cleaner visual.
Key Features
Anchoring: True ATH (lifetime max) or Period ATH (resets on 1W/1M/3M intervals).
Drawdown Levels: 8 adjustable levels (defaults: -5%, -10%, -15%, -20% on; -25% to -50% off). Toggle each, set drop % (0.1-99.9), pick color, style (solid/dashed/dotted), width (1-3).
ATH Line: Optional ATH line with custom color, style, width.
Unified Look: Global overrides for all levels' color, style, width.
Labels: Show % drops (with/without prices) via text boxes or full tags; sizes from tiny to large.
Projection: Lines extend 5-100 bars right (default 20).
Settings
Anchor: Mode and timeframe.
Display: Toggle levels/ATH, set extension.
Labels: Style (text/full/none), size, price display.
Global/ATH/Levels: Colors, styles, widths (per-level or shared).
How to Use
Load on chart (overlays prices; handles up to 500 lines).
Choose anchor for your high.
Tune levels for key pullbacks (e.g., -5% minor, -20% major).
Customize visuals where the lines update on new peaks.
Relative Strength vs Benchmark SPYRelative Strength vs Benchmark (SPY)
This indicator compares the performance of the charted symbol (stock or ETF) against a benchmark index — by default, SPY (S&P 500). It plots a Relative Strength (RS) ratio line (Symbol / SPY) and its EMA(50) to visualize when the asset is outperforming or underperforming the market.
Key Features
📈 RS Line (blue): Shows how the asset performs relative to SPY.
🟠 EMA(50): Smooths the RS trend to highlight sustained leadership.
🟩 Green background: Symbol is outperforming SPY (RS > EMA).
🟥 Red background: Symbol is underperforming SPY (RS < EMA).
🔔 Alerts: Automatic notifications when RS crosses above/below its EMA — signaling new leadership or weakness.
How to Use
Apply to any stock or ETF chart.
Keep benchmark = SPY, or switch to another index (e.g., QQQ, IWM, XLK).
Watch for RS crossovers and trends:
Rising RS → money flowing into the asset.
Falling RS → rotation away from the asset.
Perfect for sector rotation, ETF comparison, and momentum analysis workflows.
BSL / SSL Liquidity Zones + Alerts//@version=5
indicator("BSL / SSL Liquidity Zones + Alerts", overlay=true, max_labels_count=500)
Ben's BTC Macro Fair Value OscillatorBen's BTC Macro Fair Value Oscillator
Overview
The **BTC Macro Fair Value Oscillator** is a non-crypto fair value framework that uses macro asset relationships (equities, dollar, gold) to estimate Bitcoin's "macro-driven fair value" and identify mean-reversion opportunities.
"Is BTC cheap or expensive right now?" on the 4 Hour Timeframe ONLY
### Key Features
✅ **Macro-driven**: Uses QQQ, DXY, XAUUSD instead of on-chain or crypto metrics
✅ **Dynamic weighting**: Assets weighted by rolling correlation strength
✅ **Mean-reversion signals**: Identifies when BTC is cheap/expensive vs macro
✅ **Validated parameters**: Optimized through 5-year backtest (Sharpe 6.7-9.9)
✅ **Visual transparency**: Live correlation panel, fair value bands, statistics
✅ **Non-repainting**: All calculations use confirmed historical data only
### What This Indicator Does
- Builds a **synthetic macro composite** from traditional assets
- Runs a **rolling regression** to predict BTC price from macro
- Calculates **deviation z-score** (how far BTC is from macro fair value)
- Generates **entry signals** when BTC is extremely cheap vs macro (dev < -2)
- Generates **exit signals** when BTC returns to fair value (dev > 0)
### What This Indicator Is NOT
❌ Not a high-frequency trading system (sparse signals by design)
❌ Not optimized for absolute returns (optimized for Sharpe ratio)
❌ Not suitable as standalone trading system (best as overlay/confirmation)
❌ Not predictive of short-term price movements (mean-reversion timeframe: days to weeks)
---
## Core Concept
### The Premise
Bitcoin doesn't trade in a vacuum. It's influenced by:
- **Risk appetite** (equities: QQQ, SPX)
- **Dollar strength** (DXY - inverse to risk assets)
- **Safe haven flows** (Gold: XAUUSD)
When macro conditions are "good for BTC" (risk-on, weak dollar, strong equities), BTC should trade higher. When macro conditions turn against it, BTC should trade lower.
### The Innovation
Instead of looking at BTC in isolation, this indicator:
1. **Measures how strongly** BTC currently correlates with each macro asset
2. **Builds a weighted composite** of those macro returns (the "D" driver)
3. **Regresses BTC price on D** to estimate "macro fair value"
4. **Tracks the deviation** between actual price and fair value
5. **Signals mean reversion** when deviation becomes extreme
### The Edge
The validated edge comes from:
- **Extreme deviations predict future returns** (dev < -2 → +1.67% over 12 bars)
- **Monotonic relationship** (more negative dev → higher forward returns)
- **Works out-of-sample** (test Sharpe +83-87% better than training)
- **Low correlation with buy & hold** (provides diversification value)
---
## Methodology
### Step 1: Macro Composite Driver D(t)
The indicator builds a weighted composite of macro asset returns:
**Process:**
1. Calculate **log returns** for BTC and each macro reference (QQQ, DXY, XAUUSD)
2. Compute **rolling correlation** between BTC and each reference over `corrLen` bars
3. **Weight each asset** by `|correlation|` if above `minCorrAbs` threshold, else 0
4. **Sign-adjust** weights (+1 for positive corr, -1 for negative) to handle inverse relationships
5. **Z-score normalize** each reference's returns over `fvWindow`
6. **Composite D(t)** = weighted sum of sign-adjusted z-scores
**Formula:**
```
For each reference i:
corr_i = correlation(BTC_returns, ref_i_returns, corrLen)
weight_i = |corr_i| if |corr_i| >= minCorrAbs else 0
sign_i = +1 if corr_i >= 0 else -1
z_i = (ref_i_returns - mean) / std
contrib_i = sign_i * z_i * weight_i
D(t) = sum(contrib_i) / sum(weight_i)
```
**Key Insight:** D(t) represents "how good macro conditions are for BTC right now" in a normalized, correlation-weighted way.
---
### Step 2: Fair Value Regression
Uses rolling linear regression to predict BTC price from D(t):
**Model:**
```
BTC_price(t) = α + β * D(t)
```
**Calculation (Pine Script approach):**
```
corr_CD = correlation(BTC_price, D, fvWindow)
sd_price = stdev(BTC_price, fvWindow)
sd_D = stdev(D, fvWindow)
cov = corr_CD * sd_price * sd_D
var_D = variance(D, fvWindow)
β = cov / var_D
α = mean(BTC_price) - β * mean(D)
fair_value(t) = α + β * D(t)
```
**Result:** A time-varying "macro fair value" line that adapts as correlations change.
---
### Step 3: Deviation Oscillator
Measures how far BTC price has deviated from fair value:
**Calculation:**
```
residual(t) = BTC_price(t) - fair_value(t)
residual_std = stdev(residual, normWindow)
deviation(t) = residual(t) / residual_std
```
**Interpretation:**
- `dev = 0` → BTC at fair value
- `dev = -2` → BTC is 2 standard deviations **cheap** vs macro
- `dev = +2` → BTC is 2 standard deviations **rich** vs macro
---
### Step 4: Signal Generation
**Long Entry:** `dev` crosses below `-2.0` (BTC extremely cheap vs macro)
**Long Exit:** `dev` crosses above `0.0` (BTC returns to fair value)
**No shorting** in default config (risk management choice - crypto volatility)
---
## How It Works
### Visual Components
#### 1. Price Chart (Main Panel)
**Fair Value Line (Orange):**
- The estimated "macro-driven fair value" for BTC
- Calculated from rolling regression on macro composite
**Fair Value Bands:**
- **±1σ** (light): 68% confidence zone
- **±2σ** (medium): 95% confidence zone
- **±3σ** (dark, dots): 99.7% confidence zone
**Entry/Exit Markers:**
- **Green "LONG" label** below bar: Entry signal (dev < -2)
- **Red "EXIT" label** above bar: Exit signal (dev > 0)
#### 2. Deviation Oscillator (Separate Pane)
**Line plot:**
- Shows current deviation z-score
- **Green** when dev < -2 (cheap)
- **Red** when dev > +2 (rich)
- **Gray** when neutral
**Histogram:**
- Visual representation of deviation magnitude
- Green bars = negative deviation (cheap)
- Red bars = positive deviation (rich)
**Threshold lines:**
- **Green dashed at -2.0**: Entry threshold
- **Red dashed at 0.0**: Exit threshold
- **Gray solid at 0**: Fair value line
#### 3. Correlation Panel (Top-Right)
Shows live correlation and weighting for each macro asset:
| Asset | Corr | Weight |
|-------|------|--------|
| QQQ | +0.45 | 0.45 |
| DXY | -0.32 | 0.32 |
| XAUUSD | +0.15 | 0.00 |
| Avg \|Corr\| | 0.31 | 0.77 |
**Reading:**
- **Corr**: Current rolling correlation with BTC (-1 to +1)
- **Weight**: How much this asset contributes to fair value (0 = excluded)
- **Avg |Corr|**: Average correlation strength (should be > 0.2 for reliable signals)
**Colors:**
- Green/Red corr = positive/negative correlation
- White weight = asset included, Gray = excluded (below minCorrAbs)
#### 4. Statistics Label (Bottom-Right)
```
━━━ BTC Macro FV ━━━
Dev: -2.34
Price: $103,192
FV: $110,500
Status: CHEAP ⬇
β: 103.52
```
**Fields:**
- **Dev**: Current deviation z-score
- **Price**: Current BTC close price
- **FV**: Current macro fair value estimate
- **Status**: CHEAP (< -2), RICH (> +2), or FAIR
- **β**: Current regression beta (sensitivity to macro)
---
## Installation & Setup
### TradingView Setup
1. Open TradingView and navigate to any **BTC chart** (BTCUSD, BTCUSDT, etc.)
2. Open **Pine Editor** (bottom panel)
3. Click **"+ New"** → **"Blank indicator"**
4. **Delete** all default code
5. **Copy** the entire Pine Script from `GHPT_optimized.pine`
6. **Paste** into the editor
7. Click **"Save"** and name it "BTC Macro Fair Value Oscillator"
8. Click **"Add to Chart"**
### Recommended Chart Settings
**Timeframe:** 4h (validated timeframe)
**Chart Type:** Candlestick or Heikin Ashi
**Overlay:** Yes (indicator plots on price chart + separate pane)
**Alternative Timeframes:**
- Daily: Works but slower signals
- 1h-2h: May work but not validated
- < 1h: Not recommended (too noisy)
### Symbol Requirements
**Primary:** BTC/USD or BTC/USDT on any exchange
**Macro References:** Automatically fetched
- QQQ (Nasdaq 100 ETF)
- DXY (US Dollar Index)
- XAUUSD (Gold spot)
**Data Requirements:**
- At least **90 bars** of history (warmup period)
- Premium TradingView recommended for full historical data
---
## Reading the Indicator
### Identifying Signals
#### Strong Long Signal (High Conviction)
- ✅ Deviation < -2.0 (extreme undervaluation)
- ✅ Avg |Corr| > 0.3 (strong macro relationships)
- ✅ Price touching or below -2σ band
- ✅ "LONG" label appears below bar
**Interpretation:** BTC is extremely cheap relative to macro conditions. Historical data shows +1.67% average return over next 12 bars (48 hours at 4h timeframe).
#### Moderate Long Signal (Lower Conviction)
- ⚠️ Deviation between -1.5 and -2.0
- ⚠️ Avg |Corr| between 0.2-0.3
- ⚠️ Price approaching -2σ band
**Interpretation:** BTC is cheap but not extreme. Consider as confirmation for other signals.
#### Exit Signal
- 🔴 Deviation crosses above 0 (returns to fair value)
- 🔴 "EXIT" label appears above bar
**Interpretation:** Mean reversion complete. Close long positions.
#### Strong Short/Avoid Signal
- 🔴 Deviation > +2.0 (extreme overvaluation)
- 🔴 Avg |Corr| > 0.3
- 🔴 Price touching or above +2σ band
**Interpretation:** BTC is expensive vs macro. Historical data shows -1.79% average return over next 12 bars. Consider exiting longs or reducing exposure.
### Regime Detection
**Strong Regime (Reliable Signals):**
- Avg |Corr| > 0.3
- Multiple assets weighted > 0
- Fair value line tracking price reasonably well
**Weak Regime (Unreliable Signals):**
- Avg |Corr| < 0.2
- Most weights = 0 (grayed out)
- Fair value line diverging wildly from price
- **Action:** Ignore signals until correlations strengthen
ROC & Momentum FusionROC & Momentum Fusion
(by HabibiTrades ©)
Purpose:
“ROC & Momentum Fusion” combines the Rate of Change (ROC) with a MACD-style signal engine to identify early momentum reversals, confirmed trend shifts, and low-volatility choppy zones.
It’s built for traders who want early momentum detection with the clarity of trend persistence — adaptable to any instrument and timeframe.
⚙️ How It Works
Rate of Change (ROC):
Measures the percentage speed of price change over time, showing the raw momentum strength.
Signal Line (EMA):
A short EMA of the ROC — responds faster to new directional shifts, similar to a MACD signal line.
Histogram:
Displays acceleration and deceleration between the ROC and its signal line.
Persistent Trend States:
When the ROC crosses the signal line or zero, the indicator enters a new momentum regime
(bullish or bearish) and stays in that color until another flip occurs.
Dynamic Choppy Zone:
When ROC momentum fades within the zero buffer zone, the indicator turns orange, signaling a sideways or indecisive market.
🟢 Visual Regimes
Regime Description Color
Bullish Momentum ROC above zero or signal line 🟢 Neon Green
Bearish Momentum ROC below zero or signal line 🔴 Neon Red
Choppy / Neutral ROC hovering within ±threshold range 🟠 Neon Orange
This color system makes it visually effortless to see whether the market is trending, reversing, or consolidating.
🧭 Adaptive Intelligence
The script automatically adjusts to market type and session for consistent accuracy:
Session Adaptive: Adjusts smoothing based on global sessions (Asian, London, New York, Sydney).
Instrument Adaptive: Fine-tunes sensitivity automatically for major assets — NASDAQ (NQ), S&P 500 (ES), Gold (GC), Oil (CL), Bitcoin (BTC).
Volatility Normalization: Optionally divides ROC by its own standard deviation to stabilize noisy assets and maintain consistent scaling.
🔔 Signals & Alerts
Bullish Reversal:
ROC crosses above its signal or zero line — early momentum flip.
Bearish Reversal:
ROC crosses below its signal or zero line — downward momentum flip.
Alerts:
Both reversal conditions include built-in alert triggers for automation and notifications.
🎨 Visual Features
Main ROC Line: Adaptive EMA of ROC, color-coded by trend regime.
Signal Line: Optional white EMA overlay for MACD-style crossovers.
Histogram: Visual burst display of acceleration (green/red).
Reversal Markers: Optional triangles marking exact crossover points.
Threshold Lines: Highlight the zero and buffer zones for visual clarity.
🧩 Best Use Cases
Identify early momentum shifts before price confirms them.
Confirm trend continuation or exhaustion with color persistence.
Detect choppy / low-volatility periods instantly.
Works across all timeframes — from 1-minute scalping to weekly swings.
Combine with structure, EMAs, or volume for confirmation.
⚙️ Recommended Settings
Setting Default Description
ROC Period 6 Core momentum length (lower = faster response).
Signal EMA Length 3 MACD-style responsiveness (lower = more reactive).
Zero Buffer Threshold 0.15 Defines the width of the neutral zone around zero.
Choppy Zone Multiplier 1.0 Expands or tightens the orange zone sensitivity.
These defaults have been optimized through real-market testing to balance responsiveness and smoothness across different asset classes.
⚠️ Notes
The color regime is persistent, meaning once the line turns bullish or bearish, it remains in that state until momentum structurally flips.
The orange zone represents momentum uncertainty and helps avoid false entries in range-bound markets.
Works seamlessly on any timeframe and with any asset.
davidqqq//@version=5
indicator('CD', overlay=false, max_bars_back=500)
// 输入参数
S = input(12, title='Short EMA Period')
P = input(26, title='Long EMA Period')
M = input(9, title='Signal Line Period')
// 计算DIFF, DEA和MACD值
fastEMA = ta.ema(close, S)
slowEMA = ta.ema(close, P)
DIFF = fastEMA - slowEMA
DEA = ta.ema(DIFF, M)
MACD = (DIFF - DEA) * 2
// 计算N1和MM1
N1 = ta.barssince(ta.crossunder(MACD, 0))
MM1 = ta.barssince(ta.crossover(MACD, 0))
// 确保长度参数大于0
N1_safe = na(N1) ? 1 : math.max(N1 + 1, 1)
MM1_safe = na(MM1) ? 1 : math.max(MM1 + 1, 1)
// 计算CC和DIFL系列值
CC1 = ta.lowest(close, N1_safe)
CC2 = nz(CC1 , CC1)
CC3 = nz(CC2 , CC2)
DIFL1 = ta.lowest(DIFF, N1_safe)
DIFL2 = nz(DIFL1 , DIFL1)
DIFL3 = nz(DIFL2 , DIFL2)
// 计算CH和DIFH系列值
CH1 = ta.highest(close, MM1_safe)
CH2 = nz(CH1 , CH1)
CH3 = nz(CH2 , CH2)
DIFH1 = ta.highest(DIFF, MM1_safe)
DIFH2 = nz(DIFH1 , DIFH1)
DIFH3 = nz(DIFH2 , DIFH2)
// 判断买入条件
AAA = CC1 < CC2 and DIFL1 > DIFL2 and MACD < 0 and DIFF < 0
BBB = CC1 < CC3 and DIFL1 < DIFL2 and DIFL1 > DIFL3 and MACD < 0 and DIFF < 0
CCC = (AAA or BBB) and DIFF < 0
LLL = not CCC and CCC
XXX = AAA and DIFL1 <= DIFL2 and DIFF < DEA or BBB and DIFL1 <= DIFL3 and DIFF < DEA
JJJ = CCC and math.abs(DIFF ) >= math.abs(DIFF) * 1.01
BLBL = JJJ and CCC and math.abs(DIFF ) * 1.01 <= math.abs(DIFF)
DXDX = not JJJ and JJJ
DJGXX = (close < CC2 or close < CC1) and (JJJ or JJJ ) and not LLL and math.sum(JJJ ? 1 : 0, 24) >= 1
DJXX = not(math.sum(DJGXX ? 1 : 0, 2) >= 1) and DJGXX
DXX = (XXX or DJXX) and not CCC
// 判断卖出条件
ZJDBL = CH1 > CH2 and DIFH1 < DIFH2 and MACD > 0 and DIFF > 0
GXDBL = CH1 > CH3 and DIFH1 > DIFH2 and DIFH1 < DIFH3 and MACD > 0 and DIFF > 0
DBBL = (ZJDBL or GXDBL) and DIFF > 0
DBL = not DBBL and DBBL and DIFF > DEA
DBLXS = ZJDBL and DIFH1 >= DIFH2 and DIFF > DEA or GXDBL and DIFH1 >= DIFH3 and DIFF > DEA
DBJG = DBBL and DIFF >= DIFF * 1.01
DBJGXC = not DBJG and DBJG
DBJGBL = DBJG and DBBL and DIFF * 1.01 <= DIFF
ZZZZZ = (close > CH2 or close > CH1) and (DBJG or DBJG ) and not DBL and math.sum(DBJG ? 1 : 0, 23) >= 1
YYYYY = not(math.sum(ZZZZZ ? 1 : 0, 2) >= 1) and ZZZZZ
WWWWW = (DBLXS or YYYYY) and not DBBL
// plot买入和卖出信号
if DXDX
label.new(bar_index, low, text='抄底', style=label.style_label_up, color=color.red, textcolor=color.white, size=size.small)
if DBJGXC
label.new(bar_index, high, text='卖出', style=label.style_label_down, color=color.green, textcolor=color.white, size=size.small)






















