Hi Sergei,
What's happening here is that when you call data['user5']['value'] it only retrieves the current day's 'value' since 'fetch_csv' transforms your data into a TimeSeries. So when you try doing mavg(10) on data['user5']['value'], you're trying to find the 10 day moving average for 1 value.
There are a couple ways around this.
One way is to append your data['user5']['value'] to a Python list and take the moving average of that. So something like
#: Append data to a Python list
context.past_values.append(my_data)
#: Only when the list is greater than 10 find the numpy.average
if len(context.past_values) > 10:
my_avg = np.average(context.past_values[10:])
record(my_avg = my_avg)
Or what you could do is something like what's found here: https://www.quantopian.com/posts/method-to-get-historic-values-from-fetcher-data
def pre_func(df):
value = df['value']
# dates = df['date']
df['mean'] = pd.rolling_mean(value, 10)
return df
def initialize(context):
context.sid = sid(24)
fetch_csv('https://dl.dropboxusercontent.com/u/12299882/user51.csv',
date_column = 'date',
#: Different date formats apply
#: No leading zero in front of month: %-m
#: Full year syntax (2014 not 14): %Y
#: Hour and minute = %H:%M
date_format = '%-m.%d.%Y %H:%M',
symbol = 'user5',
pre_func=pre_func
)
# Will be called on every trade event for the securities you specify.
def handle_data(context, data):
# Implement your algorithm logic here.
if 'value' in data['user5']:
# Show my data
my_data = data['user5']['value']
record(my_data=my_data)
# Show my data 10 days MA value
my_avg = data['user5']['mean']
record(my_avg=my_avg)
Try both out and let me know what you think!
Seong
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.