Giter Site home page Giter Site logo

grinply / kate-backtester Goto Github PK

View Code? Open in Web Editor NEW
16.0 1.0 4.0 3.2 MB

A fast and simple backtest implementation for algorithmic trading in golang

License: MIT License

Go 100.00%
go golang finance financial-analysis algorithmic-trading backtesting-frameworks backtesting-engine trading-simulation forex-trading cryptocurrency

kate-backtester's Introduction

Go Report Card Maintainability Rating Software License Go Reference

Kate Backtester

A fast and simple backtest implementation for algorithmic trading focused on cryptocurrencies written in GoLang.

Data

The price data used to run the backtests can be from any time interval, but it must contain a OHLCV structure (Open High Low Close Volume). It is possible to load data from csv files with the following format:

open high low close volume
7922.0700 7924.9900 7920.1600 7924.7500 9.90606700
7923.4300 7929.1400 7920.8000 7922.9000 15.83760800
7923.1300 7934.0900 7922.9000 7932.2600 9.98577900

Usage

To start using kate backtester you will need to implement the Strategy interface and provide a csv a dataset for execution. The Strategy interface contains 4 functions that describe how/when to trade: PreProcessIndicators, OpenNewPosition, SetStoploss and SetTakeProfit.

PreProcessIndicators

Allows the strategy to pre calculate the indicators and values for reuse in the other functions, making the execution faster and less redundant. This pre processing step is the first function called with every new price data avaliable.

OpenNewPosition

This function is responsible for opening new trade positions when there are none open already, the function is called with every new price data to check, a nil return denotes that no positions should be open yet. When opening a position a OpenPositionEvt is returned containing the Direction for the trade (LONG/SHORT) and the desired leverage, a possible return would be return &kate.OpenPositionEvt{Direction: kate.LONG, Leverage: 30}

SetStoploss

As the name already implies this function is responsible for setting the stoploss price for the already open position, the function is called when new price data is avaliable and a position is open. This function makes possible changing the stoploss dynamically as the position evolves, the updated PNL is avaliable for checking. A nil return denotes that no changes should be made, a example return would be return &kate.StoplossEvt{Price: openPosition.EntryPrice * 0.995}

SetTakeProfit

This function has the same behavior as SetStoploss but instead it manipulates the take profit price. A example return would be return &kate.TakeProfitEvt{Price: openPosition.EntryPrice * 1.005}

A basic implementation where a strategy opens a long position every time the latest close price is higher than the last close is:

package main

import (
	"fmt"

	"github.com/victorl2/kate-backtester/kate"
)

type SimpleStrategy struct{
	lastPrice    *kate.DataPoint
	currentPrice *kate.DataPoint
}

func main() {
	data, err := kate.PricesFromCSV("../../testdata/ETHUSD5.csv")
	
	if err != nil {
		panic("could`t load data." + err.Error())
	}
    kate.NewBacktester(&SimpleStrategy{}, data)
	backtester := kate.NewBacktester(&SimpleStrategy{}, data)
	fmt.Println(backtester.Run())
}

//PreProcessIndicators allows the pre processing of indicators
func (stg *SimpleStrategy) PreProcessIndicators(latestPrice kate.DataPoint) {
	stg.lastPrice = strategy.currentPrice
	stg.currentPrice = &latestPrice
}

//OpenNewPosition process the next data point and checks if a position should be opened
func (stg *SimpleStrategy) OpenNewPosition(latestPrice kate.DataPoint) *kate.OpenPositionEvt {
	if stg.lastPrice != nil && stg.currentPrice.Close() > stg.lastPrice.Close() {
		return &kate.OpenPositionEvt{Direction: kate.LONG, Leverage: 30}
	}
	return nil
}

//SetStoploss defines a stoploss for the current open position
func (stg *SimpleStrategy) SetStoploss(openPosition kate.Position) *kate.StoplossEvt {
	if openPosition.Direction == kate.LONG && openPosition.Stoploss <= 0 {
		return &kate.StoplossEvt{Price: openPosition.EntryPrice * 0.995}
	}
	return nil
}

//SetTakeProfit defines a takeprofit for the current open position
func (stg *SimpleStrategy) SetTakeProfit(openPosition kate.Position) *kate.TakeProfitEvt {
	if openPosition.Direction == kate.LONG && openPosition.TakeProfit <= 0 {
		return &kate.TakeProfitEvt{Price: openPosition.EntryPrice * 1.005}
	}
	return nil
}

kate-backtester's People

Contributors

victorl2 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

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.