本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © NJ_Algo
//@version=6
indicator("Consecutive Candle Streaks & Forward Returns [NJ]", "CCSFR", overlay = false)
// ███╗ ██╗ ██╗ █████╗ ██╗ ██████╗ ██████╗
// ████╗ ██║ ██║ ██╔══██╗██║ ██╔════╝ ██╔═══██╗
// ██╔██╗ ██║ ██║ ███████║██║ ██║ ███╗██║ ██║
// ██║╚██╗██║ ██║ ██╔══██║██║ ██║ ██║██║ ██║
// ██║ ╚████║ ██╗ ██║ ██║ ██║██║ ██║ ██║██║ ██║
// ██║ ╚███║ ╚████╔╝ ██║ ██║███████╗╚██████╔╝╚██████╔╝
// ╚═╝ ╚══╝ ╚═══╝ ╚═╝ ╚═╝╚══════╝ ╚═════╝ ╚═════╝
// ✧═════════════════✧
// ║ INPUTS ║
// ✧═════════════════✧
const string t1 = "Only candles from this date onward are counted. On short timeframes the chart may not go back this far, so counting starts from the oldest candle available."
const string t2 = "A streak is a run of green or red candles in a row. A candle that closes exactly where it opened (a doji) ends any streak.\n\nStreaks this long or longer are added together in the last column (e.g. 12+). Streaks of just 1 candle are not shown."
const string t3 = "How much price moved on average after a streak, in %. It is measured from the close of the streak's last candle to the close this many candles later.\n\nExample with 4: the next 4 candles after the streak, including the one that ended it.\n\nVery recent streaks that don't have enough candles after them yet are left out."
const string t4 = "Also counts the streak that is still going on right now. It is not used in the averages, because we don't know yet what happens after it. The current candle that hasn't closed yet is never counted."
const string t5 = "Lets a green streak survive one red candle in between, once per streak. That red candle doesn't add to the streak length.\n\nExample: green, green, red, green = a 3-candle streak.\n\nTwo red candles in a row, a second red candle later, or a doji still end the streak."
const string t6 = "Lets a red streak survive one green candle in between, once per streak. That green candle doesn't add to the streak length.\n\nExample: red, red, green, red = a 3-candle streak.\n\nTwo green candles in a row, a second green candle later, or a doji still end the streak."
const string g1 = "✧ DATE FILTER SETTINGS ✧"
startDate = input.time(timestamp("1 Jan 2018"), "Count streaks from", group = g1, tooltip = t1)
const string g2 = "✧ DISPLAY ✧"
dispCandles = input.bool(true, "Color candles", group = g2)
tableSize = input.int(18, "Table size", 8, 100, group = g2)
maxLen = input.int(12, "Longest streak shown", 2, 30, group = g2, tooltip = t2)
const string g3 = "✧ CALCULATION ✧"
forwardBars = input.int(4, "Forward return period (candles)", 1, 100, group = g3, tooltip = t3)
includeOngoing = input.bool(false, "Include latest unfinished streak", group = g3, tooltip = t4)
allowOneBear = input.bool(false, "Allow 1 bearish candle inside bullish streak", group = g3, tooltip = t5)
allowOneBull = input.bool(false, "Allow 1 bullish candle inside bearish streak", group = g3, tooltip = t6)
// ✧══════════════════════✧
// ║ CALCULATION ║
// ✧══════════════════════✧
// Variables
var int bullStreak = 0
var bool bullUsedBear = false
var bool bullPendingBear = false
var array<int> bullCounts = array.new_int(maxLen + 1, 0)
var int bearStreak = 0
var bool bearUsedBull = false
var bool bearPendingBull = false
var array<int> bearCounts = array.new_int(maxLen + 1, 0)
var int bullLastBar = na
var float bullLastClose = na
var int bearLastBar = na
var float bearLastClose = na
var array<float> bullRetSum = array.new_float(maxLen + 1, 0.0)
var array<float> bearRetSum = array.new_float(maxLen + 1, 0.0)
var array<int> bullRetCount = array.new_int(maxLen + 1, 0)
var array<int> bearRetCount = array.new_int(maxLen + 1, 0)
// Pending forward returns
type StreakEvent
bool bullish
int streakLength
int lastCandleIndex
float lastCandleClose
var array<StreakEvent> pendingStreaks = array.new<StreakEvent>()
if barstate.isconfirmed and time >= startDate
// Bullish streak counting
if close > open
if bullPendingBear
bullUsedBear := true
bullPendingBear := false
bullStreak += 1
bullLastBar := bar_index
bullLastClose := close
else if allowOneBear and close < open and bullStreak > 0 and not bullUsedBear and not bullPendingBear
bullPendingBear := true
else
if bullStreak > 0
int bucket = math.min(bullStreak, maxLen)
array.set(bullCounts, bucket, array.get(bullCounts, bucket) + 1)
if bullStreak >= 2
array.push(pendingStreaks, StreakEvent.new(true, bucket, bullLastBar, bullLastClose))
bullStreak := 0
bullUsedBear := false
bullPendingBear := false
bullLastBar := na
bullLastClose := na
// Bearish streak counting
if close < open
if bearPendingBull
bearUsedBull := true
bearPendingBull := false
bearStreak += 1
bearLastBar := bar_index
bearLastClose := close
else if allowOneBull and close > open and bearStreak > 0 and not bearUsedBull and not bearPendingBull
bearPendingBull := true
else
if bearStreak > 0
int bucket = math.min(bearStreak, maxLen)
array.set(bearCounts, bucket, array.get(bearCounts, bucket) + 1)
if bearStreak >= 2
array.push(pendingStreaks, StreakEvent.new(false, bucket, bearLastBar, bearLastClose))
bearStreak := 0
bearUsedBull := false
bearPendingBull := false
bearLastBar := na
bearLastClose := na
// Forward returns
if barstate.isconfirmed and array.size(pendingStreaks) > 0
for i = array.size(pendingStreaks) - 1 to 0
StreakEvent event = array.get(pendingStreaks, i)
int barsAfterTarget = bar_index - event.lastCandleIndex - forwardBars
if barsAfterTarget >= 0
float targetClose = barsAfterTarget == 0 ? close : close[1]
float forwardReturn = (targetClose / event.lastCandleClose - 1.0) * 100.0
int n = event.streakLength
if event.bullish
array.set(bullRetSum, n, array.get(bullRetSum, n) + forwardReturn)
array.set(bullRetCount, n, array.get(bullRetCount, n) + 1)
else
array.set(bearRetSum, n, array.get(bearRetSum, n) + forwardReturn)
array.set(bearRetCount, n, array.get(bearRetCount, n) + 1)
array.remove(pendingStreaks, i)
// ✧════════════════✧
// ║ PLOTS ║
// ✧════════════════✧
// Colors
longCol = color.rgb(0, 136, 100)
shortCol = color.rgb(120, 0, 32)
neutCol = color.rgb(0, 43, 136)
// Statistics table
var table t = table.new(position.middle_center, maxLen + 1, 5, bgcolor = chart.bg_color, frame_color = color.new(chart.fg_color, 80), frame_width = 1, border_color = color.new(chart.fg_color, 80), border_width = 1)
if barstate.islast
table.cell(t, 0, 0, "Streak (" + timeframe.period + ")", bgcolor = neutCol, text_color = color.white, text_size = tableSize)
table.cell(t, 0, 1, "Bullish count", bgcolor = longCol, text_color = color.white, text_size = tableSize)
table.cell(t, 0, 2, "Avg. returns", bgcolor = longCol, text_color = color.white, text_size = tableSize)
table.cell(t, 0, 3, "Bearish count", bgcolor = shortCol, text_color = color.white, text_size = tableSize)
table.cell(t, 0, 4, "Avg. returns", bgcolor = shortCol, text_color = color.white, text_size = tableSize)
int totalBull = 0
int totalBear = 0
float totalBullRetSum = 0.0
float totalBearRetSum = 0.0
int totalBullRetCount = 0
int totalBearRetCount = 0
for n = 2 to maxLen
bool isLast = n == maxLen
int bullTotal = array.get(bullCounts, n)
int bearTotal = array.get(bearCounts, n)
if includeOngoing
if isLast ? bullStreak >= maxLen : bullStreak == n
bullTotal += 1
if isLast ? bearStreak >= maxLen : bearStreak == n
bearTotal += 1
totalBull += bullTotal
totalBear += bearTotal
string label = isLast ? str.tostring(n) + "+" : str.tostring(n)
table.cell(t, n - 1, 0, label, text_color = chart.fg_color, text_size = tableSize)
table.cell(t, n - 1, 1, str.tostring(bullTotal), text_color = chart.fg_color, text_size = tableSize)
table.cell(t, n - 1, 3, str.tostring(bearTotal), text_color = chart.fg_color, text_size = tableSize)
int bullN = array.get(bullRetCount, n)
int bearN = array.get(bearRetCount, n)
totalBullRetSum += array.get(bullRetSum, n)
totalBearRetSum += array.get(bearRetSum, n)
totalBullRetCount += bullN
totalBearRetCount += bearN
float bullAvg = bullN > 0 ? array.get(bullRetSum, n) / bullN : na
float bearAvg = bearN > 0 ? array.get(bearRetSum, n) / bearN : na
table.cell(t, n - 1, 2, na(bullAvg) ? "—" : str.tostring(bullAvg, "0.0") + "%", text_color = chart.fg_color, text_size = tableSize)
table.cell(t, n - 1, 4, na(bearAvg) ? "—" : str.tostring(bearAvg, "0.0") + "%", text_color = chart.fg_color, text_size = tableSize)
table.cell(t, maxLen, 0, "TOTAL", bgcolor = neutCol, text_color = color.white, text_size = tableSize)
table.cell(t, maxLen, 1, str.tostring(totalBull), bgcolor = longCol, text_color = color.white, text_size = tableSize)
table.cell(t, maxLen, 3, str.tostring(totalBear), bgcolor = shortCol, text_color = color.white, text_size = tableSize)
float totalBullAvg = totalBullRetCount > 0 ? totalBullRetSum / totalBullRetCount : na
float totalBearAvg = totalBearRetCount > 0 ? totalBearRetSum / totalBearRetCount : na
table.cell(t, maxLen, 2, na(totalBullAvg) ? "—" : str.tostring(totalBullAvg, "0.0") + "%", bgcolor = longCol, text_color = color.white, text_size = tableSize)
table.cell(t, maxLen, 4, na(totalBearAvg) ? "—" : str.tostring(totalBearAvg, "0.0") + "%", bgcolor = shortCol, text_color = color.white, text_size = tableSize)
// Color candles
sigCol = close > open ? longCol : close < open ? shortCol : na
plotcandle(
open, high, low, close,
"Colored candles",
dispCandles ? sigCol : na,
dispCandles ? sigCol : na,
bordercolor = dispCandles ? sigCol : na,
display = display.all - display.status_line - display.price_scale,
force_overlay = true)