Almazov Trade: Structure

Algorithms_ATS · study · 246 行 · 点赞 39 · TradingView 原页

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

Pine Script

// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Algorithms_ATS
 
//@version=6
indicator("Almazov Trade: Structure", overlay = true, max_boxes_count = 500, max_labels_count = 500, max_lines_count = 500)
 
int structure_tf = input.int(60, "Таймфрейм структуры (минуты)", minval=1, tooltip="Например: 60 — 1 час, 120 — 2 часа. Значение должно быть не меньше таймфрейма графика, делиться на него без остатка и превышать его не более чем в 100 раз.", display=display.none)
if barstate.isfirst
    if not timeframe.isminutes
        runtime.error("Индикатор работает только на минутных и часовых графиках.")
    if structure_tf<timeframe.multiplier
        runtime.error("Таймфрейм структуры должен быть не меньше таймфрейма графика.")
    if structure_tf%timeframe.multiplier!=0
        runtime.error("Таймфрейм структуры должен делиться на таймфрейм графика без остатка.")
    if structure_tf>100*timeframe.multiplier
        runtime.error("Таймфрейм структуры не может быть больше таймфрейма графика более чем в 100 раз.")
 
int tf = int(structure_tf/timeframe.multiplier)
 
int D_swing_high = input.int(2, "Swing high: баров слева и справа", minval=1, maxval=60, display=display.none)
int D_swing_low = input.int(2, "Swing low: баров слева и справа", minval=1, maxval=60, display=display.none)
var int need_choch = input.bool(true, "Показывать линии и подписи CHoCH", display=display.none) ? 1 : 0
 
color up_trend = input.color(color.green, "Восходящая структура", display=display.none)
color down_trend = input.color(color.red, "Нисходящая структура", display=display.none)
color choch_up_trend = input.color(color.black, "Линия CHoCH при пробое вниз", display=display.none)
color choch_down_trend = input.color(color.black, "Линия CHoCH при пробое вверх", display=display.none)
 
type mypair
    float q
    int bar_id
    int sup_id
 
type mybar
    float hg
    float lw
    int high_id
    int low_id
 
var array< mybar > bars = array.new<mybar>(0)
 
var int sz_bars=0
 
var int dh = D_swing_high+1
var int dl = D_swing_low+1
 
get_high_id(int cnt)=>
    float mx = high
    int ans = bar_index
    int pos=bar_index
    for int i=0 to cnt-1
        if high[i]>mx
            mx:=high[i]
            ans:=pos
        pos-=1
    ans
get_low_id(int cnt)=>
    float mn = low
    int ans = bar_index
    int pos=bar_index
    for int i=0 to cnt-1
        if low[i]<mn
            mn:=low[i]
            ans:=pos
        pos-=1
    ans
 
get_high(int cnt)=>
    float mx = high
    for int i=0 to cnt-1
        if high[i]>mx
            mx:=high[i]
    mx
get_low(int cnt)=>
    float mn = low
    for int i=0 to cnt-1
        if low[i]<mn
            mn:=low[i]
    mn
 
cT = time
currentTime = int(cT / (1000*60*timeframe.multiplier))
 
is_swing_high(int pos)=>
    ans = true
    if pos-D_swing_high<0 or pos+D_swing_high>=sz_bars
        ans:=false
    else
        float H = bars.get(pos).hg
        for int i=pos-D_swing_high to pos+D_swing_high
            if bars.get(i).hg>H and i!=pos
                ans:=false
    ans
is_swing_low(int pos)=>
    ans = true
    if pos-D_swing_low<0 or pos+D_swing_low>=sz_bars
        ans:=false
    else
        float L = bars.get(pos).lw
        for int i=pos-D_swing_low to pos+D_swing_low
            if bars.get(i).lw<L and i!=pos
                ans:=false
    ans
get_min(int l, int r)=>
    float ans = 100000000
    if l<=r
        for int i=l to r
            ans:=math.min(ans, bars.get(i).lw)
    ans
