|
1 | 1 | {-# OPTIONS_HADDOCK show-extensions #-}
|
2 | 2 |
|
3 | 3 | module Bfx.Math
|
4 |
| - ( tweakMoneyAmount, |
| 4 | + ( roundMoneyAmount, |
| 5 | + roundQuotePerBase, |
| 6 | + tweakMoneyAmount, |
5 | 7 | tweakQuotePerBase,
|
6 |
| - mkCounterOrder, |
7 | 8 | CounterArgs (..),
|
8 | 9 | CounterRates (..),
|
9 | 10 | CounterExit (..),
|
10 |
| - roundMoneyAmount, |
11 |
| - roundQuotePerBase, |
| 11 | + mkCounterOrder, |
12 | 12 | )
|
13 | 13 | where
|
14 | 14 |
|
15 | 15 | import Data.Ratio.Rounding (dpRound, sdRound)
|
16 | 16 | import Functora.Money
|
17 | 17 | import Functora.Prelude
|
18 | 18 |
|
| 19 | +roundMoneyAmount :: (MonadThrow m) => MoneyAmount -> m MoneyAmount |
| 20 | +roundMoneyAmount arg@(MoneyAmount raw) = |
| 21 | + if raw >= 0 && rounded >= 0 |
| 22 | + then pure $ MoneyAmount rounded |
| 23 | + else throwString $ "Rounding error for " <> inspect @String arg |
| 24 | + where |
| 25 | + rounded = |
| 26 | + unsafeFrom @Rational @(Ratio Natural) |
| 27 | + . roundMoneyAmountRat |
| 28 | + $ from @(Ratio Natural) @Rational raw |
| 29 | + |
| 30 | +roundQuotePerBase :: (MonadThrow m) => QuotePerBase -> m QuotePerBase |
| 31 | +roundQuotePerBase arg@(QuotePerBase raw) = |
| 32 | + if raw > 0 && rounded > 0 |
| 33 | + then pure $ QuotePerBase rounded |
| 34 | + else throwString $ "Rounding error for " <> inspect @String arg |
| 35 | + where |
| 36 | + rounded = |
| 37 | + unsafeFrom @Rational @(Ratio Natural) |
| 38 | + . roundQuotePerBaseRat |
| 39 | + $ from @(Ratio Natural) @Rational raw |
| 40 | + |
| 41 | +roundMoneyAmountRat :: Rational -> Rational |
| 42 | +roundMoneyAmountRat = dpRound 8 |
| 43 | + |
| 44 | +roundQuotePerBaseRat :: Rational -> Rational |
| 45 | +roundQuotePerBaseRat = sdRound 5 . dpRound 8 |
| 46 | + |
19 | 47 | tweakMoneyAmount ::
|
20 | 48 | ( MonadThrow m
|
21 | 49 | ) =>
|
@@ -188,31 +216,3 @@ mkCounterOrder args = do
|
188 | 216 | feeRate = counterRatesFee $ counterArgsRates args
|
189 | 217 | profitRate :: ProfitRate
|
190 | 218 | profitRate = counterRatesProfit $ counterArgsRates args
|
191 |
| - |
192 |
| -roundMoneyAmount :: (MonadThrow m) => MoneyAmount -> m MoneyAmount |
193 |
| -roundMoneyAmount arg@(MoneyAmount raw) = |
194 |
| - if raw >= 0 && rounded >= 0 |
195 |
| - then pure $ MoneyAmount rounded |
196 |
| - else throwString $ "Rounding error for " <> inspect @String arg |
197 |
| - where |
198 |
| - rounded = |
199 |
| - unsafeFrom @Rational @(Ratio Natural) |
200 |
| - . roundMoneyAmountRat |
201 |
| - $ from @(Ratio Natural) @Rational raw |
202 |
| - |
203 |
| -roundQuotePerBase :: (MonadThrow m) => QuotePerBase -> m QuotePerBase |
204 |
| -roundQuotePerBase arg@(QuotePerBase raw) = |
205 |
| - if raw > 0 && rounded > 0 |
206 |
| - then pure $ QuotePerBase rounded |
207 |
| - else throwString $ "Rounding error for " <> inspect @String arg |
208 |
| - where |
209 |
| - rounded = |
210 |
| - unsafeFrom @Rational @(Ratio Natural) |
211 |
| - . roundQuotePerBaseRat |
212 |
| - $ from @(Ratio Natural) @Rational raw |
213 |
| - |
214 |
| -roundMoneyAmountRat :: Rational -> Rational |
215 |
| -roundMoneyAmountRat = dpRound 8 |
216 |
| - |
217 |
| -roundQuotePerBaseRat :: Rational -> Rational |
218 |
| -roundQuotePerBaseRat = sdRound 5 . dpRound 8 |
0 commit comments