我正在将ThinkScript指示器转换为PineScript。我目前在将ThinkScript的barNumber()函数转换为PineScript时遇到了问题。我想我知道应该使用什么作为它的等价物,但即使在阅读了文档和示例之后,我也不确定我是否理解了barNumber()。
该指标基本上用作出入境指示符。我认为代码使用barNumber()所做的工作是在绘制新信号时删除信号,但是如果新信号无效,则返回到以前的信号。
下面是代码的一部分,我与前几个def混淆了,它们后面有更多的肉,只是解释它们不相关,它们都应该作为浮标返回(def stateUp通过def linDev):
def bar = barNumber();
def stateUp;
def stateDn;
def atrCCI;
def price;
def linDev;
def CCI = if linDev == 0
then 0
else (price - avg(price, length)) / linDev / 0.05;
def MT1 = if CCI > 0
then max(MT1[1], hl2 - ATRCCI)
else (min(MT1[1], hl2 + ATRCCI)
def state = if close > ST and close > MT1 then StateUp
else if close < ST and close < MT1 then StateDn
else State[1];
def newState = HighestAll(if state <> state[1] then bar else 0);代码使用了很多条件语句,下面是该代码的其他一些用法:
CSA = if bar >= newState then MT1 else Double.NaN;
signal = if bar >= newState and state == stateUp and. . .在PineScript中有一种简单的方法来解决这个问题吗?
谢谢你的帮助!
发布于 2021-03-22 22:35:22
与thinkScript的条形. BarNumber() 等价的是thinkScript的
thinkScript和thinkScript都使用一个循环来表示有效的交易期范围。BarNumber/bar_index值表示正在通过循环计算的每个度量周期。
要将其与其他类型的编码进行比较,您可以考虑像for bar_index in 0 to 10这样的10天交易期,其中bar_index将从0到9进行计数(即,它的作用类似于for循环中的典型i )。
索引所代表的时间段可以是一天、一分钟、十点等--无论您为图表或计算设置了什么。注:*“日”代表“交易日”,而不是“日历日”。
一个令人困惑的问题是:条子从哪里开始计数?BarNumber()或bar_index开始于你时间范围内最早的期间,并将向上向上计算到时间框架的最近一个交易期。例如,0的bar_index可能代表10天前,而bar_index的9可能代表今天。
下面是一些来自Kodify站点(提供松脚本教程)的示例代码
//@version=4
study("Example of bar_index variable", overlay=true)
// Make a new label once
var label myLabel = label.new(x=bar_index, y=high + tr,
textcolor=color.white, color=color.blue)
// On the last bar, show the chart's bar count
if (barstate.islast)
// Set the label content
label.set_text(id=myLabel, text="Bars on\nthe chart:\n" +
tostring(bar_index + 1))
// Update the label's location
label.set_x(id=myLabel, x=bar_index)
label.set_y(id=myLabel, y=high + tr)注意tostring(bar_index + 1))。他们增加了一个,因为,记住,索引是基于零的,所以一个计数为0到9的索引实际上是计数10条。
我认为BarNumber()/bar_index概念混淆的原因是我们也以另一种方式考虑值。例如,如果我们将close与close[1]进行比较,即当前期间的close值与前一期间的close值(close[1])相比较。这与条形数字相反!close[1]实际上有一个较低的条号/索引,因为它来自于close之前的句号。
当我们使用类似于close[1]**,的东西时,脚本实际上是从右到左(最近到早期)计数--与BarNumber()/bar_index概念相反。
https://stackoverflow.com/questions/66379669
复制相似问题