mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
types: add position openedAt time field
This commit is contained in:
parent
9f06be14aa
commit
56bfa22dbe
|
@ -50,6 +50,7 @@ type Position struct {
|
||||||
// TotalFee stores the fee currency -> total fee quantity
|
// TotalFee stores the fee currency -> total fee quantity
|
||||||
TotalFee map[string]fixedpoint.Value `json:"totalFee" db:"-"`
|
TotalFee map[string]fixedpoint.Value `json:"totalFee" db:"-"`
|
||||||
|
|
||||||
|
OpenedAt time.Time `json:"openedAt,omitempty" db:"-"`
|
||||||
ChangedAt time.Time `json:"changedAt,omitempty" db:"changed_at"`
|
ChangedAt time.Time `json:"changedAt,omitempty" db:"changed_at"`
|
||||||
|
|
||||||
Strategy string `json:"strategy,omitempty" db:"strategy"`
|
Strategy string `json:"strategy,omitempty" db:"strategy"`
|
||||||
|
@ -447,6 +448,7 @@ func (p *Position) AddTrade(td Trade) (profit fixedpoint.Value, netProfit fixedp
|
||||||
switch td.Side {
|
switch td.Side {
|
||||||
|
|
||||||
case SideTypeBuy:
|
case SideTypeBuy:
|
||||||
|
// was short position, now trade buy should cover the position
|
||||||
if p.Base.Sign() < 0 {
|
if p.Base.Sign() < 0 {
|
||||||
// convert short position to long position
|
// convert short position to long position
|
||||||
if p.Base.Add(quantity).Sign() > 0 {
|
if p.Base.Add(quantity).Sign() > 0 {
|
||||||
|
@ -457,9 +459,10 @@ func (p *Position) AddTrade(td Trade) (profit fixedpoint.Value, netProfit fixedp
|
||||||
p.AverageCost = price
|
p.AverageCost = price
|
||||||
p.ApproximateAverageCost = price
|
p.ApproximateAverageCost = price
|
||||||
p.AccumulatedProfit = p.AccumulatedProfit.Add(profit)
|
p.AccumulatedProfit = p.AccumulatedProfit.Add(profit)
|
||||||
|
p.OpenedAt = td.Time.Time()
|
||||||
return profit, netProfit, true
|
return profit, netProfit, true
|
||||||
} else {
|
} else {
|
||||||
// covering short position
|
// after adding quantity it's still short position
|
||||||
p.Base = p.Base.Add(quantity)
|
p.Base = p.Base.Add(quantity)
|
||||||
p.Quote = p.Quote.Sub(quoteQuantity)
|
p.Quote = p.Quote.Sub(quoteQuantity)
|
||||||
profit = p.AverageCost.Sub(price).Mul(quantity)
|
profit = p.AverageCost.Sub(price).Mul(quantity)
|
||||||
|
@ -469,6 +472,7 @@ func (p *Position) AddTrade(td Trade) (profit fixedpoint.Value, netProfit fixedp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// here the case is: base == 0 or base > 0
|
||||||
divisor := p.Base.Add(quantity)
|
divisor := p.Base.Add(quantity)
|
||||||
p.ApproximateAverageCost = p.ApproximateAverageCost.Mul(p.Base).
|
p.ApproximateAverageCost = p.ApproximateAverageCost.Mul(p.Base).
|
||||||
Add(quoteQuantity).
|
Add(quoteQuantity).
|
||||||
|
@ -481,6 +485,7 @@ func (p *Position) AddTrade(td Trade) (profit fixedpoint.Value, netProfit fixedp
|
||||||
return fixedpoint.Zero, fixedpoint.Zero, false
|
return fixedpoint.Zero, fixedpoint.Zero, false
|
||||||
|
|
||||||
case SideTypeSell:
|
case SideTypeSell:
|
||||||
|
// was long position, the sell trade should reduce the base amount
|
||||||
if p.Base.Sign() > 0 {
|
if p.Base.Sign() > 0 {
|
||||||
// convert long position to short position
|
// convert long position to short position
|
||||||
if p.Base.Compare(quantity) < 0 {
|
if p.Base.Compare(quantity) < 0 {
|
||||||
|
@ -491,6 +496,7 @@ func (p *Position) AddTrade(td Trade) (profit fixedpoint.Value, netProfit fixedp
|
||||||
p.AverageCost = price
|
p.AverageCost = price
|
||||||
p.ApproximateAverageCost = price
|
p.ApproximateAverageCost = price
|
||||||
p.AccumulatedProfit = p.AccumulatedProfit.Add(profit)
|
p.AccumulatedProfit = p.AccumulatedProfit.Add(profit)
|
||||||
|
p.OpenedAt = td.Time.Time()
|
||||||
return profit, netProfit, true
|
return profit, netProfit, true
|
||||||
} else {
|
} else {
|
||||||
p.Base = p.Base.Sub(quantity)
|
p.Base = p.Base.Sub(quantity)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user