当数据仅在单个时间范围内可用,需要在不同的时间范围内进行分析时,就需要进行一些重采样。
“重采样”实际上应该称为“上采样”,因为它是从一个源时间区间到一个更大的时间区间(例如:几天到几周)
语法:
cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes, compression=1)
两个目的:
参数:
前置条件:
程序:
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/2006-day-001.txt'
data = btfeeds.BacktraderCSVData(dataname=datapath)
# Handy dictionary for the argument timeframe conversion
tframes = dict(
daily=bt.TimeFrame.Days,
weekly=bt.TimeFrame.Weeks,
monthly=bt.TimeFrame.Months)
# Add the resample data instead of the original
cerebro.resampledata(data,
timeframe=tframes[args.timeframe],
compression=args.compression)
# Run over everything
cerebro.run()
# Plot the result
cerebro.plot(style='bar')
def parse_args():
parser = argparse.ArgumentParser(
description='Pandas test script')
parser.add_argument('--dataname', default='', required=False,
help='File Data to Load')
parser.add_argument('--timeframe', default='weekly', required=False,
choices=['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()
执行效果:
python ./resampling-example.py --timeframe weekly --compression 1
python ./resampling-example.py --timeframe monthly --compression 1
用分钟线模拟日线当然不是市场精确的走势,但比孤立地等待每天完全形成的日线好得多。
如果策略定义在日线中实时运行处理,日线形成的近似值就可能在真实条件下重演策略的实际操作。
语法:
cerebro.replaydata(data,timeframe,compression)
参数与resample一样。
程序:
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import argparse
import backtrader as bt
import backtrader.feeds as btfeeds
import backtrader.indicators as btind
class SMAStrategy(bt.Strategy):
params = (
('period', 10),
('onlydaily', False),
)
def __init__(self):
self.sma = btind.SMA(self.data, period=self.p.period)
def start(self):
self.counter = 0
def prenext(self):
self.counter += 1
print('prenext len %d - counter %d' % (len(self), self.counter))
def next(self):
self.counter += 1
print('---next len %d - counter %d' % (len(self), self.counter))
def runstrat():
args = parse_args()
# Create a cerebro entity
cerebro = bt.Cerebro(stdstats=False)
cerebro.addstrategy(
SMAStrategy,
# args for the strategy
period=args.period,
)
# Load the Data
datapath = args.dataname or './datas/2006-day-001.txt'
data = btfeeds.BacktraderCSVData(dataname=datapath)
# Handy dictionary for the argument timeframe conversion
tframes = dict(
daily=bt.TimeFrame.Days,
weekly=bt.TimeFrame.Weeks,
monthly=bt.TimeFrame.Months)
# First add the original data - smaller timeframe
cerebro.replaydata(data,
timeframe=tframes[args.timeframe],
compression=args.compression)
# Run over everything
cerebro.run()
# Plot the result
cerebro.plot(style='bar')
def parse_args():
parser = argparse.ArgumentParser(
description='Pandas test script')
parser.add_argument('--dataname', default='', required=False,
help='File Data to Load')
parser.add_argument('--timeframe', default='weekly', required=False,
choices=['daily', 'weekly', 'monthly'],
help='Timeframe to resample to')
parser.add_argument('--compression', default=1, required=False, type=int,
help='Compress n bars into 1')
parser.add_argument('--period', default=10, required=False, type=int,
help='Period to apply to indicator')
return parser.parse_args()
if __name__ == '__main__':
runstrat()
python replaying_example.py --timeframe daily --compression 2
prenext len 1 - counter 1
prenext len 1 - counter 2
prenext len 2 - counter 3
prenext len 2 - counter 4
prenext len 3 - counter 5
prenext len 3 - counter 6
prenext len 4 - counter 7
prenext len 4 - counter 8
prenext len 5 - counter 9
prenext len 5 - counter 10
prenext len 6 - counter 11
prenext len 6 - counter 12
prenext len 7 - counter 13
prenext len 7 - counter 14
prenext len 8 - counter 15
prenext len 8 - counter 16
prenext len 9 - counter 17
prenext len 9 - counter 18
---next len 10 - counter 19
---next len 10 - counter 20
---next len 11 - counter 21
---next len 11 - counter 22
---next len 12 - counter 23
---next len 12 - counter 24
---next len 13 - counter 25
---next len 13 - counter 26
---next len 14 - counter 27
---next len 14 - counter 28
---next len 15 - counter 29
---next len 15 - counter 30
---next len 16 - counter 31
---next len 16 - counter 32
---next len 17 - counter 33
---next len 17 - counter 34
---next len 18 - counter 35
---next len 18 - counter 36
---next len 19 - counter 37
---next len 19 - counter 38
---next len 20 - counter 39
---next len 20 - counter 40
---next len 21 - counter 41
---next len 21 - counter 42
---next len 22 - counter 43
---next len 22 - counter 44
---next len 23 - counter 45
---next len 23 - counter 46
---next len 24 - counter 47
---next len 24 - counter 48
---next len 25 - counter 49
---next len 25 - counter 50
---next len 26 - counter 51
---next len 26 - counter 52
---next len 27 - counter 53
---next len 27 - counter 54
---next len 28 - counter 55
---next len 28 - counter 56
---next len 29 - counter 57
---next len 29 - counter 58
---next len 30 - counter 59
---next len 30 - counter 60
---next len 31 - counter 61
---next len 31 - counter 62
---next len 32 - counter 63
---next len 32 - counter 64
---next len 33 - counter 65
---next len 33 - counter 66
---next len 34 - counter 67
---next len 34 - counter 68
---next len 35 - counter 69
---next len 35 - counter 70
---next len 36 - counter 71
---next len 36 - counter 72
---next len 37 - counter 73
---next len 37 - counter 74
---next len 38 - counter 75
---next len 38 - counter 76
---next len 39 - counter 77
---next len 39 - counter 78
---next len 40 - counter 79
---next len 40 - counter 80
---next len 41 - counter 81
---next len 41 - counter 82
---next len 42 - counter 83
---next len 42 - counter 84
---next len 43 - counter 85
---next len 43 - counter 86
---next len 44 - counter 87
---next len 44 - counter 88
---next len 45 - counter 89
---next len 45 - counter 90
---next len 46 - counter 91
---next len 46 - counter 92
---next len 47 - counter 93
---next len 47 - counter 94
---next len 48 - counter 95
---next len 48 - counter 96
---next len 49 - counter 97
---next len 49 - counter 98
---next len 50 - counter 99
---next len 50 - counter 100
---next len 51 - counter 101
---next len 51 - counter 102
---next len 52 - counter 103
---next len 52 - counter 104
---next len 53 - counter 105
---next len 53 - counter 106
---next len 54 - counter 107
---next len 54 - counter 108
---next len 55 - counter 109
---next len 55 - counter 110
---next len 56 - counter 111
---next len 56 - counter 112
---next len 57 - counter 113
---next len 57 - counter 114
---next len 58 - counter 115
---next len 58 - counter 116
---next len 59 - counter 117
---next len 59 - counter 118
---next len 60 - counter 119
---next len 60 - counter 120
---next len 61 - counter 121
---next len 61 - counter 122
---next len 62 - counter 123
---next len 62 - counter 124
---next len 63 - counter 125
---next len 63 - counter 126
---next len 64 - counter 127
---next len 64 - counter 128
---next len 65 - counter 129
---next len 65 - counter 130
---next len 66 - counter 131
---next len 66 - counter 132
---next len 67 - counter 133
---next len 67 - counter 134
---next len 68 - counter 135
---next len 68 - counter 136
---next len 69 - counter 137
---next len 69 - counter 138
---next len 70 - counter 139
---next len 70 - counter 140
---next len 71 - counter 141
---next len 71 - counter 142
---next len 72 - counter 143
---next len 72 - counter 144
---next len 73 - counter 145
---next len 73 - counter 146
---next len 74 - counter 147
---next len 74 - counter 148
---next len 75 - counter 149
---next len 75 - counter 150
---next len 76 - counter 151
---next len 76 - counter 152
---next len 77 - counter 153
---next len 77 - counter 154
---next len 78 - counter 155
---next len 78 - counter 156
---next len 79 - counter 157
---next len 79 - counter 158
---next len 80 - counter 159
---next len 80 - counter 160
---next len 81 - counter 161
---next len 81 - counter 162
---next len 82 - counter 163
---next len 82 - counter 164
---next len 83 - counter 165
---next len 83 - counter 166
---next len 84 - counter 167
---next len 84 - counter 168
---next len 85 - counter 169
---next len 85 - counter 170
---next len 86 - counter 171
---next len 86 - counter 172
---next len 87 - counter 173
---next len 87 - counter 174
---next len 88 - counter 175
---next len 88 - counter 176
---next len 89 - counter 177
---next len 89 - counter 178
---next len 90 - counter 179
---next len 90 - counter 180
---next len 91 - counter 181
---next len 91 - counter 182
---next len 92 - counter 183
---next len 92 - counter 184
---next len 93 - counter 185
---next len 93 - counter 186
---next len 94 - counter 187
---next len 94 - counter 188
---next len 95 - counter 189
---next len 95 - counter 190
---next len 96 - counter 191
---next len 96 - counter 192
---next len 97 - counter 193
---next len 97 - counter 194
---next len 98 - counter 195
---next len 98 - counter 196
---next len 99 - counter 197
---next len 99 - counter 198
---next len 100 - counter 199
---next len 100 - counter 200
---next len 101 - counter 201
---next len 101 - counter 202
---next len 102 - counter 203
---next len 102 - counter 204
---next len 103 - counter 205
---next len 103 - counter 206
---next len 104 - counter 207
---next len 104 - counter 208
---next len 105 - counter 209
---next len 105 - counter 210
---next len 106 - counter 211
---next len 106 - counter 212
---next len 107 - counter 213
---next len 107 - counter 214
---next len 108 - counter 215
---next len 108 - counter 216
---next len 109 - counter 217
---next len 109 - counter 218
---next len 110 - counter 219
---next len 110 - counter 220
---next len 111 - counter 221
---next len 111 - counter 222
---next len 112 - counter 223
---next len 112 - counter 224
---next len 113 - counter 225
---next len 113 - counter 226
---next len 114 - counter 227
---next len 114 - counter 228
---next len 115 - counter 229
---next len 115 - counter 230
---next len 116 - counter 231
---next len 116 - counter 232
---next len 117 - counter 233
---next len 117 - counter 234
---next len 118 - counter 235
---next len 118 - counter 236
---next len 119 - counter 237
---next len 119 - counter 238
---next len 120 - counter 239
---next len 120 - counter 240
---next len 121 - counter 241
---next len 121 - counter 242
---next len 122 - counter 243
---next len 122 - counter 244
---next len 123 - counter 245
---next len 123 - counter 246
---next len 124 - counter 247
---next len 124 - counter 248
---next len 125 - counter 249
---next len 125 - counter 250
---next len 126 - counter 251
---next len 126 - counter 252
---next len 127 - counter 253
---next len 127 - counter 254
---next len 128 - counter 255
python ./resampling-example.py --timeframe weekly --compression 1
python ./resampling-example.py --timeframe monthly --compression 1
Replay和Resample 应用场景:
在 BackTrader 框架中主要用于回放历史数据,以在策略中进行回测和验证。以下是其应用场景的一些示例:
在 BackTrader 框架中主要用于对数据进行重采样,以适应不同的时间间隔或频率。以下是其应用场景的一些示例:
貌似处理期货交割的时间连续性。
import backtrader as bt
cerebro = bt.Cerebro()
data0 = bt.feeds.MyFeed(dataname='Expiry0')
data1 = bt.feeds.MyFeed(dataname='Expiry1')
...
dataN = bt.feeds.MyFeed(dataname='ExpiryN')
drollover = bt.feeds.RollOver(data0, data1, ..., dataN, dataname='MyRoll', **kwargs)
cerebro.adddata(drollover)
cerebro.run()
没有数据,无法测试理解。
应用场景没有明白,暂时放一下。
其他的除了Panda ,用Panda 加载数据前面应用多次了,其他也没有应用场景。