首页 > 解决方案 > ThinkScript 到 PinScript 转换问题

问题描述

我正在将 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-scriptthinkscript

解决方案


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


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

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

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

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

以下是Kodify 站点(提供 Pine-Script 教程)的一些示例代码:

//@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 概念相反。


推荐阅读