如何使用 pine 脚本查找一天中的最后一个柱

use*_*673 5 pine-script

我需要在特定时间平仓,例如我想在15:15平仓。

pine 脚本中有内置函数来检查时间吗?

tra*_*-ap 8

方法一 \n这里是一个使用简单函数的研究,该函数在常规会话的第一个柱上触发 TRUE,允许您使用引用运算符来识别最后一个柱。如果您不需要它实时/盘中工作,这可能很有用。

\n
// https://uk.tradingview.com/u/trader-ap2/#published-scripts\n\n//@version=4\nstudy("Last Bar", overlay=true)\n\n// Last Bar of Regular Session\nf_isLastBar() =>\n    t = time("1440", session.regular) // Resolution of 1440 = 60*24 minutes in a whole day\n    not na(t[1]) and na(t) or t[1] < t\n\nif f_isLastBar()\n    label.new(bar_index[1], high, "Last Bar")\n
Run Code Online (Sandbox Code Playgroud)\n

笔记:这对于历史数据有效。如果日内/实时需要,它不起作用,因为它会在第二天常规交易时段的第一个柱上触发,或者使用当日“扩展时间(仅限日内)”交易时段的第一个柱的解决方法(如果它被打开)添加指标时在图表设置中打开。在图表上启用延长时间可能会对其他指标(例如 VWAP、EMA、SMA 等)产生计算影响。

\n

方法二 \n这里是修改为使用 input.session 参数来配置会话持续时间的代码

\n
// https://uk.tradingview.com/u/trader-ap2/#published-scripts\n\n//@version=4\nstudy("Custom Session Duration", overlay=true)\n\ni_session = input(defval="0930-1545", title="\xe2\x80\x83\xe2\x80\x83\xe2\x80\x83Session", type=input.session)\n\n// Last Bar of Custom Session\nf_isLastBar() =>\n    t = time("1440", i_session) // Resolution of 1440 = 60*24 minutes in a whole day\n    not na(t[1]) and na(t) or t[1] < t\n\nif f_isLastBar()\n    label.new(bar_index, high, "Last Bar")\n
Run Code Online (Sandbox Code Playgroud)\n

笔记:这对于日内/实时有效,并且可用于配置任何自定义会话。它还可以处理交易所代码的首日上市。它不会自动处理在特定公共假期(例如平安夜或除夕)之前或之前提前交易的情况。这些天需要调整 input.session。

\n

方法三

\n
// https://uk.tradingview.com/u/trader-ap2/#published-scripts\n\n//@version=4\nstudy("Last Bar", overlay=true)\n\n// Last Bar of Regular Session\nf_isLastBar() =>\n\n    var int sessionDuration = na\n    var int sessionCalculatedLastBarTime = na\n\n    t = time(resolution = "1440", session = session.regular) // Resolution of 1440 = 60*24 minutes in a whole day\n\n    // Calculate regular session duration for the symbol using very first day of the Bar Set\n    if na(sessionDuration) and (na(t[1]) and not na(t) or t[1] < t)\n        sessionDuration := (time[1] - t[1])\n\n    // Calculate time of the session's last bar by adding session duration to the time of the first bar of the session\n    // This does not correctly handle early close of exchange on or ahead of public holiday\n    // This will also not work correctly in realtime on the very first day of a new symbol listing on an Exchange\n    if na(t[1]) and not na(t) or t[1] < t\n        sessionCalculatedLastBarTime := t[0] + sessionDuration\n\n    // Return true if current bar's time == session's calculated last bar time otherwise return false\n    time[0] == sessionCalculatedLastBarTime\n\nif f_isLastBar() and barstate.isconfirmed\n    label.new(bar_index, high, "Last Bar")\n
Run Code Online (Sandbox Code Playgroud)\n

笔记:这是通过使用图表条形图的第一天计算交易品种的常规会话持续时间来实现的,但在第二天的开盘条形图上触发

\n

它实时有效地工作,但有以下限制:

\n
    \n
  1. 它不处理交易所提前关闭的日子,例如平安夜或除夕夜。
  2. \n
  3. 它不会实时处理交易所第一天列出的交易品种的最后一根柱,因为它会在第二个常规日的第一根柱上触发,或者作为一种解决方法,它可以通过“扩展”的开盘柱来触发。小时(仅限日内)如果在将指标添加到图表时启用了图表设置。
  4. \n
\n


Bar*_*kut 1

有多种方法可以处理会话和时间信息

为此,您可以使用内置变量“小时”“分钟” 。

//@version=4
study("tim", overlay=true)
hour_bar = input(title="Hour", type=input.integer, defval=15, minval=0, maxval=23)
minute_bar = input(title="Minute", type=input.integer, defval=15, minval=0, maxval=59)

_h = hour(time)
_m = minute(time)
is_my_time = (_h == hour_bar) and (_m == minute_bar)

bgcolor(color=is_my_time ? color.green : na)
Run Code Online (Sandbox Code Playgroud)

请注意,这些将返回 UTC 时间的值。例如,我居住在 UTC+2 时区。这意味着,即使我将输入设置为 15 和 15,17:15 处的条也会突出显示。

在此输入图像描述

如果需要,您可以将时区更改为 UTC 或从图表设置中进行交换。

在此输入图像描述