Skip to content

Commit

Permalink
Merge pull request c9s#899 from c9s/fix/local-timezone
Browse files Browse the repository at this point in the history
fix: fix localtime zone issue for the web-based backtest report
  • Loading branch information
c9s authored Aug 29, 2022
2 parents 6314a31 + 179a9b1 commit 251d1b7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
1 change: 1 addition & 0 deletions pkg/bbgo/exit_roi_stop_loss.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func (s *RoiStopLoss) checkStopPrice(closePrice fixedpoint.Value, position *type
}

roi := position.ROI(closePrice)
// logrus.Debugf("ROIStopLoss: price=%f roi=%s stop=%s", closePrice.Float64(), roi.Percentage(), s.Percentage.Neg().Percentage())
if roi.Compare(s.Percentage.Neg()) < 0 {
// stop loss
Notify("[RoiStopLoss] %s stop loss triggered by ROI %s/%s, price: %f", position.Symbol, roi.Percentage(), s.Percentage.Neg().Percentage(), closePrice.Float64())
Expand Down
12 changes: 9 additions & 3 deletions pkg/cmd/backtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,11 @@ var BacktestCmd = &cobra.Command{
endTime = now
}

log.Infof("starting backtest with startTime %s", startTime.Format(time.ANSIC))
// ensure that we're using local time
startTime = startTime.Local()
endTime = endTime.Local()

log.Infof("starting backtest with startTime %s", startTime.Format(time.RFC3339))

environ := bbgo.NewEnvironment()
if err := BootstrapBacktestEnvironment(ctx, environ); err != nil {
Expand Down Expand Up @@ -200,6 +204,8 @@ var BacktestCmd = &cobra.Command{
if syncFromTime.After(startTime) {
return fmt.Errorf("sync-from time %s can not be latter than the backtest start time %s", syncFromTime, startTime)
}

syncFromTime = syncFromTime.Local()
} else {
// we need at least 1 month backward data for EMA and last prices
syncFromTime = startTime.AddDate(0, -1, 0)
Expand All @@ -208,13 +214,13 @@ var BacktestCmd = &cobra.Command{

if wantSync {
log.Infof("starting synchronization: %v", userConfig.Backtest.Symbols)
if err := sync(ctx, userConfig, backtestService, sourceExchanges, syncFromTime.Local(), endTime.Local()); err != nil {
if err := sync(ctx, userConfig, backtestService, sourceExchanges, syncFromTime, endTime); err != nil {
return err
}
log.Info("synchronization done")

if shouldVerify {
err := verify(userConfig, backtestService, sourceExchanges, syncFromTime.Local(), endTime.Local())
err := verify(userConfig, backtestService, sourceExchanges, syncFromTime, endTime)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/types/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,8 @@ func (o Order) CsvRecords() [][]string {
string(o.Status),
o.Price.String(),
o.Quantity.String(),
o.CreationTime.Time().Format(time.RFC1123),
o.UpdateTime.Time().Format(time.RFC1123),
o.CreationTime.Time().Local().Format(time.RFC1123),
o.UpdateTime.Time().Local().Format(time.RFC1123),
o.Tag,
},
}
Expand Down

0 comments on commit 251d1b7

Please sign in to comment.