Session TPO Profile

noop-noop · study · 174 行 · 点赞 5,616 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © noop42
 
//@version=5
indicator("Session TPO Profile", overlay=true, max_bars_back=5000, max_boxes_count = 500, max_lines_count = 500)
 
reset_h = input.int(09, minval=0, maxval=23, group="Session configuration")
reset_m = input.int(30, minval=0, maxval=59, group="Session configuration")
candles_nb = input.int(13, "Number of candles", group="Session configuration")
 
extend_levels = input.bool(true, "Extend yesterday's levels", group="Global Options")
show_box_background = input.bool(true, "Show Market Profile calculation area background", group="Global Options")
bg_color=input.color(#43465120, "Market profile area background color", group="Global Options")
show_histogram = input.bool(true, "Show TPO's", group="Global Options")
tpo_va_color = input.color(color.white, "TPO Color (in VA)", group="Global Options")
tpo_nova_color = input.color(color.gray, "TPO Color (out of VA)", group="Global Options")
bars_nb = input.int(50, "Bars number", maxval=50, minval=3, group="Global Options")
 
limits_color=input.color(color.black, "High and Low colors", group="Highs & Lows")
show_high_low = input.bool(true, "Show high/low levels", group="Highs & Lows")
 
poc_col = input.color(#b58900, "POC color", group="POC Options")
show_poc = input.bool(true, "Show POC as line", group="POC Options")
 
va_line_color = input.color(#2962ff, "Value area lines color", group="Value area options")
show_va_lines = input.bool(true, "Show value areas as lines",group="Value area options")
va_percent = input.int(68, "Value area percent",group="Value area options") / 100
current_price = input.bool(true, "Show current price", group="Current price")
curr_price_color = input.color(#20f1f5, "Current price line", group="Current price")
 
show_singleprints = input.bool(true, "Highlight Single prints", group="Single prints")
sp_col = input.color(#709b00, "Single prints color", group="Single prints")
 
new_session = hour == reset_h and minute == reset_m and barstate.isconfirmed
 
letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
 
var line topline = na
var line botline = na
var line vah_line = na
var line val_line = na
var line poc = na
 
//var max_score = 0
var poc_price = 0.0
var bars_score = array.new<int>(bars_nb)
var profile_bars = array.new<box>(bars_nb)
var letter_bars = array.new<string>(bars_nb)
var vah_offset = 0
var val_offset = 0
 
var line curr_price_line = na
 
if current_price
    line.delete(curr_price_line)
    curr_price_line := line.new(bar_index+10, close, bar_index, close, extend=extend.left, color=curr_price_color, style=line.style_arrow_right)
 
find_h(x) =>
    h = high
    for i=0 to x
        if high[i] > h
            h := high[i]
    h
find_l(x) =>
    l = low
    for i=0 to x
        if low[i] < l
            l := low[i]
    l
count_tpo(x, y) =>
    r = 0
    for i=x to y
        r +=  array.get(bars_score,i)
    r
 
biggest_tpo(x, y) =>
    t1 = array.get(bars_score, x)
    t2 = array.get(bars_score, y)
    t1 >= t2 ? "h": "l"
 
var candle_offset = 0
var first_daily_candle_offset = 0
var poc_position = 0
var total_tpo = 0
var open_price = open
 
if ta.change(dayofweek)
    first_daily_candle_offset := 0
 
if new_session
    candle_offset := 0
    open_price := open
 
if candle_offset == candles_nb -1
    ext = extend_levels ? extend.right: extend.none
    if extend_levels
        line.set_extend(vah_line, extend.none)
        line.set_extend(val_line, extend.none)
        line.set_extend(poc, extend.none)
        line.set_extend(topline, extend.none)
        line.set_extend(botline, extend.none)
        
    max_score = 0
    total_tpo := 0
    h = find_h(candle_offset)
    l = find_l(candle_offset)
 
    box_width = (h-l) / bars_nb
 
    if show_poc
        poc := line.new(bar_index-first_daily_candle_offset, 0.0, bar_index, 0.0, color=poc_col, width=2, extend=ext)
    if show_high_low
        topline := line.new(bar_index-first_daily_candle_offset, h, bar_index, h, color=limits_color, extend=ext)
        botline := line.new(bar_index-first_daily_candle_offset, l, bar_index, l, color=limits_color, extend=ext)
    if show_box_background
        box.new(bar_index-candle_offset, h, bar_index, l, bgcolor=bg_color, border_color=#00000000)
 
    // TPO calculation
    for i=0 to bars_nb-1
        tpo_letters = ""
        score = 0
        for x=0 to candle_offset
            in_bar = (high[x] <= h and high[x] >= h-box_width) or (low[x] <= h and low[x] >= h-box_width) or (high[x] > h and low[x] < h-box_width)
            if in_bar
                score += 1
                if x <= candle_offset and candle_offset <= 52
                    tpo_letters := str.substring(letters, candle_offset-(x), (candle_offset - (x-1)))+tpo_letters+" "
                
        if score >= max_score
            max_score := score
            poc_price :=  h - (box_width/2)
            poc_position := i
        
        line.set_y1(poc, poc_price)
        line.set_y2(poc, poc_price)
        array.insert(profile_bars,i, box.new(bar_index-first_daily_candle_offset, h, bar_index-(first_daily_candle_offset-score), h-box_width, text=tpo_letters, text_size=size.small, text_color=tpo_va_color, text_halign=text.align_left, bgcolor=#00000000, border_color=#00000000))
        array.insert(bars_score, i, score)
        h -= box_width
        total_tpo += score
 
    // VA Calculation
    vah_price = 0.0
    val_price = 0.0
    vah_offset := poc_position > 0 ? poc_position -1 : poc_position
    val_offset := poc_position < bars_nb -1 ? poc_position +1 : poc_position
 
    for i=0 to bars_nb-1
        d = vah_offset == 0 ? "l" : val_offset == bars_nb-1 ? "h" : biggest_tpo(vah_offset, val_offset)
        if d == "h"
            vah_offset := vah_offset > 0 ? vah_offset -1 : vah_offset
        else
            val_offset := val_offset < bars_nb-1 ? val_offset +1 : val_offset
        if count_tpo(vah_offset, val_offset) / total_tpo >= va_percent
            break
 
    vah_price := box.get_top(array.get(profile_bars, vah_offset))-(box_width/2)
    val_price := box.get_bottom(array.get(profile_bars, val_offset))+(box_width/2)
 
    // Set tpo colors
    for i=0 to bars_nb-1
        bar_col = #00000000
        text_col =  (i > vah_offset and i < val_offset ? tpo_va_color : tpo_nova_color)
        text_col := val_offset == i or vah_offset == i ? va_line_color : poc_position == i ? poc_col : (array.get(bars_score, i) == 1 and show_singleprints) ? sp_col : text_col
        if not show_histogram
            text_col := #00000000
        box.set_text_color(array.get(profile_bars, i), text_col)
        
    //plot vah/val
    if show_va_lines
        vah_line := line.new(bar_index-first_daily_candle_offset, vah_price, bar_index, vah_price, color=va_line_color, extend=ext)
        val_line := line.new(bar_index-first_daily_candle_offset, val_price, bar_index, val_price, color=va_line_color, extend=ext)
    label.new(bar_index-first_daily_candle_offset-1, open_price, text="🢒", style=label.style_none, textcolor=color.yellow)
candle_offset += 1
first_daily_candle_offset += 1

← 返回列表