Converting time bars to tick bars or volume bars in python

Converting time bars to tick bars or volume bars in python

Manage alerts

Loading saved threads...

Occhima · External communityPost link
External question — Quantitative Finance Stack Exchange Author: Occhima Original post: https://quant.stackexchange.com/questions/54471 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. Recently I've started reading Advances in Financial Machine Learning by Marcos Lopez de Prado. In the second chapter the author defines some essential financial data structures, like tick bars, volume bars, etc. I was wondering how I could transform a series of daily returns of forex data, acquired using yfinance lib for python 3.7 , in to any kind of those bars de Prado mentions. Below, I'll leave the snippet I used to get the data. import yfinance as yf df = yf.Ticker("BRL=X").history(period='max').Close.pct_change().dropna()
Quote
Report
John · External communityPost link
External answer — Quantitative Finance Stack Exchange Author: John Original post: https://quant.stackexchange.com/a/54480 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. a tick is a change in the price, it is not a second or minute at regular tine intervals, it's frequency is driven by market moves whilst daily data is aggregated, therefore by definition it is impossible to deduct tick data from any other time frequency. to extract this in python, there are multiple ways, I personally use Dukascopy, where you can download free csv extract and feed them easily into your python program using pandas.
Quote
Report
chrisaycock · External communityPost link
External answer — Quantitative Finance Stack Exchange Author: chrisaycock Original post: https://quant.stackexchange.com/a/54495 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. As mentioned in my comment, tick data is the individual quotes and trades; Yahoo only has daily data. As an analogy, you can always make a high-definition photo more blurry and pixelated, but you can't add detail and definition to a bad picture. Daily data is just an aggregate of individual ticks, so you can't get the individual ticks from daily data. You will need a different source for tick data, and those are usually commercial. (Vendors sell to professional traders, after all.) With that said, once you do get some tick data, aggregations are pretty straightforward. I've included some pandas code here for posterity; this assumes a trades Dataframe with price and size columns, indexed by timestamp. Time Bars Just give the frequency you desire. Here is an example of five-minute bars: trades.groupby(pd.Grouper(freq="5min")).agg({'price': 'ohlc', 'size': 'sum'}) Tick Bars We'll define a helper function to round-down to the nearest integer: def bar(xs, y): return np.int64(xs / y) * y Then group by the bars of the Dataframe's row number. Here's an example of 10-tick bars: trades.groupby(bar(np.arange(len(trades)), 10)).agg({'price': 'ohlc', 'size': 'sum'}) Volume Bars Group by the bars of the cumulative volume. Here's an example for n shares traded: trades.groupby(bar(np.cumsum(trades['size']), n)).agg({'price': 'ohlc', 'size': 'sum'}
Quote
Report

Post Reply

Checking account access…