0

我正在寻找修改和现有的指标,使其成为多时间框架。免费指标的来源是 Tradingview 上的 Reverse Stochastic [BApig Gift],但我希望它输出一个显示 MTF 随机斜率方向和价格的表格。

作为 Pinescript 的初学者,我已尽力实现它,但我被卡住了。请参阅随附的 pinescript 以获得所需的结果。

//@author = Motgench, balipour and Wugamlo

//@version=5
indicator('STOCHTABLE', 'STOCHTABLE', overlay=true, precision=2)

len = input.int(14, 'K', minval=2)
D = input.int(3, 'D', minval=1)
E = input.string('Forecasted KD', 'KD Estimation', options=['Current Dynamic KD', 'Current Static KD', 'Forecasted KD'])
smooth = input.int(6, 'Smooth', minval=1)
ures = input(true, 'Use Multi Time Frame')
reso = input.string('1 hour', '  Multi Time Frame Resolution ', options=['1 hour', '2 hours', '3 hours', '4 hours', '12 hours', '1 day'])
reso1 = input.string('2 hours', '  Multi Time Frame Resolution ', options=['1 hour', '2 hours', '3 hours', '4 hours', '12 hours', '1 day'])
reso2 = input.string('3 hours', '  Multi Time Frame Resolution ', options=['1 hour', '2 hours', '3 hours', '4 hours', '12 hours', '1 day'])
reso3 = input.string('4 hours', '  Multi Time Frame Resolution ', options=['1 hour', '2 hours', '3 hours', '4 hours', '12 hours', '1 day'])
reso4 = input.string('12 hours', '  Multi Time Frame Resolution ', options=['1 hour', '2 hours', '3 hours', '4 hours', '12 hours', '1 day'])
reso5 = input.string('1 day', '  Multi Time Frame Resolution ', options=['1 hour', '2 hours', '3 hours', '4 hours', '12 hours', '1 day'])

showPanel = input(true, 'Show Stoch Cross Value Panel')
bs = input(false, 'Show OverBought OverSold Panel')
BL = input.string('Line Levels', '▼▼▼ Overbought Oversold Style ▼▼▼', options=['Line Levels', 'Bands'])
plb = input(false, 'Plot OverBought Price')
ob = input.float(80, '  OverBought Level', minval=50, maxval=100)
pls = input(false, 'Plot Oversold Price')
os = input.float(20, '  OverSold Level', minval=0, maxval=50)
sm = input(false, '======     Stoch Cross Smoothing     ======')
slen = input(8, '  Smoothing Length')
s = input(false, 'Plot Stoch Historical Value')
sv = input.string('K', '  Stoch Value', options=['K', 'D'])
//Cir = input(true, 'Plot Crossing Circles')
ib = input(true, 'Dark Background ⬛')
step = input(false, 'Use Step Lines')
dec = input.int(1, 'Decimals', minval=0, maxval=10)

ss(Series, Period) =>  // Super Smoother Function
    // period = input(8, "Super Smoother Period", input.float, minval=2.0, step=0.5)
    var PI = 2.0 * math.asin(1.0)
    var SQRT2 = math.sqrt(2.0)
    lambda = PI * SQRT2 / Period
    a1 = math.exp(-lambda)
    coeff2 = 2.0 * a1 * math.cos(lambda)
    coeff3 = -math.pow(a1, 2.0)
    coeff1 = 1.0 - coeff2 - coeff3
    filt1 = 0.0
    filt1 := coeff1 * (Series + nz(Series[1])) * 0.5 + coeff2 * nz(filt1[1]) + coeff3 * nz(filt1[2])
    filt1

getRez(intval) =>
    iff_1 = intval == '1 day' ? 'D' : '720'
    iff_2 = intval == '12 hours' ? '720' : iff_1
    iff_3 = intval == '4 hours' ? '240' : iff_2
    iff_4 = intval == '3 hours' ? '180' : iff_3
    iff_5 = intval == '2 hours' ? '120' : iff_4
    iff_6 = intval == '1 hour' ? '60' : iff_5

    int_1 = intval == '5 min' ? '5' : iff_6
    int_1


res = ures ? getRez(reso) : timeframe.period
//res2 = ures ? getRez(reso2) : timeframe.period
//res3 = ures ? getRez(reso3) : timeframe.period
//res4 = ures ? getRez(reso4) : timeframe.period
//res5 = ures ? getRez(reso5) : timeframe.period
//res6 = ures ? getRez(reso6) : timeframe.period


