# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import pandas as pd from zipline import TradingAlgorithm from zipline.api import order, sid, get_datetime from zipline.data.loader import load_data from zipline.api import order_target, record, symbol, history, add_history, symbol, set_commission, order_percent, set_long_only, get_open_orders from zipline.finance.commission import OrderCost from pylab import * mpl.rcParams['font.sans-serif'] = ['SimHei'] mpl.rcParams['axes.unicode_minus'] = False # loading the data input_data = load_data(stockList=['002057'], start="2015-11-04", end="2016-01-16") def analyze(context=None, results=None): import matplotlib.pyplot as plt # Plot the portfolio and asset data. ax1 = plt.subplot(211) results.algorithm_period_return.plot(ax=ax1, color='blue', legend=u'策略收益') ax1.set_ylabel(u'收益') results.benchmark_period_return.plot(ax=ax1, color='red', legend=u'基准收益') # Show the plot. plt.gcf().set_size_inches(18, 8) plt.show()
from zipline.api import order, sid,get_datetime from zipline.data.loader import load_data from zipline.api import order_target, record, \ symbol, history, add_history,symbol,set_commission,order_percent,set_long_only,get_open_orders,order_value,order_target from zipline.finance.commission import OrderCost from pylab import * mpl.rcParams['font.sans-serif'] = ['SimHei'] mpl.rcParams['axes.unicode_minus'] = False import pytz from datetime import datetime, timedelta input_data = load_data( stockList=['000001'], start="2014-11-04", end="2016-01-16" ) # 初始化函数,设定要操作的股票、基准等等 def initialize(context): # 定义一个全局变量, 保存要操作的股票 # 000001(股票:平安银行) context.security = '000001' # 每个单位时间(如果按天回测,则每天调用一次,如果按分钟,则每分钟调用一次)调用一次 def handle_data(context, data): # 获取股票的收盘价 close_data = history(12,'1d','close') # 取得过去五天的平均价格
# Plot the portfolio and asset data. ax1 = plt.subplot(211) results.algorithm_period_return.plot(ax=ax1,color='blue',legend=u'策略收益') ax1.set_ylabel(u'收益') results.benchmark_period_return.plot(ax=ax1,color='red',legend=u'基准收益') # Show the plot. plt.gcf().set_size_inches(18, 8) plt.show() # loading the data input_data = load_data( stockList= ['000001','000002','000004','000005'], start="2013-11-01", end="2016-01-16" ) def initialize(context): # 初始化此策略 # 设置我们要操作的股票池 context.stocks = ['000001','000002','000004','000005'] # 每个单位时间(如果按天回测,则每天调用一次,如果按分钟,则每分钟调用一次)调用一次 def handle_data(context, data): # context.i+=1
moment = Moment(app) app.config['MONGO_HOST'] = '127.0.0.1' app.config['MONGO_PORT'] = 27017 mongo = PyMongo(app) client = pymongo.MongoClient('127.0.0.1',27017) oriprice=client.stockoriginalprice #股票原始数据 stocks=oriprice.collection_names() stocklist=[] # for s in stocks: # stocklist.append(s[2:8]) # client.close() input_data = load_data(stockList=['000919'],start="1990-01-01",end="2016-01-16") print input_data def is_valid_date(str): '''判断是否是一个有效的日期字符串''' try: time.strptime(str, "%Y-%m-%d") return True except: return False def toJson(data): return json.dumps(data, default=json_util.default) # @app.errorhandler(404) # def page_not_found(e):