Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
A couple of algo ideas from a newbie

I've messed around with backtesting for several years.
One idea i ran on backtest.org in 2009-2010 turned $10k into $13 million in 25 years and keep in mind that if the end date of the backtest was 2016 it would be closer to $20 million. Hopefully, one of you can code this since I'm just starting to learn Python:
Go for companies between market cap of $200 - $500 million dollars. Set EPS growth to %15 over past 3 years. Do not set the date higher than 3 years because you want new companies. Price per share over $4 so you are weeding out penny stocks. Stock pays no dividend. This weeds out the oil exploration and other unsavory business types. Also, you want a small company to be investing its money back into itself. Hold the stock for specifically 8 months. This is the window when micro-cap specific funds first come....when the stock breaches $250million or $500million market cap.

Around the same time in 2010, I ran a comparison of 36 strategies on my TD Ameritrade account. The winner is RSI 30 70 but even better is if I tweak the numbers to be closer to 25 75. I used a script from this site to replicate. Unfortunately I can't seem to add more than 4 stocks to the list. I would probably add a few other liquid & volatile ($1billion+ market cap) stocks such as Tesla, Shake Shack. In a perfect world I would combine the idea above with the one below. Go in and out of based on RSI of small explosive growth companies. You would really only be able to do this with a smaller account (sub $100k) because microcap stocks don't have the liquidity. This script below has Sharpe ratio of over 4. I was trying to do FANG but didn't know how to add Facebook.

This example algorithm uses the Relative Strength Index indicator as a buy/sell signal.

When the RSI is over 70, a stock can be seen as overbought and it's time to sell.

When the RSI is below 30, a stock can be seen as oversold and it's time to buy.

Because this algorithm uses the history function, it will only run in minute mode.

We will constrain the trading to once per day at market open in this example.

import talib

Setup our variables

def initialize(context):
context.stocks = symbols('NFLX', 'AMZN', 'BWLD', 'GOOG')
context.max_cash_per_stock = 100000.0 / len(context.stocks)
context.LOW_RSI = 24
context.HIGH_RSI = 76

# Create a variable to track the date change  
context.date = None

def handle_data(context, data):
todays_date = get_datetime().date()

# Do nothing unless the date has changed  
if todays_date == context.date:  
    return  
# Set the new date  
context.date = todays_date

cash = context.portfolio.cash  

# Load historical data for the stocks  
prices = history(18, '1d', 'open_price')  

# Use pandas dataframe.apply to get the last RSI value  
# for for each stock in our basket  
rsi = prices.apply(talib.RSI, timeperiod=14).iloc[-1]  

# Loop through our list of stocks  
for stock in context.stocks:  
    current_position = context.portfolio.positions[stock].amount  

    # RSI is above 70 and we own shares, time to sell  
    if rsi[stock] > context.HIGH_RSI and current_position > 0:  
        order_target(stock, 0)  
        log.info('{0}: RSI is at {1}, selling {2} shares'.format(  
            stock.symbol, rsi[stock], current_position  
        ))  

    # RSI is below 30 and we don't have any shares, time to buy  
    elif rsi[stock] < context.LOW_RSI and current_position == 0:  
        # Use floor division to get a whole number of shares  
        target_shares = cash // data[stock].price  
        order_target(stock, target_shares)  
        log.info('{0}: RSI is at {1}, buying {2} shares.'.format(  
            stock.symbol, rsi[stock], target_shares  
        ))

# record the current RSI values of each stock  
record(nflx_rsi=rsi[symbol('NFLX')],  
       amzn_rsi=rsi[symbol('AMZN')],  
       bwld_rsi=rsi[symbol('bwld')],  
       goog_rsi=rsi[symbol('goog')],)  
4 responses

Some similar opinion is reflected on this website and this guy is really good, so perhaps disregard that $4 stock price I mentioned:
http://thepatternsite.com/10Baggers.html

Hi Richard,

Interesting stuff! You'll find it much easier to share your code if you 'attach' a backtest from your algo to your post. There's an "Attach" button in the top right of the textbox when you write or edit a post/response. You can select a backtest to add in, it will include the code as well!

Cheers,
Jamie

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.

I tried to backtest the error. I am very new to algo trading. This is probably a easy fix.
Exception: inputs are all NaN
There was a runtime error on line 37.

You may try something simple like this:

import talib  
# -----------------------------------------------------------------------------------  
STOCKS, PERIOD, LB, UB, LEV = symbols('NFLX','AMZN','GOOG_L','BWLD'), 14, 24, 76, 1.0  
# -----------------------------------------------------------------------------------  
def initialize(context):  
    schedule_function(trade, date_rules.every_day(), time_rules.market_open(minutes = 65))

def trade(context, data):  
    if get_open_orders(): return  
    wt_stk = LEV/ len(STOCKS) 

    for stock in STOCKS:  
        if data.can_trade(stock):  
            prices = data.history(stock, 'price',  PERIOD + 1, '1d')  
            rsi = talib.RSI(prices, PERIOD)[-1]  
            if rsi > UB:  
                order_target_percent(stock, 0)  
            elif rsi < LB:  
                order_target_percent(stock, wt_stk)     

def before_trading_start(context, data):  
    record(leverage = context.account.leverage)