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 )
0 commit comments