mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-30 15:10:40 +00:00
backtester: Fix ensureOrderFitsWithinHLV (#1338)
Co-authored-by: xiaoniu <yang.ruoqi@outlook.com>
This commit is contained in:
@@ -439,20 +439,16 @@ func ensureOrderFitsWithinHLV(price, amount, high, low, volume decimal.Decimal)
|
||||
if adjustedPrice.GreaterThan(high) {
|
||||
adjustedPrice = high
|
||||
}
|
||||
orderVolume := amount.Mul(adjustedPrice)
|
||||
if volume.LessThanOrEqual(decimal.Zero) || orderVolume.LessThanOrEqual(volume) {
|
||||
if volume.LessThanOrEqual(decimal.Zero) || amount.LessThanOrEqual(volume) {
|
||||
return adjustedPrice, amount
|
||||
}
|
||||
if orderVolume.GreaterThan(volume) {
|
||||
if amount.GreaterThan(volume) {
|
||||
// reduce the volume to not exceed the total volume of the candle
|
||||
// it is slightly less than the total to still allow for the illusion
|
||||
// that open high low close values are valid with the remaining volume
|
||||
// this is very opinionated
|
||||
orderVolume = volume.Mul(decimal.NewFromFloat(0.99999999))
|
||||
adjustedAmount = volume.Mul(decimal.NewFromFloat(0.99999999))
|
||||
}
|
||||
// extract the amount from the adjusted volume
|
||||
adjustedAmount = orderVolume.Div(adjustedPrice)
|
||||
|
||||
return adjustedPrice, adjustedAmount
|
||||
}
|
||||
|
||||
|
||||
@@ -152,7 +152,7 @@ func TestSetCurrency(t *testing.T) {
|
||||
|
||||
func TestEnsureOrderFitsWithinHLV(t *testing.T) {
|
||||
t.Parallel()
|
||||
adjustedPrice, adjustedAmount := ensureOrderFitsWithinHLV(decimal.NewFromInt(123), decimal.NewFromInt(1), decimal.NewFromInt(100), decimal.NewFromInt(99), decimal.NewFromInt(100))
|
||||
adjustedPrice, adjustedAmount := ensureOrderFitsWithinHLV(decimal.NewFromInt(123), decimal.NewFromInt(1), decimal.NewFromInt(100), decimal.NewFromInt(99), decimal.NewFromInt(10))
|
||||
if !adjustedAmount.Equal(decimal.NewFromInt(1)) {
|
||||
t.Error("expected 1")
|
||||
}
|
||||
@@ -160,7 +160,7 @@ func TestEnsureOrderFitsWithinHLV(t *testing.T) {
|
||||
t.Error("expected 100")
|
||||
}
|
||||
|
||||
adjustedPrice, adjustedAmount = ensureOrderFitsWithinHLV(decimal.NewFromInt(123), decimal.NewFromInt(1), decimal.NewFromInt(100), decimal.NewFromInt(99), decimal.NewFromInt(80))
|
||||
adjustedPrice, adjustedAmount = ensureOrderFitsWithinHLV(decimal.NewFromInt(123), decimal.NewFromInt(1), decimal.NewFromInt(100), decimal.NewFromInt(99), decimal.NewFromFloat(0.8))
|
||||
if !adjustedAmount.Equal(decimal.NewFromFloat(0.799999992)) {
|
||||
t.Errorf("received: %v, expected: %v", adjustedAmount, decimal.NewFromFloat(0.799999992))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user