Skip to content

Commit

Permalink
💱 Pricing point local calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
JFWooten4 committed Dec 9, 2024
1 parent 1f31ba4 commit 34fb3b0
Showing 1 changed file with 81 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,64 @@ This page specifically covers liquidity from the Stellar Decentralized Exchange

## Reading Prices

Exmaple narrative assuming after the orderbook resolution later on:

Now that we have seen the depth of the market (up to the [paging token limit](HREF_TXN_RES_QUERY_LIM)), we can find the midpoint price. This number comes from the average of the best (highest) bid and (lowest) offer. To affirm the fairness of this price, we might also check the recent trade prices.

<CodeExample>

```python
# This ex should propb be moved somehwere else todo

# orderbook = server.orderbook(selling=astroPeso, buying=astroDollar).call()
def getMidpointPrice(orderbook);
try:
highestBid = float(orderbook['bids'][0]['price'])
lowestAsk = float(orderbook['asks'][0]['price'])
except IndexError:
print("Missing exusting buy or sell offers.")
return midpointPrice = (highestBid + lowestAsk) / 2

def getAverageRecentPrice():
trades = server.trades().for_asset_pair(
selling = astroPeso,
buying = astroDollar
).call() # This should really use at least .limit(25) TODO
recentPrices = [
float(trade['price'])
for trade
in trades['_embedded']['records'][:9]
]
if recentPrices:
return sum(recentPrices) / len(recentPrices)
else:
print("No recent trades available.")

def sanityCheckOK(midpointPrice, averageRecentPrice):
if abs(midpointPrice - averageRecentPrice) / midpointPrice <= 0.05:
return 1
else:
print("Warning: Midpoint and average recent trade prices differ by more than 5%.")
return 0

offerPrice = round(midpointPrice, 7)
$ in new offer op, price = str(id)
```

```js

```

```java

```

```go

```

</CodeExample>

This section covers querying the live order book and accessing price data. By interacting with the SDEX's public [Horizon endpoints](../../../data/horizon/api-reference/resources/README.mdx), you can obtain real-time market data and make informed trading decisions. While Stellar provides transparent access to price information, external oracles can also be used when integrating with other systems or when off-chain data is required.

The Stellar network takes a crowdsourced approach to liquidity. Namely, the SDEX gives all users equal access to a global decentralized order book for any pair of assets on he network. Since launching with the genesis of the Stellar network in 2014, the SDEX has processed over 4.2 billion trades worth upwards of \$25 billion.
Expand Down Expand Up @@ -317,69 +375,71 @@ We will see these in action in the later example preamvle (TODO: move above), fo
<CodeExample>

```python
orderBook = server.orderbook(
orderbook = server.orderbook(
selling = astroPeso,
buying = astroDollar
).call()

print("Bids:")
for bid in orderBook['bids']:
for bid in orderbook['bids']:
print(f"Price: {bid['price']}, Amount: {bid['amount']}")

print("\nAsks:")
for ask in orderBook['asks']:
for ask in orderbook['asks']:
print(f"Price: {ask['price']}, Amount: {ask['amount']}")
```

```js
const orderBook = await server.orderbook(astroPeso, astroDollar).call();
const orderbook = await server.orderbook(astroPeso, astroDollar).call();

console.log("Bids:");
orderBook.bids.forEach((bid) => {
orderbook.bids.forEach((bid) => {
console.log(`Price: ${bid.price}, Amount: ${bid.amount}`);
});

console.log("\nAsks:");
orderBook.asks.forEach((ask) => {
orderbook.asks.forEach((ask) => {
console.log(`Price: ${ask.price}, Amount: ${ask.amount}`);
});
```

```java
import java.util.List;

public static void main(String[] args) {
OrderBookResponse orderBook = server.orderBook(astroPeso, astroDollar).execute();
class orderbook {
public static void main(String[] args) {
OrderBookResponse orderbook = server.orderbook(astroPeso, astroDollar).execute();

System.out.println("Bids:");
List<OrderBookResponse.Row> bids = orderBook.getBids();
for (OrderBookResponse.Row bid : bids) {
System.out.println("Price: " + bid.getPrice() + ", Amount: " + bid.getAmount());
}
System.out.println("Bids:");
List<OrderBookResponse.Row> bids = orderbook.getBids();
for (OrderBookResponse.Row bid : bids) {
System.out.println("Price: " + bid.getPrice() + ", Amount: " + bid.getAmount());
}

System.out.println("\nAsks:");
List<OrderBookResponse.Row> asks = orderBook.getAsks();
for (OrderBookResponse.Row ask : asks) {
System.out.println("Price: " + ask.getPrice() + ", Amount: " + ask.getAmount());
System.out.println("\nAsks:");
List<OrderBookResponse.Row> asks = orderbook.getAsks();
for (OrderBookResponse.Row ask : asks) {
System.out.println("Price: " + ask.getPrice() + ", Amount: " + ask.getAmount());
}
}
}
```

```go
func main() {
orderBook, err := server.OrderBook(horizonclient.OrderBookRequest{
orderbook, err := server.OrderBook(horizonclient.OrderBookRequest{
Selling: astroPeso,
Buying: astroDollar,
})
check(err)

fmt.Println("Bids:")
for _, bid := range orderBook.Bids {
for _, bid := range orderbook.Bids {
fmt.Printf("Price: %s, Amount: %s\n", bid.Price, bid.Amount)
}

fmt.Println("\nAsks:")
for _, ask := range orderBook.Asks {
for _, ask := range orderbook.Asks {
fmt.Printf("Price: %s, Amount: %s\n", ask.Price, ask.Amount)
}
}
Expand All @@ -401,13 +461,7 @@ The network and by extension its [validators](https://stellarbeat.io) match orde

Let's pretend you just got paid in Mexican Pesos, but you'd prefer to hold your savings in U.S. Dollars. After querying the [exchange rate](#reading-prices), we can send a new offer to the network, swapping Pesos for Dollars at our desired rate. By submitting a sell offer that matches the current market price, we can ensure our pesos are converted to dollars promptly.

<CodeExample>

```python
should be implemented already ###
```

</CodeExample>
doIngG iutT

## Source of Truth

Expand Down

0 comments on commit 34fb3b0

Please sign in to comment.