All Projects → sanjeevai → Trading With Momentum

sanjeevai / Trading With Momentum

Implement a momentum trading strategy in Python and test to see if it has the potential to be profitable

Projects that are alternatives of or similar to Trading With Momentum

Optopsy
A nimble options backtesting library for Python
Stars: ✭ 373 (+1232.14%)
Mutual labels:  algorithmic-trading
Quantmod
Quantitative Financial Modelling Framework
Stars: ✭ 578 (+1964.29%)
Mutual labels:  algorithmic-trading
Quantstats
Portfolio analytics for quants, written in Python
Stars: ✭ 823 (+2839.29%)
Mutual labels:  algorithmic-trading
Quantdom
Python-based framework for backtesting trading strategies & analyzing financial markets [GUI ]
Stars: ✭ 449 (+1503.57%)
Mutual labels:  algorithmic-trading
Introneuralnetworks
Introducing neural networks to predict stock prices
Stars: ✭ 486 (+1635.71%)
Mutual labels:  algorithmic-trading
Stock Analysis Engine
Backtest 1000s of minute-by-minute trading algorithms for training AI with automated pricing data from: IEX, Tradier and FinViz. Datasets and trading performance automatically published to S3 for building AI training datasets for teaching DNNs how to trade. Runs on Kubernetes and docker-compose. >150 million trading history rows generated from +5000 algorithms. Heads up: Yahoo's Finance API was disabled on 2019-01-03 https://developer.yahoo.com/yql/
Stars: ✭ 605 (+2060.71%)
Mutual labels:  algorithmic-trading
Algo Coin
Python library for algorithmic trading cryptocurrencies across multiple exchanges
Stars: ✭ 365 (+1203.57%)
Mutual labels:  algorithmic-trading
Finta
Common financial technical indicators implemented in Pandas.
Stars: ✭ 901 (+3117.86%)
Mutual labels:  algorithmic-trading
Qlib
Qlib is an AI-oriented quantitative investment platform, which aims to realize the potential, empower the research, and create the value of AI technologies in quantitative investment. With Qlib, you can easily try your ideas to create better Quant investment strategies. An increasing number of SOTA Quant research works/papers are released in Qlib.
Stars: ✭ 7,582 (+26978.57%)
Mutual labels:  algorithmic-trading
Sgx Full Orderbook Tick Data Trading Strategy
Providing the solutions for high-frequency trading (HFT) strategies using data science approaches (Machine Learning) on Full Orderbook Tick Data.
Stars: ✭ 733 (+2517.86%)
Mutual labels:  algorithmic-trading
Algotrader
Simple algorithmic stock and option trading for Node.js.
Stars: ✭ 468 (+1571.43%)
Mutual labels:  algorithmic-trading
Pandapy
PandaPy has the speed of NumPy and the usability of Pandas 10x to 50x faster (by @firmai)
Stars: ✭ 474 (+1592.86%)
Mutual labels:  algorithmic-trading
Peregrine
Detects arbitrage opportunities across 131 cryptocurrency exchanges in 50 countries
Stars: ✭ 638 (+2178.57%)
Mutual labels:  algorithmic-trading
Tdameritrade
Python interface to TD Ameritrade (https://developer.tdameritrade.com)
Stars: ✭ 427 (+1425%)
Mutual labels:  algorithmic-trading
Moonchart
Performance tear sheets and backtest analysis for Moonshot
Stars: ✭ 19 (-32.14%)
Mutual labels:  algorithmic-trading
Tickgrinder
Low-latency algorithmic trading platform written in Rust
Stars: ✭ 365 (+1203.57%)
Mutual labels:  algorithmic-trading
Superalgos
Free, open-source crypto trading bot, automated bitcoin / cryptocurrency trading software, algorithmic trading bots. Visually design your crypto trading bot, leveraging an integrated charting system, data-mining, backtesting, paper trading, and multi-server crypto bot deployments.
Stars: ✭ 587 (+1996.43%)
Mutual labels:  algorithmic-trading
Vectorbt
Ultimate Python library for time series analysis and backtesting at scale
Stars: ✭ 855 (+2953.57%)
Mutual labels:  algorithmic-trading
Machinelearningstocks
Using python and scikit-learn to make stock predictions
Stars: ✭ 897 (+3103.57%)
Mutual labels:  algorithmic-trading
Research
Notebooks based on financial machine learning.
Stars: ✭ 714 (+2450%)
Mutual labels:  algorithmic-trading

Artificial Intelligence for Trading Nanodegree

Momentum Trading

Project: Trading with Momentum

Table of Contents

  1. Project Overview
  2. Data
  3. Trading Signal
  4. Trading Strategy
  5. Performace of Portfolio
  6. Statistical Tests
    1. Annualized Rate of Return
    2. T-Test
  7. Conclusion
  8. Files
  9. Libraries


Project Overview

In this project, we will implement a momentum trading strategy, and test it to see if it has the potential to be profitable. We are supplied with a universe of stocks and time range. We are also provided with a textual description of how to generate a trading signal based on a momentum indicator. We will then compute the signal for the time range given and apply it to the dataset to produce projected returns. Finally, we will perform a statistical test on the mean of the returns to conclude if there is an alpha in the signal.

Data

For the dataset, we will use the end of day from Quotemedia. This contains data for many stocks, but we will look at stocks in the S&P 500. We will also make things a little easier to run by narrowing down our range of time period instead of using all of the data.

Udacity doesn't have a license to redistribute the data to us. They are working on alternatives to this problem.

For all the examples, we will use Apple's stock (AAPL). If we try to graph all the stocks, it would be too much information.

Trading Signal

The trading signal we'll develop in this project does not need to be based on daily prices, for instance, we can use month-end prices to perform trading once a month. To do this, we must first resample the daily adjusted closing prices into monthly buckets, and select the last observation of each month.

Computed Log returns from prices is our primary momentum indicator.

A trading signal is a sequence of trading actions, or results that can be used to take trading actions. A common form is to produce a "long" and "short" portfolio of stocks on each date (e.g. end of each month, or whatever frequency you desire to trade at). This signal can be interpreted as rebalancing your portfolio on each of those dates, entering long ("buy") and short ("sell") positions as indicated.

Trading Strategy

Here's a strategy that we will try:

For each month-end observation period, rank the stocks by previous returns, from the highest to the lowest. Select the top performing stocks for the long portfolio, and the bottom performing stocks for the short portfolio.

Performance of Portfolio

It's now time to check if our trading signal has the potential to become profitable!

We'll start by computing the net returns this portfolio would return. For simplicity, we'll assume every stock gets an equal dollar amount of investment. This makes it easier to compute a portfolio's returns as the simple arithmetic average of the individual stock returns.

The portfolio_returns function to compute the expected portfolio returns. Using df_long to indicate which stocks to long and df_short to indicate which stocks to short, it calculates the returns using lookahead_returns. To help with calculation, n_stocks is the number of stocks we're investing in a single period.

def portfolio_returns(df_long, df_short, lookahead_returns, n_stocks):
    """
    Compute expected returns for the portfolio, assuming equal
    investment in each long/short stock.

    Parameters
    ----------
    df_long : DataFrame
        Top stocks for each ticker and date marked with a 1
    df_short : DataFrame
        Bottom stocks for each ticker and date marked with a 1
    lookahead_returns : DataFrame
        Lookahead returns for each ticker and date
    n_stocks: int
        The number number of stocks chosen for each month

    Returns
    -------
    portfolio_returns : DataFrame
        Expected portfolio returns for each ticker and date
    """

    return ((df_long - df_short) * lookahead_returns)/n_stocks

Statistical Tests

Annualized Rate of Return

The annualized rate of return allows you to compare the rate of return from this strategy to other quoted rates of return, which are usually quoted on an annual basis.

T-test

Our null hypothesis (H0) is that the actual mean return from the signal is zero. We'll perform a one-sample, one-sided t-test on the observed mean return, to see if we can reject H0.

For this project, we'll use alpha level = 0.05, since it's a common value to use.

The analyze_alpha function performs a t-test on the sample of portfolio returns.

Conclusion

T-test returned a p-value of 0.21. This is a very high p-value so we cannot reject the null hypothesis. We come to the conclusion from t-test that our signal was not strong enough to give us positive returns. In other words, our signal is not profitable.

Files

  • helper and project_helper module contain utility functions and graph functions.

  • project_tests contains unit tests for all the problems

  • tests is used to construct test cases for unit tests.

Libraries

These necessary libraries are mentioned in requirements.txt:

Converts and manipulates common color representation (RGB, HSL, web, …)

Modeling language for convex optimization problems. It allows you to express your problem in a natural way that follows the math, rather than in the restrictive standard form required by solvers.

Composable style cycles

NumPy is the fundamental package for scientific computing with Python.

Flexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, statistical functions, and much more.

Python plotting library for collaborative, interactive, publication-quality graphs.

The pyparsing module is an alternative approach to creating and executing simple grammars, vs. the traditional lex/yacc approach, or the use of regular expressions.

Extensions to the standard Python datetime module.

This library allows accurate and cross platform timezone calculations using Python 2.4 or higher.

Requests is an elegant and simple HTTP library for Python, built for human beings.

Python-based ecosystem of open-source software for mathematics, science, and engineering.

A set of python modules for machine learning and data mining.

Six is a Python 2 and 3 compatibility library. It provides utility functions for smoothing over the differences between the Python versions with the goal of writing Python code that is compatible on both Python versions.

A fast, extensible progress bar for Python and CLI

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].