pine-script - 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 中是否有一种简单的方法来解决这个问题?
感谢您的帮助!
解决方案
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 站点(提供 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 概念令人困惑的原因是因为我们也以另一种方式考虑值。例如,如果我们要比较close
,close[1]
那就是当前期间的close
值与上一期间的close
值 ( close[1]
) 的比较。这与酒吧数字相反!close[1]
实际上会有一个较低的柱数/指数,因为它来自之前 close
的时期。
当我们使用类似的东西close[1]
时,脚本实际上是从右到左计数(最近到更早的时期) - 与 BarNumber()/bar_index 概念相反。
推荐阅读
- reactjs - VSCode:如何在 scss 文件中自动完成 React 组件类名
- winapi - 检查 WM_PARENTNOTIFY 是否已弃用 API?
- c++ - 不允许使用 Visual Studio 图像类型
- laravel - 输入未更改时观察者未触发更新事件 - Laravel
- ios - 错误:您的项目不拥有动态链接域,同时使用 firebase 快速创建动态链接
- sql - 打印两个表中的所有字段,并使用 ORACLE CASE 语句根据列将某些列值设置为空
- logging - 使用 PutSQL 处理器登录 NiFi 时出错
- python - 使用线程/多进程从 API 加载时如何计算时间并设置时间限制?
- python - 如何在 django 类基础视图中注册后自动登录
- laravel - 如何在laravel中将值数组转换为字符串?