-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Round limit-price to avoid sub-penny issue
Fixes #260
- Loading branch information
Showing
4 changed files
with
98 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package alpaca | ||
|
||
import ( | ||
"github.com/shopspring/decimal" | ||
) | ||
|
||
// RoundLimitPrice calculates the limit price that respects the minimum price variance rule. | ||
// | ||
// Orders received in excess of the minimum price variance will be rejected. | ||
// | ||
// Limit price >= $1.00: Max Decimals = 2 | ||
// Limit price < $1.00: Max Decimals = 4 | ||
// | ||
// https://docs.alpaca.markets/docs/orders-at-alpaca#sub-penny-increments-for-limit-orders | ||
func RoundLimitPrice(price float64, side Side) *decimal.Decimal { | ||
maxDecimals := int32(2) | ||
if price < 1 { | ||
maxDecimals = 4 | ||
} | ||
limitPrice := decimal.NewFromFloat(price) | ||
switch side { | ||
case Buy: | ||
limitPrice = limitPrice.RoundCeil(maxDecimals) | ||
case Sell: | ||
limitPrice = limitPrice.RoundFloor(maxDecimals) | ||
} | ||
return &limitPrice | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package alpaca | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/shopspring/decimal" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_calculateLimitPrice(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
price float64 | ||
side Side | ||
exp decimal.Decimal | ||
}{ | ||
{ | ||
name: "buy expensive", | ||
price: 41.085, | ||
side: Buy, | ||
exp: decimal.RequireFromString("41.09"), | ||
}, | ||
{ | ||
name: "buy cheap no rounding", | ||
price: 0.9999, | ||
side: Buy, | ||
exp: decimal.RequireFromString("0.9999"), | ||
}, | ||
{ | ||
name: "buy cheap rounding", | ||
price: 0.12182, | ||
side: Buy, | ||
exp: decimal.RequireFromString("0.1219"), | ||
}, | ||
{ | ||
name: "sell expensive", | ||
price: 41.085, | ||
side: Sell, | ||
exp: decimal.RequireFromString("41.08"), | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := RoundLimitPrice(tt.price, tt.side) | ||
assert.True(t, tt.exp.Equal(*got), "expected: %s, got: %s", tt.exp.String(), got.String()) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters