From 06d75eefcc7cb54338b3862880b2f145b9dd06fb Mon Sep 17 00:00:00 2001 From: skynetcap <100323448+skynetcap@users.noreply.github.com> Date: Tue, 28 Nov 2023 11:27:18 -0800 Subject: [PATCH] Phoenix: convertPriceToPriceInTicks and convertSizeToNumBaseLots --- .../mmorrell/phoenix/model/PhoenixMarket.java | 20 ++++++++++++- phoenix/src/test/java/PhoenixTest.java | 30 +++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/phoenix/src/main/java/com/mmorrell/phoenix/model/PhoenixMarket.java b/phoenix/src/main/java/com/mmorrell/phoenix/model/PhoenixMarket.java index 5d769ff..a4ea61c 100644 --- a/phoenix/src/main/java/com/mmorrell/phoenix/model/PhoenixMarket.java +++ b/phoenix/src/main/java/com/mmorrell/phoenix/model/PhoenixMarket.java @@ -10,7 +10,6 @@ import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.Comparator; import java.util.HashSet; import java.util.List; @@ -333,4 +332,23 @@ private static void readAskBuffer(byte[] bidBuffer, PhoenixMarket market) { } } } + + public long convertPriceToPriceInTicks(double price) { + // Multiply by denominator + double result = price * (Math.pow(10, phoenixMarketHeader.getQuoteDecimals()) * + phoenixMarketHeader.getRawBaseUnitsPerBaseUnit()); + + // Divide out getTickSizeInQuoteLotsPerBaseUnit and getQuoteLotSize + result = result / tickSizeInQuoteLotsPerBaseUnit; + result = result / phoenixMarketHeader.getQuoteLotSize(); + + return (long) result; + } + + public long convertSizeToNumBaseLots(double size) { + double result = size * Math.pow(10, phoenixMarketHeader.getBaseDecimals()); + result = result / phoenixMarketHeader.getBaseLotSize(); + + return (long) result; + } } diff --git a/phoenix/src/test/java/PhoenixTest.java b/phoenix/src/test/java/PhoenixTest.java index b624461..1ae1743 100644 --- a/phoenix/src/test/java/PhoenixTest.java +++ b/phoenix/src/test/java/PhoenixTest.java @@ -33,6 +33,10 @@ import java.util.Base64; import java.util.List; import java.util.Map; +import java.util.Optional; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; @Slf4j public class PhoenixTest { @@ -70,6 +74,32 @@ public void phoenixGetMarketsTest() throws RpcException { }); } + // Given a marketId, and double values, convert to lots/atoms + @Test + public void orderLotsConversionTest() { + PhoenixManager phoenixManager = new PhoenixManager(client); + Optional marketOptional = phoenixManager.getMarket( + new PublicKey("4DoNfFBfF7UokCC2FQzriy7yHK6DY6NVdYpuekQ5pRgg") + ); + + if (marketOptional.isEmpty()) { + log.error("Unable to get market for test."); + return; + } + + PhoenixMarket market = marketOptional.get(); + log.info("Market: {}", market); + + double price = 58.31; + double size = 4; + long numBaseLots = market.convertSizeToNumBaseLots(size); + long priceInTicks = market.convertPriceToPriceInTicks(price); + log.info("Price: {}, Size: {}, numBaseLots: {}, priceInTicks: {}", price, size, numBaseLots, priceInTicks); + + assertEquals(4000, numBaseLots); + assertEquals(58310, priceInTicks); + } + @Test public void orderNormalizedTest() { PhoenixManager phoenixManager = new PhoenixManager(client);