从您的脚本中请求更多数据

May 31, 2022

两个新的Pine Script™函数 request.security_lower_tf()request.economic(),以及对现有 request.security() 函数的改进允许您的脚本访问更多数据。

request.security_lower_tf()

新的 request.security_lower_tf() 函数可以更轻松地从比图表更短的时间周期请求数据。在这个新函数之前,访问构成60分钟图表K线的所有1分钟intrabar需要复杂的用户定义函数和 request.security() 调用。新的 request.security_lower_tf() 通过返回一个阵列,该阵列包含为每个intrabar提供的表达式的一个值,使它变得轻而易举。请注意,每个图表条的intrabar数可能会有所不同。

以下指标着眼于每根图表K线扩张中1分钟intrabar的方向。如果大多数intrabar与图表K线的极性不同,我们将其主体着色为橙色。

//@version=5
indicator("Polarity Divergences", overlay = true)
// Fetch an array containing the +1/0/-1 direction of each 1min intrabar.
array<float> directionsArray = request.security_lower_tf(syminfo.tickerid, "1", math.sign(close - open))
// Color the chart bar orange when the majority of 
// intrabar directions does not match the chart bar's direction.
barcolor(math.sign(array.sum(directionsArray)) != math.sign(close - open) ? color.orange : na)
// Plot the number of intrabars in indicator values and the Data Window.
plotchar(array.size(directionsArray), "Intrabars", "", location.top)

request.economic()

request.economic() 函数获取一个国家或地区的经济数据。经济数据包括国家经济状况(GDP、通货膨胀率等)或特定行业(钢铁生产、ICU病床等)的信息:

//@version=5
indicator("Gross domestic product of the US")
plot(request.economic("US", "GDP"))

我们的帮助中心文章列出了所有可用的国家/地区和指标。每个指标的附加帮助中心文章对其进行了描述,并列出了可用的国家/地区。这一个是GDP。

request.security() 改进

request.security() 现在可以返回这些类型的阵列、字符串、布尔值、颜色和元组:

//@version=5
indicator("")

// Create an array containing OHLC values.
array<float>  ohlc = array.from(open, high, low, close)

// Request a tuple evaluated in the context of the "NASDAQ:TSLA" symbol at the chart's timeframe.
// The tuple contains:
//   - The array of OHLC values.
//   - A string representation of the OHLC values using the symbol's tick precision.
//   - The currency string of the symbol.
[ohlcArray, ohlcString, curString]  = request.security("NASDAQ:TSLA", timeframe.period, [ohlc, str.tostring(ohlc, format.mintick), syminfo.currency])

if barstate.islastconfirmedhistory
    label.new(bar_index, high, 
      "OHLC values: " + str.tostring(ohlcArray) +
      "\nFormatted OHLC values: " + ohlcString +
      "\nCurrency: " + curString)

要随时了解Pine的新功能,请密切关注用户手册的发行说明PineCoders帐户还通过其Squawk Box Telegram频道、Twitter帐户和TradingView上的“Pine Script™ Q&A”公共聊天广播更新。

我们希望大家发现这些改进很有用。请继续向我们发送您的反馈。我们为用户打造TradingView,很高兴收到您的信息。


 

还没有关注我们的中文微信公众号?快来扫二维码吧!

Look first Then leap

TradingView专门为您而打造,请确保您充分利用我们出色的功能
开启图表