get_max(int l, int r)=>
    float ans = -1
    if l<=r
        for int i=l to r
            ans:=math.max(ans, bars.get(i).hg)
    ans
fnd_sw_low(int l, int r, int from_bar, int to_bar)=>
    mypair res = mypair.new(-1, -1, -1)
    if l<=r
        for int i=l to r
            if bars.get(i).low_id>from_bar and bars.get(i).low_id<to_bar and (res.q==-1 or res.q>bars.get(i).lw) and is_swing_low(i)
                res := mypair.new(bars.get(i).lw, bars.get(i).low_id, i)
    res
fnd_sw_high(int l, int r, int from_bar, int to_bar)=>
    mypair res = mypair.new(-1, -1, -1)
 
    if l<=r
        for int i=l to r
            if bars.get(i).high_id>from_bar and bars.get(i).high_id<to_bar and (res.q==-1 or res.q<bars.get(i).hg) and is_swing_high(i)
                res := mypair.new(bars.get(i).hg, bars.get(i).high_id, i)
    res
 
var line[] lines_trand = array.new_line(0)
 
var mypair now_low = mypair.new(-1, -1, -1)
var mypair now_high = mypair.new(-1, -1, -1)
var int first_build = -2
var int started = -1
 
var int type_of_trend = 0
 
