In honor of Rob Smith (R.I.P), the creator of TheStrat, I decided to try to figure out how to make an indicator that graphically shows you the price levels and movements on higher timeframes.
The goal of TheStrat is to trade with full timeframe continuity.
How the Script Works
1. User Input Configuration
Code:
timeframe_opt = input.timeframe("60", "Select Timeframe", options=["1", "3", "5", "15", "30", "60", "240", "D", "W", "M", "3M", "12M"])
show_hl = input.bool(false, "Show Hi/Lo")
The timeframe_opt input lets users pick a custom timeframe, such as 1 minute or 1 month. The default is 60 minutes. The show_hl option allows users to toggle the visibility of high/low markers on the custom candles.
2. Detecting a New Candle for the Selected Timeframe
Code:
is_new_candle = (time(timeframe_opt) != time(timeframe_opt)[1])
A new candle in the selected timeframe is detected when the timestamp for the current bar in the chosen timeframe differs from the previous bar.
3. Variable Initialization for Open, High, Low, and Close Offsets
pinescript
Copy code
var float open_offset = na
var float high_offset = na
var float low_offset = na
var float close_offset = na
var line ln = na
These variables store the custom candle's open, high, low, and close values, relative to the current custom timeframe.
4. Calculating Candle Values
Code:
if is_new_candle
open_offset := open
high_offset := high - open_offset
low_offset := low - open_offset
close_offset := close - open_offset
else
high_offset := math.max(high - open_offset, high_offset[1])
low_offset := math.min(low - open_offset, low_offset[1])
close_offset := close - open_offset
When a new candle starts:
open_offset is set to the current bar's open price.
high_offset, low_offset, and close_offset are calculated relative to this open price.
For subsequent bars within the same candle:
The high and low offsets are updated based on the highest high and lowest low.
The close offset is recalculated continuously.
5. Plotting the Custom Timeframe Candle
Code
plotcandle(
open_offset - open_offset, high_offset, low_offset, close_offset,
title="Custom Timeframe Candle",
color=(close_offset >= 0 ? color.green : color.red),
bordercolor=color.black, wickcolor=color.black, editable=true
)
A custom candle is plotted using plotcandle().
Color: If the close price is greater than or equal to the open price, the candle is green; otherwise, it is red.
Borders and wicks are colored black.
6. Plotting High and Low Markers
Code:
l ? high_offset : na, color=color.black, style=plot.style_circles, linewidth=1)
plot(show_hl ? low_offset : na, color=color.black, style=plot.style_circles, linewidth=1)
If the show_hl option is enabled, the high and low markers are plotted as black circles.
7. Drawing Dashed Line for New Candles on Intraday Timeframes
Code:
if is_new_candle and timeframe.isintraday
ln := line.new(
bar_index, 2.22, bar_index, -2.22,
xloc=xloc.bar_index, color=color.new(color.black, 45),
style=line.style_dashed, width=1, extend=extend.both
)
For new intraday candles, a dashed line is drawn at the start of the candle to visually mark it on the chart.
8. Displaying the Selected Timeframe in a Table
pinescript
Code:
var table tf_table = table.new(position = position.top_right, columns = 1, rows = 1, frame_color = color.gray, frame_width = 1, border_width = 1)
if is_new_candle
table.cell(
tf_table, 0, 0, "Timeframe: " + timeframe_opt,
text_color = color.white, text_halign = text.align_center
)
A table is created at the top-right corner of the chart, displaying the selected timeframe. The table updates only when a new candle starts.
How to Use It:
Ideally when you are analyzing the lower time, you want it to be in continuity with the higher time frames.
All you have to do is put the indicator on the number of timeframes you want to monitor.
So if you want the hourly, the daily, the weekly, and the monthly, then you have to just put on the indicator 4 times--once for each timeframe.
Then you go into each one you put on the chart and go to the drop-down menu where you choose the timeframe you want displayed.