f_resInMinutes() =>
    _resInMinutes = timeframe.multiplier * (timeframe.isseconds ? 1. / 60 : timeframe.isminutes ? 1. : timeframe.isdaily ? 60. * 24 : timeframe.isweekly ? 60. * 24 * 7 : timeframe.ismonthly ? 60. * 24 * 30.4375 : na)
    _resInMinutes
// ————— Returns the float minutes value of the string _res.
f_tfResInMinutes(_res) =>
    // _res: resolution of any TF (in "timeframe.period" string format).
    // Dependency: f_resInMinutes().
    request.security(syminfo.tickerid, _res, f_resInMinutes())

// —————————— Determine if current timeframe is smaller that higher timeframe selected in Inputs.
// Get higher timeframe in minutes.
higherTfInMinutes = f_tfResInMinutes(res)
// Get current timeframe in minutes.
currentTfInMinutes = f_resInMinutes()
// Compare current TF to higher TF to make sure it is smaller, otherwise our plots don't make sense.
chartOnLowerTf = currentTfInMinutes <= higherTfInMinutes

mtf(data) =>
    chartOnLowerTf ? request.security(syminfo.tickerid, res, data) : data

st(len) =>
    (close - ta.lowest(low, len)) / (ta.highest(high, len) - ta.lowest(low, len))

x = ta.stoch(close, high, low, len) * 0.01
x2 = ta.sma(x, smooth)
x3 = ta.sma(x2, D)


C(level, len, smooth) =>
    if smooth == 1
        C = (ta.highest(high, len) - ta.lowest(low, len)) * level + ta.lowest(low, len)
        C
    else
        sum = 0.0
        for i = 1 to smooth - 1 by 1
            sum := x[i] + sum
            sum
        a = smooth * level - sum
        C = a * (ta.highest(high, len) - ta.lowest(low, len)) + ta.lowest(low, len)
        C

C2(len, smooth) =>
    float sumK = 0
    for i = 1 to smooth - 1 by 1
        sumK += x[i]
        sumK
    float sumKS = 0
    for i = 1 to D - 1 by 1
        sumKS += x2[i]
        sumKS
    C2 = (sumKS * smooth + sumK - sumK * D) * (ta.highest(high, len) - ta.lowest(low, len)) / (D - 1) + ta.lowest(low, len)
    C2


Cf(level, len, smooth) =>
    if smooth == 1
        Cf = (ta.highest(high, len) - ta.lowest(low, len)) * level + ta.lowest(low, len)
        Cf
    else
        sum = 0.0
        for i = 0 to smooth - 2 by 1
            sum := x[i] + sum
            sum
        a = smooth * level - sum
        Cf = a * (ta.highest(high, len - 1) - ta.lowest(low, len - 1)) + ta.lowest(low, len - 1)
        Cf


stp(len) =>
    (close - ta.lowest(low, len - 1)) / (ta.highest(high, len - 1) - ta.lowest(low, len - 1))

kk = ta.stoch(close, high, low, len) * 0.01

X = stp(len)
xk = 100 * ta.sma(kk, smooth)

k1(len) =>
    sum = 0.0
    for i = 0 to len - 2 by 1
        sum := kk[i] + sum
        mean = (sum + X) / len
        mean

X2 = 100 * k1(smooth)


d1(len) =>
    sum = 0.0
    for i = 0 to len - 2 by 1
        sum := xk[i] + sum
        sum
    mean = (sum + X2) / len
    mean


x3p = d1(D) * 0.01


ccks = mtf(ss(C(x3, len, smooth), slen))
cck = mtf(C(x3, len, smooth))
ccks2 = mtf(ss(C2(len, smooth), slen))
cck2 = mtf(C2(len, smooth))
cfks = mtf(ss(Cf(x3p, len, smooth), slen))
cfk = mtf(Cf(x3p, len, smooth))

stochk = mtf(x)
stochks = mtf(x2)
stochd = mtf(x3)
highmtf = mtf(ta.highest(high, len))
lowmtf = mtf(ta.lowest(low, len))

//Highest and Lowest K you can get on current Bar
sumk = 0.0
for i = 1 to smooth - 1 by 1
    sumk := stochk[i] + sumk
    sumk
hk = (1 + sumk) / smooth
lk = sumk / smooth

//Highest D and Lowest D you can get on current Bar
sumd = 0.0
for i = 1 to D - 1 by 1
    sumd := stochks[i] + sumd
    sumd
