当前位置:网站首页>Tick Data and Resampling
Tick Data and Resampling
2022-07-02 07:44: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()边栏推荐
猜你喜欢

2022 love analysis · panoramic report of digital manufacturers of state-owned enterprises
![[play with FPGA learning 4 in simple terms ----- talk about state machine design]](/img/e0/95f8b8c5116c57455e54ad12372f12.png)
[play with FPGA learning 4 in simple terms ----- talk about state machine design]

三.芯片啟動和時鐘系統

一招快速实现自定义快应用titlebar

I STM32 development environment, keil5/mdk5.14 installation tutorial (with download link)

【快应用】text组件里的文字很多,旁边的div样式会被拉伸如何解决

首份中国企业敏捷实践白皮书发布| 附完整下载

华为游戏初始化init失败,返回错误码907135000

How to use ide to automatically sign and debug Hongmeng application

Hdu1228 a + B (map mapping)
随机推荐
二叉树专题--AcWing 3384. 二叉树遍历(已知先序遍历 边建树 边输出中序遍历)
[AGC] how to solve the problem that the local display of event analysis data is inconsistent with that in AGC panel?
二叉树专题--AcWing 3540. 二叉搜索树建树(实用板子 构建二叉搜索树 并输出前、中、后序遍历)
Flick two open, realized a batch lookup join (with source code)
Special topic of binary tree -- acwing 47 Path with a certain value in binary tree (preorder traversal)
华为联机对战服务玩家掉线重连案例总结
二叉树专题--AcWing 47. 二叉树中和为某一值的路径(前序遍历)
JSP webshell免殺——JSP的基礎
Special topic of binary tree -- acwing 19 The next node of the binary tree (find the successor of the node in the tree)
Special topic of binary tree -- acwing 3384 Binary tree traversal (known preorder traversal, while building a tree, while outputting middle order traversal)
Matlab processing of distance measurement of experimental electron microscope
[quick application] win7 system cannot run and debug projects using Huawei ide
计算序列之和
Primary key policy problem
Leetcode 182 Find duplicate email (2022.07.01)
[AI application] Hikvision ivms-4200 software installation
二叉树专题--P1030 [NOIP2001 普及组] 求先序排列
OpenMLDB Meetup No.4 会议纪要
TIPC protocol
三.芯片启动和时钟系统