gobacktest

package module
v0.2.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 13, 2018 License: MIT Imports: 0 Imported by: 1

README

Software License Travis Go Doc Coverage Status Go Report Card

Heads up: This is a framework in development, with only basic functionality.


gobacktest - Fundamental stock analysis backtesting

An event-driven backtesting framework to test stock trading strategies based on fundamental analysis. Preferably this package will be the core of a backend service exposed via a REST API.

Usage

Basic example:

package main

import (
  "github.com/dirkolbrich/gobacktest/pkg/backtest"
  "github.com/dirkolbrich/gobacktest/pkg/data"
  "github.com/dirkolbrich/gobacktest/pkg/strategy"
)

func main() {
  // initiate new backtester
  test := backtest.New()

  // define and load symbols
  symbols := []string{"TEST.DE"}
  test.SetSymbols(symbols)

  // create data provider and load data into the backtest
  data := &data.BarEventFromCSVFile{FileDir: "../testdata/test/"}
  data.Load(symbols)
  test.SetData(data)

  // create strategy provider and load into the backtest
  strategy := &strategy.Basic{}
  test.SetStrategy(strategy)

  // run the backtest
  test.Run()

  // print the result of the test
  test.Stats().PrintResult()
}

More example tests are in the /examples folder.

The single parts of the backtester can be set independently:

// initiate new backtester
test := &Test{}

// set the portfolio with initial cash and a default size and risk manager
portfolio := &backtest.Portfolio{}
portfolio.SetInitialCash(10000)

sizeManager := &backtest.Size{DefaultSize: 100, DefaultValue: 1000}
portfolio.SetSizeManager(sizeManager)

riskManager := &backtest.Risk{}
portfolio.SetRiskManager(riskManager)

test.SetPortfolio(portfolio)

// create a strategy provider and load it into the backtest
strategy := &strategy.Basic{}
test.SetStrategy(strategy)

// create an execution provider and load it into the backtest
exchange := &backtest.Exchange{
    Symbol:      "TEST",
    Commission:  &FixedCommission{Commission: 0},
    ExchangeFee: &FixedExchangeFee{ExchangeFee: 0},
}
test.SetExchange(exchange)

// choose a statistic and load into the backtest
statistic := &backtest.Statistic{}
test.SetStatistic(statistic)

Dependencies

The internal calculations use the github.com/shopspring/decimal package for arbitrary-precision fixed-point decimals.

Make sure to install it into your $GOPATH with

go get github.com/shopspring/decimal

Basic components

These are the basic components of an event-driven framework.

  1. BackTester - general test case, bundles the following elements into a single test
  2. EventHandler - the different types of events, which travel through this system - data event, signal event, order event and fill event
  3. DataHandler - interface to a set of data, e.g historical quotes, fundamental data, dividends etc.
  4. StrategyHandler - generates a buy/sell signal based on the data
  5. PortfolioHandler - generates orders and manages profit & loss
    • (SizeHandler) - manages the size of an order
    • (RiskHandler) - manages the risk allocation of a portfolio
  6. ExecutionHandler - sends orders to the broker and receives the “fills” or signals that the stock has been bought or sold
  7. StatisticHandler - tracks all events during the backtests and calculates useful statistics like equity return, drawdown or sharp ratio etc., could be used to replay the complete backtest for later reference
    • (ComplianceHandler) - tracks and documents all trades to the portfolio for compliance reasons

Infrastructure example

An overviev of the infrastructure of a complete backtesting and trading environment. Taken from the production roadmap of QuantRocket.

  • General
    • API gateway
    • configuration loader
    • logging service
    • cron service
  • Data
    • database backup and download service
    • securities master services
    • historical market data service
    • fundamental data service
    • earnings data service
    • dividend data service
    • real-time market data service
    • exchange calendar service
  • Strategy
    • performance analysis service - tearsheet
  • Portfolio
    • account and portfolio service
    • risk management service
  • Execution
    • trading platform gateway service
    • order management and trade ledger service
    • backtesting and trading engine

Resources

Articles

These links to articles are a good starting point to understand the intentions and basic functions of an event-driven backtesting framework.

Other backtesting frameworks
General information on Quantitative Finance

Documentation

Overview

Package gobacktest is a simple placeholder to root all gobacktest documentation

Directories

Path Synopsis
examples
basic command
buy-and-hold command
pkg
backtest
Package backtest provides a simple stock backtesting framework.
Package backtest provides a simple stock backtesting framework.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL