Skip to content

Commit

Permalink
Phoenix: convertPriceToPriceInTicks and convertSizeToNumBaseLots
Browse files Browse the repository at this point in the history
  • Loading branch information
skynetcap committed Nov 28, 2023
1 parent 4530b13 commit 06d75ee
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
30 changes: 30 additions & 0 deletions phoenix/src/test/java/PhoenixTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<PhoenixMarket> 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);
Expand Down

0 comments on commit 06d75ee

Please sign in to comment.