Back to Community
Manipulating input for Moving Average (mavg) function from days to minutes

I am trying to do some very simple stuff with displaying moving averages of a single stock. I am attempting to backtest the 15 minute and 30 minute moving averages for IBM stock for the period of 10/26/2015 to 10/27/2015 using mavg. From what I understand, in order to do this I need to enter the time period in minutes inside the mavg function. However, mavg, to the best of my knowledge, only takes days as input. Is it possible to input the time period in minutes? I have been struggling with this.

I have attached my code and the backtest. It doesn't look right at all.'

Someone please help. Thanks.

Clone Algorithm
7
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 563113862a3e9010fc7662e9
There was a runtime error.
7 responses

Hi Nikhil - does this help? Not sure what kind of MA you were looking for, so I just did SMA

https://www.quantopian.com/help#api-talib

Some of the TA-Lib methods have an integer matype parameter. Here's the list of moving average types:
0: SMA (simple)
1: EMA (exponential)
2: WMA (weighted)
3: DEMA (double exponential)
4: TEMA (triple exponential)
5: TRIMA (triangular)
6: KAMA (Kaufman adaptive)
7: MAMA (Mesa adaptive)
8: T3 (triple exponential T3)

Clone Algorithm
11
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 56327107415f541102833d73
There was a runtime error.

How do you specify the frequency, i.e.

BM_Avg_15 = data[context.security].mavg(15)

is this 15 minutes, hours, days, etc? Where do you specify minutes?

That is 15 days, the built-in mavg function will always calculate the value based on daily data: https://www.quantopian.com/help#ide-transforms

If you want a different time window, you can use the history function and pandas manipulation:

# calculate the 15 min mavg  
minute_mavg = history(15, '1m', 'price').mean()  
Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Could someone post the code to implement the Kaufman Adaptive Moving average (KAMA), utilizing TALIB? I am a little confused on how to do this as majority of the code I have seen requires a time period to be specified for which the calculation is performed, but KAMA is adaptive, and the time period changes

Ronnie,

Kaufman's Adaptive Moving Average (KAMA) has a parameter - period for
calculating Efficiency Ratio

Efficiency Ratio = Change/Volatility  
Change = ABS(Close - Close (N periods ago))  
Volatility is the sum of the absolute value of the last N price changes (Close - Prior Close)  
Clone Algorithm
8
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 5850f3ca8a2fe9624e56b004
There was a runtime error.

Thank you Vladimir. Very helpful.

Vladimir, while the code was very helpful and produced the moving average, I'm unsure if the calculation is correct. The output does not match the output of KAMA from stockcharts.com even with efficiency ratio being calculated over a 21 day period. Any idea where the difference is?