improve hot reload

This commit is contained in:
c9s 2020-09-07 14:33:58 +08:00
parent ec839d0dc9
commit 3e573ebb10
3 changed files with 19 additions and 13 deletions

View File

@ -42,7 +42,10 @@ func (store *KLineStore) AddKLine(kline types.KLine) {
var window = store.Windows[interval]
window.Add(kline)
if kline.GetMaxChange() > store.MaxChanges[interval].GetMaxChange() {
store.MaxChanges[interval] = kline
if _, ok := store.MaxChanges[interval] ; ok {
if kline.GetMaxChange() > store.MaxChanges[interval].GetMaxChange() {
store.MaxChanges[interval] = kline
}
}
}

View File

@ -41,17 +41,20 @@ type Trader struct {
func NewTrader(db *sqlx.DB, exchange *binance.Exchange, symbol string) *Trader {
tradeService := &service.TradeService{DB: db}
tradeSync := &service.TradeSync{Service: tradeService, Exchange: exchange}
return &Trader{
Symbol: symbol,
Exchange: exchange,
TradeService: tradeService,
TradeSync: tradeSync,
TradeSync: &service.TradeSync{
Service: tradeService,
Exchange: exchange,
},
}
}
func (trader *Trader) Initialize(ctx context.Context, startTime time.Time) error {
log.Info("syncing trades...")
log.Info("syncing trades from exchange...")
if err := trader.TradeSync.Sync(ctx, trader.Symbol, startTime); err != nil {
return err
}
@ -154,7 +157,7 @@ func (trader *Trader) RunStrategy(ctx context.Context, strategy Strategy) (chan
}
trader.reportTimer = time.AfterFunc(1*time.Second, func() {
trader.ReportPnL()
trader.reportPnL()
})
stream.OnTrade(func(trade *types.Trade) {
@ -166,7 +169,7 @@ func (trader *Trader) RunStrategy(ctx context.Context, strategy Strategy) (chan
log.WithError(err).Error("trade insert error")
}
trader.ReportTrade(trade)
trader.Notifier.ReportTrade(trade)
trader.ProfitAndLossCalculator.AddTrade(*trade)
_, err := trader.Context.StockManager.AddTrades([]types.Trade{*trade})
if err != nil {
@ -178,7 +181,7 @@ func (trader *Trader) RunStrategy(ctx context.Context, strategy Strategy) (chan
}
trader.reportTimer = time.AfterFunc(1*time.Minute, func() {
trader.ReportPnL()
trader.reportPnL()
})
})
@ -214,11 +217,7 @@ func (trader *Trader) RunStrategy(ctx context.Context, strategy Strategy) (chan
return done, nil
}
func (trader *Trader) ReportTrade(trade *types.Trade) {
trader.Notifier.ReportTrade(trade)
}
func (trader *Trader) ReportPnL() {
func (trader *Trader) reportPnL() {
report := trader.ProfitAndLossCalculator.Calculate()
report.Print()
trader.Notifier.ReportPnL(report)

View File

@ -23,6 +23,10 @@ func FormatFloat(val float64, prec int) string {
}
func MustParseFloat(s string) float64 {
if len(s) == 0 {
return 0.0
}
v, err := strconv.ParseFloat(s, 64)
if err != nil {
panic(err)