首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >非重绘脚本有时不能正确更新。

非重绘脚本有时不能正确更新。
EN

Stack Overflow用户
提问于 2022-09-21 13:35:58
回答 1查看 55关注 0票数 0

这三个时间帧波趋势指示器不重新油漆后蜡烛关闭。它只使用来自较高时间帧的值时,较高的时间框栏已关闭。

但有时,两个较高的时间框架不会更新(编辑:保持相同的值),即使各自的栏已经关闭。不过,这种情况只发生在浏览器中。然后,在几个关闭之后,它再次正确地更新。这是在没有刷新浏览器的情况下发生的,如果我在两个浏览器选项卡中打开相同脚本的相同图表,则只能在其中一个选项卡中进行,但不能在另一个浏览器选项卡中进行。

如果使用服务器端警报,则始终传递正确的值。我已经在htf1WaveTrend值上测试了这个值。

有人知道为什么会这样吗?

编辑:图表的时间框架是1分钟。

我没有注意到其他指标也有类似的问题。

..。

代码语言:javascript
复制
//@version=5


indicator("TEST 3 Time Frames Wave Trend v5", overlay = false)

// @author LazyBear (original)
//
// If you use this code in its original/modified form, do drop me a note. 
//

//INPUTS
i_n1 =                      input(   10,            'Channel Length',                                                       inline='1',                             group='Wave Trend Settings')
i_n2 =                      input(   21,            'Average Length',                                                       inline='1',                             group='Wave Trend Settings')
i_ma =                      input(    8,            'Moving Average Length',                                                inline='1',                             group='Wave Trend Settings')
i_obLevel1 =                input(   60,            'Over Bought Level 1',                                                  inline='1',                             group='Wave Trend Settings')
i_obLevel2 =                input(   53,            'Over Bought Level 2. Cross below for Short',                           inline='1',                             group='Wave Trend Settings')
i_osLevel1 =                input(  -60,            'Over Sold Level 1',                                                    inline='1',                             group='Wave Trend Settings')
i_osLevel2 =                input(  -53,            'Over Sold Level 2. Cross above for Long',                              inline='1',                             group='Wave Trend Settings')
i_ap =                      input( hlc3,            'source',                                                               inline='1',                             group='Wave Trend Settings')
i_htf1 =                    input.timeframe('2',    'HTF 1',                                                                                                        group='Higher Time Frame Wave Trend')
i_htf2 =                    input.timeframe('4',    'HTF 2',                                                                                                        group='Higher Time Frame Wave Trend')



var htf1WaveTrend =         float(na)
var htf1WaveTrendSignal =   float(na)
var htf2WaveTrend =         float(na)
var htf2WaveTrendSignal =   float(na)

var wtColor =               color(na)
var htf1WaveTrendColor =    color(na)
var htf2WaveTrendColor =    color(na)

// FUNCTIONS
//      Wave Trend
f_waveTrend(_tf, _ap, _n1, _n2, _ma) =>
    _esa = request.security(syminfo.tickerid, _tf, ta.ema(_ap, _n1))
    _d = request.security(syminfo.tickerid, _tf, ta.ema(math.abs(_ap - _esa), _n1))
    _ci = (_ap - _esa) / (0.015 * _d)
    _wt = request.security(syminfo.tickerid, _tf, ta.ema(_ci, _n2))
    _wts = request.security(syminfo.tickerid, _tf, ta.sma(_wt, _ma))
    [_wt, _wts]


// CALCULATIONS
[NewHtf1WaveTrend, NewHtf1WaveTrendSignal] = f_waveTrend(i_htf1, i_ap, i_n1, i_n2, i_ma)
htf1BarIsClosed = request.security(syminfo.tickerid, i_htf1, barstate.isconfirmed) // Only use if the HTF bar is closed. Prevents repainting
// if htfBarIsClosed
//     htf1WaveTrend := NewHtf1WaveTrend
//     htf1WaveTrendSignal := NewHtf1WaveTrendSignal
// Alternative to "if"
htf1WaveTrend := ta.valuewhen(htf1BarIsClosed, NewHtf1WaveTrend, 0)
htf1WaveTrendSignal := ta.valuewhen(htf1BarIsClosed, NewHtf1WaveTrendSignal, 0)

[NewHtf2WaveTrend, NewHtf2WaveTrendSignal] = f_waveTrend(i_htf2, i_ap, i_n1, i_n2, i_ma)
htf2BarIsClosed = request.security(syminfo.tickerid, i_htf2, barstate.isconfirmed) // Only use if the HTF bar is closed. Prevents repainting
// if htfBarIsClosed
//     htf2WaveTrend := NewHtf2WaveTrend
//     htf2WaveTrendSignal := NewHtf2WaveTrendSignal
// Alternative to "if"
htf2WaveTrend := ta.valuewhen(htf2BarIsClosed, NewHtf2WaveTrend, 0)
htf2WaveTrendSignal := ta.valuewhen(htf2BarIsClosed, NewHtf2WaveTrendSignal, 0)

if htf1BarIsClosed
    alert('htf1 is: ' + str.tostring(htf1WaveTrend))


[wt, wts] = f_waveTrend(timeframe.period, i_ap, i_n1, i_n2, i_ma)

wtColor := wt[1] < wt[0] ? #00ff00 : wt[1] > wt[0] ? #eb4d4d : wtColor
htf1WaveTrendColor := htf1WaveTrend[1] < htf1WaveTrend[0] ? #018a01 : htf1WaveTrend[1] > htf1WaveTrend[0] ? #b51b1b : htf1WaveTrendColor
htf2WaveTrendColor := htf2WaveTrend[1] < htf2WaveTrend[0] ? #024d02 : htf2WaveTrend[1] > htf2WaveTrend[0] ? #800000 : htf2WaveTrendColor


plot(0, color=color.new(color.gray, 0))
plot(i_obLevel1, title='overbought', color=color.new(color.red, 0))
plot(i_osLevel1, title='oversold', color=color.new(color.green, 0))
plot(i_obLevel2, title='overbought min', color=color.new(color.red, 0), style=plot.style_cross)
plot(i_osLevel2, title='oversold max', color=color.new(color.green, 0), style=plot.style_cross)


plot(wt,title="WT",linewidth=3,color=wtColor)
plot(htf1WaveTrend,title="htfWT1",linewidth=3,color=htf1WaveTrendColor)
plot(htf2WaveTrend,title="htfWT2",linewidth=3,color=htf2WaveTrendColor)

..。

编辑:这是@Daveatt建议的修改版本,不幸的是,这给出的值与Wave趋势在更高时间范围内提供的实际值不匹配。不管怎样,谢谢你。

..。

代码语言:javascript
复制
//@version=5


indicator("TEST 3 Time Frames Wave Trend v5", overlay = false, precision=4)

// @author LazyBear (original)
//


//INPUTS
i_n1 =                      input(   10,            'Channel Length',                                                       inline='1',                             group='Wave Trend Settings')
i_n2 =                      input(   21,            'Average Length',                                                       inline='1',                             group='Wave Trend Settings')
i_ma =                      input(    8,            'Moving Average Length',                                                inline='1',                             group='Wave Trend Settings')
i_obLevel1 =                input(   60,            'Over Bought Level 1',                                                  inline='1',                             group='Wave Trend Settings')
i_obLevel2 =                input(   53,            'Over Bought Level 2. Cross below for Short',                           inline='1',                             group='Wave Trend Settings')
i_osLevel1 =                input(  -60,            'Over Sold Level 1',                                                    inline='1',                             group='Wave Trend Settings')
i_osLevel2 =                input(  -53,            'Over Sold Level 2. Cross above for Long',                              inline='1',                             group='Wave Trend Settings')
i_ap =                      input( hlc3,            'source',                                                               inline='1',                             group='Wave Trend Settings')
i_htf1 =                    input.timeframe('2',    'HTF 1',                                                                                                        group='Higher Time Frame Wave Trend')
i_htf2 =                    input.timeframe('4',    'HTF 2',                                                                                                        group='Higher Time Frame Wave Trend')



// var htf1WaveTrend =         float(na)
// var htf1WaveTrendSignal =   float(na)
// var htf2WaveTrend =         float(na)
// var htf2WaveTrendSignal =   float(na)

var wtColor =               color(na)
var htf1WaveTrendColor =    color(na)
var htf2WaveTrendColor =    color(na)

// FUNCTIONS
// Security
f_security(_sym, _res, _src, _rep) => 
    request.security(_sym, _res, _src[not _rep and barstate.isrealtime ? 1 : 0])[_rep or barstate.isrealtime ? 0 : 1]

//      Wave Trend
f_waveTrend(_tf, _ap, _n1, _n2, _ma) =>
    _esa = f_security(syminfo.tickerid, _tf, ta.ema(_ap, _n1), false)
    _d = f_security(syminfo.tickerid, _tf, ta.ema(math.abs(_ap - _esa), _n1), false)
    _ci = (_ap - _esa) / (0.015 * _d)
    _wt = f_security(syminfo.tickerid, _tf, ta.ema(_ci, _n2), false)
    _wts = f_security(syminfo.tickerid, _tf, ta.sma(_wt, _ma), false)
    [_wt, _wts]



// CALCULATIONS
[htf1WaveTrend, htf1WaveTrendSignal] = f_waveTrend(i_htf1, i_ap, i_n1, i_n2, i_ma)
// htf1BarIsClosed = request.security(syminfo.tickerid, i_htf1, barstate.isconfirmed) // Only use if the HTF bar is closed. Prevents repainting
// // if htfBarIsClosed
// //     htf1WaveTrend := NewHtf1WaveTrend
// //     htf1WaveTrendSignal := NewHtf1WaveTrendSignal
// // Alternative to "if"
// htf1WaveTrend := ta.valuewhen(htf1BarIsClosed, NewHtf1WaveTrend, 0)
// htf1WaveTrendSignal := ta.valuewhen(htf1BarIsClosed, NewHtf1WaveTrendSignal, 0)

[htf2WaveTrend,hHtf2WaveTrendSignal] = f_waveTrend(i_htf2, i_ap, i_n1, i_n2, i_ma)
// htf2BarIsClosed = request.security(syminfo.tickerid, i_htf2, barstate.isconfirmed) // Only use if the HTF bar is closed. Prevents repainting
// // if htfBarIsClosed
// //     htf2WaveTrend := NewHtf2WaveTrend
// //     htf2WaveTrendSignal := NewHtf2WaveTrendSignal
// // Alternative to "if"
// htf2WaveTrend := ta.valuewhen(htf2BarIsClosed, NewHtf2WaveTrend, 0)
// htf2WaveTrendSignal := ta.valuewhen(htf2BarIsClosed, NewHtf2WaveTrendSignal, 0)

// if htf1BarIsClosed
//     alert('htf1 is: ' + str.tostring(htf1WaveTrend))


[wt, wts] = f_waveTrend(timeframe.period, i_ap, i_n1, i_n2, i_ma)

wtColor := wt[1] < wt[0] ? #00ff00 : wt[1] > wt[0] ? #eb4d4d : wtColor
htf1WaveTrendColor := htf1WaveTrend[1] < htf1WaveTrend[0] ? #018a01 : htf1WaveTrend[1] > htf1WaveTrend[0] ? #b51b1b : htf1WaveTrendColor
htf2WaveTrendColor := htf2WaveTrend[1] < htf2WaveTrend[0] ? #024d02 : htf2WaveTrend[1] > htf2WaveTrend[0] ? #800000 : htf2WaveTrendColor


plot(0, color=color.new(color.gray, 0))
plot(i_obLevel1, title='overbought', color=color.new(color.red, 0))
plot(i_osLevel1, title='oversold', color=color.new(color.green, 0))
plot(i_obLevel2, title='overbought min', color=color.new(color.red, 0), style=plot.style_cross)
plot(i_osLevel2, title='oversold max', color=color.new(color.green, 0), style=plot.style_cross)


plot(wt,title="WT",linewidth=3,color=wtColor)
plot(htf1WaveTrend,title="htfWT1",linewidth=3,color=htf1WaveTrendColor)
plot(htf2WaveTrend,title="htfWT2",linewidth=3,color=htf2WaveTrendColor)

..。

EN

回答 1

Stack Overflow用户

发布于 2022-09-21 13:45:50

您可以在这里找到最好的request.security指南:https://www.tradingview.com/script/00jFIl5w-security-revisited-PineCoders/

调用此函数的最佳方法如下

代码语言:javascript
复制
// ————— Uses only one `security()` call but allows repainting control.
//          - Cannot be used when the source is a tuple-returning function (use the following method then).
//          The first three parameters are the same as those of `security()`. 
//          The last one: `_rep`, must be a boolean (true/false) value. It indicates if you want repainting (true) or not (false).
//▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼
f_security(_sym, _res, _src, _rep) => security(_sym, _res, _src[not _rep and barstate.isrealtime ? 1 : 0])[_rep or barstate.isrealtime ? 0 : 1]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73801715

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档