banta

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2024 License: MIT Imports: 5 Imported by: 0

README

Ban Technical Analysis

中文文档

This is an event-based technical analysis calculation library. Each candlestick is calculated and cached, and the calculation results of the indicators are reused globally.

Core Concept

Traditional technical analysis libraries such as ta-lib and pandas-ta are very popular and have undergone a lot of performance optimization. They are very fast when calculating hundreds or thousands of candlesticks at a time.

However, when using these libraries in your robot's real-time trading, you need to pass the data of the previous hundreds of candlesticks at the same time when you receive a new candlestick. If you are running more symbols or if you are running on 1m or even 1s, the calculation delay will be too large to be tolerated.

Many people have used TradingView. The PineScript it uses is an event-based technical analysis engine. It does not re-run the previous candlesticks when receiving a new candlestick, but uses the cached results from the previous candlesticks.

This is also the design concept of BanTA, based on events, running once for each candlestick, and using the cached results from the previous candlesticks.

How BanTA Works

In BanTA, Series sequence is the key type. Basically, all return values are sequences. The Series has the Data []float64 field, which records the values of the current indicator for each candlestick.

The most commonly used e.Close is the closing price sequence. e.Close.Get(0) is the current closing price of type float64;

Calculating the average is also simple: ma5 := ta.SMA(e.Close, 5), the returned ma5 is also a sequence;

Some indicators such as KDJ generally return two fields k and d kdjRes := ta.KDJ(e.High, e.Low, e.Close, 9, 3, 3).Cols, you can get an array of two sequences from Cols.

How to Use

package main
import (
	"fmt"
	ta "github.com/banbox/banta"
)

var envMap = make(map[string]*ta.BarEnv)

func OnBar(symbol string, timeframe string, bar *ta.Kline) {
	envKey := fmt.Sprintf("%s_%s", symbol, timeframe)
	e, ok := envMap[envKey]
	if !ok {
		e = &ta.BarEnv{
			TimeFrame: timeframe,
			BarNum:    1,
		}
		envMap[envKey] = e
	}
	e.OnBar(bar)
	ma5 := ta.SMA(e.Close, 5)
	ma30 := ta.SMA(e.Close, 30)
	atr := ta.ATR(e.High, e.Low, e.Close, 14).Get(0)
	xnum := ta.Cross(ma5, ma30)
	if xnum == 1 {
		// ma5 cross up ma30
		curPrice := e.Close.Get(0) // or bar.Close
		stopLoss := curPrice - atr
		fmt.Printf("open long at %f, stoploss: %f", curPrice, stopLoss)
	} else if xnum == -1 {
		// ma5 cross down ma30
		curPrice := e.Close.Get(0)
		fmt.Printf("close long at %f", curPrice)
	}
	kdjRes := ta.KDJ(e.High, e.Low, e.Close, 9, 3, 3).Cols
	k, d := kdjRes[0], kdjRes[1]
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidSeriesVal = errors.New("invalid val for Series")
	ErrGetDataOfMerged  = errors.New("try get Data of merged series var")
	ErrRepeatAppend     = errors.New("repeat append on Series")
)

Functions

func Cross

func Cross(obj1 interface{}, obj2 interface{}) int

Cross 计算最近一次交叉的距离。如果两个都变化,则两个都必须是序列。或者一个是常数一个是序列 返回值:正数上穿,负数下穿,0表示未知或重合;abs(ret) - 1表示交叉点与当前bar的距离

Types

type AdxState

type AdxState struct {
	Num    int     // 计算次数
	DmHSum float64 // 缓存初始DmH的和
	DmLSum float64 // 缓存初始DmL的和
	DmHMA  float64 // 缓存DMH的均值
	DmLMA  float64 // 缓存DML的均值
	TRMA   float64 // 缓存TR的均值
}

type BarEnv

type BarEnv struct {
	TimeStart  int64
	TimeStop   int64
	Exchange   string
	MarketType string
	Symbol     string
	TimeFrame  string
	TFMSecs    int64 //周期的毫秒间隔
	BarNum     int
	MaxCache   int
	Open       *Series
	High       *Series
	Low        *Series
	Close      *Series
	Volume     *Series
	Items      map[string]*Series
	XLogs      map[string]*CrossLog
	Data       map[string]interface{}
}

func (*BarEnv) GetSeries

func (e *BarEnv) GetSeries(key string) *Series

