mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
simplify the calculator api
This commit is contained in:
parent
a6b99f6828
commit
63ea81b648
|
@ -115,9 +115,8 @@ var pnlCmd = &cobra.Command{
|
|||
Symbol: symbol,
|
||||
StartTime: startTime,
|
||||
CurrentPrice: currentPrice,
|
||||
Trades: trades,
|
||||
}
|
||||
report := calculator.Calculate()
|
||||
report := calculator.Calculate(trades)
|
||||
report.Print()
|
||||
return nil
|
||||
},
|
||||
|
|
|
@ -13,22 +13,11 @@ import (
|
|||
type AverageCostCalculator struct {
|
||||
Symbol string
|
||||
StartTime time.Time
|
||||
CurrentPrice float64
|
||||
Trades []types.Trade
|
||||
TradingFeeCurrency string
|
||||
}
|
||||
|
||||
func (c *AverageCostCalculator) AddTrade(trade types.Trade) {
|
||||
c.Trades = append(c.Trades, trade)
|
||||
}
|
||||
|
||||
func (c *AverageCostCalculator) SetCurrentPrice(price float64) {
|
||||
c.CurrentPrice = price
|
||||
}
|
||||
|
||||
func (c *AverageCostCalculator) Calculate() *accounting.ProfitAndLossReport {
|
||||
func (c *AverageCostCalculator) Calculate(trades []types.Trade, currentPrice float64) *accounting.ProfitAndLossReport {
|
||||
// copy trades, so that we can truncate it.
|
||||
var trades = c.Trades
|
||||
var bidVolume = 0.0
|
||||
var bidAmount = 0.0
|
||||
|
||||
|
@ -95,14 +84,14 @@ func (c *AverageCostCalculator) Calculate() *accounting.ProfitAndLossReport {
|
|||
|
||||
stock := bidVolume - askVolume
|
||||
if stock > 0 {
|
||||
stockFee := c.CurrentPrice * stock * feeRate
|
||||
unrealizedProfit += (c.CurrentPrice-averageCost)*stock - stockFee
|
||||
stockFee := currentPrice * stock * feeRate
|
||||
unrealizedProfit += (currentPrice-averageCost)*stock - stockFee
|
||||
}
|
||||
|
||||
return &accounting.ProfitAndLossReport{
|
||||
Symbol: c.Symbol,
|
||||
StartTime: c.StartTime,
|
||||
CurrentPrice: c.CurrentPrice,
|
||||
CurrentPrice: currentPrice,
|
||||
NumTrades: len(trades),
|
||||
|
||||
BidVolume: bidVolume,
|
||||
|
|
Loading…
Reference in New Issue
Block a user