@@ -17,12 +17,16 @@ import (
17
17
indicatorv2 "github.com/c9s/bbgo/pkg/indicator/v2"
18
18
"github.com/c9s/bbgo/pkg/pricesolver"
19
19
"github.com/c9s/bbgo/pkg/risk/circuitbreaker"
20
+ "github.com/c9s/bbgo/pkg/strategy/common"
20
21
"github.com/c9s/bbgo/pkg/types"
21
22
"github.com/c9s/bbgo/pkg/util"
22
23
)
23
24
24
25
var defaultMargin = fixedpoint .NewFromFloat (0.003 )
25
- var Two = fixedpoint .NewFromInt (2 )
26
+ var two = fixedpoint .NewFromInt (2 )
27
+
28
+ var lastPriceModifier = fixedpoint .NewFromFloat (1.001 )
29
+ var minGap = fixedpoint .NewFromFloat (1.02 )
26
30
27
31
const priceUpdateTimeout = 30 * time .Second
28
32
@@ -89,6 +93,9 @@ type Strategy struct {
89
93
// Pips is the pips of the layer prices
90
94
Pips fixedpoint.Value `json:"pips"`
91
95
96
+ // ProfitFixerConfig is the profit fixer configuration
97
+ ProfitFixerConfig * common.ProfitFixerConfig `json:"profitFixer,omitempty"`
98
+
92
99
// --------------------------------
93
100
// private field
94
101
@@ -215,7 +222,7 @@ func (s *Strategy) updateQuote(ctx context.Context, orderExecutionRouter bbgo.Or
215
222
}
216
223
217
224
// use mid-price for the last price
218
- s .lastPrice = bestBid .Price .Add (bestAsk .Price ).Div (Two )
225
+ s .lastPrice = bestBid .Price .Add (bestAsk .Price ).Div (two )
219
226
220
227
s .priceSolver .Update (s .Symbol , s .lastPrice )
221
228
@@ -543,9 +550,6 @@ func (s *Strategy) updateQuote(ctx context.Context, orderExecutionRouter bbgo.Or
543
550
_ = createdOrders
544
551
}
545
552
546
- var lastPriceModifier = fixedpoint .NewFromFloat (1.001 )
547
- var minGap = fixedpoint .NewFromFloat (1.02 )
548
-
549
553
func (s * Strategy ) Hedge (ctx context.Context , pos fixedpoint.Value ) {
550
554
side := types .SideTypeBuy
551
555
if pos .IsZero () {
@@ -852,6 +856,47 @@ func (s *Strategy) CrossRun(
852
856
})
853
857
}
854
858
859
+ if s .ProfitFixerConfig != nil {
860
+ bbgo .Notify ("Fixing %s profitStats and position..." , s .Symbol )
861
+
862
+ log .Infof ("profitFixer is enabled, checking checkpoint: %+v" , s .ProfitFixerConfig .TradesSince )
863
+
864
+ if s .ProfitFixerConfig .TradesSince .Time ().IsZero () {
865
+ return errors .New ("tradesSince time can not be zero" )
866
+ }
867
+
868
+ makerMarket , _ := makerSession .Market (s .Symbol )
869
+ position := types .NewPositionFromMarket (makerMarket )
870
+ profitStats := types .NewProfitStats (makerMarket )
871
+
872
+ fixer := common .NewProfitFixer ()
873
+ // fixer.ConverterManager = s.ConverterManager
874
+
875
+ if ss , ok := makerSession .Exchange .(types.ExchangeTradeHistoryService ); ok {
876
+ log .Infof ("adding makerSession %s to profitFixer" , makerSession .Name )
877
+ fixer .AddExchange (makerSession .Name , ss )
878
+ }
879
+
880
+ if ss , ok := sourceSession .Exchange .(types.ExchangeTradeHistoryService ); ok {
881
+ log .Infof ("adding hedgeSession %s to profitFixer" , sourceSession .Name )
882
+ fixer .AddExchange (sourceSession .Name , ss )
883
+ }
884
+
885
+ if err2 := fixer .Fix (ctx , makerMarket .Symbol ,
886
+ s .ProfitFixerConfig .TradesSince .Time (),
887
+ time .Now (),
888
+ profitStats ,
889
+ position ); err2 != nil {
890
+ return err2
891
+ }
892
+
893
+ bbgo .Notify ("Fixed %s position" , s .Symbol , position )
894
+ bbgo .Notify ("Fixed %s profitStats" , s .Symbol , profitStats )
895
+
896
+ s .Position = position
897
+ s .ProfitStats .ProfitStats = profitStats
898
+ }
899
+
855
900
s .book = types .NewStreamBook (s .Symbol , s .sourceSession .ExchangeName )
856
901
s .book .BindStream (s .sourceSession .MarketDataStream )
857
902
0 commit comments