Documentation ¶
Overview ¶
Package trade_knife will provide fundamental concept and utils to operate with financial time-series data.
Index ¶
- Constants
- Variables
- func HPFilter(values []float64, lambda float64) []float64
- func HPIndicator(values []float64, lambda float64, length int) []float64
- func NewCandle(symbol string, open, high, low, close, volume float64, ...) (candle *Candle, err CandleError)
- func Plot(quote Quote, width, height vg.Length, extension string, indicators []string) (io.WriterTo, error)
- type Candle
- func (c *Candle) AddIndicator(name string, value float64)
- func (c *Candle) Csv(indicators ...string) (csv string)
- func (c *Candle) Get(source Source) float64
- func (c *Candle) IndicatorsCrossedOver(fastIndicator, slowIndicator string) bool
- func (c *Candle) IndicatorsCrossedUnder(fastIndicator, slowIndicator string) bool
- func (c *Candle) IsAboveIndicator(indicator string) bool
- func (c *Candle) IsBelowIndicator(indicator string) bool
- func (c *Candle) IsIndicatorMiddle(indicator string) bool
- func (c *Candle) TouchedDownIndicator(indicator string) bool
- func (c *Candle) TouchedUpIndicator(indicator string) bool
- type CandleChannel
- type CandleError
- type EnterCause
- type EnterChannel
- type EnterSignal
- type ExitCause
- type ExitChannel
- type ExitSignal
- type Interval
- type PaperTrader
- func (pt *PaperTrader) Close(id, exit float64, closeCandle *Candle)
- func (pt *PaperTrader) CloseWatcher()
- func (pt *PaperTrader) EntryWatcher()
- func (pt *PaperTrader) ExitWatcher()
- func (pt *PaperTrader) Open(id, symbol, base string, quote, entry float64, position PositionType, ...) *Trade
- func (pt *PaperTrader) Start() TradeError
- type PositionType
- type Quote
- func NewQuoteFromBinanceDelivery(apiKey, secretKey, symbol string, interval Interval, openTimestamp ...int64) (*Quote, error)
- func NewQuoteFromBinanceFutures(apiKey, secretKey, symbol string, interval Interval, openTimestamp ...int64) (*Quote, error)
- func NewQuoteFromBinanceSpot(apiKey, secretKey, symbol string, interval Interval, openTimestamp ...int64) (*Quote, error)
- func NewQuoteFromCsv(filename string) (*Quote, error)
- func (q *Quote) AddIndicator(name string, values []float64) error
- func (q Quote) Csv(indicators ...string) (csv string)
- func (q *Quote) Find(symbol string, timestamp int64) (*Candle, int)
- func (q *Quote) Get(source Source) []float64
- func (q *Quote) IndicatorNames() []string
- func (q *Quote) Merge(target *Quote)
- func (q *Quote) RefreshBinanceDelivery(apiKey, secretKey string) error
- func (q *Quote) RefreshBinanceFutures(apiKey, secretKey string) error
- func (q *Quote) RefreshBinanceSpot(apiKey, secretKey string) error
- func (q *Quote) ScoreByCrossIndicators(score float64, fastIndicator, slowIndicator string, ...)
- func (q *Quote) Sort()
- func (q *Quote) Sync(symbol string, interval Interval, open, high, low, close, volume float64, ...) (candle *Candle, err CandleError)
- func (q *Quote) SyncBinanceDelivery(update CandleChannel) (doneC chan struct{}, err error)
- func (q *Quote) SyncBinanceFutures(update CandleChannel) (doneC chan struct{}, err error)
- func (q *Quote) SyncBinanceSpot(update CandleChannel) (doneC chan struct{}, err error)
- func (q Quote) WriteToCsv(filename string, indicators ...string) error
- type Source
- type SourceError
- type Trade
- type TradeError
- type TradeStatus
- type Trader
- type Trades
- type TradesChannel
Constants ¶
const ( // Intervals 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") // single sources SourceOpen = Source("open") SourceHigh = Source("high") SourceLow = Source("low") SourceClose = Source("close") SourceVolume = Source("volume") // double sources SourceOpenHigh = Source("oh2") SourceOpenLow = Source("ol2") SourceOpenClose = Source("oc2") SourceHighLow = Source("hl2") SourceHighClose = Source("hc2") SourceLowClose = Source("lc2") // triple sources SourceOpenHighLow = Source("ohl3") SourceOpenHighClose = Source("ohc3") SourceOpenLowClose = Source("olc3") SourceHighLowClose = Source("hlc3") // all together SourceOpenHighLowClose = Source("ohlc4") // Position types PositionBuy = PositionType("Buy") PositionSell = PositionType("Sell") // Trade statuses TradeStatusOpen = TradeStatus("Open") TradeStatusClose = TradeStatus("Close") // Exit signals ExitCauseStopLossTriggered = ExitCause("Stop loss") ExitCauseTakeProfitTriggered = ExitCause("Take profit") ExitCauseMarket = ExitCause("Market") )
Variables ¶
var ( ErrInvalidCandleData = errors.New("invalid data provided for candle").(CandleError) ErrNotEnoughCandles = errors.New("not enough candles to operate").(CandleError) ErrInvalidSource = errors.New("invalid source provided").(SourceError) )
Functions ¶
func HPIndicator ¶
Calculate HP filter for each entry by group of last [length] candles.
Types ¶
type Candle ¶
type Candle struct { Symbol string `json:"symbol"` 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[string]float64 `json:"indicators"` Interval Interval `json:"interval"` 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 ¶
Add indicator value by the given name into the candle.
func (*Candle) IndicatorsCrossedOver ¶
Check if fast indicator crossed over the slow indicator.
FastIndicator > SlowIndicator PrevFastIndicator <= PrevSlowIndicator
func (*Candle) IndicatorsCrossedUnder ¶
FastIndicator < SlowIndicator PrevFastIndicator >= PrevSlowIndicator
func (*Candle) IsIndicatorMiddle ¶
Check if indicator is passed through middle of the candle
O >= Indicator, H > Indicator, L < Indicator, C <= Indicator,
func (*Candle) TouchedDownIndicator ¶
Check if candle closed above the indicator but the Low shadow touched the indicator.
O,H,C > Indicator L <= Indicator
func (*Candle) TouchedUpIndicator ¶
Check if candle close above the indicator but the High shadow touched the indicator. H >= Indicator O,L,C < Indicator
type CandleChannel ¶
type CandleChannel chan *Candle
type CandleError ¶
type CandleError error
type EnterCause ¶
type EnterCause string
type EnterChannel ¶
type EnterChannel chan EnterSignal
type EnterSignal ¶
type ExitChannel ¶
type ExitChannel chan ExitSignal
type ExitSignal ¶
type Interval ¶
type Interval string
Interval is the timeframe concept and determines duration of each candle.
type PaperTrader ¶
type PaperTrader struct { Trades Trades BuyScoreTrigger float64 SellScoreTrigger float64 CloseOnOpposite bool Cross bool Debug bool // contains filtered or unexported fields }
PaperTrader exchange papertrade driver.
func NewPaperTrader ¶
func NewPaperTrader(candleChannel CandleChannel, entryChannel EnterChannel, buyscoreTrigger, sellscoreTrigger float64, closeOnOpposite, cross bool) *PaperTrader
Returns a pointer to fresh binance papertrade driver.
func (*PaperTrader) Close ¶
func (pt *PaperTrader) Close(id, exit float64, closeCandle *Candle)
Close an open trade immediately.
func (*PaperTrader) CloseWatcher ¶
func (pt *PaperTrader) CloseWatcher()
Watch for close signals and close the trade immediately.
func (*PaperTrader) EntryWatcher ¶
func (pt *PaperTrader) EntryWatcher()
Watch for entry signals and open proper positions.
func (*PaperTrader) ExitWatcher ¶
func (pt *PaperTrader) ExitWatcher()
Watch for exit signals and and fire proper close signals.
func (*PaperTrader) Open ¶
func (pt *PaperTrader) Open(id, symbol, base string, quote, entry float64, position PositionType, sl, tp float64, openCandle *Candle) *Trade
Create a new trade immediately.
func (*PaperTrader) Start ¶
func (pt *PaperTrader) Start() TradeError
Launch all watchers of the driver.
type PositionType ¶
type PositionType string
type Quote ¶
type Quote []*Candle
Quote is the group of candles and make time-series.
func NewQuoteFromBinanceDelivery ¶
func NewQuoteFromBinanceDelivery(apiKey, secretKey, symbol string, interval Interval, openTimestamp ...int64) (*Quote, error)
Fetches quote from binace delivery market.
func NewQuoteFromBinanceFutures ¶
func NewQuoteFromBinanceFutures(apiKey, secretKey, symbol string, interval Interval, openTimestamp ...int64) (*Quote, error)
Fetches quote from binace futures market.
func NewQuoteFromBinanceSpot ¶
func NewQuoteFromBinanceSpot(apiKey, secretKey, symbol string, interval Interval, openTimestamp ...int64) (*Quote, error)
Fetches quote from binance spot market.
func NewQuoteFromCsv ¶
Read quote from csv file.
func (*Quote) AddIndicator ¶
Add indicator values by the given name into the quote.
func (*Quote) Find ¶
Search for a candle and it's index amoung Quote by it's symbol and provided timestamp.
func (*Quote) IndicatorNames ¶
Returns a list of indicators used in quote.
func (*Quote) RefreshBinanceDelivery ¶
Fetch all delivery candles after last candle including itself.
func (*Quote) RefreshBinanceFutures ¶
Fetch all futures candles after last candle including itself.
func (*Quote) RefreshBinanceSpot ¶
Fetch all spot candles after last candle including itself.
func (*Quote) ScoreByCrossIndicators ¶
func (q *Quote) ScoreByCrossIndicators(score float64, fastIndicator, slowIndicator string, fastSource, slowSource Source)
Score candles by checking indicators cross over condition on each of them.
func (*Quote) Sort ¶
func (q *Quote) Sort()
Run through the quote and reorder candles by the open time.
func (*Quote) Sync ¶
func (q *Quote) Sync(symbol string, interval Interval, open, high, low, close, volume float64, openTime, closeTime time.Time, sCandle ...*Candle) (candle *Candle, err CandleError)
Search 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) SyncBinanceDelivery ¶
func (q *Quote) SyncBinanceDelivery(update CandleChannel) (doneC chan struct{}, err error)
Will sync quote with latest binance spot kline info.
func (*Quote) SyncBinanceFutures ¶
func (q *Quote) SyncBinanceFutures(update CandleChannel) (doneC chan struct{}, err error)
Will sync quote with latest binance futures kline info.
func (*Quote) SyncBinanceSpot ¶
func (q *Quote) SyncBinanceSpot(update CandleChannel) (doneC chan struct{}, err error)
Will sync quote with latest binance spot kline info.
type SourceError ¶
type SourceError error
type Trade ¶
type Trade struct { Id string Driver string Symbol string Base 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 }
type TradeError ¶
type TradeError error
type TradeStatus ¶
type TradeStatus string
type Trader ¶
type Trader interface { Open(id, symbol, base string, quote, entry float64, position PositionType, sl, tp float64, openCandle *Candle) *Trade Close(id, exit float64, closeCandle *Candle) EntryWatcher() ExitWatcher() CloseWatcher() }
Interface to determine how a trader should implements.
type TradesChannel ¶
type TradesChannel chan *Trade
Source Files ¶
- candle.go
- condition_above.go
- condition_below.go
- condition_crossover.go
- condition_crossunder.go
- condition_middle.go
- condition_touched_down.go
- condition_touched_up.go
- csv.go
- definitions.go
- fetcher_binance.go
- filter_hodrick_prescott.go
- indicator_hodrick_prescott.go
- interval.go
- paper_trader_binance.go
- plot.go
- quote.go
- source.go
- strategy_cross_indicator.go
- trade.go