strategies

package
v0.0.0-...-f659ba7 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2020 License: GPL-3.0 Imports: 14 Imported by: 0

Documentation

Overview

Package strategies contains all strategies which can be used by the bot. They may use one or more algorithms.

Index

Constants

View Source
const (
	DB = "crypto_bot"
)

Variables

This section is empty.

Functions

func AddCustomStrategy

func AddCustomStrategy(s Strategy)

AddCustomStrategy adds a strategy to the available set.

func ApplyAllStrategies

func ApplyAllStrategies(wrappers []exchanges.ExchangeWrapper)

ApplyAllStrategies applies all matched strategies concurrently.

func MatchWithMarkets

func MatchWithMarkets(strategyName string, markets []*environment.Market) error

MatchWithMarkets matches a strategy with the markets.

Types

type IntervalStrategy

type IntervalStrategy struct {
	Model    StrategyModel
	Interval time.Duration
}

IntervalStrategy is an interval based strategy.

func (IntervalStrategy) Apply

func (is IntervalStrategy) Apply(wrappers []exchanges.ExchangeWrapper, markets []*environment.Market)

Apply executes Cyclically the On Update, basing on provided interval.

func (IntervalStrategy) Name

func (is IntervalStrategy) Name() string

Name returns the name of the strategy.

func (IntervalStrategy) String

func (is IntervalStrategy) String() string

String returns a string representation of the object.

type Strategy

type Strategy interface {
	Name() string                                             // Name returns the name of the strategy.
	Apply([]exchanges.ExchangeWrapper, []*environment.Market) // Apply applies the strategy when called, using the specified wrapper.
}

Strategy represents a generic strategy.

var DataCollector Strategy = IntervalStrategy{
	Model: StrategyModel{
		Name: "DataCollector",
		Setup: func(wrappers []exchanges.ExchangeWrapper, markets []*environment.Market) error {
			fmt.Println("DataCollector starting")
			var err error
			c, err = client.NewHTTPClient(client.HTTPConfig{
				Addr:     "http://localhost:8086",
				Username: username,
				Password: password,
			})
			if err != nil {
				fmt.Println(err)
			}
			return nil
		},
		OnUpdate: func(wrappers []exchanges.ExchangeWrapper, markets []*environment.Market) error {

			lastTs, err := influxdb.GetLastTimestamp(c, DB, precision, measurement, "open")

			if err != nil || lastTs == 0 {
				fmt.Println("first data")
				lastTs = 1483228800
			}
			fmt.Println(lastTs * 1000)
			results, err := wrappers[0].GetKlines(lastTs*1000, "BTCUSDT", "1d")

			bp, err := client.NewBatchPoints(client.BatchPointsConfig{
				Database:  DB,
				Precision: precision,
			})

			if err != nil {
				fmt.Println(err)
				return err
			}

			for _, val := range results.CandleSticks {

				fields := map[string]interface{}{
					"open":     val.Open,
					"close":    val.Close,
					"low":      val.Low,
					"high":     val.High,
					"trade_nb": val.TradeNb,
					"volume":   val.Volume,
				}

				pt, err := client.NewPoint(
					measurement,
					nil,
					fields,
					time.Unix(val.OpenTime/1000, 0),
				)
				if err != nil {
					fmt.Println(err)
					return err
				}
				bp.AddPoint(pt)
			}

			influxdb.WritePoints(c, bp)
			fmt.Println("write points")
			return nil
		},
		OnError: func(err error) {
			fmt.Println(err)
		},
		TearDown: func(wrappers []exchanges.ExchangeWrapper, markets []*environment.Market) error {
			c.Close()
			fmt.Println("DataCollector exited")
			return nil
		},
	},
	Interval: 5 * time.Second,
}
var ShowTa Strategy = IntervalStrategy{
	Model: StrategyModel{
		Name: "ShowTa",
		Setup: func(wrappers []exchanges.ExchangeWrapper, markets []*environment.Market) error {
			fmt.Println("ShowTa starting")
			return nil
		},
		OnUpdate: func(wrappers []exchanges.ExchangeWrapper, markets []*environment.Market) error {
			series := techan.NewTimeSeries()
			i := 0
			for i < 14 {
				results, err := wrappers[0].GetKlines(time.Now().UnixNano()/1000000-int64((14-i)*6060000), "BTCUSDT", "1m")
				if err != nil {
					return err
				}

				for _, c := range results.CandleSticks {
					period := techan.NewTimePeriod(time.Unix(c.OpenTime, 0), time.Minute)
					candle := techan.NewCandle(period)
					candle.OpenPrice = big.NewFromString(c.Open.String())
					candle.ClosePrice = big.NewFromString(c.Close.String())
					candle.MaxPrice = big.NewFromString(c.High.String())
					candle.MinPrice = big.NewFromString(c.Low.String())
					series.AddCandle(candle)
				}
				i++
			}
			closePrices := techan.NewClosePriceIndicator(series)
			movingAverage := techan.NewEMAIndicator(closePrices, 100)

			fmt.Printf("closePrices : %v \n", closePrices.Calculate(0).FormattedString(2))
			fmt.Printf("Moving average : %v \n", movingAverage.Calculate(0).FormattedString(2))
			return nil
		},
		OnError: func(err error) {
			fmt.Println(err)
		},
		TearDown: func(wrappers []exchanges.ExchangeWrapper, markets []*environment.Market) error {
			c.Close()
			fmt.Println("ShowTa exited")
			return nil
		},
	},
	Interval: 60 * time.Second,
}
var Watch1Min Strategy = IntervalStrategy{
	Model: StrategyModel{
		Name: "Watch1Min",
		Setup: func(wrappers []exchanges.ExchangeWrapper, markets []*environment.Market) error {
			fmt.Println("Watch1Min starting")
			return nil
		},
		OnUpdate: func(wrappers []exchanges.ExchangeWrapper, markets []*environment.Market) error {
			markets, err := wrappers[0].GetMarkets()
			if err != nil {
				return err
			}
			fmt.Println(markets)
			return nil
		},
		OnError: func(err error) {
			fmt.Println(err)
		},
		TearDown: func(wrappers []exchanges.ExchangeWrapper, markets []*environment.Market) error {
			fmt.Println("Watch1Min exited")
			return nil
		},
	},
	Interval: time.Minute,
}

Watch1Min prints out the info of the market every 5 minutes.

var Watch5Min Strategy = IntervalStrategy{
	Model: StrategyModel{
		Name: "Watch5Min",
		Setup: func(wrappers []exchanges.ExchangeWrapper, markets []*environment.Market) error {
			fmt.Println("Watch5Min starting")
			return nil
		},
		OnUpdate: func(wrappers []exchanges.ExchangeWrapper, markets []*environment.Market) error {
			_, err := wrappers[0].GetTicker(markets[0])
			if err != nil {
				return err
			}
			fmt.Println(markets)
			return nil
		},
		OnError: func(err error) {
			fmt.Println(err)
		},
		TearDown: func(wrappers []exchanges.ExchangeWrapper, markets []*environment.Market) error {
			fmt.Println("Watch5Min exited")
			return nil
		},
	},
	Interval: time.Minute * 5,
}

Watch5Min prints out the info of the market every 5 minutes.

type StrategyFunc

type StrategyFunc func([]exchanges.ExchangeWrapper, []*environment.Market) error

StrategyFunc represents a standard function binded to a strategy model execution.

Can define a Setup, TearDown and Update behaviour.

type StrategyModel

type StrategyModel struct {
	Name     string
	Setup    StrategyFunc
	TearDown StrategyFunc
	OnUpdate StrategyFunc
	OnError  func(error)
}

StrategyModel represents a strategy model used by strategies.

type Tactic

type Tactic struct {
	Markets  []*environment.Market
	Strategy Strategy
}

Tactic represents the effective appliance of a strategy.

func (*Tactic) Execute

func (t *Tactic) Execute(wrappers []exchanges.ExchangeWrapper)

Execute executes effectively a tactic.

Jump to

Keyboard shortcuts

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