2022-07-01 10:38:25 +00:00
|
|
|
package drift
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-07-14 03:46:19 +00:00
|
|
|
"encoding/json"
|
2022-07-01 10:38:25 +00:00
|
|
|
"fmt"
|
2022-07-14 03:46:19 +00:00
|
|
|
"math"
|
2022-07-01 10:38:25 +00:00
|
|
|
"os"
|
2022-07-14 03:46:19 +00:00
|
|
|
"strings"
|
2022-07-01 10:38:25 +00:00
|
|
|
"sync"
|
|
|
|
|
2022-07-14 03:46:19 +00:00
|
|
|
"github.com/fatih/color"
|
2022-07-01 10:38:25 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2022-07-12 10:14:57 +00:00
|
|
|
"github.com/wcharczuk/go-chart/v2"
|
2022-07-01 10:38:25 +00:00
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/bbgo"
|
|
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
|
|
|
"github.com/c9s/bbgo/pkg/indicator"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
"github.com/c9s/bbgo/pkg/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
const ID = "drift"
|
|
|
|
|
|
|
|
var log = logrus.WithField("strategy", ID)
|
2022-07-14 03:46:19 +00:00
|
|
|
var Four fixedpoint.Value = fixedpoint.NewFromInt(4)
|
|
|
|
var Three fixedpoint.Value = fixedpoint.NewFromInt(3)
|
|
|
|
var Two fixedpoint.Value = fixedpoint.NewFromInt(2)
|
|
|
|
var Delta fixedpoint.Value = fixedpoint.NewFromFloat(0.01)
|
2022-07-01 10:38:25 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
bbgo.RegisterStrategy(ID, &Strategy{})
|
|
|
|
}
|
|
|
|
|
2022-07-14 03:46:19 +00:00
|
|
|
type SourceFunc func(*types.KLine) fixedpoint.Value
|
|
|
|
|
2022-07-01 10:38:25 +00:00
|
|
|
type Strategy struct {
|
|
|
|
Symbol string `json:"symbol"`
|
|
|
|
|
|
|
|
bbgo.StrategyController
|
|
|
|
types.Market
|
|
|
|
types.IntervalWindow
|
|
|
|
|
|
|
|
*bbgo.Environment
|
2022-07-14 03:51:55 +00:00
|
|
|
*types.Position `persistence:"position"`
|
|
|
|
*types.ProfitStats `persistence:"profit_stats"`
|
|
|
|
*types.TradeStats `persistence:"trade_stats"`
|
2022-07-01 10:38:25 +00:00
|
|
|
|
2022-07-12 10:14:57 +00:00
|
|
|
drift *indicator.Drift
|
2022-07-04 12:13:54 +00:00
|
|
|
atr *indicator.ATR
|
2022-07-01 10:38:25 +00:00
|
|
|
midPrice fixedpoint.Value
|
2022-07-04 12:13:54 +00:00
|
|
|
lock sync.RWMutex
|
2022-07-01 10:38:25 +00:00
|
|
|
|
2022-07-14 03:46:19 +00:00
|
|
|
Source string `json:"source"`
|
|
|
|
Stoploss fixedpoint.Value `json:"stoploss"`
|
|
|
|
CanvasPath string `json:"canvasPath"`
|
|
|
|
PredictOffset int `json:"predictOffset"`
|
2022-07-12 10:14:57 +00:00
|
|
|
|
2022-07-05 11:43:05 +00:00
|
|
|
ExitMethods bbgo.ExitMethodSet `json:"exits"`
|
|
|
|
Session *bbgo.ExchangeSession
|
2022-07-01 10:38:25 +00:00
|
|
|
*bbgo.GeneralOrderExecutor
|
2022-07-14 03:46:19 +00:00
|
|
|
|
|
|
|
getLastPrice func() fixedpoint.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) Print() {
|
|
|
|
b, _ := json.MarshalIndent(s.ExitMethods, " ", " ")
|
|
|
|
hiyellow := color.New(color.FgHiYellow).FprintfFunc()
|
|
|
|
hiyellow(os.Stderr, "------ %s Settings ------\n", s.InstanceID())
|
|
|
|
hiyellow(os.Stderr, "canvasPath: %s\n", s.CanvasPath)
|
|
|
|
hiyellow(os.Stderr, "source: %s\n", s.Source)
|
|
|
|
hiyellow(os.Stderr, "stoploss: %v\n", s.Stoploss)
|
|
|
|
hiyellow(os.Stderr, "predictOffset: %d\n", s.PredictOffset)
|
|
|
|
hiyellow(os.Stderr, "exits:\n %s\n", string(b))
|
|
|
|
hiyellow(os.Stderr, "symbol: %s\n", s.Symbol)
|
|
|
|
hiyellow(os.Stderr, "interval: %s\n", s.Interval)
|
|
|
|
hiyellow(os.Stderr, "window: %d\n", s.Window)
|
2022-07-01 10:38:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) ID() string {
|
|
|
|
return ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) InstanceID() string {
|
|
|
|
return fmt.Sprintf("%s:%s", ID, s.Symbol)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
|
|
|
|
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{
|
|
|
|
Interval: s.Interval,
|
|
|
|
})
|
|
|
|
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{
|
|
|
|
Interval: types.Interval1m,
|
|
|
|
})
|
|
|
|
|
|
|
|
if !bbgo.IsBackTesting {
|
|
|
|
session.Subscribe(types.MarketTradeChannel, s.Symbol, types.SubscribeOptions{})
|
|
|
|
}
|
2022-07-05 11:43:05 +00:00
|
|
|
s.ExitMethods.SetAndSubscribe(session, s)
|
2022-07-01 10:38:25 +00:00
|
|
|
}
|
|
|
|
|
2022-07-04 12:13:54 +00:00
|
|
|
func (s *Strategy) ClosePosition(ctx context.Context) (*types.Order, bool) {
|
|
|
|
order := s.Position.NewMarketCloseOrder(fixedpoint.One)
|
2022-07-12 10:14:57 +00:00
|
|
|
if order == nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
2022-07-04 12:13:54 +00:00
|
|
|
order.TimeInForce = ""
|
|
|
|
balances := s.Session.GetAccount().Balances()
|
|
|
|
baseBalance := balances[s.Market.BaseCurrency].Available
|
2022-07-14 03:46:19 +00:00
|
|
|
price := s.getLastPrice()
|
2022-07-04 12:13:54 +00:00
|
|
|
if order.Side == types.SideTypeBuy {
|
|
|
|
quoteAmount := balances[s.Market.QuoteCurrency].Available.Div(price)
|
|
|
|
if order.Quantity.Compare(quoteAmount) > 0 {
|
|
|
|
order.Quantity = quoteAmount
|
|
|
|
}
|
|
|
|
} else if order.Side == types.SideTypeSell && order.Quantity.Compare(baseBalance) > 0 {
|
|
|
|
order.Quantity = baseBalance
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
if s.Market.IsDustQuantity(order.Quantity, price) {
|
|
|
|
return nil, true
|
|
|
|
}
|
|
|
|
createdOrders, err := s.GeneralOrderExecutor.SubmitOrders(ctx, *order)
|
|
|
|
if err != nil {
|
|
|
|
order.Quantity = order.Quantity.Mul(fixedpoint.One.Sub(Delta))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return &createdOrders[0], true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-14 03:46:19 +00:00
|
|
|
func (s *Strategy) SourceFuncGenerator() SourceFunc {
|
|
|
|
switch strings.ToLower(s.Source) {
|
|
|
|
case "close":
|
|
|
|
return func(kline *types.KLine) fixedpoint.Value { return kline.Close }
|
|
|
|
case "high":
|
|
|
|
return func(kline *types.KLine) fixedpoint.Value { return kline.High }
|
|
|
|
case "low":
|
|
|
|
return func(kline *types.KLine) fixedpoint.Value { return kline.Low }
|
|
|
|
case "hl2":
|
|
|
|
return func(kline *types.KLine) fixedpoint.Value {
|
|
|
|
return kline.High.Add(kline.Low).Div(Two)
|
|
|
|
}
|
|
|
|
case "hlc3":
|
|
|
|
return func(kline *types.KLine) fixedpoint.Value {
|
|
|
|
return kline.High.Add(kline.Low).Add(kline.Close).Div(Three)
|
|
|
|
}
|
|
|
|
case "ohlc4":
|
|
|
|
return func(kline *types.KLine) fixedpoint.Value {
|
|
|
|
return kline.Open.Add(kline.High).Add(kline.Low).Add(kline.Close).Div(Four)
|
|
|
|
}
|
|
|
|
case "open":
|
|
|
|
return func(kline *types.KLine) fixedpoint.Value { return kline.Open }
|
|
|
|
case "":
|
|
|
|
return func(kline *types.KLine) fixedpoint.Value {
|
|
|
|
log.Infof("source not set, use hl2 by default")
|
|
|
|
return kline.High.Add(kline.Low).Div(Two)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("Unable to parse: %s", s.Source))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-01 10:38:25 +00:00
|
|
|
func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
|
|
|
|
instanceID := s.InstanceID()
|
|
|
|
if s.Position == nil {
|
|
|
|
s.Position = types.NewPositionFromMarket(s.Market)
|
|
|
|
}
|
|
|
|
if s.ProfitStats == nil {
|
|
|
|
s.ProfitStats = types.NewProfitStats(s.Market)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.TradeStats == nil {
|
2022-07-12 10:14:57 +00:00
|
|
|
s.TradeStats = types.NewTradeStats(s.Symbol)
|
2022-07-01 10:38:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StrategyController
|
|
|
|
s.Status = types.StrategyStatusRunning
|
|
|
|
|
|
|
|
s.OnSuspend(func() {
|
|
|
|
_ = s.GeneralOrderExecutor.GracefulCancel(ctx)
|
|
|
|
})
|
|
|
|
|
|
|
|
s.OnEmergencyStop(func() {
|
|
|
|
_ = s.GeneralOrderExecutor.GracefulCancel(ctx)
|
2022-07-04 12:13:54 +00:00
|
|
|
_, _ = s.ClosePosition(ctx)
|
2022-07-01 10:38:25 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
s.Session = session
|
|
|
|
s.GeneralOrderExecutor = bbgo.NewGeneralOrderExecutor(session, s.Symbol, ID, instanceID, s.Position)
|
|
|
|
s.GeneralOrderExecutor.BindEnvironment(s.Environment)
|
|
|
|
s.GeneralOrderExecutor.BindProfitStats(s.ProfitStats)
|
|
|
|
s.GeneralOrderExecutor.BindTradeStats(s.TradeStats)
|
|
|
|
s.GeneralOrderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) {
|
|
|
|
bbgo.Sync(s)
|
|
|
|
})
|
|
|
|
s.GeneralOrderExecutor.Bind()
|
2022-07-05 11:43:05 +00:00
|
|
|
for _, method := range s.ExitMethods {
|
|
|
|
method.Bind(session, s.GeneralOrderExecutor)
|
|
|
|
}
|
2022-07-01 10:38:25 +00:00
|
|
|
|
|
|
|
store, _ := session.MarketDataStore(s.Symbol)
|
|
|
|
|
2022-07-14 03:46:19 +00:00
|
|
|
getSource := s.SourceFuncGenerator()
|
2022-07-12 10:14:57 +00:00
|
|
|
|
2022-07-14 03:46:19 +00:00
|
|
|
s.drift = &indicator.Drift{
|
|
|
|
MA: &indicator.SMA{IntervalWindow: s.IntervalWindow},
|
|
|
|
IntervalWindow: s.IntervalWindow,
|
|
|
|
}
|
2022-07-12 10:14:57 +00:00
|
|
|
s.atr = &indicator.ATR{IntervalWindow: types.IntervalWindow{Interval: s.Interval, Window: 14}}
|
2022-07-01 10:38:25 +00:00
|
|
|
|
|
|
|
klines, ok := store.KLinesOfInterval(s.Interval)
|
|
|
|
if !ok {
|
|
|
|
log.Errorf("klines not exists")
|
|
|
|
return nil
|
|
|
|
}
|
2022-07-14 03:46:19 +00:00
|
|
|
|
|
|
|
dynamicKLine := &types.KLine{}
|
2022-07-01 10:38:25 +00:00
|
|
|
for _, kline := range *klines {
|
2022-07-12 10:14:57 +00:00
|
|
|
source := getSource(&kline).Float64()
|
|
|
|
s.drift.Update(source)
|
2022-07-01 10:38:25 +00:00
|
|
|
s.atr.Update(kline.High.Float64(), kline.Low.Float64(), kline.Close.Float64())
|
|
|
|
}
|
|
|
|
|
2022-07-14 03:46:19 +00:00
|
|
|
if s.Environment.IsBackTesting() {
|
|
|
|
s.getLastPrice = func() fixedpoint.Value {
|
|
|
|
lastPrice, ok := s.Session.LastPrice(s.Symbol)
|
|
|
|
if !ok {
|
|
|
|
log.Error("cannot get lastprice")
|
|
|
|
}
|
|
|
|
return lastPrice
|
2022-07-01 10:38:25 +00:00
|
|
|
}
|
2022-07-14 03:46:19 +00:00
|
|
|
} else {
|
|
|
|
session.MarketDataStream.OnBookTickerUpdate(func(ticker types.BookTicker) {
|
|
|
|
bestBid := ticker.Buy
|
|
|
|
bestAsk := ticker.Sell
|
|
|
|
|
|
|
|
if util.TryLock(&s.lock) {
|
|
|
|
if !bestAsk.IsZero() && !bestBid.IsZero() {
|
|
|
|
s.midPrice = bestAsk.Add(bestBid).Div(Two)
|
|
|
|
} else if !bestAsk.IsZero() {
|
|
|
|
s.midPrice = bestAsk
|
|
|
|
} else {
|
|
|
|
s.midPrice = bestBid
|
|
|
|
}
|
|
|
|
s.lock.Unlock()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
s.getLastPrice = func() (lastPrice fixedpoint.Value) {
|
|
|
|
var ok bool
|
|
|
|
s.lock.RLock()
|
|
|
|
if s.midPrice.IsZero() {
|
|
|
|
lastPrice, ok = s.Session.LastPrice(s.Symbol)
|
|
|
|
if !ok {
|
|
|
|
log.Error("cannot get lastprice")
|
|
|
|
return lastPrice
|
|
|
|
}
|
2022-07-01 10:38:25 +00:00
|
|
|
} else {
|
2022-07-14 03:46:19 +00:00
|
|
|
lastPrice = s.midPrice
|
2022-07-01 10:38:25 +00:00
|
|
|
}
|
2022-07-14 03:46:19 +00:00
|
|
|
s.lock.RUnlock()
|
|
|
|
return lastPrice
|
2022-07-01 10:38:25 +00:00
|
|
|
}
|
2022-07-14 03:46:19 +00:00
|
|
|
}
|
2022-07-01 10:38:25 +00:00
|
|
|
|
2022-07-12 10:14:57 +00:00
|
|
|
priceLine := types.NewQueue(100)
|
2022-07-14 03:46:19 +00:00
|
|
|
stoploss := s.Stoploss.Float64()
|
2022-07-12 10:14:57 +00:00
|
|
|
|
2022-07-01 10:38:25 +00:00
|
|
|
session.MarketDataStream.OnKLineClosed(func(kline types.KLine) {
|
|
|
|
if s.Status != types.StrategyStatusRunning {
|
|
|
|
return
|
|
|
|
}
|
2022-07-12 10:14:57 +00:00
|
|
|
if kline.Symbol != s.Symbol {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var driftPred, atr float64
|
|
|
|
var drift []float64
|
|
|
|
|
|
|
|
if !kline.Closed {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if kline.Interval == types.Interval1m {
|
2022-07-01 10:38:25 +00:00
|
|
|
return
|
|
|
|
}
|
2022-07-12 10:14:57 +00:00
|
|
|
dynamicKLine.Copy(&kline)
|
|
|
|
|
|
|
|
source := getSource(dynamicKLine)
|
|
|
|
sourcef := source.Float64()
|
|
|
|
priceLine.Update(sourcef)
|
|
|
|
s.drift.Update(sourcef)
|
|
|
|
drift = s.drift.Array(2)
|
2022-07-14 03:46:19 +00:00
|
|
|
driftPred = s.drift.Predict(s.PredictOffset)
|
2022-07-12 10:14:57 +00:00
|
|
|
atr = s.atr.Last()
|
2022-07-14 03:46:19 +00:00
|
|
|
price := s.getLastPrice()
|
2022-07-13 08:08:57 +00:00
|
|
|
pricef := price.Float64()
|
2022-07-12 10:14:57 +00:00
|
|
|
avg := s.Position.AverageCost.Float64()
|
|
|
|
|
2022-07-13 08:08:57 +00:00
|
|
|
shortCondition := (driftPred <= 0 && drift[0] <= 0)
|
|
|
|
longCondition := (driftPred >= 0 && drift[0] >= 0)
|
|
|
|
exitShortCondition := ((drift[1] < 0 && drift[0] >= 0) || avg+atr/2 <= pricef || avg*(1.+stoploss) <= pricef) &&
|
|
|
|
(!s.Position.IsClosed() && !s.Position.IsDust(fixedpoint.Max(price, source))) && !longCondition
|
|
|
|
exitLongCondition := ((drift[1] > 0 && drift[0] < 0) || avg-atr/2 >= pricef || avg*(1.-stoploss) >= pricef) &&
|
|
|
|
(!s.Position.IsClosed() && !s.Position.IsDust(fixedpoint.Min(price, source))) && !shortCondition
|
2022-07-12 10:14:57 +00:00
|
|
|
|
2022-07-14 03:46:19 +00:00
|
|
|
if exitShortCondition || exitLongCondition {
|
|
|
|
if err := s.GeneralOrderExecutor.GracefulCancel(ctx); err != nil {
|
|
|
|
log.WithError(err).Errorf("cannot cancel orders")
|
|
|
|
return
|
2022-07-13 08:08:57 +00:00
|
|
|
}
|
|
|
|
_, _ = s.ClosePosition(ctx)
|
|
|
|
}
|
2022-07-12 10:14:57 +00:00
|
|
|
if shortCondition {
|
2022-07-14 03:46:19 +00:00
|
|
|
if err := s.GeneralOrderExecutor.GracefulCancel(ctx); err != nil {
|
|
|
|
log.WithError(err).Errorf("cannot cancel orders")
|
|
|
|
return
|
2022-07-01 10:38:25 +00:00
|
|
|
}
|
2022-07-04 12:13:54 +00:00
|
|
|
baseBalance, ok := s.Session.GetAccount().Balance(s.Market.BaseCurrency)
|
|
|
|
if !ok {
|
|
|
|
log.Errorf("unable to get baseBalance")
|
|
|
|
return
|
|
|
|
}
|
2022-07-12 10:14:57 +00:00
|
|
|
if source.Compare(price) < 0 {
|
|
|
|
source = price
|
2022-07-04 12:13:54 +00:00
|
|
|
}
|
|
|
|
|
2022-07-12 10:14:57 +00:00
|
|
|
if s.Market.IsDustQuantity(baseBalance.Available, source) {
|
2022-07-05 11:43:05 +00:00
|
|
|
return
|
|
|
|
}
|
2022-07-04 12:13:54 +00:00
|
|
|
_, err := s.GeneralOrderExecutor.SubmitOrders(ctx, types.SubmitOrder{
|
|
|
|
Symbol: s.Symbol,
|
|
|
|
Side: types.SideTypeSell,
|
|
|
|
Type: types.OrderTypeLimitMaker,
|
2022-07-12 10:14:57 +00:00
|
|
|
Price: source,
|
2022-07-14 03:46:19 +00:00
|
|
|
StopPrice: fixedpoint.NewFromFloat(math.Min(sourcef+atr/2, sourcef*(1.+stoploss))),
|
2022-07-04 12:13:54 +00:00
|
|
|
Quantity: baseBalance.Available,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Errorf("cannot place sell order")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2022-07-12 10:14:57 +00:00
|
|
|
if longCondition {
|
2022-07-14 03:46:19 +00:00
|
|
|
if err := s.GeneralOrderExecutor.GracefulCancel(ctx); err != nil {
|
|
|
|
log.WithError(err).Errorf("cannot cancel orders")
|
|
|
|
return
|
2022-07-12 10:14:57 +00:00
|
|
|
}
|
|
|
|
if source.Compare(price) > 0 {
|
|
|
|
source = price
|
2022-07-04 12:13:54 +00:00
|
|
|
}
|
|
|
|
quoteBalance, ok := s.Session.GetAccount().Balance(s.Market.QuoteCurrency)
|
|
|
|
if !ok {
|
|
|
|
log.Errorf("unable to get quoteCurrency")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if s.Market.IsDustQuantity(
|
2022-07-12 10:14:57 +00:00
|
|
|
quoteBalance.Available.Div(source), source) {
|
2022-07-04 12:13:54 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
_, err := s.GeneralOrderExecutor.SubmitOrders(ctx, types.SubmitOrder{
|
|
|
|
Symbol: s.Symbol,
|
|
|
|
Side: types.SideTypeBuy,
|
|
|
|
Type: types.OrderTypeLimitMaker,
|
2022-07-12 10:14:57 +00:00
|
|
|
Price: source,
|
2022-07-14 03:46:19 +00:00
|
|
|
StopPrice: fixedpoint.NewFromFloat(math.Max(sourcef-atr/2, sourcef*(1.-stoploss))),
|
2022-07-12 10:14:57 +00:00
|
|
|
Quantity: quoteBalance.Available.Div(source),
|
2022-07-04 12:13:54 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Errorf("cannot place buy order")
|
|
|
|
return
|
2022-07-01 10:38:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-07-04 12:13:54 +00:00
|
|
|
bbgo.OnShutdown(func(ctx context.Context, wg *sync.WaitGroup) {
|
2022-07-01 10:38:25 +00:00
|
|
|
_, _ = fmt.Fprintln(os.Stderr, s.TradeStats.String())
|
2022-07-14 03:46:19 +00:00
|
|
|
s.Print()
|
2022-07-12 10:14:57 +00:00
|
|
|
canvas := types.NewCanvas(s.InstanceID(), s.Interval)
|
|
|
|
mean := priceLine.Mean(100)
|
2022-07-13 02:31:23 +00:00
|
|
|
highestPrice := priceLine.Minus(mean).Highest(100)
|
2022-07-12 10:14:57 +00:00
|
|
|
highestDrift := s.drift.Highest(100)
|
|
|
|
ratio := highestDrift / highestPrice
|
|
|
|
canvas.Plot("drift", s.drift, dynamicKLine.StartTime, 100)
|
|
|
|
canvas.Plot("zero", types.NumberSeries(0), dynamicKLine.StartTime, 100)
|
|
|
|
canvas.Plot("price", priceLine.Minus(mean).Mul(ratio), dynamicKLine.StartTime, 100)
|
2022-07-13 08:08:57 +00:00
|
|
|
f, err := os.Create(s.CanvasPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("%v cannot create on %s", err, s.CanvasPath)
|
|
|
|
}
|
2022-07-12 10:14:57 +00:00
|
|
|
defer f.Close()
|
|
|
|
canvas.Render(chart.PNG, f)
|
2022-07-01 10:38:25 +00:00
|
|
|
wg.Done()
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|