Skip to content

Instantly share code, notes, and snippets.

@bharddwaj
Created July 6, 2019 02:26
Show Gist options
  • Save bharddwaj/b9de8eaf377c4d10d2712189c86f2607 to your computer and use it in GitHub Desktop.
Save bharddwaj/b9de8eaf377c4d10d2712189c86f2607 to your computer and use it in GitHub Desktop.
top 15 most frequent bid ask spread percentages. the bid ask spread percentage is ((bid - ask)/ask)*10000
import pandas as pd
import numpy as np
import csv
import seaborn as sns
import matplotlib.pyplot as plt
import math
from collections import OrderedDict
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_percentage = round(((bid - ask)/ask)*10000,2)
#print(difference_percentage)
if difference_percentage in frequency:
frequency[difference_percentage] += 1
else:
frequency[difference_percentage] = 1
print(count)
frequency = OrderedDict(sorted(frequency.items(), key=lambda t: t[1]))
keys = list(frequency.keys())
k = keys[len(keys)-15:len(keys)]
sns.barplot(x = k ,y =[frequency[i] for i in k ])
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