简体   繁体   English

PINE-SCRIPT:创建直方图平均值 - 24 天回顾

[英]PINE-SCRIPT: Creating a Histogram Average - 24 Day Lookback

I am using the hist to represent the histogram data based on the MACD indicator.我使用 hist 来表示基于 MACD 指标的直方图数据。

I am trying to create a buy signal whereby the buy would be placed when the histogram is a positive number that is greater than the average of the past 24 days previous histogram data.我试图创建一个买入信号,当直方图是一个大于过去 24 天前一个直方图数据的平均值的正数时,买入将被放置。

However, histogram data can be positive or negative, the number, whether positive or negative, doesn't matter, but I can't find a better solution than this, but there has to be.但是,直方图数据可以是正数或负数,数字是正数还是负数都没有关系,但我找不到比这更好的解决方案,但必须有。

Currently I am taking the multiple of the hist * hist / 2 for every day then dividing the whole.目前我每天取 hist * hist / 2 的倍数然后除以整体。 This seems clunky and unnecessary but I don't know another way to do it.这似乎笨拙且不必要,但我不知道另一种方法。

Any help would be greatly appreciated!任何帮助将不胜感激!

    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)

the sma(source,length) function will calculate the average of the last 'n' bars. sma(source,length)函数将计算最后“n”根柱线的平均值。

use the sma() function in place of the code you are using , it will perform the same function as you want使用sma()函数代替您正在使用的代码,它将执行您想要的相同功能

histGreater24 =hist>sma((hist*hist)/2,24)

I think you must use security() to get data from the higher timeframe (Daily) to calculate the average of hist or you can use different sources such as hl2 hlc3 ohlc4 .我认为您必须使用 security() 从更高的时间范围(每日)获取数据来计算 hist 的平均值,或者您可以使用不同的来源,例如hl2 hlc3 ohlc4 (V5) Try this code -> (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)
// # ========================================================================= #

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM