Select Page

Building and implementing a Random Forest Algorithm

Now we’ll be implementing a simple Random Forest algorithm into code using Python 3.

Let’s get started!

 

Importing the necessary packages and dependencies:

import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
%matplotlib inline

from zipline.data.data_portal import DataPortal
from zipline.data import bundles
from zipline.utils.calendars import get_calendar

Getting the data

Now we have to import the data, we’ll import it from QuandlWe’ll specify the dates from start and finish as follows:

end date: 2019-01-01

start date: The first trading day available in Quandl. (If we run this line, it will output a Timestamp: “Timestamp(‘1990-01-02 00:00:00+0000′, tz=’UTC’)

bundle_data = bundles.load("quandl")

end_date = pd.Timestamp("2019-01-01", tz="utc")

bundle_data.equity_daily_bar_reader.first_trading_day

Then we initiate the DataPortal object:

data_por = DataPortal(
    asset_finder=bundle_data.asset_finder, 
    trading_calendar=get_calendar("NYSE"),
    first_trading_day=bundle_data.equity_daily_bar_reader.first_trading_day,
    equity_daily_reader=bundle_data.equity_daily_bar_reader
)

We’ll be using the NETFLIX stock data:

NFLX = data_por.asset_finder.lookup_symbol(
    "NFLX",
    as_of_date = None
)

df = data_por.get_history_window(
    assets = [NFLX],
    end_dt = end_date,
    bar_count = 5000,
    frequency = '1d',
    data_frequency = 'daily',
    field = "close"
)
Want new articles before they get published?
Subscribe to our Awesome Newsletter.

Decision Trees (part 1)

Decision Trees (part 1)In this post we’ll learn the basics but very powerful concepts that will guide us in the quest to find a powerful (and profitable) machine learning model that in further posts, we’ll be implementing it to a real life Algorithmic Trading Bot....

Random Forest

Random ForestRandom Forest, as the name suggest, it’s the bagging of many Decision Trees, in this way we get better results than using just a single tree. Random Forest not only sample subsets of a given data set but also sample subsets or features, letting each model...

Decision Trees (part 2)

Decision Trees (part 2)Ensemble Learning Ensemble learning is one of the most interesting and most used concepts in machine learning applied to finance. It consist of two main ideas: Bagging Boosting (This concept we’ll cover in further posts) Bagging This is a short...