[英]PINE-SCRIPT: Creating a Histogram Average - 24 Day Lookback
我使用 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)
sma(source,length)
函數將計算最后“n”根柱線的平均值。
使用sma()
函數代替您正在使用的代碼,它將執行您想要的相同功能
histGreater24 =hist>sma((hist*hist)/2,24)
我認為您必須使用 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)
// # ========================================================================= #
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.