forked from redapple1990/Python-trading-Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CustomChartingAlgorithm.py
90 lines (77 loc) · 3.7 KB
/
CustomChartingAlgorithm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 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 clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Common")
from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
import numpy as np
import decimal as d
from datetime import timedelta, datetime
### <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.SetStartDate(2016,1,1)
self.SetEndDate(2017,1,1)
self.SetCash(100000)
self.AddEquity("SPY", Resolution.Daily)
# In your initialize method:
# Chart - Master Container for the Chart:
stockPlot = Chart("Trade Plot")
# On the Trade Plotter Chart we want 3 series: trades and price:
stockPlot.AddSeries(Series("Buy", SeriesType.Scatter, 0))
stockPlot.AddSeries(Series("Sell", SeriesType.Scatter, 0))
stockPlot.AddSeries(Series("Price", SeriesType.Line, 0))
self.AddChart(stockPlot)
# On the Average Cross Chart we want 2 series, slow MA and fast MA
avgCross = Chart("Average Cross")
avgCross.AddSeries(Series("FastMA", SeriesType.Line, 0))
avgCross.AddSeries(Series("SlowMA", SeriesType.Line, 0))
self.AddChart(avgCross)
self.fastMA = 0
self.slowMA = 0
self.lastPrice = 0
self.resample = datetime.min
self.resamplePeriod = (self.EndDate - self.StartDate) / 2000
def OnData(self, slice):
if slice["SPY"] is None: return
self.lastPrice = slice["SPY"].Close
if self.fastMA == 0: self.fastMA = self.lastPrice
if self.slowMA == 0: self.slowMA = self.lastPrice
self.fastMA = (0.01 * self.lastPrice) + (0.99 * self.fastMA)
self.slowMA = (0.001 * self.lastPrice) + (0.999 * self.slowMA)
if self.Time > self.resample:
self.resample = self.Time + self.resamplePeriod
self.Plot("Average Cross", "FastMA", self.fastMA)
self.Plot("Average Cross", "SlowMA", self.slowMA)
# 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.MarginRemaining / self.lastPrice))
self.Plot("Trade Plot", "Buy", self.lastPrice)
elif self.Time.day % 21 == 0 and self.Portfolio.Invested:
self.Plot("Trade Plot", "Sell", self.lastPrice)
self.Liquidate()
def OnEndOfDay(self):
#Log the end of day prices:
self.Plot("Trade Plot", "Price", self.lastPrice)