松树脚本中的移动平均线 [英] Moving average in pine-script

查看:110
本文介绍了松树脚本中的移动平均线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据历史数据计算两天时间段的简单移动平均线.我正在使用以下代码获取前一天的高低收盘价.

I want to calculate the Simple Moving Average for a time period of two days from historical data. I am using the following code to get the high low close of the previous day.

// Getting previous 2 days day high low close
prev_daily_high = security(syminfo.tickerid, 'D', high)
prev_daily_low = security(syminfo.tickerid, 'D', low)
prev_daily_close = security(syminfo.tickerid, 'D', close)

但上面的代码只获取前一天的数据来计算移动平均我需要两天的数据.

But above code fetches only previous day data to calculate Moving average I need two days data.

cp=(prev_daily_high+prev_daily_low+prev_daily_close)/3
bc=(prev_daily_high+prev_daily_low)/2
tc=2*cp-bc

下面的代码计算了两天的 SMA(简单移动平均线),但我获取的数据是一天的'如何获取两天的数据并获得正确的 SMA 值?

Below Code calculates SMA (Simple moving average) for two days but data I am fetching is for one day' How to fetch two days data and get correct SMA values?

// two days moving average for the central pivot, top central, bottom central
MAC = sma(cp, 2)
MAB = sma(bc, 2)
MAT = sma(tc, 2)

推荐答案

您需要让 security() 在其 HTF 上下文中进行计算.这里我们使用一个元组通过一次调用获取所有 3 个值:

You need to let security() make the calculations in its HTF context. Here we use a tuple to fetch all 3 values with one call:

MAC = sma(hlc3, 2)
MAB = sma(hl2, 2)
MAT = sma(2*hlc3-hl2, 2)
[dMAC, dMAB, dMAT] = security(syminfo.tickerid, 'D', [MAC, MAB, MAT])

见:
https://www.tradingview.com/pine-script-reference/v4/#var_hlc3
https://www.tradingview.com/pine-script-reference/v4/#var_hl2

这篇关于松树脚本中的移动平均线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