mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 23:05:15 +00:00
strategy/supertrend: add accumulated profit SMA report
This commit is contained in:
parent
b16abee6e4
commit
dc9ecdd6ca
|
@ -1,8 +1,10 @@
|
||||||
package supertrend
|
package supertrend
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/fatih/color"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
@ -84,6 +86,12 @@ type Strategy struct {
|
||||||
|
|
||||||
// StrategyController
|
// StrategyController
|
||||||
bbgo.StrategyController
|
bbgo.StrategyController
|
||||||
|
|
||||||
|
// Accumulated profit report
|
||||||
|
accumulatedProfit fixedpoint.Value
|
||||||
|
accumulatedProfitMA *indicator.SMA
|
||||||
|
// AccumulatedProfitMAWindow Accumulated profit SMA window
|
||||||
|
AccumulatedProfitMAWindow int `json:"accumulatedProfitMAWindow"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Strategy) ID() string {
|
func (s *Strategy) ID() string {
|
||||||
|
@ -264,6 +272,18 @@ func (s *Strategy) calculateQuantity(currentPrice fixedpoint.Value) fixedpoint.V
|
||||||
return quantity
|
return quantity
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PrintResult prints accumulated profit status
|
||||||
|
func (s *Strategy) PrintResult(o *os.File) {
|
||||||
|
f := bufio.NewWriter(o)
|
||||||
|
defer f.Flush()
|
||||||
|
hiyellow := color.New(color.FgHiYellow).FprintfFunc()
|
||||||
|
hiyellow(f, "------ %s Results ------\n", s.InstanceID())
|
||||||
|
hiyellow(f, "Symbol: %v\n", s.Symbol)
|
||||||
|
hiyellow(f, "Accumulated Profit: %v\n", s.accumulatedProfit)
|
||||||
|
hiyellow(f, "Accumulated Profit %dMA: %f\n", s.AccumulatedProfitMAWindow, s.accumulatedProfitMA.Last())
|
||||||
|
hiyellow(f, "\n")
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
|
func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
|
||||||
s.session = session
|
s.session = session
|
||||||
|
|
||||||
|
@ -305,6 +325,20 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
|
||||||
s.orderExecutor.BindTradeStats(s.TradeStats)
|
s.orderExecutor.BindTradeStats(s.TradeStats)
|
||||||
s.orderExecutor.Bind()
|
s.orderExecutor.Bind()
|
||||||
|
|
||||||
|
// Accumulated profit report
|
||||||
|
if s.AccumulatedProfitMAWindow <= 0 {
|
||||||
|
s.AccumulatedProfitMAWindow = 60
|
||||||
|
}
|
||||||
|
s.accumulatedProfitMA = &indicator.SMA{IntervalWindow: types.IntervalWindow{Interval: s.Interval, Window: s.AccumulatedProfitMAWindow}}
|
||||||
|
s.orderExecutor.TradeCollector().OnProfit(func(trade types.Trade, profit *types.Profit) {
|
||||||
|
if profit == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s.accumulatedProfit = s.accumulatedProfit.Add(profit.Profit)
|
||||||
|
s.accumulatedProfitMA.Update(s.accumulatedProfit.Float64())
|
||||||
|
})
|
||||||
|
|
||||||
// Sync position to redis on trade
|
// Sync position to redis on trade
|
||||||
s.orderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) {
|
s.orderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) {
|
||||||
bbgo.Sync(s)
|
bbgo.Sync(s)
|
||||||
|
@ -409,6 +443,9 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
|
||||||
bbgo.OnShutdown(func(ctx context.Context, wg *sync.WaitGroup) {
|
bbgo.OnShutdown(func(ctx context.Context, wg *sync.WaitGroup) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
|
|
||||||
|
// Print accumulated profit report
|
||||||
|
defer s.PrintResult(os.Stdout)
|
||||||
|
|
||||||
_ = s.orderExecutor.GracefulCancel(ctx)
|
_ = s.orderExecutor.GracefulCancel(ctx)
|
||||||
_, _ = fmt.Fprintln(os.Stderr, s.TradeStats.String())
|
_, _ = fmt.Fprintln(os.Stderr, s.TradeStats.String())
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue
Block a user