请大佬指导”计算历史区间最高价、最低价“的正确写法,谢谢。

Author: hope, Created: 2022-10-17 00:13:14, Updated: 2022-10-17 15:28:07

请大佬指导“计算历史区间最高价、最低价”的正确写法,谢谢。

indicator("计算历史区间最高价、最低价", overlay=true)
//目的:想在开单时,找出开单前(输入历史长度)的最高价,或最低价,用于计算开单止损价。

varip ishistory_high_Price0 = array.new_float(0)                   //定义变量,初始化历史高价格为空的数组
varip ishistory_low_Price0 = array.new_float(0)                    //定义变量,初始化历史低价格为空的数组
var ishistory_length = input.int(15, minval=1, maxval=100, step=1) //定义变量,历史长度,输入为15,最小值1,最大值100,步长1
var ishistory_high_Price = na
var ishistory_low_Price  = na

if barstate.ishistory                                              //对历史K线执行计算(not barstate.ishistory当在实时abr时在执行)
    array.push(ishistory_high_Price0, nz(high[1], open))           //写入数组(变量ishistory_high_Price0的历史最高价元素,空值用开盘价)

    if array.size(ishistory_high_Price0) > ishistory_length        //当数组的长度大于变量长度的时候
        array.shift(ishistory_high_Price0)                         //删除数组(ishistory_high_Price0的第一个元素)
    [ishistory_high_Price0]

if barstate.ishistory                                              //对历史K线执行计算
    array.push(ishistory_low_Price0, nz(low[1], open))             //写入数组(变量ishistory_low_Price0的历史最低价元素,空值用开盘价)

    if array.size(ishistory_low_Price0) > ishistory_length         //当数组的长度大于变量长度的时候
        array.shift(ishistory_low_Price0)                          //删除数组(ishistory_low_Price0的第一个元素)
    [ishistory_low_Price0]

//需对历史高价格、历史低价格作出比较,并返回最大值(输入周期内的历史最高价、历史最低价)

//下面这个写法不正确!!!

ishistory_high_Price = array.max(ishistory_high_Price0, nz(high[1], open), ishistory_length)
ishistory_low_Price = array.min(ishistory_low_Price0, nz(low[1], open),ishistory_length)


plot(title = "数组ishistory_high_Price中的历史高价格:", ishistory_high_Price, color = color.blue)
plot(title = "数组ishistory_low_Price中的历史低价格:", ishistory_low_Price, color = color.red)

虽然学习了PINE语言的入门教程,数组的求和或数组中的元素比较大小可以套用,但换成历史K线就不会改写了! 请大佬指导一下变量定义,和历史高价格、历史低价格作出比较,求历史高价格和历史低价格,谢谢。


More

hope 没有问题了,把图表打印改到附图后,主图显示就正常了。

hope 好的,谢谢大佬,这是更改的代码: ''' indicator("计算历史区间最高价、最低价") //目的:想在开单时,找出开单前(输入历史长度)的最高价,或最低价,用于计算开单止损价。 var ishistory_length = input.int(15, title = "ishistory length", minval=1, maxval=100, step=1) //定义变量,历史长度,输入为15,最小值1,最大值100,步长1 // var float highest = na //过去k线的给定数目的最高值。 var float lowest = na //过去k线的给定数目的最低值。 var float highestbars = na //过去k线的给定数目的最高值偏移。 var float lowestbars = na //过去k线的给定数目的最低值偏移。 highest := ta.highest(high, ishistory_length) //重新赋值历史K线长度为15的最高值。 plot(highest, title = "历史K线中的高价格:", color = color.blue, overlay=true) lowest := ta.lowest(low, ishistory_length) //重新赋值历史K线长度为15的最低值。 plot(lowest, title = "历史K线中的低价格:", color = color.red, overlay=true) highest := ta.highest(high[1], ishistory_length) //重新赋值历史K线长度为15的最高值。 plot(highest, title = "历史K线中的高价格:", color = color.blue, overlay=true) lowest := ta.lowest(low[1], ishistory_length) //重新赋值历史K线长度为15的最低值。 plot(lowest, title = "历史K线中的低价格:", color = color.red, overlay=true) 下面的偏移量打印显示有点问题,不知该怎么改。 highestbars := ta.highestbars(high, ishistory_length) //重新赋值历史K线长度为15的最高值偏移。 plot(highestbars, title = "历史K线中的高价格偏移:", color = color.orange, overlay=true) lowestbars := ta.lowestbars(low, ishistory_length) //过去k线的给定数目的最低值偏移。 plot(lowestbars, title = "历史K线中的低价格偏移:", color = color.white, overlay=true) ''' 还有三个小问题请教学习: 1、只要是调用的pine内置函数,求历史最高值或最低值,在代码中,比如high,和high[1],他们的结果都是一样的,对吗? 2、如果我后面在策略中需要调用这些值,直接在策略条件中使用“highest”或者“lowest”,就可以了,对吧? 3、上面的偏移量打印显示有点问题,不知该怎么改。

小小梦 求一定范围内的数据系列的最大、最小值,有直接用的内置函数: ``` ta.highest(source, length) ta.lowest(source, length) ``` 例如要求当前10个K线BAR范围内的收盘价最大值: ``` highest10 = ta.highest(close, 10) ```

小小梦 OK

小小梦 1、high,和high[1] 肯定不一样,[]是历史操作引用,是引用前一个BAR上的快照数据。 2、你计算出某个值,用变量记录下就可以。 3、描述下具体问题,测试跑了下,没有问题呀。