Skip to content

Instantly share code, notes, and snippets.

@davidzhaodz
Last active December 15, 2020 00:24
Show Gist options
  • Save davidzhaodz/5b2c212d6b30dc418d52c69743bd66b8 to your computer and use it in GitHub Desktop.
Save davidzhaodz/5b2c212d6b30dc418d52c69743bd66b8 to your computer and use it in GitHub Desktop.
Financial Data Structures (Bars)
#install shrimpy
pip install shrimpy-python
#import requisite packages
import numpy as np
import pandas as pd
from datetime import datetime, timedelta
import math
import plotly
import shrimpy
import plotly.graph_objects as go
from plotly.subplots import make_subplots
#collecting historical candlestick data
shrimpy_public_key ='...'
shrimpy_secret_key = '...'
client = shrimpy.ShrimpyApiClient(shrimpy_public_key, shrimpy_secret_key)
candles = client.get_candles(
'coinbasepro', # exchange
'BTC', # base_trading_symbol
'USD', # quote_trading_symbol
'1m' # interval
)
time = []
open = []
high = []
low = []
close = []
vol =[]
#formatting the data to match the plotting library
for candle in candles:
time.append(candle['time'])
open.append(candle['open'])
high.append(candle['high'])
low.append(candle['low'])
close.append(candle['close'])
vol.append(candle['volume'])
#plotting the candlesticks
fig = go.Figure(data=[go.Candlestick(x=time,
open=open_data, high=high_data,
low=low_data, close=close_data)])
fig.show()
#formatting data and converting into a dictionary
df = pd.DataFrame(
{'time': [datetime.strptime(x,"%Y-%m-%dT%H:%M:%S.%fZ") for x in time],
'open': [float(x.strip('0')) for x in open],
'high': [float(x.strip('0')) for x in high],
'low': [float(x.strip('0')) for x in low],
'close': [float(x.strip('0')) for x in close],
'vol': [float(x.strip('0')) for x in vol]
})
df_dict = df.to_dict('records')
#creating our dollar_bars function -> from Max Bodola https://alpaca.markets/learn/machine-learning-pipeline-01/
def get_dollar_bars(time_bars, dollar_threshold):
# initialize an empty list of dollar bars
dollar_bars = []
# initialize the running dollar volume at zero
running_volume = 0
# initialize the running high and low with placeholder values
running_high, running_low = 0, math.inf
# for each time bar...
for i in range(len(time_bars)):
# get the timestamp, open, high, low, close, and volume of the next bar
#next_timestamp, next_open, next_high, next_low, next_close, next_volume = [time_bars[i][k] for k in ['time', 'open', 'high', 'low', 'close', 'vol']]
next_close, next_high, next_low, next_open, next_timestamp, next_volume = [time_bars[i][k] for k in ['close', 'high', 'low', 'open', 'time', 'vol']]
# get the midpoint price of the next bar (the average of the open and the close)
midpoint_price = ((next_open) + (next_close))/2
# get the approximate dollar volume of the bar using the volume and the midpoint price
dollar_volume = next_volume * midpoint_price
# update the running high and low
running_high, running_low = max(running_high, next_high), min(running_low, next_low)
# if the next bar's dollar volume would take us over the threshold...
if dollar_volume + running_volume >= dollar_threshold:
# set the timestamp for the dollar bar as the timestamp at which the bar closed (i.e. one minute after the timestamp of the last minutely bar included in the dollar bar)
bar_timestamp = next_timestamp + timedelta(minutes=1)
# add a new dollar bar to the list of dollar bars with the timestamp, running high/low, and next close
dollar_bars += [{'timestamp': bar_timestamp, 'open': next_open, 'high': running_high, 'low': running_low, 'close': next_close}]
# reset the running volume to zero
running_volume = 0
# reset the running high and low to placeholder values
running_high, running_low = 0, math.inf
# otherwise, increment the running volume
else:
running_volume += dollar_volume
# return the list of dollar bars
return dollar_bars
#creating dollar bars
dollar_bars = get_dollar_bars(df_dict, 1000000) #1,000,000 specified as dollar threshold, arbitrary
df = pd.DataFrame(new)
df.head()
#install shrimpy
pip install shrimpy-python
#import requisite packages
import numpy as np
import pandas as pd
from datetime import datetime, timedelta
import math
import plotly
import shrimpy
import plotly.graph_objects as go
from plotly.subplots import make_subplots
#collecting historical candlestick data
shrimpy_public_key ='...'
shrimpy_secret_key = '...'
client = shrimpy.ShrimpyApiClient(shrimpy_public_key, shrimpy_secret_key)
candles = client.get_candles(
'coinbasepro', # exchange
'BTC', # base_trading_symbol
'USD', # quote_trading_symbol
'1m' # interval
)
time = []
open = []
high = []
low = []
close = []
vol =[]
#formatting the data to match the plotting library
for candle in candles:
time.append(candle['time'])
open.append(candle['open'])
high.append(candle['high'])
low.append(candle['low'])
close.append(candle['close'])
vol.append(candle['volume'])
#plotting the candlesticks
fig = go.Figure(data=[go.Candlestick(x=time,
open=open_data, high=high_data,
low=low_data, close=close_data)])
fig.show()
#formatting data and converting into a dictionary
df = pd.DataFrame(
{'time': [datetime.strptime(x,"%Y-%m-%dT%H:%M:%S.%fZ") for x in time],
'open': [float(x.strip('0')) for x in open],
'high': [float(x.strip('0')) for x in high],
'low': [float(x.strip('0')) for x in low],
'close': [float(x.strip('0')) for x in close],
'vol': [float(x.strip('0')) for x in vol]
})
df_dict = df.to_dict('records')
#creating our dollar_bars function -> from Max Bodola https://alpaca.markets/learn/machine-learning-pipeline-01/
def get_dollar_bars(time_bars, dollar_threshold):
# initialize an empty list of dollar bars
dollar_bars = []
# initialize the running dollar volume at zero
running_volume = 0
# initialize the running high and low with placeholder values
running_high, running_low = 0, math.inf
# for each time bar...
for i in range(len(time_bars)):
# get the timestamp, open, high, low, close, and volume of the next bar
#next_timestamp, next_open, next_high, next_low, next_close, next_volume = [time_bars[i][k] for k in ['time', 'open', 'high', 'low', 'close', 'vol']]
next_close, next_high, next_low, next_open, next_timestamp, next_volume = [time_bars[i][k] for k in ['close', 'high', 'low', 'open', 'time', 'vol']]
# get the midpoint price of the next bar (the average of the open and the close)
midpoint_price = ((next_open) + (next_close))/2
# get the approximate dollar volume of the bar using the volume and the midpoint price
dollar_volume = next_volume * midpoint_price
# update the running high and low
running_high, running_low = max(running_high, next_high), min(running_low, next_low)
# if the next bar's dollar volume would take us over the threshold...
if dollar_volume + running_volume >= dollar_threshold:
# set the timestamp for the dollar bar as the timestamp at which the bar closed (i.e. one minute after the timestamp of the last minutely bar included in the dollar bar)
bar_timestamp = next_timestamp + timedelta(minutes=1)
# add a new dollar bar to the list of dollar bars with the timestamp, running high/low, and next close
dollar_bars += [{'timestamp': bar_timestamp, 'open': next_open, 'high': running_high, 'low': running_low, 'close': next_close}]
# reset the running volume to zero
running_volume = 0
# reset the running high and low to placeholder values
running_high, running_low = 0, math.inf
# otherwise, increment the running volume
else:
running_volume += dollar_volume
# return the list of dollar bars
return dollar_bars
#creating dollar bars
dollar_bars = get_dollar_bars(df_dict, 1000000) #1,000,000 specified as dollar threshold, arbitrary
df = pd.DataFrame(new)
df.head()
#plotting the dollar bars
fig = go.Figure(data=[go.Candlestick(x=df.timestamp,
open=df.open, high=df.high,
low=df.low, close=df.close)])
fig.show()
#plotting both bars together
trace1 = go.Candlestick(x = time,
open=open_data, high=high_data,
low=low_data, close=close_data,
name = 'time_bars'
)
trace2 = go.Candlestick(x=df.timestamp,
open=df.open, high=df.high,
low=df.low, close=df.close,
name = 'dollar_bars',
increasing_line_color= 'cyan', decreasing_line_color= 'gray'
)
fig = make_subplots(specs=[[{"secondary_y": True}]])
fig.add_trace(trace1)
fig.add_trace(trace2,secondary_y=True)
fig.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment