当前位置:网站首页>Tick Data and Resampling
Tick Data and Resampling
2022-07-02 11:12:00 【lk5423968】
Resample Tick Data - Backtrader
backtrader could already do resampling up from minute data. Accepting tick data was not a problem, by simply setting the 4 usual fields (open, high, low, close) to the tick value.
But passing the tick data to be resampled produced the same data again. As or release 1.1.11.88 this is no longer so. Now
TimeFrame (backtrader.TimeFrame) has been extended to contain constants and names for “Ticks”, “MicroSeconds” and “Seconds”
Resampling can manage the 3 aforementioned timeframes and sample them up.
Note
Because tick data is the lowest possible timeframe it can actually be “compressed” (n bars to 1 bar) but not be sampled up from a smallest timeframe.
The new release contains a small tickdata.csv sample added to the sources data a new sample script resample-tickdata.py to play with it.
Note
Updated the script to use the new Cerebro.resampledata method which avoids the need to manually instantiate a backtrader.DataResampler
The default execution doesn’t touch the data:
$ ./resample-tickdata.py
Producing this chart:
Compressing 3 ticks to 1:
$ ./resample-tickdata.py --timeframe ticks --compression 3
Producing this chart:

After the compression we no longer have single “ticks” but “bars”.
Now compressing to seconds and 5 bars compression:
$ ./resample-tickdata.py --timeframe seconds --compression 5
With a new chart:

And finally to minutes. The sample data contains tick data from 4 different minutes (the last tick in the file is the only tick for the 4th minute):
$ ./resample-tickdata.py --timeframe minutes
With a 4 bars (at the top it can be seen the final price was 3069). The 4th bar is a single point given for this minute a single tick is present in the file.

The script usage:
$ ./resample-tickdata.py --help
usage: resample-tickdata.py [-h] [--dataname DATANAME]
[--timeframe {ticks,microseconds,seconds,minutes,daily,weekly,monthly}]
[--compression COMPRESSION]
Resampling script down to tick data
optional arguments:
-h, --help show this help message and exit
--dataname DATANAME File Data to Load
--timeframe {ticks,microseconds,seconds,minutes,daily,weekly,monthly}
Timeframe to resample to
--compression COMPRESSION
Compress n bars into 1
And the code.
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import argparse
import backtrader as bt
import backtrader.feeds as btfeeds
def runstrat():
args = parse_args()
# Create a cerebro entity
cerebro = bt.Cerebro(stdstats=False)
# Add a strategy
cerebro.addstrategy(bt.Strategy)
# Load the Data
datapath = args.dataname or '../../datas/ticksample.csv'
data = btfeeds.GenericCSVData(
dataname=datapath,
dtformat='%Y-%m-%dT%H:%M:%S.%f',
timeframe=bt.TimeFrame.Ticks,
)
# Handy dictionary for the argument timeframe conversion
tframes = dict(
ticks=bt.TimeFrame.Ticks,
microseconds=bt.TimeFrame.MicroSeconds,
seconds=bt.TimeFrame.Seconds,
minutes=bt.TimeFrame.Minutes,
daily=bt.TimeFrame.Days,
weekly=bt.TimeFrame.Weeks,
monthly=bt.TimeFrame.Months)
# Resample the data
data = cerebro.resampledata(data,
timeframe=tframes[args.timeframe],
compression=args.compression)
# add a writer
cerebro.addwriter(bt.WriterFile, csv=True)
# Run over everything
cerebro.run()
# Plot the result
cerebro.plot(style='bar')
def parse_args():
parser = argparse.ArgumentParser(
description='Resampling script down to tick data')
parser.add_argument('--dataname', default='', required=False,
help='File Data to Load')
parser.add_argument('--timeframe', default='ticks', required=False,
choices=['ticks', 'microseconds', 'seconds',
'minutes', 'daily', 'weekly', 'monthly'],
help='Timeframe to resample to')
parser.add_argument('--compression', default=1, required=False, type=int,
help=('Compress n bars into 1'))
return parser.parse_args()
if __name__ == '__main__':
runstrat()边栏推荐
- 【快应用】Win7系统使用华为IDE无法运行和调试项目
- 洛谷 P5536 【XR-3】核心城市(贪心 + 树形 dp 寻找树的中心)
- flink二開,實現了個 batch lookup join(附源碼)
- static 函数中的静态变量
- One trick to quickly realize custom application titlebar
- The difference between self and static in PHP in methods
- Filtering of PCL
- 使用华为性能管理服务,按需配置采样率
- Luogu p1892 [boi2003] Gang (and search for variant anti set)
- spritejs
猜你喜欢
![Binary tree topic -- Luogu p3884 [jloi2009] binary tree problem (DFS for binary tree depth BFS for binary tree width Dijkstra for shortest path)](/img/c2/bb85b681af0f78b380b1d179c7ea49.png)
Binary tree topic -- Luogu p3884 [jloi2009] binary tree problem (DFS for binary tree depth BFS for binary tree width Dijkstra for shortest path)

How to implement tabbar title bar with list component

QT learning diary 7 - qmainwindow

使用华为性能管理服务,按需配置采样率

flink二開,實現了個 batch lookup join(附源碼)

2022 love analysis · panoramic report of digital manufacturers of state-owned enterprises

How does the whole network display IP ownership?

Hdu1234 door opener and door closer (water question)

How to transfer event objects and user-defined parameters simultaneously in Huawei express applications

【AI应用】海康威视iVMS-4200软件安装
随机推荐
The first white paper on agile practice in Chinese enterprises was released | complete download is attached
TIPC介绍1
What are the software product management systems? Inventory of 12 best product management tools
2022 love analysis · panoramic report of digital manufacturers of state-owned enterprises
Array splitting (regular thinking
QT learning diary 8 - resource file addition
一.STM32的开发环境,keil5/MDK5.14安装教程(附下载链接)
洛谷 P3398 仓鼠找 sugar(树上倍增 lca 判断树中两条路径是否相交 结论)
QT learning diary 7 - qmainwindow
PCL extracts a subset from a point cloud
Calculate the sum of sequences
Win11 arm system configuration Net core environment variable
金山云——2023届暑期实习
二.Stm32f407芯片GPIO编程,寄存器操作,库函数操作和位段操作
TIPC 寻址2
Leetcode 182 Find duplicate email (2022.07.01)
V2x SIM dataset (Shanghai Jiaotong University & New York University)
二叉树专题--AcWing 3540. 二叉搜索树建树(实用板子 构建二叉搜索树 并输出前、中、后序遍历)
php中self和static在方法中的区别
Overview of integrated learning