hd = (hk + sumd) / D
ld = (lk + sumd) / D


float Cross = na

if E == 'Forecasted KD'
    Cross := sm ? cfks : cfk
    Cross


Round(_val, _decimals) =>
    // Rounds _val to _decimals places.
    _p = math.pow(10, _decimals)
    math.round(math.abs(_val) * _p) / _p * math.sign(_val)


cob = mtf(C(0.01 * ob, len, smooth))
fob = mtf(Cf(0.01 * ob, len, smooth))

cos = mtf(C(0.01 * os, len, smooth))
fos = mtf(Cf(0.01 * os, len, smooth))
float obp = na

if E == 'Forecasted KD'
    obp := fob
    obp

float osp = na

if E == 'Forecasted KD'
    osp := fos
    osp

Co = close[1] > Cross[1] ? color.aqua : color.fuchsia
US = close > Cross and close[1] < Cross[1] ? Cross : na
UD = close < Cross and close[1] > Cross[1] ? Cross : na

float ps = na
if sv == 'K'
    ps := mtf(x2)
    ps
if sv == 'D'
    ps := mtf(x3)
    ps



lable(P, T, s, color_PnL) =>  // show_panel
    label PnL_Label = na
    PnL_Label := label.new(bar_index, P, text=T, color=color_PnL, textcolor=color.white, style=s, yloc=yloc.price, xloc=xloc.bar_index, size=size.small)
    PnL_Label


lable1(P, T, s, color_PnL) =>  // show_panel
    label PnL_Label = na
    PnL_Label := label.new(bar_index, P, text=T, color=color_PnL, textcolor=color.white, style=s, yloc=yloc.price, xloc=xloc.bar_index, size=size.normal)
    label.delete(PnL_Label[1])




//IMPORTANT for PLOT///
//label positioning and background//
ab = Cross > close
labelstyle = ab ? label.style_label_down : label.style_label_up
lbg = ib ? color.new(#000000, 45) : color.new(#000000, 99)


ud() =>
    if mtf(x3) > mtf(x2)
        'UP'
    else
        'DOWN'

revud() =>
    if mtf(x3) > mtf(x2)
        'DOWN'
    else
        'UP'


bslabel() =>
    bs ? '\n' + '\n' + 'OverBought : ' + str.tostring(Round(obp, dec)) + '\n' + '\n' + 'OverSold : ' + str.tostring(Round(osp, dec)) : na

/////////
////////


float obl = na
if BL == 'Line Levels'
    obl := obp
    obl
else
    na

float osl = na
if BL == 'Line Levels'
    osl := osp
    osl
else
    na


lb() =>
    float lb = na
    if barstate.islast
        lb := obl
        lb
    else
        na

ls() =>
    float ls = na
    if barstate.islast
        ls := osl
        ls
    else
        na


tf() =>
    if not ures or not chartOnLowerTf
        ''
    else

        reso


if showPanel
    lable1(Cross, tf() + ' Stoch Crossing ' + ud() + ' Price: ' + str.tostring(Round(Cross, dec)) + bslabel(), labelstyle, lbg)



var table t = table.new(position.top_right, 2, 6, border_width=1)

if showPanel and barstate.islast

    table.cell(t, 0, 0, '1HR is Crossing', width=5, bgcolor=#aaaaaa)
    table.cell(t, 0, 1, '2HR is Crossing', width=5, bgcolor=#aaaaaa)
    table.cell(t, 0, 2, '3HR is Crossing', width=5, bgcolor=#aaaaaa)
    table.cell(t, 0, 3, '4HR is Crossing', width=5, bgcolor=#aaaaaa)
    table.cell(t, 0, 4, '12HR is Crossing', width=5, bgcolor=#aaaaaa)
    table.cell(t, 0, 5, '1D is Crossing', width=5, bgcolor=#aaaaaa)

    table.cell(t, 1, 0, revud(), width=5, bgcolor=color.green)
    table.cell(t, 1, 1, revud(), width=5, bgcolor=color.green)
    table.cell(t, 1, 2, revud(), width=5, bgcolor=color.green)
    table.cell(t, 1, 3, revud(), width=5, bgcolor=color.green)
    table.cell(t, 1, 4, revud(), width=5, bgcolor=color.green)
    table.cell(t, 1, 5, revud(), width=5, bgcolor=color.green)
4

0 回答 0