cerebro

package module
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2024 License: GPL-3.0 Imports: 17 Imported by: 0

README

Live Trader

This project is still in progress and is not a full version.

golang live trading framework

made-with-Go codecov GitHub go.mod Go version of a Go module GitHub release Go Report Card Godoc LICENSE

Introduce

This project was inspired by backtrader

python backtrader is a great project but it has disadvantage of python GIL so i want solve by golang

Installation

go get github.com/gobenpark/cerebro

Usage

🙏 Plz wait for beta version

1. first implement interface store.Store

package main

import (
	"context"
	"sort"
	"time"

	"github.com/gobenpark/proto/stock"
	"github.com/gobenpark/cerebro/container"
	"github.com/gobenpark/cerebro/order"
	"github.com/rs/zerolog/log"
	uuid "github.com/satori/go.uuid"
)

type store struct {
	name string
	uid  string
	cli  Client
}

func NewStore(name string) *store {
	cli, err := stock.NewSocketClient(context.Background(), "localhost:50051")
	if err != nil {
		panic(err)
	}

	return &store{name, uuid.NewV4().String(), cli}
}

func (s *store) LoadHistory(ctx context.Context, code string, du time.Duration) ([]container.Candle, error) {
	panic("implement me ")
}

func (s *store) LoadTick(ctx context.Context, code string) (<-chan container.Tick, error) {
	panic("implement me ")
}

func (s *store) Order(code string, ot order.OType, size int64, price float64) error {
	panic("implement me ")
}

func (s *store) Cancel(id string) error {
	panic("implement me ")
}

func (s *store) Uid() string {
	return s.uid
}

2. implement your own strategy

Next(broker,container) is incomming tick or candle data in this part you can every using indicator and buy, sell using broker


import (
	"fmt"

	"github.com/gobenpark/cerebro/broker"
	"github.com/gobenpark/cerebro/container"
	"github.com/gobenpark/cerebro/indicators"
	"github.com/gobenpark/cerebro/order"
)

type Bighands struct {
	Broker broker.Broker
	indi   indicators.Indicator
}

func (s *Bighands) Next(broker *broker.Broker, container container.Container) {
	rsi := indicators.NewRsi(14)
	rsi.Calculate(container)
	fmt.Println(rsi.Get()[0])

	fmt.Println(broker.GetCash())

	obv := indicators.NewObv()

	obv.Calculate(container)
	fmt.Println(obv.Get()[0])
	fmt.Println(container.Code())

	
	sma := indicators.NewSma(20)
	sma.Calculate(container)
	fmt.Println(sma.Get()[0])
	fmt.Println(container.Code())
}

func (s *Bighands) NotifyOrder(o *order.Order) {
	switch o.Status() {
	case order.Submitted:
		fmt.Printf("%s:%s\n", o.Code, "Submitted")
		fmt.Println(o.ExecutedAt)
	case order.Expired:
		fmt.Println("expired")
		fmt.Println(o.ExecutedAt)
	case order.Rejected:
		fmt.Println("rejected")
		fmt.Println(o.ExecutedAt)
	case order.Canceled:
		fmt.Println("canceled")
		fmt.Println(o.ExecutedAt)
	case order.Completed:
		fmt.Printf("%s:%s\n", o.Code, "Completed")
		fmt.Println(o.ExecutedAt)
		fmt.Println(o.Price)
		fmt.Println(o.Code)
		fmt.Println(o.Size)
	case order.Partial:
		fmt.Println("partial")
		fmt.Println(o.ExecutedAt)
	}
}

func (s *Bighands) NotifyTrade() {
	panic("implement me")
}

func (s *Bighands) NotifyCashValue() {
	panic("implement me")
}

func (s *Bighands) NotifyFund() {
	panic("implement me")
}

3. using cerebro !!

package main

import (
	"time"
	"github.com/gobenpark/cerebro/broker"
	"github.com/gobenpark/cerebro/cerebro"
	"github.com/gobenpark/cerebro/strategy"
)

func main() {
	bk := broker.NewBroker(100000, 0.0005)

	upbit := NewStore("binance")

	smart := &strategy.Bighands{
		Broker: bk,
	}

	cb := cerebro.NewCerebro(
		cerebro.WithBroker(bk),
		cerebro.WithStore(upbit, "KRW-MFT", "KRW-LBC"),
		cerebro.WithStrategy(smart),
		cerebro.WithResample("KRW-MFT", time.Minute*3, true),
		cerebro.WithResample("KRW-LBC", time.Minute*3, true),
		cerebro.WithLive(true),
		cerebro.WithPreload(true),
	)

	err := cb.Start()
	if err != nil {
		panic(err)
	}
}

Concepts

Live Trader have several part of trading components

  1. Cerebro : the Cerebro managements all trading components and make dependency graph

  2. Store components is user base implements for external real server ex) Binance , upbit , etc

  3. Strategy is user base own strategy