if barstate.isconfirmed and bar_index>=tf-1 and (currentTime+1)%tf==0 and int(time[tf-1]/(60000*structure_tf))==int(time/(60000*structure_tf))
    bars.push(mybar.new(get_high(tf), get_low(tf), get_high_id(tf), get_low_id(tf)))
    sz_bars+=1
 
    sw_low = is_swing_low(sz_bars-D_swing_low-1)
    sw_high = is_swing_high(sz_bars-D_swing_high-1)
    if first_build!=0
        if sw_low==true and now_low.q==-1
            now_low:=mypair.new(bars.get(sz_bars-dl).lw, bars.get(sz_bars-dl).low_id, sz_bars-dl)
            first_build+=1
        if sw_high==true and now_high.q==-1
            now_high:=mypair.new(bars.get(sz_bars-dh).hg, bars.get(sz_bars-dh).high_id, sz_bars-dh)
            first_build+=1
 
    if started==-1
        if now_low.q!=-1 and now_low.q>get_min(now_low.sup_id, sz_bars-1)
            now_low:=fnd_sw_low(now_low.sup_id+1, sz_bars-dl, -1, bar_index+1)
            if now_low.q!=-1 and now_low.q>get_min(now_low.sup_id, sz_bars-1)
                now_low:=mypair.new(-1, -1, -1)
 
        if now_high.q!=-1 and now_high.q<get_max(now_high.sup_id, sz_bars-1)
            now_high:=fnd_sw_high(now_high.sup_id+1, sz_bars-dh, -1, bar_index+1)
            if now_high.q!=-1 and now_high.q<get_max(now_high.sup_id, sz_bars-1)
                now_high:=mypair.new(-1, -1, -1)
 
        first_build:=(now_low.q==-1 ? -1 : 0)+(now_high.q==-1 ? -1 : 0)
 
    if started==1
        if type_of_trend==1
            if sw_high==true and bars.get(sz_bars-dh).hg>now_high.q 
                new_low = fnd_sw_low(now_high.sup_id, math.min(sz_bars-dl, sz_bars-dh), now_high.bar_id, bars.get(sz_bars-dh).high_id)
                new_high = mypair.new(bars.get(sz_bars-dh).hg, bars.get(sz_bars-dh).high_id, sz_bars-dh)
 
                if new_low.q!=-1 and new_low.q<=get_min(new_low.sup_id, sz_bars-1) and new_low.bar_id>now_high.bar_id and new_low.q<now_high.q
                    now = line.new(x1=now_high.bar_id, y1=now_high.q, x2=new_low.bar_id, y2=new_low.q, color=up_trend, width = 3) 
                    lines_trand.push(now)
                    now := line.new(x1=new_low.bar_id, y1=new_low.q, x2=new_high.bar_id, y2=new_high.q, color=up_trend, width = 3) 
                    lines_trand.push(now)
 
                    now_high:=new_high
                    now_low:=new_low
                else
                    last = lines_trand.get(lines_trand.size()-1)
                    last.set_x2(new_high.bar_id)
                    last.set_y2(new_high.q)
                    now_high:=new_high
 
            if bars.get(sz_bars-1).lw<now_low.q
                if need_choch==1
                    line.new(x1=now_low.bar_id, y1=now_low.q, x2=bar_index, y2=now_low.q, color=choch_up_trend, width = 2)
                    label.new(int((now_low.bar_id+bar_index)/2), now_low.q, color=color.white, textcolor=down_trend, style = label.style_label_center, text="CHoCH")
 
                now_low:=mypair.new(-1, -1, -1)
                first_build:=-1
                started:=-1
                type_of_trend:=0
        else
            if sw_low==true and bars.get(sz_bars-dl).lw<now_low.q
                new_high = fnd_sw_high(now_low.sup_id, math.min(sz_bars-dh, sz_bars-dl), now_low.bar_id, bars.get(sz_bars-dl).low_id)
                new_low = mypair.new(bars.get(sz_bars-dl).lw, bars.get(sz_bars-dl).low_id, sz_bars-dl)
                if new_high.q!=-1 and new_high.q>=get_max(new_high.sup_id, sz_bars-1) and new_high.bar_id>now_low.bar_id and new_high.q>now_low.q
                    now = line.new(x1=now_low.bar_id, y1=now_low.q, x2=new_high.bar_id, y2=new_high.q, color=down_trend, width = 3) 
                    lines_trand.push(now)
                    now := line.new(x1=new_high.bar_id, y1=new_high.q, x2=new_low.bar_id, y2=new_low.q, color=down_trend, width = 3) 
                    lines_trand.push(now)
 
                    now_low:=new_low
                    now_high:=new_high
                else
                    last = lines_trand.get(lines_trand.size()-1)
                    last.set_x2(new_low.bar_id)
                    last.set_y2(new_low.q)
                    now_low:=new_low
 
            if bars.get(sz_bars-1).hg>now_high.q
                if need_choch==1
                    line.new(x1=now_high.bar_id, y1=now_high.q, x2= bar_index, y2=now_high.q, color=choch_down_trend, width = 2)
                    label.new(int((now_high.bar_id+bar_index)/2), now_high.q, color=color.white, textcolor=up_trend, style = label.style_label_center, text="CHoCH")
 
                now_high:=mypair.new(-1, -1, -1)
                first_build:=-1
                started:=-1
                type_of_trend:=1
 
    if started==-1 and first_build==0
        started:=1
        if now_low.bar_id<now_high.bar_id
            if lines_trand.size()>0
                last = lines_trand.get(lines_trand.size()-1)
                last.set_x2(now_low.bar_id)
                last.set_y2(now_low.q)
 
            now = line.new(x1=now_low.bar_id, y1=now_low.q, x2=now_high.bar_id, y2=now_high.q, color=up_trend, width = 3) 
            lines_trand.push(now)
            type_of_trend:=1
        else
            if lines_trand.size()>0
                last = lines_trand.get(lines_trand.size()-1)
                last.set_x2(now_high.bar_id)
                last.set_y2(now_high.q)
 
            now = line.new(x1=now_high.bar_id, y1=now_high.q, x2=now_low.bar_id, y2=now_low.q, color=down_trend, width = 3) 
            lines_trand.push(now)
            type_of_trend:=0
 
plot((type_of_trend==1 ? 1 : -1), color = (type_of_trend==1 ? color.green : color.red), title = 'type_of_trend', display = display.status_line)

← 返回列表