From 6285e145a7bbb91bf976e315224d630ac071ac76 Mon Sep 17 00:00:00 2001 From: Andy Cheng Date: Tue, 31 May 2022 15:46:55 +0800 Subject: [PATCH] strategy: margin side effect --- config/supertrend.yaml | 3 +++ doc/README.md | 1 + doc/strategy/supertrend.md | 3 ++- pkg/strategy/supertrend/strategy.go | 19 +++++++++---------- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/config/supertrend.yaml b/config/supertrend.yaml index 6bacf46b8..f9222df50 100644 --- a/config/supertrend.yaml +++ b/config/supertrend.yaml @@ -9,6 +9,9 @@ sessions: binance: exchange: binance envVarPrefix: binance + margin: true + isolatedMargin: true + isolatedMarginSymbol: BTCUSDT backtest: sessions: [binance] diff --git a/doc/README.md b/doc/README.md index bac675a10..ade7d4089 100644 --- a/doc/README.md +++ b/doc/README.md @@ -22,6 +22,7 @@ * [Grid](strategy/grid.md) - Grid Strategy Explanation * [Interaction](strategy/interaction.md) - Interaction registration for strategies * [Price Alert](strategy/pricealert.md) - Send price alert notification on price changes +* [Supertrend](strategy/supertrend.md) - Supertrend strategy uses Supertrend indicator as trend, and DEMA indicator as noise filter * [Support](strategy/support.md) - Support strategy that buys on high volume support ### Development diff --git a/doc/strategy/supertrend.md b/doc/strategy/supertrend.md index 507a87b1f..01421c94f 100644 --- a/doc/strategy/supertrend.md +++ b/doc/strategy/supertrend.md @@ -1,7 +1,8 @@ ### Supertrend Strategy This strategy uses Supertrend indicator as trend, and DEMA indicator as noise filter. -This strategy needs margin enabled in order to submit short orders, but you can use `leverage` parameter to limit your risk. +Supertrend strategy needs margin enabled in order to submit short orders, and you can use `leverage` parameter to limit your risk. +**Please note, using leverage higher than 1 is highly risky.** #### Parameters diff --git a/pkg/strategy/supertrend/strategy.go b/pkg/strategy/supertrend/strategy.go index 60da26c59..2c7dc07cf 100644 --- a/pkg/strategy/supertrend/strategy.go +++ b/pkg/strategy/supertrend/strategy.go @@ -13,8 +13,6 @@ import ( "sync" ) -// TODO: Margin side effect - const ID = "supertrend" const stateKey = "state-v1" @@ -210,7 +208,7 @@ func (s *Strategy) ClosePosition(ctx context.Context, percentage fixedpoint.Valu return fmt.Errorf("order quantity %v is too small, less than %v", quantity, s.Market.MinQuantity) } - orderForm := s.GenerateOrderForm(side, quantity) + orderForm := s.GenerateOrderForm(side, quantity, types.SideEffectTypeAutoRepay) s.Notify("Submitting %s %s order to close position by %v", s.Symbol, side.String(), percentage, orderForm) @@ -264,13 +262,14 @@ func (s *Strategy) UpdateIndicators(kline types.KLine) { } } -func (s *Strategy) GenerateOrderForm(side types.SideType, quantity fixedpoint.Value) types.SubmitOrder { +func (s *Strategy) GenerateOrderForm(side types.SideType, quantity fixedpoint.Value, marginOrderSideEffect types.MarginOrderSideEffectType) types.SubmitOrder { orderForm := types.SubmitOrder{ - Symbol: s.Symbol, - Market: s.Market, - Side: side, - Type: types.OrderTypeMarket, - Quantity: quantity, + Symbol: s.Symbol, + Market: s.Market, + Side: side, + Type: types.OrderTypeMarket, + Quantity: quantity, + MarginSideEffect: marginOrderSideEffect, } return orderForm @@ -432,7 +431,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.session.GetAccount().UpdateBalances(balances) } - orderForm := s.GenerateOrderForm(side, s.CalculateQuantity(kline.GetClose())) + orderForm := s.GenerateOrderForm(side, s.CalculateQuantity(kline.GetClose()), types.SideEffectTypeMarginBuy) log.Infof("submit open position order %v", orderForm) order, err := orderExecutor.SubmitOrders(ctx, orderForm) if err != nil {