【问题标题】:ThinkScript to PineScript Conversion QuestionThinkScript 到 PinScript 转换问题
【发布时间】:2021-02-26 03:22:31
【问题描述】:

我正在将 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 中是否有一种简单的方法来解决这个问题?

感谢您的帮助!

【问题讨论】:

    标签: pine-script thinkscript


    【解决方案1】:

    thinkScript 的 BarNumber() 等效于 Pine-Script 的 bar_index


    thinkScript 和 Pine-Script 都使用一个循环来表示有效的交易周期范围。 BarNumber/bar_index 值表示通过循环计算的每个测量周期。

    要将其与其他类型的编码进行比较,您可能会考虑使用类似 for bar_index in 0 to 10 的 10 天交易期,其中 bar_index 将从 0 到 9 计数(即,它的行为类似于传统的 i for 循环)。

    指数代表的时间段可以是一天、一分钟、刻度等 - 无论您为图表或计算设置什么。并且,请注意:*“日”代表“交易日”,不是“日历日”。

    一个令人困惑的问题是:条形图从哪里开始计算? BarNumber() 或 bar_index 从您时间框架的最早时段开始,并向上计入您时间框架的最近交易时段。 例如, bar_index 为 0 可能代表 10 天前,而 bar_index 为 9 可能代表今天。

    这是来自Kodify site (which provides Pine-Script tutorials)的一些示例代码:

    //@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 概念令人困惑的原因是因为我们也以另一种方式考虑值。例如,如果我们将closeclose[1] 进行比较,则即当前期间的close 值与上一期间的close 值(close[1]) 的比较。这与条形数字相反close[1] 实际上会有一个较低的柱形编号/索引,因为它来自close 之前的时期

    当我们使用 close[1] 之类的东西时,脚本实际上是从右向左计数(最近到早期) - 与 BarNumber()/bar_index 概念相反。 p>

    【讨论】:

    • 非常感谢你的解释,你真的把事情放在眼里并回答了我的问题!
    • 因为它对你有帮助,@edgregor,请点击复选标记选择这个作为答案。而且,HTH!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-10
    • 2014-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多