# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.# Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# 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.from AlgorithmImports import *### <summary>### Algorithm demonstrating custom charting support in QuantConnect.### The entire charting system of quantconnect is adaptable. You can adjust it to draw whatever you'd like.### Charts can be stacked, or overlayed on each other. Series can be candles, lines or scatter plots.### Even the default behaviours of QuantConnect can be overridden.### </summary>### <meta name="tag" content="charting" />### <meta name="tag" content="adding charts" />### <meta name="tag" content="series types" />### <meta name="tag" content="plotting indicators" />class CustomChartingAlgorithm(QCAlgorithm):def initialize(self):self.set_start_date(2016,1,1)self.set_end_date(2017,1,1)self.set_cash(100000)spy = self.add_equity("SPY", Resolution.DAILY).symbol# In your initialize method:# Chart - Master Container for the Chart:stock_plot = Chart("Trade Plot")# On the Trade Plotter Chart we want 3 series: trades and price:stock_plot.add_series(Series("Buy", SeriesType.SCATTER, 0))stock_plot.add_series(Series("Sell", SeriesType.SCATTER, 0))stock_plot.add_series(Series("Price", SeriesType.LINE, 0))self.add_chart(stock_plot)# On the Average Cross Chart we want 2 series, slow MA and fast MAavg_cross = Chart("Average Cross")avg_cross.add_series(Series("FastMA", SeriesType.LINE, 0))avg_cross.add_series(Series("SlowMA", SeriesType.LINE, 0))self.add_chart(avg_cross)# There's support for candlestick charts built-in:weekly_spy_plot = Chart("Weekly SPY")spy_candlesticks = CandlestickSeries("SPY")weekly_spy_plot.add_series(spy_candlesticks)self.add_chart(weekly_spy_plot)self.consolidate(spy, Calendar.WEEKLY, lambda bar: self.plot("Weekly SPY", "SPY", bar))self.fast_ma = 0self.slow_ma = 0self.last_price = 0self.resample = datetime.minself.resample_period = (self.end_date - self.start_date) / 2000def on_data(self, slice):if slice["SPY"] is None: returnself.last_price = slice["SPY"].closeif self.fast_ma == 0: self.fast_ma = self.last_priceif self.slow_ma == 0: self.slow_ma = self.last_priceself.fast_ma = (0.01 * self.last_price) + (0.99 * self.fast_ma)self.slow_ma = (0.001 * self.last_price) + (0.999 * self.slow_ma)if self.time > self.resample:self.resample = self.time + self.resample_periodself.plot("Average Cross", "FastMA", self.fast_ma)self.plot("Average Cross", "SlowMA", self.slow_ma)# On the 5th days when not invested buy:if not self.portfolio.invested and self.time.day % 13 == 0:self.order("SPY", (int)(self.portfolio.margin_remaining / self.last_price))self.plot("Trade Plot", "Buy", self.last_price)elif self.time.day % 21 == 0 and self.portfolio.invested:self.plot("Trade Plot", "Sell", self.last_price)self.liquidate()def on_end_of_day(self, symbol):#Log the end of day prices:self.plot("Trade Plot", "Price", self.last_price)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。