func (*BarEnv) OnBar

func (e *BarEnv) OnBar(barMs int64, open, high, low, close, volume float64)

func (*BarEnv) Reset

func (e *BarEnv) Reset()

func (*BarEnv) TrimOverflow

func (e *BarEnv) TrimOverflow()

type CrossLog

type CrossLog struct {
	Key     string
	PrevVal float64
	Hist    []*XState // 正数表示上穿,负数下穿,绝对值表示BarNum
}

type Kline

type Kline struct {
	Time   int64
	Open   float64
	High   float64
	Low    float64
	Close  float64
	Volume float64
}

type Series

type Series struct {
	Env  *BarEnv
	Data []float64
	Cols []*Series
	Key  string
	Time int64
	More interface{}
}

func ADX

func ADX(high *Series, low *Series, close *Series, period int) *Series
ADX 计算平均趋向指标

参考TradingView的社区ADX指标。与tdlib略有不同

func ATR

func ATR(high *Series, low *Series, close *Series, period int) *Series

func BBANDS

func BBANDS(obj *Series, period, stdUp, stdDn int) *Series

BBANDS 布林带指标。返回:upper, mean, lower

func EMA

func EMA(obj *Series, period int) *Series

EMA 指数移动均线 最近一个权重:2/(n+1)

func EMABy

func EMABy(obj *Series, period int, initType int) *Series

EMA 指数移动均线 最近一个权重:2/(n+1) initType:0使用SMA初始化,1第一个有效值初始化

func HeikinAshi

func HeikinAshi(e *BarEnv) *Series

HeikinAshi 计算Heikin-Ashi

func Highest

func Highest(obj *Series, period int) *Series

func KDJ

func KDJ(high *Series, low *Series, close *Series, period int, sm1 int, sm2 int) *Series

KDJ指标。也称为:Stoch随机指标。返回k, d

func KDJBy

func KDJBy(high *Series, low *Series, close *Series, period int, sm1 int, sm2 int, maType string) *Series

func Lowest

func Lowest(obj *Series, period int) *Series

func MACD

func MACD(obj *Series, fast int, slow int, smooth int) *Series

MACD 计算MACD指标。 国外主流使用init_type=0,MyTT和国内主要使用init_type=1

func MACDBy

func MACDBy(obj *Series, fast int, slow int, smooth int, initType int) *Series

func RMA

func RMA(obj *Series, period int) *Series

RMA 相对移动均线

和EMA区别是:分子分母都减一
最近一个权重:1/n

func RMABy

func RMABy(obj *Series, period int, initType int, initVal float64) *Series

RMABy 相对移动均线

和EMA区别是:分子分母都减一
最近一个权重:1/n

initType:0使用SMA初始化,1第一个有效值初始化 initVal 默认Nan

func ROC

func ROC(obj *Series, period int) *Series

ROC rate of change

func RSI

func RSI(obj *Series, period int) *Series

RSI 计算相对强度指数

func SMA

func SMA(obj *Series, period int) *Series

func StdDev

func StdDev(obj *Series, period int) *Series
StdDev 计算标准差和均值

返回:stddev,mean

func StdDevBy

func StdDevBy(obj *Series, period int, ddof int) *Series
StdDevBy 计算标准差和均值

返回:stddev,mean

func Sum

func Sum(obj *Series, period int) *Series

func TD

func TD(obj *Series) *Series
TD 计算Tom DeMark Sequence(狄马克序列)

9和13表示超买;-9和-13表示超卖

func TR

func TR(high *Series, low *Series, close *Series) *Series

func (*Series) Abs

func (s *Series) Abs() *Series

func (*Series) Add

func (s *Series) Add(obj interface{}) *Series

func (*Series) Append

func (s *Series) Append(obj interface{}) *Series

func (*Series) Cached

func (s *Series) Cached() bool

func (*Series) Cut

func (s *Series) Cut(keepNum int)

func (*Series) Get

func (s *Series) Get(i int) float64

func (*Series) Len

func (s *Series) Len() int

func (*Series) Mul

func (s *Series) Mul(obj interface{}) *Series

func (*Series) Range

func (s *Series) Range(start, stop int) []float64

func (*Series) Sub

func (s *Series) Sub(obj interface{}) *Series

type XState

type XState struct {
	Sign   int
	BarNum int
}

Jump to

Keyboard shortcuts

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