Skip to content

Commit 3ddbbe5

Browse files
committed
bfx wip
1 parent 2050cf8 commit 3ddbbe5

File tree

5 files changed

+110
-32
lines changed

5 files changed

+110
-32
lines changed

pub/bfx/src/Bfx.hs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ module Bfx
2626
candlesLast,
2727
candlesHist,
2828
tickers,
29+
MinOrderArgs (..),
30+
mkMinOrder,
2931
module X,
3032
)
3133
where
@@ -577,3 +579,58 @@ tickers =
577579
[ SomeQueryParam "symbols" ("ALL" :: Text)
578580
]
579581
emptyReq
582+
583+
data MinOrderArgs = MinOrderArgs
584+
{ minOrderArgsFee :: FeeRate,
585+
minOrderArgsBuyOrSell :: BuyOrSell,
586+
minOrderArgsCurrencyPair :: CurrencyPair
587+
}
588+
deriving stock
589+
( Eq,
590+
Ord,
591+
Show,
592+
Read,
593+
Data,
594+
Generic
595+
)
596+
597+
mkMinOrder ::
598+
( MonadThrow m,
599+
MonadUnliftIO m
600+
) =>
601+
MinOrderArgs ->
602+
m SubmitOrder.Request
603+
mkMinOrder args = do
604+
syms <- symbolsDetails
605+
minBaseAmt <-
606+
maybe
607+
(throwString $ inspect @Text sym <> " is missing!")
608+
(pure . currencyPairMinOrderBaseAmt)
609+
$ Map.lookup sym syms
610+
baseAmt <-
611+
case bos of
612+
Buy ->
613+
tweakMoneyAmount Buy
614+
. MoneyAmount
615+
$ unMoneyAmount minBaseAmt
616+
/ (1 - unFeeRate (minOrderArgsFee args))
617+
Sell ->
618+
pure minBaseAmt
619+
price <-
620+
Bfx.marketAveragePrice
621+
MarketAveragePrice.Request
622+
{ MarketAveragePrice.buyOrSell = bos,
623+
MarketAveragePrice.baseAmount = baseAmt,
624+
MarketAveragePrice.symbol = sym
625+
}
626+
pure
627+
SubmitOrder.Request
628+
{ SubmitOrder.buyOrSell = bos,
629+
SubmitOrder.baseAmount = baseAmt,
630+
SubmitOrder.symbol = sym,
631+
SubmitOrder.rate = price,
632+
SubmitOrder.options = SubmitOrder.optsDef
633+
}
634+
where
635+
bos = minOrderArgsBuyOrSell args
636+
sym = minOrderArgsCurrencyPair args

pub/bfx/src/Bfx/Data/Type.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,8 @@ data CurrencyPairConf = CurrencyPairConf
348348
( Eq,
349349
Ord,
350350
Show,
351+
Read,
352+
Data,
351353
Generic
352354
)
353355

pub/bfx/src/Bfx/Math.hs

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

33
module Bfx.Math
4-
( tweakMoneyAmount,
4+
( roundMoneyAmount,
5+
roundQuotePerBase,
6+
tweakMoneyAmount,
57
tweakQuotePerBase,
6-
mkCounterOrder,
78
CounterArgs (..),
89
CounterRates (..),
910
CounterExit (..),
10-
roundMoneyAmount,
11-
roundQuotePerBase,
11+
mkCounterOrder,
1212
)
1313
where
1414

1515
import Data.Ratio.Rounding (dpRound, sdRound)
1616
import Functora.Money
1717
import Functora.Prelude
1818

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+
1947
tweakMoneyAmount ::
2048
( MonadThrow m
2149
) =>
@@ -188,31 +216,3 @@ mkCounterOrder args = do
188216
feeRate = counterRatesFee $ counterArgsRates args
189217
profitRate :: ProfitRate
190218
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

pub/bfx/test/BfxSpec.hs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,17 @@ spec = before sysEnv $ do
125125
it "candlesHist succeeds" . const $ do
126126
res <- tryAny $ Bfx.candlesHist Ctf1h adaBtc Candles.optsDef
127127
res `shouldSatisfy` isRight
128+
it "mkMinOrder" . const $ do
129+
let req =
130+
Bfx.MinOrderArgs
131+
{ Bfx.minOrderArgsFee = FeeRate 0.001,
132+
Bfx.minOrderArgsBuyOrSell = Buy,
133+
Bfx.minOrderArgsCurrencyPair = adaBtc
134+
}
135+
buyRes <- Bfx.mkMinOrder req
136+
SubmitOrder.baseAmount buyRes `shouldBe` MoneyAmount 4.00400401
137+
sellRes <- Bfx.mkMinOrder req {Bfx.minOrderArgsBuyOrSell = Sell}
138+
SubmitOrder.baseAmount sellRes `shouldBe` MoneyAmount 4
128139

129140
-- describe "End2End" $ do
130141
-- itRight "submitOrderMaker" $ \env -> do

pub/functora/src/sql/Functora/SqlOrphan.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,11 @@ deriving newtype instance PersistFieldSql QuotePerBase
104104
deriving newtype instance PersistField CurrencyCode
105105

106106
deriving newtype instance PersistFieldSql CurrencyCode
107+
108+
deriving newtype instance PersistField FeeRate
109+
110+
deriving newtype instance PersistFieldSql FeeRate
111+
112+
deriving newtype instance PersistField ProfitRate
113+
114+
deriving newtype instance PersistFieldSql ProfitRate

0 commit comments

Comments
 (0)