mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-26 00:35:15 +00:00
feature: add stoploss from stopPrice
This commit is contained in:
parent
9c73aa4adb
commit
c6563aa9bd
|
@ -18,6 +18,7 @@ exchangeStrategies:
|
||||||
stoploss: 2%
|
stoploss: 2%
|
||||||
source: hl2
|
source: hl2
|
||||||
predictOffset: 8
|
predictOffset: 8
|
||||||
|
noStopPrice: true
|
||||||
#exits:
|
#exits:
|
||||||
#- roiStopLoss:
|
#- roiStopLoss:
|
||||||
# percentage: 0.8%
|
# percentage: 0.8%
|
||||||
|
|
|
@ -55,6 +55,9 @@ type Strategy struct {
|
||||||
Stoploss fixedpoint.Value `json:"stoploss"`
|
Stoploss fixedpoint.Value `json:"stoploss"`
|
||||||
CanvasPath string `json:"canvasPath"`
|
CanvasPath string `json:"canvasPath"`
|
||||||
PredictOffset int `json:"predictOffset"`
|
PredictOffset int `json:"predictOffset"`
|
||||||
|
NoStopPrice bool `json:"noStopPrice"`
|
||||||
|
|
||||||
|
StopOrders map[uint64]types.SubmitOrder
|
||||||
|
|
||||||
ExitMethods bbgo.ExitMethodSet `json:"exits"`
|
ExitMethods bbgo.ExitMethodSet `json:"exits"`
|
||||||
Session *bbgo.ExchangeSession
|
Session *bbgo.ExchangeSession
|
||||||
|
@ -75,6 +78,7 @@ func (s *Strategy) Print() {
|
||||||
hiyellow(os.Stderr, "symbol: %s\n", s.Symbol)
|
hiyellow(os.Stderr, "symbol: %s\n", s.Symbol)
|
||||||
hiyellow(os.Stderr, "interval: %s\n", s.Interval)
|
hiyellow(os.Stderr, "interval: %s\n", s.Interval)
|
||||||
hiyellow(os.Stderr, "window: %d\n", s.Window)
|
hiyellow(os.Stderr, "window: %d\n", s.Window)
|
||||||
|
hiyellow(os.Stderr, "noStopPrice: %v\n", s.NoStopPrice)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Strategy) ID() string {
|
func (s *Strategy) ID() string {
|
||||||
|
@ -195,6 +199,43 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
|
||||||
bbgo.Sync(s)
|
bbgo.Sync(s)
|
||||||
})
|
})
|
||||||
s.GeneralOrderExecutor.Bind()
|
s.GeneralOrderExecutor.Bind()
|
||||||
|
s.StopOrders = make(map[uint64]types.SubmitOrder)
|
||||||
|
session.UserDataStream.OnOrderUpdate(func(order types.Order) {
|
||||||
|
if len(s.StopOrders) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if order.Symbol != s.Symbol {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if order.Status == types.OrderStatusCanceled {
|
||||||
|
delete(s.StopOrders, order.OrderID)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if order.Status != types.OrderStatusFilled {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if o, ok := s.StopOrders[order.OrderID]; ok {
|
||||||
|
delete(s.StopOrders, order.OrderID)
|
||||||
|
if o.Side == types.SideTypeBuy {
|
||||||
|
quoteBalance, ok := s.Session.GetAccount().Balance(s.Market.QuoteCurrency)
|
||||||
|
if !ok {
|
||||||
|
log.Errorf("unable to get quoteCurrency")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
o.Quantity = quoteBalance.Available.Div(o.Price)
|
||||||
|
} else {
|
||||||
|
baseBalance, ok := s.Session.GetAccount().Balance(s.Market.BaseCurrency)
|
||||||
|
if !ok {
|
||||||
|
log.Errorf("unable to get baseCurrency")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
o.Quantity = baseBalance.Available
|
||||||
|
}
|
||||||
|
if _, err := s.GeneralOrderExecutor.SubmitOrders(ctx, o); err != nil {
|
||||||
|
log.WithError(err).Errorf("cannot send stop order: %v", order)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
for _, method := range s.ExitMethods {
|
for _, method := range s.ExitMethods {
|
||||||
method.Bind(session, s.GeneralOrderExecutor)
|
method.Bind(session, s.GeneralOrderExecutor)
|
||||||
}
|
}
|
||||||
|
@ -222,7 +263,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
|
||||||
s.atr.PushK(kline)
|
s.atr.PushK(kline)
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.Environment.IsBackTesting() {
|
if s.IsBackTesting() {
|
||||||
s.getLastPrice = func() fixedpoint.Value {
|
s.getLastPrice = func() fixedpoint.Value {
|
||||||
lastPrice, ok := s.Session.LastPrice(s.Symbol)
|
lastPrice, ok := s.Session.LastPrice(s.Symbol)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -308,6 +349,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
|
||||||
log.WithError(err).Errorf("cannot cancel orders")
|
log.WithError(err).Errorf("cannot cancel orders")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
s.StopOrders = make(map[uint64]types.SubmitOrder)
|
||||||
_, _ = s.ClosePosition(ctx)
|
_, _ = s.ClosePosition(ctx)
|
||||||
}
|
}
|
||||||
if shortCondition {
|
if shortCondition {
|
||||||
|
@ -315,6 +357,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
|
||||||
log.WithError(err).Errorf("cannot cancel orders")
|
log.WithError(err).Errorf("cannot cancel orders")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
s.StopOrders = make(map[uint64]types.SubmitOrder)
|
||||||
baseBalance, ok := s.Session.GetAccount().Balance(s.Market.BaseCurrency)
|
baseBalance, ok := s.Session.GetAccount().Balance(s.Market.BaseCurrency)
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Errorf("unable to get baseBalance")
|
log.Errorf("unable to get baseBalance")
|
||||||
|
@ -327,24 +370,42 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
|
||||||
if s.Market.IsDustQuantity(baseBalance.Available, source) {
|
if s.Market.IsDustQuantity(baseBalance.Available, source) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_, err := s.GeneralOrderExecutor.SubmitOrders(ctx, types.SubmitOrder{
|
quantity := baseBalance.Available
|
||||||
|
stopPrice := fixedpoint.NewFromFloat(math.Min(sourcef+atr/2, sourcef*(1.+stoploss)))
|
||||||
|
stopOrder := types.SubmitOrder{
|
||||||
Symbol: s.Symbol,
|
Symbol: s.Symbol,
|
||||||
Side: types.SideTypeSell,
|
Side: types.SideTypeBuy,
|
||||||
Type: types.OrderTypeLimitMaker,
|
Type: types.OrderTypeStopLimit,
|
||||||
Price: source,
|
StopPrice: stopPrice,
|
||||||
StopPrice: fixedpoint.NewFromFloat(math.Min(sourcef+atr/2, sourcef*(1.+stoploss))),
|
Price: stopPrice,
|
||||||
Quantity: baseBalance.Available,
|
Quantity: quantity,
|
||||||
|
}
|
||||||
|
createdOrders, err := s.GeneralOrderExecutor.SubmitOrders(ctx, types.SubmitOrder{
|
||||||
|
Symbol: s.Symbol,
|
||||||
|
Side: types.SideTypeSell,
|
||||||
|
Type: types.OrderTypeLimit,
|
||||||
|
Price: source,
|
||||||
|
Quantity: quantity,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).Errorf("cannot place sell order")
|
log.WithError(err).Errorf("cannot place sell order")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if s.NoStopPrice {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if createdOrders[0].Status == types.OrderStatusFilled {
|
||||||
|
s.GeneralOrderExecutor.SubmitOrders(ctx, stopOrder)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
s.StopOrders[createdOrders[0].OrderID] = stopOrder
|
||||||
}
|
}
|
||||||
if longCondition {
|
if longCondition {
|
||||||
if err := s.GeneralOrderExecutor.GracefulCancel(ctx); err != nil {
|
if err := s.GeneralOrderExecutor.GracefulCancel(ctx); err != nil {
|
||||||
log.WithError(err).Errorf("cannot cancel orders")
|
log.WithError(err).Errorf("cannot cancel orders")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
s.StopOrders = make(map[uint64]types.SubmitOrder)
|
||||||
if source.Compare(price) > 0 {
|
if source.Compare(price) > 0 {
|
||||||
source = price
|
source = price
|
||||||
}
|
}
|
||||||
|
@ -357,18 +418,36 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
|
||||||
quoteBalance.Available.Div(source), source) {
|
quoteBalance.Available.Div(source), source) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_, err := s.GeneralOrderExecutor.SubmitOrders(ctx, types.SubmitOrder{
|
quantity := quoteBalance.Available.Div(source)
|
||||||
Symbol: s.Symbol,
|
stopPrice := fixedpoint.NewFromFloat(math.Max(sourcef-atr/2, sourcef*(1.-stoploss)))
|
||||||
Side: types.SideTypeBuy,
|
stopOrder := types.SubmitOrder{
|
||||||
Type: types.OrderTypeLimitMaker,
|
Symbol: s.Symbol,
|
||||||
Price: source,
|
Side: types.SideTypeSell,
|
||||||
StopPrice: fixedpoint.NewFromFloat(math.Max(sourcef-atr/2, sourcef*(1.-stoploss))),
|
Type: types.OrderTypeStopLimit,
|
||||||
Quantity: quoteBalance.Available.Div(source),
|
TimeInForce: types.TimeInForceGTC,
|
||||||
|
StopPrice: stopPrice,
|
||||||
|
Price: stopPrice,
|
||||||
|
Quantity: quantity,
|
||||||
|
}
|
||||||
|
createdOrders, err := s.GeneralOrderExecutor.SubmitOrders(ctx, types.SubmitOrder{
|
||||||
|
Symbol: s.Symbol,
|
||||||
|
Side: types.SideTypeBuy,
|
||||||
|
Type: types.OrderTypeLimit,
|
||||||
|
Price: source,
|
||||||
|
Quantity: quantity,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).Errorf("cannot place buy order")
|
log.WithError(err).Errorf("cannot place buy order")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if s.NoStopPrice {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if createdOrders[0].Status == types.OrderStatusFilled {
|
||||||
|
s.GeneralOrderExecutor.SubmitOrders(ctx, stopOrder)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
s.StopOrders[createdOrders[0].OrderID] = stopOrder
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user