Feature

  1. Indicator
    • bollinger band
    • RSI
    • Simple Moving Average
    • On Balance Bolume

TODO

  1. new feature Observer is observing price and volume for find big hands
  2. new feature Signal
  3. support news, etc information base trading
  4. multi store one cerebro
  5. feature broker slippage
  6. Chart

Version

  1. MAJOR version when you make incompatible API changes,
  2. MINOR version when you add functionality in a backwards compatible manner, and
  3. PATCH version when you make backwards compatible bug fixes. Additional labels for pre-release and build metadata are available as extensions to the MAJOR.MINOR.PATCH format.

https://semver.org

Contribute

if you have any idea and want to talk this project send me mail (qjadn0914@naver.com) or issue

Documentation

Overview

* Copyright 2021 The Cerebro Authors * * Licensed under the GNU General Public License v3.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * <https:fsf.org/> * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License.

* Copyright 2021 The Cerebro Authors * * Licensed under the GNU General Public License v3.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * <https:fsf.org/> * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cerebro

type Cerebro struct {
	// contains filtered or unexported fields
}

Cerebro head of trading system make all dependency manage

func NewCerebro

func NewCerebro(opts ...Option) *Cerebro

NewCerebro generate new cerebro with cerebro option

func (*Cerebro) Shutdown

func (c *Cerebro) Shutdown()

func (*Cerebro) Start

func (c *Cerebro) Start(ctx context.Context) error

Start run cerebro

type Option

type Option func(*Cerebro)

func WithAnalyzer added in v0.0.5

func WithAnalyzer(analyzer analysis.Analyzer) Option

func WithLogLevel

func WithLogLevel(lvl log.Level) Option

func WithMarket

func WithMarket(s market.Market) Option

func WithObserver

func WithObserver(o observer.Observer) Option

func WithPreload

func WithPreload(b bool) Option

func WithStartTime added in v0.0.5

func WithStartTime(ti string) Option

func WithStrategy

func WithStrategy(st ...strategy.Strategy) Option

func WithStrategyTimeout

func WithStrategyTimeout(du time.Duration) Option

func WithTargetItem

func WithTargetItem(codes ...*item.Item) Option

Directories

Path Synopsis
* Copyright 2021 The Cerebro Authors * * Licensed under the GNU General Public License v3.0 (the "License"); * you may not use this file except in compliance with the License.
* Copyright 2021 The Cerebro Authors * * Licensed under the GNU General Public License v3.0 (the "License"); * you may not use this file except in compliance with the License.
* Copyright 2023 The Cerebro Authors * * Licensed under the GNU General Public License v3.0 (the "License"); * you may not use this file except in compliance with the License.
* Copyright 2023 The Cerebro Authors * * Licensed under the GNU General Public License v3.0 (the "License"); * you may not use this file except in compliance with the License.
mock
Package mock_event is a generated GoMock package.
Package mock_event is a generated GoMock package.
* Copyright 2021 The Cerebro Authors * * Licensed under the GNU General Public License v3.0 (the "License"); * you may not use this file except in compliance with the License.
* Copyright 2021 The Cerebro Authors * * Licensed under the GNU General Public License v3.0 (the "License"); * you may not use this file except in compliance with the License.
internal
pkg
* Copyright 2021 The Cerebro Authors * * Licensed under the GNU General Public License v3.0 (the "License"); * you may not use this file except in compliance with the License.
* Copyright 2021 The Cerebro Authors * * Licensed under the GNU General Public License v3.0 (the "License"); * you may not use this file except in compliance with the License.
log
v1
* Copyright 2021 The Cerebro Authors * * Licensed under the GNU General Public License v3.0 (the "License"); * you may not use this file except in compliance with the License.
* Copyright 2021 The Cerebro Authors * * Licensed under the GNU General Public License v3.0 (the "License"); * you may not use this file except in compliance with the License.
* Copyright 2021 The Cerebro Authors * * Licensed under the GNU General Public License v3.0 (the "License"); * you may not use this file except in compliance with the License.
* Copyright 2021 The Cerebro Authors * * Licensed under the GNU General Public License v3.0 (the "License"); * you may not use this file except in compliance with the License.
* Copyright 2021 The Cerebro Authors * * Licensed under the GNU General Public License v3.0 (the "License"); * you may not use this file except in compliance with the License.
* Copyright 2021 The Cerebro Authors * * Licensed under the GNU General Public License v3.0 (the "License"); * you may not use this file except in compliance with the License.
* Copyright 2021 The Cerebro Authors * * Licensed under the GNU General Public License v3.0 (the "License"); * you may not use this file except in compliance with the License.
* Copyright 2021 The Cerebro Authors * * Licensed under the GNU General Public License v3.0 (the "License"); * you may not use this file except in compliance with the License.
mock
Package mock_strategy is a generated GoMock package.
Package mock_strategy is a generated GoMock package.

Jump to

Keyboard shortcuts

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