all: add more spreadsheet methods

This commit is contained in:
c9s 2023-07-13 17:56:16 +08:00
parent 3ed5f00bcc
commit 2de107c275
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
3 changed files with 64 additions and 2 deletions

1
go.sum
View File

@ -663,7 +663,6 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.4/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=

View File

@ -4,6 +4,7 @@ import (
"context"
"fmt"
"reflect"
"strings"
"time"
"github.com/sirupsen/logrus"
@ -19,7 +20,8 @@ type SpreadSheetService struct {
SpreadsheetID string
TokenFile string
service *sheets.Service
service *sheets.Service
spreadsheet *sheets.Spreadsheet
}
func NewSpreadSheetService(ctx context.Context, tokenFile string, spreadsheetID string) *SpreadSheetService {
@ -41,6 +43,57 @@ func NewSpreadSheetService(ctx context.Context, tokenFile string, spreadsheetID
}
}
func (s *SpreadSheetService) Load() error {
spreadsheet, err := s.service.Spreadsheets.Get(s.SpreadsheetID).Do()
if err != nil {
return err
}
s.spreadsheet = spreadsheet
return nil
}
func (s *SpreadSheetService) Get(forceReload bool) (*sheets.Spreadsheet, error) {
if s.spreadsheet == nil || forceReload {
if err := s.Load(); err != nil {
return nil, err
}
}
return s.spreadsheet, nil
}
func (s *SpreadSheetService) NewSheet(title string) (*sheets.Sheet, error) {
resp, err := AddNewSheet(s.service, s.SpreadsheetID, title)
if err != nil {
return nil, err
}
DebugBatchUpdateSpreadsheetResponse(resp)
_, err = s.Get(true)
if err != nil {
return nil, err
}
return s.LookupSheet(title)
}
func (s *SpreadSheetService) LookupSheet(title string) (*sheets.Sheet, error) {
spreadsheet, err := s.Get(false)
if err != nil {
return nil, err
}
for _, sheet := range spreadsheet.Sheets {
if strings.EqualFold(title, sheet.Properties.Title) {
return sheet, nil
}
}
return nil, nil
}
func ReadSheetValuesRange(srv *sheets.Service, spreadsheetId, readRange string) (*sheets.ValueRange, error) {
log.Infof("ReadSheetValuesRange: %s", readRange)
resp, err := srv.Spreadsheets.Values.Get(spreadsheetId, readRange).Do()

View File

@ -36,6 +36,12 @@ type Strategy struct {
RiskController
}
// TODO: use this to replace the parameters
type StrategyInstance interface {
ID() string
InstanceID() string
}
func NewStrategy(ctx context.Context, environ *bbgo.Environment, session *bbgo.ExchangeSession, market types.Market, strategyID, instanceID string) *Strategy {
s := &Strategy{
Environ: environ,
@ -81,6 +87,10 @@ func (s *Strategy) Initialize(ctx context.Context, environ *bbgo.Environment, se
// bbgo.Sync(ctx, s)
})
if environ.GoogleSpreadSheetService != nil {
// allocate a google spread sheet for this strategy
}
if !s.PositionHardLimit.IsZero() && !s.MaxPositionQuantity.IsZero() {
log.Infof("positionHardLimit and maxPositionQuantity are configured, setting up PositionRiskControl...")
s.positionRiskControl = riskcontrol.NewPositionRiskControl(s.OrderExecutor, s.PositionHardLimit, s.MaxPositionQuantity)