mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 06:53:52 +00:00
positionupdater: update base position
This commit is contained in:
parent
ae6c6c90a7
commit
8c53c7e575
|
@ -27,12 +27,19 @@ type closePositionContext struct {
|
|||
percentage fixedpoint.Value
|
||||
}
|
||||
|
||||
type updatePositionContext struct {
|
||||
signature string
|
||||
updater StrategyPositionUpdater
|
||||
value fixedpoint.Value
|
||||
}
|
||||
|
||||
type CoreInteraction struct {
|
||||
environment *Environment
|
||||
trader *Trader
|
||||
|
||||
exchangeStrategies map[string]SingleExchangeStrategy
|
||||
closePositionContext closePositionContext
|
||||
exchangeStrategies map[string]SingleExchangeStrategy
|
||||
closePositionContext closePositionContext
|
||||
updatePositionContext updatePositionContext
|
||||
}
|
||||
|
||||
func NewCoreInteraction(environment *Environment, trader *Trader) *CoreInteraction {
|
||||
|
@ -387,6 +394,56 @@ func (it *CoreInteraction) Commands(i *interact.Interact) {
|
|||
reply.Message(fmt.Sprintf("Strategy %s stopped and the position closed.", signature))
|
||||
return nil
|
||||
})
|
||||
|
||||
// Position updater
|
||||
i.PrivateCommand("/updatepositionbase", "Update Strategy Position: Base", func(reply interact.Reply) error {
|
||||
// it.trader.exchangeStrategies
|
||||
// send symbol options
|
||||
if strategies, found := filterStrategyByInterface((*StrategyPositionUpdater)(nil), it.exchangeStrategies); found {
|
||||
reply.AddMultipleButtons(generateStrategyButtonsForm(strategies))
|
||||
reply.Message("Please choose one strategy")
|
||||
} else {
|
||||
reply.Message("No strategy supports StrategyPositionUpdater")
|
||||
}
|
||||
return nil
|
||||
}).Next(func(signature string, reply interact.Reply) error {
|
||||
strategy, ok := it.exchangeStrategies[signature]
|
||||
if !ok {
|
||||
reply.Message("Strategy not found")
|
||||
return fmt.Errorf("strategy %s not found", signature)
|
||||
}
|
||||
|
||||
updater, implemented := strategy.(StrategyPositionUpdater)
|
||||
if !implemented {
|
||||
reply.Message(fmt.Sprintf("Strategy %s does not support StrategyPositionUpdater", signature))
|
||||
return fmt.Errorf("strategy %s does not implement StrategyPositionUpdater", signature)
|
||||
}
|
||||
|
||||
it.updatePositionContext.updater = updater
|
||||
it.updatePositionContext.signature = signature
|
||||
|
||||
reply.Message("Enter the amount to change")
|
||||
|
||||
return nil
|
||||
}).Next(func(valueStr string, reply interact.Reply) error {
|
||||
value, err := strconv.ParseFloat(valueStr, 64)
|
||||
if err != nil {
|
||||
reply.Message(fmt.Sprintf("%q is not a valid value string", valueStr))
|
||||
return err
|
||||
}
|
||||
|
||||
if kc, ok := reply.(interact.KeyboardController); ok {
|
||||
kc.RemoveKeyboard()
|
||||
}
|
||||
|
||||
if err := it.updatePositionContext.updater.UpdateBase(value); err != nil {
|
||||
reply.Message(fmt.Sprintf("Failed to update base position of the strategy, %s", err.Error()))
|
||||
return err
|
||||
}
|
||||
|
||||
reply.Message(fmt.Sprintf("Base position of strategy %s updated.", it.updatePositionContext.signature))
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (it *CoreInteraction) Initialize() error {
|
||||
|
|
46
pkg/bbgo/positionupdater.go
Normal file
46
pkg/bbgo/positionupdater.go
Normal file
|
@ -0,0 +1,46 @@
|
|||
package bbgo
|
||||
|
||||
import (
|
||||
"github.com/c9s/bbgo/pkg/fixedpoint"
|
||||
"github.com/c9s/bbgo/pkg/types"
|
||||
)
|
||||
|
||||
//go:generate callbackgen -type PositionUpdater -interface
|
||||
type PositionUpdater struct {
|
||||
Position *types.Position
|
||||
|
||||
// Callbacks
|
||||
updateBaseCallbacks []func()
|
||||
updateQuoteCallbacks []func()
|
||||
updateAverageCostCallbacks []func()
|
||||
}
|
||||
|
||||
func (s *PositionUpdater) UpdateBase(qty float64) error {
|
||||
s.Position.Base = fixedpoint.NewFromFloat(qty)
|
||||
|
||||
s.EmitUpdateBase()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *PositionUpdater) UpdateQuote(qty float64) error {
|
||||
s.Position.Quote = fixedpoint.NewFromFloat(qty)
|
||||
|
||||
s.EmitUpdateQuote()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *PositionUpdater) UpdateAverageCost(price float64) error {
|
||||
s.Position.AverageCost = fixedpoint.NewFromFloat(price)
|
||||
|
||||
s.EmitUpdateAverageCost()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type StrategyPositionUpdater interface {
|
||||
UpdateBase(qty float64) error
|
||||
UpdateQuote(qty float64) error
|
||||
UpdateAverageCost(price float64) error
|
||||
}
|
43
pkg/bbgo/positionupdater_callbacks.go
Normal file
43
pkg/bbgo/positionupdater_callbacks.go
Normal file
|
@ -0,0 +1,43 @@
|
|||
// Code generated by "callbackgen -type PositionUpdater -interface"; DO NOT EDIT.
|
||||
|
||||
package bbgo
|
||||
|
||||
import ()
|
||||
|
||||
func (s *PositionUpdater) OnUpdateBase(cb func()) {
|
||||
s.updateBaseCallbacks = append(s.updateBaseCallbacks, cb)
|
||||
}
|
||||
|
||||
func (s *PositionUpdater) EmitUpdateBase() {
|
||||
for _, cb := range s.updateBaseCallbacks {
|
||||
cb()
|
||||
}
|
||||
}
|
||||
|
||||
func (s *PositionUpdater) OnUpdateQuote(cb func()) {
|
||||
s.updateQuoteCallbacks = append(s.updateQuoteCallbacks, cb)
|
||||
}
|
||||
|
||||
func (s *PositionUpdater) EmitUpdateQuote() {
|
||||
for _, cb := range s.updateQuoteCallbacks {
|
||||
cb()
|
||||
}
|
||||
}
|
||||
|
||||
func (s *PositionUpdater) OnUpdateAverageCost(cb func()) {
|
||||
s.updateAverageCostCallbacks = append(s.updateAverageCostCallbacks, cb)
|
||||
}
|
||||
|
||||
func (s *PositionUpdater) EmitUpdateAverageCost() {
|
||||
for _, cb := range s.updateAverageCostCallbacks {
|
||||
cb()
|
||||
}
|
||||
}
|
||||
|
||||
type PositionUpdaterEventHub interface {
|
||||
OnUpdateBase(cb func())
|
||||
|
||||
OnUpdateQuote(cb func())
|
||||
|
||||
OnUpdateAverageCost(cb func())
|
||||
}
|
|
@ -84,6 +84,9 @@ type Strategy struct {
|
|||
|
||||
// StrategyController
|
||||
bbgo.StrategyController
|
||||
|
||||
// PositionUpdater
|
||||
bbgo.PositionUpdater
|
||||
}
|
||||
|
||||
func (s *Strategy) ID() string {
|
||||
|
|
Loading…
Reference in New Issue
Block a user