Simple Machine Learning Example Mk II

Copyright reserved by Gus Gordon- posted Jul 18, 2015 in Quantopian

My original machine learning example was a popular post, and I figure it’s about time for an update.

Although machine learning usually seems complicated at first, it’s actually easy to work with.

Here, a model is created based off of past events and their outcomes. There are 3 input variables, or previous events, considered in this algorithm. They are the previous 3 days’ changes in price. The outcome is whether a price increased or decreased in the following bar. Many of these events and their outcomes are used to generate a model using regression in scikit-learn . The model is then used to try to predict future changes in price.

Note that this is just an example, and should be improved before real use. Clone the algorithm, and let me know if you have any questions!

# Use the previous 10 bars' movements to predict the next movement.

# Use a random forest classifier. More here: http://scikit-learn.org/stable/user_guide.html
from sklearn.ensemble import RandomForestRegressor
import numpy as np

def initialize(context):
    context.security = sid(8554) # Trade SPY
    context.model = RandomForestRegressor()

    context.lookback = 5 # Look back 5 days, was 3 days
    context.history_range = 200 # Only consider the past 200 days' history, was 400

    # Generate a new model every week
    schedule_function(create_model, date_rules.week_end(), time_rules.market_close(minutes=10))

    # Trade 1 minute after the start of every day
    schedule_function(trade, date_rules.every_day(), time_rules.market_open(minutes=1))

def create_model(context, data):
    # Get the relevant daily prices
    ##Changed to volume
    recent_volumes = data.history(context.security, 'volume', context.history_range, '1d').values
    # Get the price changes
    # Volume here too
    volume_changes = np.diff(recent_volumes).tolist()

    X = [] # Independent, or input variables
    Y = [] # Dependent, or output variable
    # For each day in our history
    for i in range(context.history_range-context.lookback-1):
        X.append(volume_changes[i:i+context.lookback]) # Store prior price changes
        Y.append(volume_changes[i+context.lookback]) # Store the day's price change

    context.model.fit(X, Y) # Generate our model

def trade(context, data):
    if context.model: # Check if our model is generated
        # Get recent prices
        recent_volumes = data.history(context.security, 'volume', context.lookback+1, '1d').values
        # Get the price changes
        volume_changes = np.diff(recent_volumes).tolist()
        # Predict using our model and the recent prices
        prediction = context.model.predict(volume_changes)
        record(prediction = prediction)
        # Go long if we predict the price will rise, short otherwise
        ## Volume again!
        if prediction > 0:
            order_target_percent(context.security, 1.0)
            order_target_percent(context.security, -1.0)