Back to Community
Quantopian/Reuters: How Mass Shootings & Politics Boost Gun Shares

A few weeks ago we were approached by Reuters about doing some research on how mass shootings and politics affects the price of gun manufacturers stocks. Here is the backtest and code so you can see for yourself.

Check out the article by Tim McLaughlin Trading algorithm shows how mass shootings, politics boost gun shares

Clone Algorithm
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
from datetime import timedelta

# Put any initialization logic here.  The context object will be passed to
# the other methods in your algorithm.
def initialize(context):
    context.small_arms_portfolio = [sid(24519), # Smith & Wesson
                                    sid(6458),  # Sturm Ruger
    context.shooting_name = None
    context.holding_period = 90
    context.current_shootings = []
    context.max_leverage = -9999999
    # My dates
    fetch_csv('', symbol='event', date_column="knowledge_date")
    schedule_function(check_for_shooting, date_rules.every_day(), time_rules.market_open())

# Will be called on every trade event for the securities you specify. 
def handle_data(context, data):
    if context.account.leverage > context.max_leverage:
        context.max_leverage = context.account.leverage

def check_for_shooting(context, data):
    for oo in get_open_orders():
        print oo
    for date in context.current_shootings:
        if get_datetime() - date > timedelta(days=context.holding_period):
        current_shooting = data["event"]["event_name"]
        if context.shooting_name != current_shooting:
            print current_shooting + " happened on %s" % data["event"]["event_date"]
            context.shooting_name = current_shooting
    except Exception as E:
    if len(context.current_shootings) > 0:
        for stock in context.small_arms_portfolio:
            order_target_percent(stock, 1.0 / len(context.small_arms_portfolio))
        for stock in context.small_arms_portfolio:
            order_target_percent(stock, 0)
    context.max_leverage = -9999999
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.

13 responses

Sorry for this beginner question. I think this is more about quantopian than anything else. I was using your algo to test this hypothesis of investing in gun companies the day after a shooting.

I think there may be an issue with the performance of my tests:
Now for my question: my backtest says the cumulative performance of my test is 350.7% (the percentage that you get when you move the blue dot). However, for the total returns it says 698.3%. Why is that? Is that a glitch?

Zachary. If you think you found a bug send a screenshot to [email protected] and we'll try to clear things up.

I don't know if I was doing something wrong, it was fixed, or it was an issue with a particular browser, but it is not happening anymore. Thanks.

@James. Why do you use SWHC as the benchmark and not RGR? You probably know that RGR outperforms your algo.

The Reuters reporter wanted to use Smith & Wesson, and yeah RGR does outperform on a returns only basis.

If you wanted to run an algorithm similar to this but with live information, what csv source would you recommend?

This source seems to have a pretty good list going.

Can someone make this Q2 compliant. I would like to trade this.

I may do it eventually. What's stopping you?

I don't know how to code :(

Does anyone know if the CSV will be updated in the future?

I have no plans to update the Google Sheet used in the algorithm. See my response above for a source that seems to be updated regularly, though I can't vouch for the timeliness.

typo in Reuters' article's chart legend.