Skip to content

Instantly share code, notes, and snippets.

@beatzxbt
Created May 29, 2024 23:35
Show Gist options
  • Select an option

  • Save beatzxbt/97e9c81b600619acf7cc9dcca949e888 to your computer and use it in GitHub Desktop.

Select an option

Save beatzxbt/97e9c81b600619acf7cc9dcca949e888 to your computer and use it in GitHub Desktop.
def generate_new_buffer(size: float, strength: float) -> tuple[float, float]:
"""
Generates a new buffer range based on the size and strength.
"""
return (size * (1 - strength), size * (1 + strength))
def process_data(data) -> None:
"""
Processes the data with varying buffer strengths and prints the results.
"""
for i in range(1, 25):
buffer_strength = i / 100
final = [data[0]]
times = []
# Initialize old best bid and ask prices and sizes
best_bid_price_old, best_bid_size_old = data[0]["bids"][0]
best_ask_price_old, best_ask_size_old = data[0]["asks"][0]
# Generate initial buffers
buffer_bid = generate_new_buffer(best_bid_size_old, buffer_strength)
buffer_ask = generate_new_buffer(best_ask_size_old, buffer_strength)
for row in data[1:]:
t1 = time.time_ns()
best_bid_price, best_bid_size = row["bids"][0]
best_ask_price, best_ask_size = row["asks"][0]
# Check if prices have changed
prices_changed = (best_bid_price != best_bid_price_old) or (best_ask_price != best_ask_price_old)
# Check if sizes are out of buffer range
sizes_out_of_buffer = (best_bid_size <= buffer_bid[0]) or (best_bid_size >= buffer_bid[1]) or \
(best_ask_size <= buffer_ask[0]) or (best_ask_size >= buffer_ask[1])
if prices_changed or sizes_out_of_buffer:
final.append(row)
# Update buffers and old prices/sizes
buffer_bid = generate_new_buffer(best_bid_size, buffer_strength)
buffer_ask = generate_new_buffer(best_ask_size, buffer_strength)
best_bid_price_old, best_bid_size_old = best_bid_price, best_bid_size
best_ask_price_old, best_ask_size_old = best_ask_price, best_ask_size
# t2 = time.time_ns()
# times.append(t2-t1)
# print(f"Buffer strength: {round(buffer_strength * 100)}% | Average time: {round(sum(times)/len(times))}ns | Total time: {round(sum(times)/1_000_000_000, 5)}s")
print(f"Buffer strength: {round(buffer_strength * 100)}% | Messages passed: {len(final)}/{len(data)} ({round((len(final)/len(data)) * 100, 3)}%)")
process_data(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment