Skip to content

Instantly share code, notes, and snippets.

@Fokko
Created May 28, 2025 08:14
Show Gist options
  • Save Fokko/d48611137a7f668318406bc2099dd47e to your computer and use it in GitHub Desktop.
Save Fokko/d48611137a7f668318406bc2099dd47e to your computer and use it in GitHub Desktop.
Events producer
import numpy as np
import matplotlib.pyplot as plt
import random
import time
from datetime import datetime
import os
import json
try:
os.mkdir('/tmp/stockprices/')
except FileExistsError:
pass
class SimulatedStock:
def __init__(self, name, base_price, volatility, freq, slope):
self.name = name
self.base_price = base_price
self.volatility = volatility
self.freq = freq
self.slope = slope
self.t = 0
def tick(self):
noise = random.uniform(-self.volatility, self.volatility)
sine_wave = np.sin(self.freq * self.t) * self.volatility
slope = self.t * self.slope
price = self.base_price + sine_wave + noise + slope
self.t += 1
return round(price, 2) if price > 0 else 0
# Define a few example stocks
stocks = [
SimulatedStock("AAPL", 150, 20, 1.0, 0.5),
SimulatedStock("GOOG", 2800, 100, 0.7, 1.),
SimulatedStock("TSLA", 1200, 200, 1.5, -1.0),
SimulatedStock("AMZN", 3300, 150, 0.5, 0.1),
SimulatedStock("MSFT", 450, 150, 1.0, 2),
]
while True:
stock_prices = [json.dumps({
"symbol": stock.name,
"time": datetime.now().isoformat(),
"ask": stock.tick()
})
for stock in stocks]
with open(f'/tmp/stockprices/{datetime.now().isoformat()}.json', "w") as output_stream:
output_stream.write("\n".join(stock_prices))
time.sleep(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment