trade_knife

package module
v0.0.33 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2021 License: Apache-2.0 Imports: 14 Imported by: 0

README

Trade Knife

Switzer knife on your trading times

More info to come soon.

Documentation

Overview

Package trade_knife will provide fundamental concept and utils to operate with financial time-series data.

Index

Constants

View Source
const (
	Interval1m  = Interval("1m")
	Interval3m  = Interval("3m")
	Interval5m  = Interval("5m")
	Interval15m = Interval("15m")
	Interval30m = Interval("30m")
	Interval1h  = Interval("1h")
	Interval2h  = Interval("2h")
	Interval4h  = Interval("4h")
	Interval6h  = Interval("6h")
	Interval8h  = Interval("8h")
	Interval12h = Interval("12h")
	Interval1d  = Interval("1d")
	Interval3d  = Interval("3d")
	Interval1w  = Interval("1w")
	Interval1M  = Interval("1M")

	SourceOpen   = Source("open")
	SourceHigh   = Source("high")
	SourceLow    = Source("low")
	SourceClose  = Source("close")
	SourceVolume = Source("volume")

	SourceOpenHigh  = Source("oh2")
	SourceOpenLow   = Source("ol2")
	SourceOpenClose = Source("oc2")
	SourceHighLow   = Source("hl2")
	SourceHighClose = Source("hc2")
	SourceLowClose  = Source("lc2")

	SourceOpenHighLow   = Source("ohl3")
	SourceOpenHighClose = Source("ohc3")
	SourceOpenLowClose  = Source("olc3")
	SourceHighLowClose  = Source("hlc3")

	SourceOpenHighLowClose = Source("ohlc4")

	PositionBuy  = PositionType("Buy")
	PositionSell = PositionType("Sell")

	TradeStatusOpen  = TradeStatus("Open")
	TradeStatusClose = TradeStatus("Close")

	ExitCauseStopLossTriggered   = ExitCause("Stop loss")
	ExitCauseTakeProfitTriggered = ExitCause("Take profit")
	ExitCauseMarket              = ExitCause("Market")

	MarketSpot     = MarketType("Spot")
	MarketFutures  = MarketType("Futures")
	MarketDelivery = MarketType("Delivery")
)

Variables

View Source
var (
	ErrInvalidCandleData = errors.New("invalid data provided for candle").(CandleError)
	ErrNotEnoughCandles  = errors.New("not enough candles to operate").(CandleError)
)

Functions

func AutoFiboRectracement added in v0.0.11

func AutoFiboRectracement(inHigh, inLow, inClose, ratios []float64, depth int, deviation float64) []map[float64]float64

func NewCandle

func NewCandle(open, high, low, close, volume float64, openTime, closeTime time.Time, previous, next *Candle) (candle *Candle, err CandleError)

NewCandle returns a pointer to a fresh candle with provided data.

Types

type AutoFibo

type AutoFibo struct {
	Tag       IndicatorTag `mapstructure:"tag"`
	Ratios    []float64    `mapstructure:"ratios"`
	Deviation float64      `mapstructure:"deviation"`
	Depth     int          `mapstructure:"depth"`
}

func (*AutoFibo) Add added in v0.0.11

func (af *AutoFibo) Add(q *Quote, c *Candle) bool

func (*AutoFibo) Is added in v0.0.12

func (af *AutoFibo) Is(tag IndicatorTag) bool

type BollingerBandsB added in v0.0.14

type BollingerBandsB struct {
	Tag IndicatorTag      `mapstructure:"tag"`
	Std StandardDeviation `mapstructure:"standardDeviation"`
}

func (*BollingerBandsB) Add added in v0.0.14

func (bbb *BollingerBandsB) Add(q *Quote, c *Candle) bool

func (*BollingerBandsB) Is added in v0.0.14

func (bbb *BollingerBandsB) Is(tag IndicatorTag) bool

type Candle

type Candle struct {
	Open       float64                  `json:"open"`
	High       float64                  `json:"high"`
	Low        float64                  `json:"low"`
	Close      float64                  `json:"close"`
	Volume     float64                  `json:"volume"`
	Score      float64                  `json:"score"`
	Indicators map[IndicatorTag]float64 `json:"indicators"`
	Opentime   time.Time                `json:"open_time"`
	Closetime  time.Time                `json:"close_time"`
	Next       *Candle                  `json:"-"`
	Previous   *Candle                  `json:"-"`
}

Candle is the main structure which contains a group of useful candlestick data.

func (*Candle) AddIndicator

func (c *Candle) AddIndicator(tag IndicatorTag, value float64)

AddIndicator adds indicator value by the given name into the candle.

func (*Candle) CrossedOver added in v0.0.15

func (c *Candle) CrossedOver(fastSource, slowSource Source) bool

CrossedOver checks if fast source crossed over the slow source.

fastSource > slowSource prevfastSource <= prevslowSource

func (*Candle) CrossedUnder added in v0.0.15

func (c *Candle) CrossedUnder(fastSource, slowSource Source) bool

CrossedUnder checks if fast source crossed under the slow source.

fastSource < slowSource prevFastSource >= prevSlowSource

func (*Candle) Csv

func (c *Candle) Csv(indicators ...IndicatorTag) (csv string)

Csv returns csv row of the candle.

func (*Candle) Get

func (c *Candle) Get(source Source) (float64, bool)

Get Retrieves the value of the target field on the candle.

func (*Candle) IsAbove added in v0.0.15

func (c *Candle) IsAbove(source Source) bool

IsAbove checks if candle is above of the source

O,H,L,C > source

func (*Candle) IsBearish added in v0.0.15

func (c *Candle) IsBearish() bool

IsBearish checks if candle is bearish

O < C,

func (*Candle) IsBelow added in v0.0.15

func (c *Candle) IsBelow(source Source) bool

IsBelow checks if candle is below of the source.

O,H,L,C < source

func (*Candle) IsBullish added in v0.0.15

func (c *Candle) IsBullish() bool

IsBullish checks if candle is bullish

O > C,

func (*Candle) IsMiddle added in v0.0.15

func (c *Candle) IsMiddle(source Source) bool

IsMiddle checks if source is passed through middle of the candle

O >= source, H > source, L < source, C <= source,

func (*Candle) TouchedDown added in v0.0.15

func (c *Candle) TouchedDown(source Source) bool

TouchedDown checks if candle closed above the source but the Low shadow touched the source.

O,H,C > source L <= source

func (*Candle) TouchedUp added in v0.0.15

func (c *Candle) TouchedUp(source Source) bool

TouchedUp checks if candle close above the source but the High shadow touched the source.

H >= source O,L,C < source

type CandleChannel

type CandleChannel chan *Candle

CandleChannel to pass Candle through it

type CandleError

type CandleError error

CandleError will occur on Candle's operations

type EnterChannel

type EnterChannel chan EnterSignal

EnterChannel to pass EnterSignal through it

type EnterSignal

type EnterSignal struct {
	Currency   goex.CurrencyPair
	Score      float64
	Quote      float64
	TakeProfit float64
	Stoploss   float64
	Cause      string
	Candle     Candle
}

EnterSignal is a signal for entering into positions

type ExitCause

type ExitCause string

ExitCause indicates why the position has been closed for

type ExitChannel

type ExitChannel chan ExitSignal

ExitChannel to pass ExitSignal through it

type ExitSignal

type ExitSignal struct {
	Trade  *Trade
	Candle *Candle
	Cause  ExitCause
}

ExitSignal is a signal for exiting from positions

type Fetcher added in v0.0.33

type Fetcher interface {
	NewQuote(currency goex.CurrencyPair, market MarketType, interval Interval, openTime *time.Time) (*Quote, error)
	Refresh(q *Quote) error
	Sync(q *Quote, update CandleChannel) (err error)
}

type Hp added in v0.0.11

type Hp struct {
	Tag    IndicatorTag `mapstructure:"tag"`
	Source Source       `mapstructure:"source"`
	Lambda float64      `mapstructure:"lambda"`
	Length int          `mapstructure:"length"`
}

func (*Hp) Add added in v0.0.11

func (hp *Hp) Add(q *Quote, c *Candle) bool

func (*Hp) Is added in v0.0.12

func (hp *Hp) Is(tag IndicatorTag) bool

type Indicator added in v0.0.11

type Indicator interface {
	Add(q *Quote, c *Candle) (ok bool)
	Is(tag IndicatorTag) bool
}

Indicator indicates how an indicator should be implemented

type IndicatorTag added in v0.0.11

type IndicatorTag string

type Interval

type Interval string

Interval is the timeframe concept and determines duration of each candle.

func (Interval) Duration

func (i Interval) Duration() time.Duration

Duration Returns actual duration of the interval.

func (Interval) GetPeriod

func (i Interval) GetPeriod(ts int64) (ot *time.Time, ct *time.Time, err error)

GetPeriod Returns the open and close time which the interval can fit in.

func (Interval) KlinePeriod added in v0.0.33

func (i Interval) KlinePeriod() goex.KlinePeriod

KlinePeriod Returns goex.KlinePeriod associated type

type LinearRegression added in v0.0.14

type LinearRegression struct {
	Tag          IndicatorTag `mapstructure:"tag"`
	Source       Source       `mapstructure:"source"`
	InTimePeriod int          `mapstructure:"period"`
}

func (*LinearRegression) Add added in v0.0.14

func (lr *LinearRegression) Add(q *Quote, c *Candle) bool

func (*LinearRegression) Is added in v0.0.14

func (lr *LinearRegression) Is(tag IndicatorTag) bool

type Logger added in v0.0.19

type Logger struct {
	Info    *log.Logger
	Debug   *log.Logger
	Success *log.Logger
	Error   *log.Logger
}

func NewLogger added in v0.0.19

func NewLogger(o Output) (*Logger, error)

func (*Logger) SwitchOutput added in v0.0.19

func (l *Logger) SwitchOutput(o Output) error

type Ma added in v0.0.11

type Ma struct {
	Tag          IndicatorTag `mapstructure:"tag"`
	Source       Source       `mapstructure:"source"`
	Type         talib.MaType `mapstructure:"type"`
	InTimePeriod int          `mapstructure:"period"`
}

func (*Ma) Add added in v0.0.11

func (ma *Ma) Add(q *Quote, c *Candle) bool

func (*Ma) Is added in v0.0.12

func (ma *Ma) Is(tag IndicatorTag) bool

type MarketType

type MarketType string

MarketType indicates the market type

type Output added in v0.0.19

type Output uint8
const (
	Stdout Output = iota + 1
	Daily
)

type PositionType

type PositionType string

PositionType indicates position direction

type Quote

type Quote struct {
	Currency goex.CurrencyPair
	Market   MarketType
	Interval Interval
	Candles  []*Candle
}

Quote is the group of candles and make time-series.

func NewQuoteFromCsv

func NewQuoteFromCsv(filename string, market MarketType, currency goex.CurrencyPair, interval Interval) (*Quote, error)

NewQuoteFromCsv reads quote from csv file.

func (*Quote) AddIndicator

func (q *Quote) AddIndicator(tag IndicatorTag, values []float64) error

AddIndicator adds indicator values by the given tag into the quote.

func (Quote) Csv

func (q Quote) Csv(indicators ...IndicatorTag) (csv string)

Csv returns csv formatted string of whole quote.

func (*Quote) Find

func (q *Quote) Find(timestamp int64) (*Candle, int)

Find searches for a candle, and its index among Quote by its symbol and provided timestamp.

func (*Quote) Get

func (q *Quote) Get(source Source) []float64

Get retrieves value of target field on all candles.

func (*Quote) IndicatorTags added in v0.0.11

func (q *Quote) IndicatorTags() []IndicatorTag

IndicatorTags returns a list of indicators used in quote.

func (*Quote) Merge

func (q *Quote) Merge(target *Quote)

Merge target quote into the current quote, rewrite duplicates and sort it.

func (*Quote) ScoreByAbove added in v0.0.16

func (q *Quote) ScoreByAbove(score float64, source Source)

ScoreByAbove scores candles by checking if candles are above source.

func (*Quote) ScoreByBands added in v0.0.15

func (q *Quote) ScoreByBands(score float64, source Source)

ScoreByBands scores candles by checking touching and turning back into the band.

func (*Quote) ScoreByBearish added in v0.0.16

func (q *Quote) ScoreByBearish(score float64)

ScoreByBearish scores candles by if candle is bearish

func (*Quote) ScoreByBelow added in v0.0.16

func (q *Quote) ScoreByBelow(score float64, source Source)

ScoreByBelow scores candles by checking if candles are below source.

func (*Quote) ScoreByBullish added in v0.0.16

func (q *Quote) ScoreByBullish(score float64)

ScoreByBullish scores candles by checking if candles are bullish.

func (*Quote) ScoreByCross added in v0.0.15

func (q *Quote) ScoreByCross(score float64, fastSource, slowSource Source)

ScoreByCross scores candles by checking sources cross over/under condition on each of them.

func (*Quote) ScoreByCrossDown added in v0.0.16

func (q *Quote) ScoreByCrossDown(score float64, fastSource, slowSource Source)

ScoreByCrossDown scores candles by checking sources cross under condition on each of them.

func (*Quote) ScoreByCrossOver added in v0.0.16

func (q *Quote) ScoreByCrossOver(score float64, fastSource, slowSource Source)

ScoreByCrossOver scores candles by checking sources cross over condition on each of them.

func (*Quote) ScoreByCrossUnder added in v0.0.18

func (q *Quote) ScoreByCrossUnder(score float64, fastSource, slowSource Source)

ScoreByCrossUnder scores candles by checking sources cross under condition on each of them.

func (*Quote) ScoreByMiddle added in v0.0.16

func (q *Quote) ScoreByMiddle(score float64, source Source)

ScoreByMiddle scores candles by checking if candles are middle of the source.

func (*Quote) ScoreBySupportResistance added in v0.0.15

func (q *Quote) ScoreBySupportResistance(score float64, source Source)

ScoreBySupportResistance scores candles by checking support/resistance line reaction.

func (*Quote) ScoreByTouchDown added in v0.0.16

func (q *Quote) ScoreByTouchDown(score float64, source Source)

ScoreByTouchDown scores candles by checking if candles are touching down source.

func (*Quote) ScoreByTouchUp added in v0.0.16

func (q *Quote) ScoreByTouchUp(score float64, source Source)

ScoreByTouchUp scores candles by checking if candles are touching up source.

func (*Quote) Sort

func (q *Quote) Sort()

Sort runs through the quote and reorder candles by the open time.

func (*Quote) Sync

func (q *Quote) Sync(open, high, low, close, volume float64, openTime, closeTime time.Time, sCandle ...*Candle) (candle *Candle, err CandleError)

Sync searches the quote for provided candle and update it if it exists, otherwise it will append to end of the quote.

If you want to update a candle directly then pass sCandle

func (Quote) WriteToCsv

func (q Quote) WriteToCsv(filename string, indicators ...IndicatorTag) error

WriteToCsv writes down whole quote into a csv file.

type Source

type Source string

Source is a target field on candle.

type SourceError

type SourceError error

SourceError will occur on Source's operations

type StandardDeviation added in v0.0.14

type StandardDeviation struct {
	Tag          IndicatorTag `mapstructure:"tag"`
	Source       Source       `mapstructure:"source"`
	InTimePeriod int          `mapstructure:"period"`
	Deviation    float64      `mapstructure:"deviation"`
}

func (*StandardDeviation) Add added in v0.0.14

func (sd *StandardDeviation) Add(q *Quote, c *Candle) bool

func (*StandardDeviation) Is added in v0.0.14

func (sd *StandardDeviation) Is(tag IndicatorTag) bool

type Stoch added in v0.0.11

type Stoch struct {
	Tag           IndicatorTag `mapstructure:"tag"`
	KTag          IndicatorTag `mapstructure:"kTag"`
	DTag          IndicatorTag `mapstructure:"dTag"`
	InFastKPeriod int          `mapstructure:"kLength"`
	InSlowKPeriod int          `mapstructure:"kSmoothing"`
	InKMaType     talib.MaType `mapstructure:"kMaType"`
	InSlowDPeriod int          `mapstructure:"dSmoothing"`
	InDMaType     talib.MaType `mapstructure:"dMaType"`
}

func (*Stoch) Add added in v0.0.11

func (s *Stoch) Add(q *Quote, c *Candle) bool

func (*Stoch) Is added in v0.0.12

func (s *Stoch) Is(tag IndicatorTag) bool

type Trade

type Trade struct {
	Currency          goex.CurrencyPair
	Id                string
	Driver            string
	Quote             float64
	Amount            float64
	Entry             float64
	Exit              float64
	ProfitPrice       float64
	ProfitPercentage  float64
	StopLossPercent   float64
	StopLossPrice     float64
	TakeProfitPercent float64
	TakeProfitPrice   float64
	Position          PositionType
	Status            TradeStatus
	OpenCandle        *Candle
	CloseCandle       *Candle
	OpenAt            *time.Time
	CloseAt           *time.Time
}

Trade represents a real world trade

func NewTrade

func NewTrade(id, driver string, currency goex.CurrencyPair, position PositionType, quote, entry float64, sl, tp float64, openCandle *Candle) *Trade

NewTrade returns a pointer to a fresh trade.

func (*Trade) Close

func (t *Trade) Close(price float64, candle *Candle)

Close closes an active trade.

func (Trade) String

func (t Trade) String() string

String Stringify the trade.

type TradeError

type TradeError error

TradeError will occur on Trade's operations

type TradeStatus

type TradeStatus string

TradeStatus indicates the trade status

type Trader

type Trader interface {
	Open(currency goex.CurrencyPair, position PositionType, quote, entry float64, sl, tp float64, openCandle *Candle) *Trade
	Close(id, exit float64, closeCandle *Candle)
	Start() TradeError
	EntryWatcher()
	ExitWatcher()
	CloseWatcher()
}

Trader determines how a trader should be implemented.

type Trades

type Trades []*Trade

func (Trades) Find

func (t Trades) Find(id string) (*Trade, int)

Find searches for a trade, and its index among all trades.

type TradesChannel

type TradesChannel chan *Trade

TradesChannel to pass Trade through it

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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