Git Product home page Git Product logo

                    Python Build Status Dependencies GitHub Issues Contributions welcome License

Basic Overview

Using stock historical data, train a supervised learning algorithm with any combination of financial indicators. Rapidly backtest your model for accuracy and simulate investment portfolio performance.


Visualize the Learning Process


Last Stable Release

pip install clairvoyant

Latest Development Changes

python -m pip install git+https://github.com/anfederico/clairvoyant

Backtesting Signal Accuracy

During the testing period, the model signals to buy or sell based on its prediction for price movement the following day. By putting your trading algorithm aside and testing for signal accuracy alone, you can rapidly build and test more reliable models.

from clairvoyant.engine import Backtest
import pandas as pd

features  = ["EMA", "SSO"]   # Financial indicators of choice
trainStart = 0               # Start of training period
trainEnd   = 700             # End of training period
testStart  = 701             # Start of testing period
testEnd    = 1000            # End of testing period
buyThreshold  = 0.65         # Confidence threshold for predicting buy (default = 0.65) 
sellThreshold = 0.65         # Confidence threshold for predicting sell (default = 0.65)
continuedTraining = False    # Continue training during testing period? (default = false)

# Initialize backtester
backtest = Backtest(features, trainStart, trainEnd, testStart, testEnd, buyThreshold, sellThreshold, continuedTraining)

# A little bit of pre-processing
data = pd.read_csv("SBUX.csv", date_parser=['date'])
data = data.round(3)                                    

# Start backtesting and optionally modify SVC parameters
# Available paramaters can be found at: http://scikit-learn.org/stable/modules/generated/sklearn.svm.SVC.html
backtest.start(data, kernel='rbf', C=1, gamma=10)
backtest.conditions()
backtest.statistics()  
backtest.visualize('SBUX')

Output

------------ Data Features ------------

X1: EMA
X2: SSO

---------------------------------------

----------- Model Arguments -----------

kernel: rbf
C: 1
gamma: 10

---------------------------------------

---------  Engine Conditions ----------

Training: 2013-03-01 -- 2015-12-09
Testing:  2015-12-10 -- 2017-02-17
Buy Threshold: 65.0%
Sell Threshold: 65.0%
Continued Training: False

---------------------------------------

------------- Statistics --------------

Total Buys: 170
Buy Accuracy: 68.24%
Total Sells: 54
Sell Accuracy: 59.3%

---------------------------------------

Simulate a Trading Strategy

Once you've established your model can accurately predict price movement a day in advance, simulate a portfolio and test your performance with a particular stock. User defined trading logic lets you control the flow of your capital based on the model's confidence in its prediction and the following next day outcome.

def logic(account, today, prediction, confidence):
    
    if prediction == 1:
        Risk         = 0.30
        EntryPrice   = today['close']
        EntryCapital = account.BuyingPower*Risk
        if EntryCapital >= 0:
            account.EnterPosition('Long', EntryCapital, EntryPrice)

    if prediction == -1:
        ExitPrice = today['close']
        for Position in account.Positions:  
            if Position.Type == 'Long':
                account.ClosePosition(Position, 1.0, ExitPrice)


simulation = backtester.Simulation(features, trainStart, trainEnd, testStart, testEnd, buyThreshold, sellThreshold, continuedTraining)
simulation.start(data, 1000, logic, kernel='rbf', C=1, gamma=10)
simulation.statistics()
simulation.chart('SBUX')

Output

------------- Statistics --------------

Buy and Hold : -6.18%
Net Profit   : -61.84
Strategy     : 5.82%
Net Profit   : 58.21
Longs        : 182
Sells        : 168
Shorts       : 0
Covers       : 0
--------------------
Total Trades : 350

---------------------------------------

Other Projects

Intensive Backtesting

The primary purpose of this project is to rapidly test datasets on machine learning algorithms (specifically Support Vector Machines). While the Simulation class allows for basic strategy testing, there are other projects more suited for that task. Once you've tested patterns within your data and simulated a basic strategy, I'd recommend taking your model to the next level with:

https://github.com/anfederico/gemini

Social Sentiment Scores

The examples shown use data derived from a project where we are data mining social media and performing stock sentiment analysis. To get an idea of how we do that, please take a look at:

https://github.com/anfederico/stocktalk

Notes

Multivariate Functionality

Remember, more is not always better!

variables = ["SSO"]                            # 1 feature
variables = ["SSO", "SSC"]                     # 2 features
variables = ["SSO", "SSC", "RSI"]              # 3 features
variables = ["SSO", "SSC", "RSI", ... , "Xn"]  # n features

Contributing

Please take a look at our contributing guidelines if you're interested in helping!

Pending Features

  • Export model
  • Support for multiple sklearn SVM models
  • Visualization for models with more than 2 features

Clair Voyant's Projects

7guis icon 7guis

Catas and speed coding based on https://eugenkiss.github.io/7guis/

algotrading icon algotrading

This is complete algo trading package is for downloading historical OHLC data for backtesting and performing live trading on Interactive Brokers. For learning step by step procedure to recreate this is also included. OHLC data for backtesting and performing live trading on interactive brokers.

anagram icon anagram

Grammar handling and parser generation Ada library

anomaliesinoptions icon anomaliesinoptions

In this notebook we will explore a machine learning approach to find anomalies in stock options pricing.

awesome-cl icon awesome-cl

A curated list of awesome Common Lisp frameworks, libraries and other shiny stuff.

awesome-cpp icon awesome-cpp

A curated list of awesome C++ (or C) frameworks, libraries, resources, and shiny things. Inspired by awesome-... stuff.

awesome-datascience icon awesome-datascience

:memo: An awesome Data Science repository to learn and apply for real world problems.

awesome-lua icon awesome-lua

A curated list of quality Lua packages and resources.

awesome-pascal icon awesome-pascal

A curated list of awesome Delphi/FreePascal/(any)Pascal frameworks, libraries, resources, and shiny things. Inspired by awesome-... stuff. Open source and freeware only!

backtrader icon backtrader

Python Backtesting library for trading strategies

binance-news-sentiment-bot icon binance-news-sentiment-bot

This is a fully functioning Binance trading bot that takes into account the news sentiment for the top 100 crypto feeds. If you like this project consider donating though the Brave browser to allow me to continuously improve the script.

binance-tutorials icon binance-tutorials

Real-Time Candlestick Charts and Crypto Trading Bot using Binance API and Websockets

build-your-own-x icon build-your-own-x

Master programming by recreating your favorite technologies from scratch.

cblas icon cblas

Pascal unit interface to cblas, atlas, openblas

cderpm icon cderpm

RPM spec file, patches, and scripts to package up the Common Desktop Environment

ch32v003fun icon ch32v003fun

An open source software development stack for the CH32V003, a 10 cent 48 MHz RISC-V Microcontroller

ci-template icon ci-template

Template project for continous integration of a C++ project.

clairvoyant icon clairvoyant

Software designed to identify and monitor social/historical cues for short term stock movement

craft icon craft

A simple Minecraft clone written in C using modern OpenGL (shaders).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.