import pandas as pd
import seaborn as sns
df_obs = pd.read_csv('/work/20170103_PFE_nasdaq_1sec.csv')
# df_obm = pd.read_csv('/work/20170103_PFE_nasdaq_1min.csv')
df_obs['Mid_Price'] = (df_obs['Bid_Price'] + df_obs['Offer_Price']) / 2
df_obs['Volume_Weighted_Mid_Price'] = (df_obs['Bid_Price'] * df_obs['Offer_Size']) + (df_obs['Offer_Price'] * df_obs['Bid_Size']) / (df_obs['Offer_Size'] + df_obs['Bid_Size'])
df_obs['return'] = df_obs['Mid_Price'].diff()
df_obs['fractional_return'] = df_obs['Mid_Price'].diff() / df_obs['Mid_Price']
df_obs
df_obs.isnull().sum()
df_obs.dropna(how='any', inplace=True)
df_obs.isnull().sum()
(df_obs == 0).sum()
import plotly.graph_objects as go
# Create traces
fig = go.Figure()
fig.add_trace(go.Scatter(x=df_obs['DateTime'], y=df_obs['Mid_Price'],
mode='lines',
name='lines'))
fig.update_layout(
title="Mid Price",
xaxis_title="Datetime",
yaxis_title="Mid Price"
)
fig.show()
fig = go.Figure()
fig.add_trace(go.Scatter(x=df_obs['DateTime'], y=df_obs['Volume_Weighted_Mid_Price'],
mode='lines',
name='lines'))
fig.update_layout(
title="Volume Weighted Mid Price",
xaxis_title="Datetime",
yaxis_title="WVMP"
)
fig.show()
fig = go.Figure()
fig.add_trace(go.Scatter(x=df_obs['DateTime'], y=df_obs['return'],
mode='lines',
name='lines'))
fig.update_layout(
title="Return",
xaxis_title="Datetime",
yaxis_title="Return"
)
fig.show()
fig = go.Figure()
fig.add_trace(go.Scatter(x=df_obs['DateTime'], y=df_obs['fractional_return'],
mode='lines',
name='lines'))
fig.update_layout(
title="Fractional",
xaxis_title="Datetime",
yaxis_title="frational_return"
)
fig.show()
df_obs['Volatility_60s'] = df_obs['return'].rolling(window = 60).std()
df_obs['Volatility_3600s'] = df_obs['return'].rolling(window = 3600).std()
from plotly.subplots import make_subplots
# Create figure with secondary y-axis
fig = make_subplots(specs=[[{"secondary_y": True}]])
fig.add_trace(go.Scatter(x=df_obs['DateTime'], y=df_obs['Volatility_60s'],
mode='lines',
name='Volatility 1 minute'))
fig.add_trace(go.Scatter(x=df_obs['DateTime'], y=df_obs['Volatility_3600s'],
mode='lines',
name='Volatility 1 hour'))
fig.add_trace(go.Scatter(x=df_obs['DateTime'], y=df_obs['return'],
mode='lines',
name='Return'), secondary_y=True)
fig.update_layout(
title="Volatility",
xaxis_title="Datetime",
yaxis_title="Volatility"
)
fig.show()
df_obs
Trades
Plot the traded volume and traded price form the trades file
df_trades = pd.read_csv('/work/20170103_PFE_trades_1sec.csv')
df_trades
df_trades.isnull().sum()
df_trades.dropna(how='any', inplace=True)
import plotly.graph_objects as go
from plotly.subplots import make_subplots
fig = go.Figure()
fig.add_trace(go.Scatter(x=df_trades['DateTime'], y=df_trades['Trade Price'],
mode='lines',
name='lines'))
fig.update_layout(
title="Trade Price",
xaxis_title="Time",
yaxis_title="Trade Price"
)
fig.show()
fig = make_subplots(specs=[[{"secondary_y": True}]])
fig.add_trace(go.Scatter(x=df_trades['DateTime'], y=df_trades['Trade Volume'],
mode='lines',
name='Volume'))
fig.add_trace(go.Scatter(x=df_obs['DateTime'], y=df_obs['Volatility_60s'],
mode='lines',
name='Volatility 1 minute'), secondary_y=True)
fig.add_trace(go.Scatter(x=df_obs['DateTime'], y=df_obs['Volatility_3600s'],
mode='lines',
name='Volatility 1 hour'), secondary_y=True)
fig.update_layout(
title="Trade Volume",
xaxis_title="Time",
yaxis_title="Trade Volume"
)
fig.show()
df_obs[['DateTime','Volatility_3600s']].plot.line()