无意中发现了一个巨牛的人工智能教程，忍不住分享一下给大家。教程不仅是零基础，通俗易懂，而且非常风趣幽默，像看小说一样！觉得太牛了，所以分享给大家。教程链接：https://www.cbedai.net/qtlyx

# 1.resampling

这个方法，字面意思看起来是“采样”，准确的来说，是上采样，从小的时间点变成大的时间点。

后面的参数主要有两个，一个是timeframe，也就是你希望变成的timeframe是多少，day还是week；另外一个是compression，就是对bar进行压缩。

# 2.代码

所有的代码是这样的:

from __future__ import (absolute_import, division, print_function,
unicode_literals)

import datetime  # For datetime objects
import pandas as pd
import numpy as np

class MyStrategy(bt.Strategy):
params = (
('ssa_window', 15),
('maperiod', 15),
)

def log(self, txt, dt=None):
''' Logging function fot this strategy'''
dt = dt or self.datas[0].datetime.date(0)
print('%s, %s' % (dt.isoformat(), txt))

def __init__(self):
# Keep a reference to the "close" line in the data[0] dataseries
self.dataclose = self.datas[0].close

# To keep track of pending orders and buy price/commission
self.order = None

self.sma = bt.indicators.SimpleMovingAverage(
self.datas[0], period=self.params.maperiod)

def start(self):
print("the world call me!")

def prenext(self):
print("not mature")

def notify_order(self, order):
if order.status in [order.Submitted, order.Accepted]:
# Buy/Sell order submitted/accepted to/by broker - Nothing to do
return

# Check if an order has been completed
# Attention: broker could reject order if not enougth cash
if order.status in [order.Completed]:
self.log(
'BUY EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' %
(order.executed.price,
order.executed.value,
order.executed.comm))

else:  # Sell
self.log('SELL EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' %
(order.executed.price,
order.executed.value,
order.executed.comm))

self.bar_executed = len(self)

elif order.status in [order.Canceled, order.Margin, order.Rejected]:
self.log('Order Canceled/Margin/Rejected')

self.order = None

return

self.log('OPERATION PROFIT, GROSS %.2f, NET %.2f' %

def next(self):
# Simply log the closing price of the series from the reference
self.log('Close, %.2f' % self.dataclose[0])

# Check if an order is pending ... if yes, we cannot send a 2nd one
if self.order:
return

# Check if we are in the market
if not self.position:

# Not yet ... we MIGHT BUY if ...
if self.dataclose[0] > self.sma[0]:

# Keep track of the created order to avoid a 2nd order

else:

if self.dataclose[0] < self.sma[0]:
# SELL, SELL, SELL!!! (with all possible default parameters)
self.log('SELL CREATE, %.2f' % self.dataclose[0])

# Keep track of the created order to avoid a 2nd order
self.order = self.sell()
def stop(self):
print("death")

if __name__ == '__main__':

# Create a cerebro entity
cerebro = bt.Cerebro(stdstats=False)

data0 = bt.feeds.PandasData(dataname=dataframe,
fromdate=datetime.datetime(2014, 5, 13),
todate=datetime.datetime(2014, 6, 20),
timeframe=bt.TimeFrame.Minutes
)
# Add the Data Feed to Cerebro

data2 = cerebro.resampledata(data0, timeframe=bt.TimeFrame.Days)
cerebro.run()
cerebro.plot(style='bar')



 
2014-05-13 08:59:00.005004,3198.0,3198.0,3198.0,3198.0,2148.0,0 2014-05-13 09:00:00.005000,3198.0,3202.0,3195.0,3195.0,37426.0,0 2014-05-13 09:01:00.004997,3195.0,3199.0,3194.0,3198.0,19704.0,0 2014-05-13 09:02:00.005003,3198.0,3199.0,3193.0,3193.0,22682.0,0 2014-05-13 09:03:00.005000,3193.0,3195.0,3192.0,3193.0,23064.0,0 2014-05-13 09:04:00.004996,3193.0,3194.0,3190.0,3190.0,29058.0,0 2014-05-13 09:05:00.005002,3191.0,3191.0,3186.0,3188.0,25044.0,0 2014-05-13 09:06:00.004999,3188.0,3189.0,3186.0,3189.0,16020.0,0 2014-05-13 09:07:00.004995,3189.0,3189.0,3187.0,3188.0,12336.0,0 2014-05-13 09:08:00.005002,3188.0,3188.0,3185.0,3186.0,20484.0,0 2014-05-13 09:09:00.004998,3186.0,3187.0,3184.0,3186.0,19234.0,0


 
	dataframe = pd.read_csv('RB.SHF.csv', index_col=0, parse_dates=True)dataframe = pd.read_csv('RB.SHF.csv', index_col=0, parse_dates=True)
    data0 = bt.feeds.PandasData(dataname=dataframe,
fromdate=datetime.datetime(2014, 5, 13),
todate=datetime.datetime(2014, 6, 20),
timeframe=bt.TimeFrame.Minutes
)
    #timeframe=bt.TimeFrame.Minutes用来指明datafeed的timeframe，默认是days

# Add the Data Feed to Cerebro，就像平常一样

cerebro.run()
cerebro.plot(style='bar')
timeframe=bt.TimeFrame.Minutes用来指明datafeed的timeframe，默认是days

# Add the Data Feed to Cerebro，就像平常一样

cerebro.run()
cerebro.plot(style='bar')


©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客