Skip to content

Instantly share code, notes, and snippets.

@bharddwaj
Last active June 30, 2019 01:12
Show Gist options
  • Save bharddwaj/1f9b2ab9f089c9a7066b66efc68e6976 to your computer and use it in GitHub Desktop.
Save bharddwaj/1f9b2ab9f089c9a7066b66efc68e6976 to your computer and use it in GitHub Desktop.
finds the bid-ask spread for each row and records the frequency of each different spread and graphs them at the end. Used this for liquidity research on 64 million rows of data!
import pandas as pd
import numpy as np
import csv
import seaborn as sns
import matplotlib.pyplot as plt
import math
data = pd.read_csv("CL_June_2019.csv")
print(type(data))
#new_data = data[59528000:59800000]
#print(new_data)
#new_data.to_csv("new_file.csv")
count = 0
spread = []
frequency = {}
spread.append( data['Bid Price'])
spread.append(data['Ask Price'])
for i in range(len(spread[0])):
count += 1
bid = spread[0][i]
ask = spread[1][i]
if not math.isnan(bid) and not math.isnan(ask):
difference = round(abs(bid - ask),2)
print(difference)
if difference in frequency:
frequency[difference] += 1
else:
frequency[difference] = 1
print(count)
sns.barplot(x = [keys for keys in frequency],y = [frequency[keys] for keys in frequency])
plt.xlabel("Spread")
plt.ylabel('Frequency')
plt.title(f"Bid-Ask Spread")
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment