一日交易比平常早期结束时,内置变量不会更改。这是一个错误吗?最后一个栏的一天结束检测可靠性
我想对算法进行编程,该算法作为其操作的一部分,检测到交易日的变化;有时是在交易当天结束之前采取行动,有时在开始之后进行。该算法旨在在普通的基于时间的图表和日内框架上使用。
在审查了Pinescript文档后,我已经看到有些变量可以帮助我,一个是 time_tradingday
(加上 ta.change()
函数要检测到当前蜡烛和前一个蜡烛之间的变量值是否发生了变化) >,, 和 。
好吧,我已经发现,当交易时间受到限制(假期)时,变量似乎无法正常工作,或者至少如我所期望的那样。我期望的是,如果一天开始或尽早结束,即使在通常的时间不在时,也会标记为限制会话的第一个/最后一个栏。但是,他们被忽略了。在实践中,这意味着该算法将两天混合在一起。
我包括一个小型演示代码和屏幕截图,以显示为例。此代码图:
- 如果检测到第一个常规条的绿色“ R”,则该代码
- 图:如果检测到第一个常规栏,则是
- 红色“ e”,如果检测到最后一个扩展条的
- 绿色“ e”,如果第一个扩展栏是绿色的“ e” 如果在新的一天的第一个酒吧中检测到交易日的变化,则检测到
- 红色(偏移-1 bar)和绿色“ D”。
// This source code is subject to the terms of the Mozilla Public License 2.0 at
https://mozilla.org/MPL/2.0/
// © x1900390
//@version=5
indicator("IBO Trading Day Change Detection Possible Bug Demo", overlay=true)
// `session.islastbar` and `session.islastbar_regular` won't change if previous trading day ends earlier than usual.
// Perhaps `session.isfirstbar` and `session.isfirstbar_regular` won't change if trading day starts before usual. (I didn't checked that.)
plotchar(session.isfirstbar, "First Extended Session Bar", "E", location.abovebar, color.green)
plotchar(session.islastbar, "First Extended Session Bar", "E", location.abovebar, color.red)
plotchar(session.isfirstbar_regular, "First Regular Session Bar", "R", location.belowbar, color.green)
plotchar(session.islastbar_regular, "First Regular Session Bar", "R", location.belowbar, color.red)
// `time_tradingday` won't change if previous trading day ends earlier than usual.
tradingDayHasChanged = ta.change(time_tradingday)
plotchar(tradingDayHasChanged, "Change of Trading Day", "D", location.bottom, color.green)
plotchar(tradingDayHasChanged, "Change of Trading Day", "D", location.bottom, color.red, -1)
我已经在S& p500(ES1!)的连续未来中观察到了这种行为,这是我正在研究的一种行为。如果我们要去2022年5月30日由于假期而较早结束,则检测到27至30之间的限制,因此在图表上正确绘制。但是,第30至31个之间的限制并非如此,因为30届会话比平常更早(即使图表确实正确地显示了垂直会话分隔符):
es1!_2022-05-30TO31交易小时更早结束,并且不检测到的更改
我想确认这是Pine脚本错误(我认为是什么),或者,如果不是,或者知道如何< em>可靠地检测到最后一个酒吧的一天结束在交易日结束之前(例如,在市场结束之前关闭位置以避免差距)。
I would like to program an algorithm that, as part of its operation, detects trading day changes; sometimes it is to do an action just before the trading day ends, and sometimes to do it just after it starts. The algorithm is intended to work on normal time-based charts, and on intraday frames.
After reviewing the PineScript documentation I have seen that there are some variables that could help me with this, one is time_tradingday
(plus ta.change()
function to detect if there has been a change in the value of the variable between the current candle and the previous one) and the others are session.isfirstbar
, session.isfirstbar_regular
, session.islastbar
and session.islastbar_regular
.
Well, I have detected that when the trading hours are restricted (on holidays) the variables do not seem to work correctly or, at least, as I would expect. What I would expect is that if the day starts or ends early, the first/last bar of the time restricted session will be marked as such, even if they are not at the usual time. However, they are ignored. In practice this means that the algorithm mixes two days together as one.
I include a small demo code and screenshots that show as an example. This code plots:
- A red "R" if the last regular bar is detected
- A green "R" if the first regular bar is detected
- A red "E" if the last extended bar is detected
- A green "E" if the first extended bar is detected
- A red (offset -1 bar) and a green "D" if change of trading day is detected on first bar of the new day.
// This source code is subject to the terms of the Mozilla Public License 2.0 at
https://mozilla.org/MPL/2.0/
// © x1900390
//@version=5
indicator("IBO Trading Day Change Detection Possible Bug Demo", overlay=true)
// `session.islastbar` and `session.islastbar_regular` won't change if previous trading day ends earlier than usual.
// Perhaps `session.isfirstbar` and `session.isfirstbar_regular` won't change if trading day starts before usual. (I didn't checked that.)
plotchar(session.isfirstbar, "First Extended Session Bar", "E", location.abovebar, color.green)
plotchar(session.islastbar, "First Extended Session Bar", "E", location.abovebar, color.red)
plotchar(session.isfirstbar_regular, "First Regular Session Bar", "R", location.belowbar, color.green)
plotchar(session.islastbar_regular, "First Regular Session Bar", "R", location.belowbar, color.red)
// `time_tradingday` won't change if previous trading day ends earlier than usual.
tradingDayHasChanged = ta.change(time_tradingday)
plotchar(tradingDayHasChanged, "Change of Trading Day", "D", location.bottom, color.green)
plotchar(tradingDayHasChanged, "Change of Trading Day", "D", location.bottom, color.red, -1)
I have observed this behavior in the continuous futures of the S&P500 (ES1!), which is the one I am studying. If we go to May 30th 2022, which ended earlier due to being a holiday, the limits between 27th and 30th are detected and therefore drawn correctly on the chart. However, the limits between 30th and 31st do not, since the session on the 30th ended earlier than usual (even though the chart does display the vertical session separator correctly):
ES1!_2022-05-27to30 regular hours and change detected
ES1!_2022-05-30to31 trading hours ends earlier and change NOT detected
I would like to confirm if this is a Pine Script bug (which is what I think) or, if not, know how to reliably detect end of day in the last bar, just before the trading day ends (for instance, to close positions before market closes to avoid gaps).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这种行为是意图的。如果您检查了
session.islastbar
和session.islastbar_regular
函数的pine参考,您会发现这是在注释中明确提到的:虽然追溯,但很容易要查明图表上最后的栏是什么条,您将无法实时这样做。如果应该在17:00关闭会话,但是实际的最后1M栏在16:45出现,即实时,您将无法知道接下来的15分钟内不会出现任何新的条。
在这种情况下,返回
对历史数据的true
将导致误导性的结果,因为它们无法在实际实时交易中复制。此外,如果您先在实时计算脚本(和session.islastbar
),则这些结果将重新粉刷在16:45的栏上不正确,然后刷新页面(突然间,它将突然成为true
)。您最好的选择是在脚本中期望
session.islastbar
,但也请检查新的session.isfirstbar
session> session.islastbar 确实如此因为这意味着最后一个会议在预期时间之前关闭。This behavior is intended. If you check the Pine Reference for the
session.islastbar
andsession.islastbar_regular
functions, you'll see that this is explicitly mentioned in the Remarks:While retroactively, it is easy to pinpoint what bar would be last on the chart, you would not be able to do so in realtime. If the session is supposed to close on 17:00, but the actual last 1m bar appeared at 16:45, on realtime, you would have no way of knowing that no new bars will appear for the next 15 minutes.
Returning
true
on historical data in such cases would lead to results that would be misleading because they could not be replicated in actual realtime trading. Additionally, these results would repaint if you were to calculate the script on realtime first (andsession.islastbar
would not be true on the bar for 16:45) and then refresh the page (and it would suddenly becometrue
).Your best bet is to expect
session.islastbar
in your script, but also check if a newsession.isfirstbar
comes beforesession.islastbar
does, because this means that the last session closed before the expected time.