Back to Community
Handful of Questions for New User

Hello everyone. I just found Quantopian this past week, and I am blown away. I was previously fumbling my way through testing theories with Amibroker, which pales in comparison. However, the transition has not been easy for me because I am not a coder, and the Python language is completely foreign to me. In an attempt to get up to speed, I have read through the Quantonian Help page, watched the YouTube tutorials, searched the forums, and tried to understand various sample codes. I'm still stumped on some basic things, and I was hoping the community could help me out.

  1. When scanning 1-minute intervals, will the mavg(5) still be the 5-day moving average and not the 5-minute moving average?
  2. When scanning 1-minute intervals, will the vwap(1) just be calculated on data from that current trading day?
  3. How do I put in context the stocks that are triggered from within my initialized universe? I am using the following at the top of my code: def initialize(context): set_universe(universe.DollarVolumeUniverse(99.0, 100.0)) Then what? I know the following won't work. What do I put instead of AAPL and/or symbol?
    def handle_data(context, data): order(symbol('AAPL'), 100)
  4. I'm also unsure on how to code some of my trading logic. For example, how would I code a BUY IF the current price is > 5 DAY moving average and CROSSES over the intraday VWAP?

Thanks very much for taking the time to read this. I'd greatly appreciate any help.

4 responses

I probably should have posted this on Monday morning during the active time. Sorry for the bump, but the thread was about to fall off the front page. Can anyone help please? Thank you very much.

Welcome to Quantopian! To answer your questions,

  1. yes, the built-in mavg(5) will always be the 5 day moving average. If you want a minutely moving average you can use the history function:
five_min_prices = history(5, '1m', 'price)  
five_min_mavg = five_min_prices.mean()  
  1. Yes, that's right

  2. There are 4 different ways to add stocks to your algorithm universe: Manually using the sid(), symbol, or symbols() methods, querying and filtering using fundamental data, using set_universe to trade a basket of stocks based on their liquidity, or import your own universe in a CSV using Fetcher.

In all of these cases, the securities get added to data. So if you want to do something to all of your stocks you can do:

for stock in data:  
   order(stock, 100)  

Or if you only want to do something to a manually defined list of stocks you can do:

for stock in context.stocks:  
  order(stock, 100)  
  1. Take a look at the attached backtest for an example. Enjoy!
Clone Algorithm
Backtest from to with initial capital
Total Returns
Max Drawdown
Benchmark Returns
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: 5575d14b4bfe1710c5f947a9
There was a runtime error.

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.

Hi Alisa,

I am trying to put your minutely moving average in my algorithm but every time I put it under handle data it tells me to put it under initialize, and when I do that, it tells me to put it under initialize. Please help.

Loading notebook preview...
Notebook previews are currently unavailable.


What is the goal of the strategy, what does it do? The initialize function is used to set the variables at the start of the stratgy. This function is only run once, at the launch of the algo. The function handle_data runs every single minute. My hunch is you're trying to trade at a different interval, daily, weekly, or at a custom signal, instead of every single minute. To do so, use schedule_function to run your query: