Skip to content

Instantly share code, notes, and snippets.

@riceissa
Last active February 9, 2021 02:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save riceissa/3e9c043b6943ee5f1b053e6d8324985a to your computer and use it in GitHub Desktop.
Save riceissa/3e9c043b6943ee5f1b053e6d8324985a to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import numpy as np
import matplotlib.pyplot as plt
buyers = [3.25 - 0.25*n for n in range(0, 11)]
sellers = [3.25 - 0.25*n for n in range(0, 11)]
price_deltas = [0.01 * x for x in range(0, 1000)]
trading_prices = []
trading_counts = []
for _ in range(5):
buyers_still_in_market = list(range(len(buyers)))
sellers_still_in_market = list(range(len(sellers)))
transactions = []
for _ in range(1000):
# First, a random buyer offers to buy at some price below their number on
# their card
b = np.random.choice(buyers_still_in_market)
delta = np.random.choice(price_deltas)
buyer_price = max(0, buyers[b] - delta)
s = np.random.choice(sellers_still_in_market)
if buyer_price >= sellers[s]:
transactions.append((b, buyers[b], s, sellers[s], buyer_price))
buyers_still_in_market.remove(b)
sellers_still_in_market.remove(s)
trading_prices.append(buyer_price)
# Then, a random seller offers to sell at some price above their number on
# their card.
s = np.random.choice(sellers_still_in_market)
delta = np.random.choice(price_deltas)
seller_price = sellers[s] + delta
b = np.random.choice(buyers_still_in_market)
if seller_price <= buyers[b]:
transactions.append((b, buyers[b], s, sellers[s], seller_price))
buyers_still_in_market.remove(b)
sellers_still_in_market.remove(s)
trading_prices.append(seller_price)
trading_counts.append(len(trading_prices))
print(len(transactions), "transactions")
plt.plot(list(range(len(trading_prices))), trading_prices)
plt.ylim(0, 4.00)
for x in trading_counts:
plt.axvline(x=x-1, ymin=0, ymax=4.0, color='red')
print("line drawn at x =", x)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment