talib.MAVP
Calculates the Moving Average with Variable Period.
talib.MAVP(inReal, inPeriods)
talib.MAVP(inReal, inPeriods, optInMinPeriod)
talib.MAVP(inReal, inPeriods, optInMinPeriod, optInMaxPeriod)
talib.MAVP(inReal, inPeriods, optInMinPeriod, optInMaxPeriod, optInMAType)Examples
Compute a variable-period average using 10 for up bars and 20 for down bars.
javascript
function main() {
var records = exchange.GetRecords()
if (!records) return
var closes = records.map(function(r) { return r.Close })
// period per bar: 10 for up bars, 20 for down bars (for illustration only)
var periods = records.map(function(r) { return r.Close > r.Open ? 10 : 20 })
var ret = talib.MAVP(closes, periods, 10, 20)
Log(ret)
}
python
import talib
import numpy as np
def main():
records = exchange.GetRecords()
if not records:
return
# period per bar: 10 for up bars, 20 for down bars (for illustration only)
periods = np.array([10.0 if r["Close"] > r["Open"] else 20.0 for r in records])
ret = talib.MAVP(records.Close, periods, 10, 20)
Log(ret)Returns
| Type | Description |
numeric array | Indicator values, the same length as the input; the leading values without enough data are |
Arguments
| Name | Type | Required | Description |
inReal | numeric array | Yes | Series to average. |
inPeriods | numeric array | Yes | Period used for each bar; it must have the same length as |
optInMinPeriod | number | No | Minimum period. Default |
optInMaxPeriod | number | No | Maximum period. Default |
optInMAType | number | No | Moving average type; see the MAType table on the |
See Also
Remarks
TA-Lib equivalent: MAVP(Array(inReal),Array(inPeriods),Min Period = 2,Max Period = 30,MA Type = 0) = Array(outReal).
The i-th value is the moving average of inReal up to element i with period inPeriods[i]; the first optInMaxPeriod - 1 values are NaN (regardless of the actual values in inPeriods).
Both inputs must be numeric arrays: in JavaScript, extract a field from the K-line data with map first; if a K-line array is passed, both inputs take the close price of the same K-line data and the result is meaningless; arrays of different lengths raise an error. In Python, both inputs must be numpy arrays of type float64.