Skip to content

Commit 7db30a6

Browse files
committed
MathSpec wip
1 parent 4b52d12 commit 7db30a6

File tree

4 files changed

+54
-20
lines changed

4 files changed

+54
-20
lines changed

pub/bfx/bfx.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ test-suite bfx-test
153153
Bfx.Data.CancelOrderMultiSpec
154154
Bfx.Data.SubmitOrderSpec
155155
Bfx.Data.TypeSpec
156+
Bfx.MathSpec
156157
Bfx.TestEnv
157158
BfxSpec
158159
Paths_bfx

pub/bfx/src/Bfx.hs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ import Bfx.Import.Internal as X
4242
import Bfx.Indicator.Atr as X
4343
import Bfx.Indicator.Ma as X
4444
import Bfx.Indicator.Tr as X
45-
import qualified Bfx.Math as Math
4645
import qualified Bfx.Rpc.Generic as Generic
4746
import qualified Data.Map as Map
4847
import qualified Data.Set as Set
@@ -261,7 +260,7 @@ submitOrderMakerRec attempt env req = do
261260
then pure order
262261
else do
263262
when (attempt >= 10) . throw $ ErrorRemoteOrderState order
264-
next <- Math.tweakMakerRate (req ^. #buyOrSell) (req ^. #rate)
263+
next <- tweakQuotePerBase (req ^. #buyOrSell) (req ^. #rate)
265264
submitOrderMakerRec (attempt + 1) env $ req & #rate .~ next
266265

267266
cancelOrderMulti ::
@@ -369,8 +368,8 @@ mkSubmitCounterOrder submit env id0 rates opts = do
369368
counterArgsRates =
370369
rates
371370
}
372-
let exitAmt = Math.counterExitNetBaseLoss counter
373-
let exitRate = Math.counterExitQuotePerBase counter
371+
let exitAmt = counterExitNetBaseLoss counter
372+
let exitRate = counterExitQuotePerBase counter
374373
currentRate <-
375374
marketAveragePrice
376375
MarketAveragePrice.Request
@@ -422,7 +421,7 @@ mkDumpIntoQuote submit env sym opts = do
422421
}
423422
catchAny (mkSubmit amt)
424423
. const
425-
$ Math.tweakMoneyPip Sell amt
424+
$ tweakMoneyAmount Sell amt
426425
>>= mkSubmit
427426

428427
dumpIntoQuote ::

pub/bfx/src/Bfx/Math.hs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{-# OPTIONS_HADDOCK show-extensions #-}
22

33
module Bfx.Math
4-
( tweakMoneyPip,
5-
tweakMakerRate,
4+
( tweakMoneyAmount,
5+
tweakQuotePerBase,
66
newCounterOrder,
77
CounterArgs (..),
88
CounterRates (..),
@@ -14,23 +14,23 @@ where
1414

1515
import Bfx.Import.External
1616

17-
tweakMoneyPip ::
17+
tweakMoneyAmount ::
1818
( MonadThrow m
1919
) =>
2020
BuyOrSell ->
2121
MoneyAmount ->
2222
m MoneyAmount
23-
tweakMoneyPip =
24-
tweakMoneyPipRec pip
23+
tweakMoneyAmount =
24+
tweakMoneyAmountRec pip
2525

26-
tweakMoneyPipRec ::
26+
tweakMoneyAmountRec ::
2727
( MonadThrow m
2828
) =>
2929
Ratio Natural ->
3030
BuyOrSell ->
3131
MoneyAmount ->
3232
m MoneyAmount
33-
tweakMoneyPipRec tweak bos prev = do
33+
tweakMoneyAmountRec tweak bos prev = do
3434
next <-
3535
roundMoneyAmount
3636
. MoneyAmount
@@ -39,33 +39,33 @@ tweakMoneyPipRec tweak bos prev = do
3939
Sell -> unMoneyAmount prev - tweak
4040
if next /= prev
4141
then pure next
42-
else tweakMoneyPipRec (tweak + pip) bos prev
42+
else tweakMoneyAmountRec (tweak + pip) bos prev
4343

44-
tweakMakerRate ::
44+
tweakQuotePerBase ::
4545
( MonadThrow m
4646
) =>
4747
BuyOrSell ->
4848
QuotePerBase ->
4949
m QuotePerBase
50-
tweakMakerRate =
51-
tweakMakerRateRec pip
50+
tweakQuotePerBase =
51+
tweakQuotePerBaseRec pip
5252

53-
tweakMakerRateRec ::
53+
tweakQuotePerBaseRec ::
5454
( MonadThrow m
5555
) =>
5656
Ratio Natural ->
5757
BuyOrSell ->
5858
QuotePerBase ->
5959
m QuotePerBase
60-
tweakMakerRateRec tweak bos prev = do
60+
tweakQuotePerBaseRec tweak bos prev = do
6161
next <- roundQuotePerBase
6262
. QuotePerBase
6363
$ case bos of
6464
Buy -> unQuotePerBase prev - tweak
6565
Sell -> unQuotePerBase prev + tweak
6666
if next /= prev
6767
then pure next
68-
else tweakMakerRateRec (tweak + pip) bos prev
68+
else tweakQuotePerBaseRec (tweak + pip) bos prev
6969

7070
pip :: Ratio Natural
7171
pip = 0.00000001
@@ -113,7 +113,7 @@ data CounterExit = CounterExit
113113

114114
newCounterOrder :: (MonadThrow m) => CounterArgs -> m CounterExit
115115
newCounterOrder args = do
116-
exitBase <- tweakMoneyPip Sell =<< roundMoneyAmount exitBaseLoss
116+
exitBase <- tweakMoneyAmount Sell =<< roundMoneyAmount exitBaseLoss
117117
exitPrice <- roundQuotePerBase exitRate
118118
pure
119119
CounterExit

pub/bfx/test/Bfx/MathSpec.hs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{-# OPTIONS_HADDOCK show-extensions #-}
2+
3+
module Bfx.MathSpec
4+
( spec,
5+
)
6+
where
7+
8+
import Bfx.Import
9+
import Test.Hspec
10+
11+
spec :: Spec
12+
spec =
13+
focus . it "newCounterOrder" $ do
14+
let rates =
15+
CounterRates
16+
{ counterRatesEnterBaseFee = FeeRate 0.001,
17+
counterRatesExitQuoteFee = FeeRate 0.001,
18+
counterRatesExitQuoteProfit = ProfitRate 0.01
19+
}
20+
let args =
21+
CounterArgs
22+
{ counterArgsEnterGrossBaseGain = MoneyAmount 0.2,
23+
counterArgsEnterQuotePerBase = QuotePerBase 5,
24+
counterArgsRates = rates
25+
}
26+
exit <- newCounterOrder args
27+
--
28+
-- TODO : !!!
29+
--
30+
exit
31+
`shouldBe` CounterExit
32+
{ counterExitNetBaseLoss = MoneyAmount 1,
33+
counterExitQuotePerBase = QuotePerBase 1
34+
}

0 commit comments

Comments
 (0)