Skip to content

Instantly share code, notes, and snippets.

@GerardBCN
Created April 26, 2019 16:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save GerardBCN/e6151a51bb14af4cabe9fbeb3f0e8bb1 to your computer and use it in GitHub Desktop.
Save GerardBCN/e6151a51bb14af4cabe9fbeb3f0e8bb1 to your computer and use it in GitHub Desktop.
Volume bar generator
import numpy as np
# expects a numpy array with trades
# each trade is composed of: [time, price, quantity]
def generate_volumebars(trades, frequency=10):
times = trades[:,0]
prices = trades[:,1]
volumes = trades[:,2]
ans = np.zeros(shape=(len(prices), 6))
candle_counter = 0
vol = 0
lasti = 0
for i in range(len(prices)):
vol += volumes[i]
if vol >= frequency:
ans[candle_counter][0] = times[i] # time
ans[candle_counter][1] = prices[lasti] # open
ans[candle_counter][2] = np.max(prices[lasti:i+1]) # high
ans[candle_counter][3] = np.min(prices[lasti:i+1]) # low
ans[candle_counter][4] = prices[i] # close
ans[candle_counter][5] = np.sum(volumes[lasti:i+1]) # volume
candle_counter += 1
lasti = i+1
vol = 0
return ans[:candle_counter]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment