mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-25 16:25:16 +00:00
move files
This commit is contained in:
parent
40d7072f78
commit
359b3c56b4
79
pkg/bbgo/reporter.go
Normal file
79
pkg/bbgo/reporter.go
Normal file
|
@ -0,0 +1,79 @@
|
|||
package bbgo
|
||||
|
||||
import (
|
||||
"github.com/robfig/cron/v3"
|
||||
|
||||
"github.com/c9s/bbgo/pkg/accounting/pnl"
|
||||
)
|
||||
|
||||
type PnLReporter interface {
|
||||
Run()
|
||||
}
|
||||
|
||||
type baseReporter struct {
|
||||
notifier Notifier
|
||||
cron *cron.Cron
|
||||
environment *Environment
|
||||
}
|
||||
|
||||
type PnLReporterManager struct {
|
||||
baseReporter
|
||||
|
||||
reporters []PnLReporter
|
||||
}
|
||||
|
||||
func NewPnLReporter(notifier Notifier) *PnLReporterManager {
|
||||
return &PnLReporterManager{
|
||||
baseReporter: baseReporter{
|
||||
notifier: notifier,
|
||||
cron: cron.New(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (manager *PnLReporterManager) AverageCostBySymbols(symbols ...string) *AverageCostPnLReporter {
|
||||
reporter := &AverageCostPnLReporter{
|
||||
baseReporter: manager.baseReporter,
|
||||
Symbols: symbols,
|
||||
}
|
||||
|
||||
manager.reporters = append(manager.reporters, reporter)
|
||||
return reporter
|
||||
}
|
||||
|
||||
type AverageCostPnLReporter struct {
|
||||
baseReporter
|
||||
|
||||
Sessions []string
|
||||
Symbols []string
|
||||
}
|
||||
|
||||
func (reporter *AverageCostPnLReporter) Of(sessions ...string) *AverageCostPnLReporter {
|
||||
reporter.Sessions = sessions
|
||||
return reporter
|
||||
}
|
||||
|
||||
func (reporter *AverageCostPnLReporter) When(specs ...string) *AverageCostPnLReporter {
|
||||
for _, spec := range specs {
|
||||
_, err := reporter.cron.AddJob(spec, reporter)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
return reporter
|
||||
}
|
||||
|
||||
func (reporter *AverageCostPnLReporter) Run() {
|
||||
for _, sessionName := range reporter.Sessions {
|
||||
session := reporter.environment.sessions[sessionName]
|
||||
calculator := &pnl.AverageCostCalculator{
|
||||
TradingFeeCurrency: session.Exchange.PlatformFeeCurrency(),
|
||||
}
|
||||
|
||||
for _, symbol := range reporter.Symbols {
|
||||
report := calculator.Calculate(symbol, session.Trades[symbol], session.lastPrices[symbol])
|
||||
report.Print()
|
||||
}
|
||||
}
|
||||
}
|
|
@ -3,10 +3,8 @@ package bbgo
|
|||
import (
|
||||
"context"
|
||||
|
||||
"github.com/robfig/cron/v3"
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/c9s/bbgo/pkg/accounting/pnl"
|
||||
"github.com/c9s/bbgo/pkg/types"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
|
@ -32,78 +30,6 @@ type CrossExchangeStrategy interface {
|
|||
Run(ctx context.Context, orderExecutionRouter OrderExecutionRouter, sessions map[string]*ExchangeSession) error
|
||||
}
|
||||
|
||||
type PnLReporter interface {
|
||||
Run()
|
||||
}
|
||||
|
||||
type baseReporter struct {
|
||||
notifier Notifier
|
||||
cron *cron.Cron
|
||||
environment *Environment
|
||||
}
|
||||
|
||||
type PnLReporterManager struct {
|
||||
baseReporter
|
||||
|
||||
reporters []PnLReporter
|
||||
}
|
||||
|
||||
func NewPnLReporter(notifier Notifier) *PnLReporterManager {
|
||||
return &PnLReporterManager{
|
||||
baseReporter: baseReporter{
|
||||
notifier: notifier,
|
||||
cron: cron.New(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (manager *PnLReporterManager) AverageCostBySymbols(symbols ...string) *AverageCostPnLReporter {
|
||||
reporter := &AverageCostPnLReporter{
|
||||
baseReporter: manager.baseReporter,
|
||||
Symbols: symbols,
|
||||
}
|
||||
|
||||
manager.reporters = append(manager.reporters, reporter)
|
||||
return reporter
|
||||
}
|
||||
|
||||
type AverageCostPnLReporter struct {
|
||||
baseReporter
|
||||
|
||||
Sessions []string
|
||||
Symbols []string
|
||||
}
|
||||
|
||||
func (reporter *AverageCostPnLReporter) Of(sessions ...string) *AverageCostPnLReporter {
|
||||
reporter.Sessions = sessions
|
||||
return reporter
|
||||
}
|
||||
|
||||
func (reporter *AverageCostPnLReporter) When(specs ...string) *AverageCostPnLReporter {
|
||||
for _, spec := range specs {
|
||||
_, err := reporter.cron.AddJob(spec, reporter)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
return reporter
|
||||
}
|
||||
|
||||
func (reporter *AverageCostPnLReporter) Run() {
|
||||
for _, sessionName := range reporter.Sessions {
|
||||
session := reporter.environment.sessions[sessionName]
|
||||
calculator := &pnl.AverageCostCalculator{
|
||||
TradingFeeCurrency: session.Exchange.PlatformFeeCurrency(),
|
||||
}
|
||||
|
||||
for _, symbol := range reporter.Symbols {
|
||||
report := calculator.Calculate(symbol, session.Trades[symbol], session.lastPrices[symbol])
|
||||
report.Print()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type Trader struct {
|
||||
Notifiability
|
||||
|
||||
|
|
|
@ -16,57 +16,6 @@ type SingleExchangeStrategyConfig struct {
|
|||
Strategy bbgo.SingleExchangeStrategy
|
||||
}
|
||||
|
||||
type StringSlice []string
|
||||
|
||||
func (s *StringSlice) decode(a interface{}) error {
|
||||
switch d := a.(type) {
|
||||
case string:
|
||||
*s = append(*s, d)
|
||||
|
||||
case []string:
|
||||
*s = append(*s, d...)
|
||||
|
||||
case []interface{}:
|
||||
for _, de := range d {
|
||||
if err := s.decode(de); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
default:
|
||||
return errors.Errorf("unexpected type %T for StringSlice: %+v", d, d)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *StringSlice) UnmarshalYAML(unmarshal func(interface{}) error) (err error) {
|
||||
var ss []string
|
||||
err = unmarshal(&ss)
|
||||
if err == nil {
|
||||
*s = ss
|
||||
return
|
||||
}
|
||||
|
||||
var as string
|
||||
err = unmarshal(&as)
|
||||
if err == nil {
|
||||
*s = append(*s, as)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *StringSlice) UnmarshalJSON(b []byte) error {
|
||||
var a interface{}
|
||||
var err = json.Unmarshal(b, &a)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return s.decode(a)
|
||||
}
|
||||
|
||||
type PnLReporter struct {
|
||||
AverageCostBySymbols StringSlice `json:"averageCostBySymbols" yaml:"averageCostBySymbols"`
|
||||
Of StringSlice `json:"of" yaml:"of"`
|
||||
|
|
58
pkg/config/string.go
Normal file
58
pkg/config/string.go
Normal file
|
@ -0,0 +1,58 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type StringSlice []string
|
||||
|
||||
func (s *StringSlice) decode(a interface{}) error {
|
||||
switch d := a.(type) {
|
||||
case string:
|
||||
*s = append(*s, d)
|
||||
|
||||
case []string:
|
||||
*s = append(*s, d...)
|
||||
|
||||
case []interface{}:
|
||||
for _, de := range d {
|
||||
if err := s.decode(de); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
default:
|
||||
return errors.Errorf("unexpected type %T for StringSlice: %+v", d, d)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *StringSlice) UnmarshalYAML(unmarshal func(interface{}) error) (err error) {
|
||||
var ss []string
|
||||
err = unmarshal(&ss)
|
||||
if err == nil {
|
||||
*s = ss
|
||||
return
|
||||
}
|
||||
|
||||
var as string
|
||||
err = unmarshal(&as)
|
||||
if err == nil {
|
||||
*s = append(*s, as)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *StringSlice) UnmarshalJSON(b []byte) error {
|
||||
var a interface{}
|
||||
var err = json.Unmarshal(b, &a)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return s.decode(a)
|
||||
}
|
Loading…
Reference in New Issue
Block a user