Skip to content

Commit 000ca59

Browse files
committed
Adds DailyAlgorithm.py
Add Python version of DailyAlgorithm
1 parent 0d493af commit 000ca59

2 files changed

Lines changed: 67 additions & 0 deletions

File tree

Algorithm.Python/DailyAlgorithm.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
2+
# Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
import clr
15+
clr.AddReference("System")
16+
clr.AddReference("QuantConnect.Algorithm")
17+
clr.AddReference("QuantConnect.Indicators")
18+
clr.AddReference("QuantConnect.Common")
19+
20+
from System import *
21+
from QuantConnect import *
22+
from QuantConnect.Algorithm import *
23+
from QuantConnect.Indicators import *
24+
25+
26+
class DailyAlgorithm(QCAlgorithm):
27+
'''Uses daily data and a simple moving average cross to place trades and an ema for stop placement'''
28+
29+
def __init__(self):
30+
self.lastAction = None
31+
self.macd = None
32+
self.ema = None
33+
34+
35+
def Initialize(self):
36+
'''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''
37+
38+
self.SetStartDate(2013,01,01) #Set Start Date
39+
self.SetEndDate(2014,01,01) #Set End Date
40+
self.SetCash(100000) #Set Strategy Cash
41+
# Find more symbols here: http://quantconnect.com/data
42+
self.AddSecurity(SecurityType.Equity, "SPY", Resolution.Daily)
43+
self.AddSecurity(SecurityType.Equity, "IBM", Resolution.Hour)
44+
self.Securities["IBM"].SetLeverage(1.0)
45+
46+
self.macd = self.MACD("SPY", 12, 26, 9, MovingAverageType.Wilders, Resolution.Daily, Field.Close)
47+
self.ema = self.EMA("IBM", 15*6, Resolution.Hour, Field.SevenBar)
48+
49+
50+
def OnData(self, data):
51+
'''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
52+
53+
Arguments:
54+
data: Slice object keyed by symbol containing the stock data
55+
'''
56+
if not self.macd.IsReady: return
57+
if not data.ContainsKey("IBM"): return
58+
if self.lastAction is not None and self.lastAction.Date == self.Time.Date: return
59+
60+
self.lastAction = self.Time
61+
holding = self.Portfolio["SPY"]
62+
63+
if holding.Quantity <= 0 and self.macd.Current.Value > self.macd.Signal.Current.Value and data["IBM"].Price > self.ema.Current.Value:
64+
self.SetHoldings("IBM", 0.25)
65+
elif holding.Quantity >= 0 and self.macd.Current.Value < self.macd.Signal.Current.Value and data["IBM"].Price < self.ema.Current.Value:
66+
self.SetHoldings("IBM", -0.25)

Algorithm.Python/QuantConnect.Algorithm.Python.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
</ItemGroup>
105105
<ItemGroup>
106106
<Content Include="AddRemoveSecurityRegressionAlgorithm.py" />
107+
<Content Include="DailyAlgorithm.py" />
107108
<Content Include="ParameterizedAlgorithm.py" />
108109
<Content Include="UpdateOrderRegressionAlgorithm.py" />
109110
<Content Include="BasicTemplateAlgorithm.py" />

0 commit comments

Comments
 (0)