mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-13 02:23:51 +00:00
add unrealized profit
This commit is contained in:
parent
887400eb83
commit
1d6b69e406
25
bbgo/pnl.go
25
bbgo/pnl.go
|
@ -75,7 +75,7 @@ func (c *ProfitAndLossCalculator) Calculate() *ProfitAndLossReport {
|
||||||
|
|
||||||
log.Infof("average bid price = (total amount %f + total feeUSD %f) / volume %f", bidAmount, bidFeeUSD, bidVolume)
|
log.Infof("average bid price = (total amount %f + total feeUSD %f) / volume %f", bidAmount, bidFeeUSD, bidVolume)
|
||||||
profit := 0.0
|
profit := 0.0
|
||||||
averageBidPrice := (bidAmount + bidFeeUSD) / bidVolume
|
averageCost := (bidAmount + bidFeeUSD) / bidVolume
|
||||||
|
|
||||||
for _, t := range trades {
|
for _, t := range trades {
|
||||||
if t.Symbol != c.Symbol {
|
if t.Symbol != c.Symbol {
|
||||||
|
@ -86,18 +86,17 @@ func (c *ProfitAndLossCalculator) Calculate() *ProfitAndLossReport {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
profit += (t.Price - averageBidPrice) * t.Quantity
|
profit += (t.Price - averageCost) * t.Quantity
|
||||||
askVolume += t.Quantity
|
askVolume += t.Quantity
|
||||||
}
|
}
|
||||||
|
|
||||||
profit -= feeUSD
|
profit -= feeUSD
|
||||||
|
unrealizedProfit := profit
|
||||||
|
|
||||||
stock := bidVolume - askVolume
|
stock := bidVolume - askVolume
|
||||||
if stock > 0 {
|
if stock > 0 {
|
||||||
_ = feeRate
|
stockFee := c.CurrentPrice * stock * feeRate
|
||||||
// stockFee := c.CurrentPrice * feeRate * stock
|
unrealizedProfit += (c.CurrentPrice-averageCost)*stock - stockFee
|
||||||
// profit += (c.CurrentPrice-averageBidPrice)*stock - stockFee
|
|
||||||
// futureFee += stockFee
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return &ProfitAndLossReport{
|
return &ProfitAndLossReport{
|
||||||
|
@ -111,7 +110,8 @@ func (c *ProfitAndLossCalculator) Calculate() *ProfitAndLossReport {
|
||||||
|
|
||||||
Stock: stock,
|
Stock: stock,
|
||||||
Profit: profit,
|
Profit: profit,
|
||||||
AverageBidCost: averageBidPrice,
|
UnrealizedProfit: unrealizedProfit,
|
||||||
|
AverageBidCost: averageCost,
|
||||||
FeeUSD: feeUSD,
|
FeeUSD: feeUSD,
|
||||||
CurrencyFees: currencyFees,
|
CurrencyFees: currencyFees,
|
||||||
}
|
}
|
||||||
|
@ -124,6 +124,7 @@ type ProfitAndLossReport struct {
|
||||||
|
|
||||||
NumTrades int
|
NumTrades int
|
||||||
Profit float64
|
Profit float64
|
||||||
|
UnrealizedProfit float64
|
||||||
AverageBidCost float64
|
AverageBidCost float64
|
||||||
BidVolume float64
|
BidVolume float64
|
||||||
AskVolume float64
|
AskVolume float64
|
||||||
|
@ -141,6 +142,7 @@ func (report ProfitAndLossReport) Print() {
|
||||||
log.Infof("fee (USD): %f", report.FeeUSD)
|
log.Infof("fee (USD): %f", report.FeeUSD)
|
||||||
log.Infof("current price: %s", USD.FormatMoneyFloat64(report.CurrentPrice))
|
log.Infof("current price: %s", USD.FormatMoneyFloat64(report.CurrentPrice))
|
||||||
log.Infof("profit: %s", USD.FormatMoneyFloat64(report.Profit))
|
log.Infof("profit: %s", USD.FormatMoneyFloat64(report.Profit))
|
||||||
|
log.Infof("unrealized profit: %s", USD.FormatMoneyFloat64(report.UnrealizedProfit))
|
||||||
log.Infof("currency fees:")
|
log.Infof("currency fees:")
|
||||||
for currency, fee := range report.CurrencyFees {
|
for currency, fee := range report.CurrencyFees {
|
||||||
log.Infof(" - %s: %f", currency, fee)
|
log.Infof(" - %s: %f", currency, fee)
|
||||||
|
@ -160,8 +162,6 @@ func (report ProfitAndLossReport) SlackAttachment() slack.Attachment {
|
||||||
return slack.Attachment{}
|
return slack.Attachment{}
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = market
|
|
||||||
|
|
||||||
return slack.Attachment{
|
return slack.Attachment{
|
||||||
Title: "Profit and Loss report of " + report.Symbol,
|
Title: "Profit and Loss report of " + report.Symbol,
|
||||||
Color: color,
|
Color: color,
|
||||||
|
@ -170,11 +170,12 @@ func (report ProfitAndLossReport) SlackAttachment() slack.Attachment {
|
||||||
Fields: []slack.AttachmentField{
|
Fields: []slack.AttachmentField{
|
||||||
{Title: "Symbol", Value: report.Symbol, Short: true},
|
{Title: "Symbol", Value: report.Symbol, Short: true},
|
||||||
{Title: "Profit", Value: USD.FormatMoney(report.Profit), Short: true},
|
{Title: "Profit", Value: USD.FormatMoney(report.Profit), Short: true},
|
||||||
|
{Title: "Unrealized Profit", Value: USD.FormatMoney(report.UnrealizedProfit), Short: true},
|
||||||
|
{Title: "Current Price", Value: market.FormatPrice(report.CurrentPrice), Short: true},
|
||||||
|
{Title: "Average Cost", Value: market.FormatPrice(report.AverageBidCost), Short: true},
|
||||||
{Title: "Fee (USD)", Value: USD.FormatMoney(report.FeeUSD), Short: true},
|
{Title: "Fee (USD)", Value: USD.FormatMoney(report.FeeUSD), Short: true},
|
||||||
{Title: "Current Price", Value: USD.FormatMoney(report.CurrentPrice), Short: true},
|
|
||||||
{Title: "Average Bid Cost", Value: USD.FormatMoney(report.AverageBidCost), Short: true},
|
|
||||||
{Title: "Number of Trades", Value: strconv.Itoa(report.NumTrades), Short: true},
|
|
||||||
{Title: "Stock", Value: strconv.FormatFloat(report.Stock, 'f', 8, 64), Short: true},
|
{Title: "Stock", Value: strconv.FormatFloat(report.Stock, 'f', 8, 64), Short: true},
|
||||||
|
{Title: "Number of Trades", Value: strconv.Itoa(report.NumTrades), Short: true},
|
||||||
},
|
},
|
||||||
Footer: report.StartTime.Format(time.RFC822),
|
Footer: report.StartTime.Format(time.RFC822),
|
||||||
FooterIcon: "",
|
FooterIcon: "",
|
||||||
|
|
Loading…
Reference in New Issue
Block a user