0

我使用 hist 来表示基于 MACD 指标的直方图数据。

我正在尝试创建一个买入信号,当直方图是一个大于过去 24 天之前直方图数据的平均值的正数时,将进行买入。

但是,直方图数据可以是正数或负数,数字,无论是正数还是负数,都无所谓,但我找不到比这更好的解决方案,但必须有。

目前我每天都取 hist * hist / 2 的倍数,然后除以整个。这似乎笨重且不必要,但我不知道另一种方法。

任何帮助将不胜感激!

    hist         = macdDaily - signalDaily

    histGreater24 = hist[0] >   ((((hist[1]  * hist[1])  / 2) + ((hist[2]  * hist[2])  / 2) + ((hist[3]  * hist[3])  / 2) + ((hist[4]  * hist[4])  / 2) + ((hist[5]  * hist[5])  / 2) + ((hist[6]  * hist[6])  / 2) + ((hist[7]  * hist[7])  / 2) + ((hist[8]  * hist[8])  / 2) + ((hist[9]  * hist[9])  / 2) + ((hist[10] * hist[10]) / 2) + ((hist[11] * hist[11]) / 2) + ((hist[12] * hist[12]) / 2) + ((hist[13] * hist[13]) / 2) + ((hist[14] * hist[14]) / 2) + ((hist[15] * hist[15]) / 2) + ((hist[16] * hist[16]) / 2) + ((hist[17] * hist[17]) / 2) + ((hist[18] * hist[18]) / 2) + ((hist[19] * hist[19]) / 2) + ((hist[20] * hist[20]) / 2) + ((hist[21] * hist[21]) / 2) + ((hist[22] * hist[22]) / 2) + ((hist[23] * hist[23]) / 2) + ((hist[24] * hist[24]) / 2)) / 24)
4

2 回答 2

0

sma(source,length)函数将计算最后 'n' 个柱的平均值。

使用该sma()函数代替您正在使用的代码,它将执行您想要的相同功能

histGreater24 =hist>sma((hist*hist)/2,24)
于 2021-11-10T04:53:08.490 回答
0

我认为您必须使用 security() 从较高的时间范围(每日)获取数据来计算 hist 的平均值,或者您可以使用不同的来源,例如hl2 hlc3 ohlc4. (V5) 试试这个代码 ->

htf_src            = math.avg(number0 = ta.highest(source = hist, length = 1), number1 = ta.lowest(source = hist, length = 1))

f_security(_symbol, _tf, _src) => request.security(symbol = _symbol, timeframe = _tf, expression = _src[barstate.isrealtime ? 1 : 0]) 

htf_source         = f_security(syminfo.tickerid, "D", htf_src) // Here the expression hist could be replaced by hl2 hlc3 ohlc4 or any source you want to get from the "D" - Daily timeframe

condition_1        = hist[1] > 0 
condition_2        = hist[1] > htf_source

signal_condition    = condition_1 and condition_2 

plotchar(
 series     = signal_condition,
 title      = "HIST Condition",
 char       = "☀",
 location   = location.abovebar,
 color      = color.new(color = color.lime, transp = 0),
 size       = size.normal
 )

// # ========================================================================= #
// *   # The opposite condition  
// *   > 
// *   > condition_3 = hist[1] < 0
// *   > signal_bottom_condition = condition_3 and not condition_2 
// *   > plotchar(series = signal_bottom_condition, "Hist Down Conditoin", char = "*", location = location.abovebar, color = color.new(color = color.red, transp = 0), size = size.normal)
// # ========================================================================= #
于 2021-11-10T10:38:27.677 回答