Skip to content

Commit a3ddbb8

Browse files
AlexCatarinomchandschuh
authored andcommitted
Python algos in a file with the algo name like C#
build.bat (batch.sh in Linux) includes the code inside the file with the desired algorithm (set in config.json by "algorithm-type-name") in the python DLL (QuantConnect.Algorithm.Python.dll) via IronPython. In the AlgorithmFactory, Loader.TryCreatePythonAlgorithm imports the module with the algorithm name instead of "main". Closes #289
1 parent 9b1cc73 commit a3ddbb8

5 files changed

Lines changed: 58 additions & 7 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 BasicTemplateAlgorithm(QCAlgorithm):
27+
'''Basic template algorithm simply initializes the date range and cash'''
28+
29+
def Initialize(self):
30+
'''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''
31+
32+
self.SetStartDate(2013,10,07) #Set Start Date
33+
self.SetEndDate(2013,10,11) #Set End Date
34+
self.SetCash(100000) #Set Strategy Cash
35+
# Find more symbols here: http://quantconnect.com/data
36+
self.AddSecurity(SecurityType.Equity, "SPY", Resolution.Second)
37+
38+
def OnData(self, data):
39+
'''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
40+
41+
Arguments:
42+
data: Slice object keyed by symbol containing the stock data
43+
'''
44+
if not self.Portfolio.Invested:
45+
self.SetHoldings("SPY", 1)

‎Algorithm.Python/QuantConnect.Algorithm.Python.csproj‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,14 @@
9696
</None>
9797
<None Include="Lib\ensurepip\_bundled\pip-1.5.6-py2.py3-none-any.whl" />
9898
<None Include="Lib\ensurepip\_bundled\setuptools-3.6-py2.py3-none-any.whl" />
99-
<None Include="main.py">
100-
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
101-
</None>
10299
<None Include="build.bat">
103100
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
104101
</None>
105102
<None Include="packages.config" />
106103
<None Include="readme.md" />
107104
</ItemGroup>
108105
<ItemGroup>
106+
<Content Include="BasicTemplateAlgorithm.py" />
109107
<Content Include="Lib\abc.py" />
110108
<Content Include="Lib\aifc.py" />
111109
<Content Include="Lib\antigravity.py" />

‎Algorithm.Python/build.bat‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ REM Clean the output directory
88
del "QuantConnect.Algorithm.Python.dll"
99
del "../Launcher/bin/Debug/QuantConnect.Algorithm.Python.dll"
1010

11+
REM Get algorithm-type-name from config.json
12+
setlocal EnableDelayedExpansion
13+
for /f "tokens=2 delims=:, " %%a in (' find "algorithm-type-name" ^< "../Launcher/config.json" ') do ( set pyfile=%%~a.py )
14+
1115
REM IronPython Compile the Algorithm
12-
ipy %pyc% /target:dll /out:QuantConnect.Algorithm.Python main.py
16+
ipy %pyc% /target:dll /out:QuantConnect.Algorithm.Python !pyfile!
1317

1418
REM Copy to the Lean Algorithm Project
1519
echo f|xcopy /Y "QuantConnect.Algorithm.Python.dll" "../Launcher/bin/Debug/QuantConnect.Algorithm.Python.dll"

‎Algorithm.Python/build.sh‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ rm ../Launcher/bin/Debug/QuantConnect.Algorithm.Python.dll
77
# Set the script variables: assuming installing the ./compiler/library/ and the caller is in ./compiler/bin/Debug/build.sh
88
ipy="../../IronPython-2.7.5/ipy.exe"
99
pyc="../../IronPython-2.7.5/Tools/Scripts/pyc.py"
10-
10+
11+
# Get algorithm-type-name from config.json
12+
pyfile=$(grep -Po '(?<="algorithm-type-name": ")[^"]*' ../Launcher/config.json).py
13+
1114
# Call the compiler:
12-
mono $ipy $pyc /target:dll /out:QuantConnect.Algorithm.Python main.py
15+
mono $ipy $pyc /target:dll /out:QuantConnect.Algorithm.Python $pyfile
1316

1417
# Copy to the Lean Algorithm Project
1518
cp QuantConnect.Algorithm.Python.dll ../Launcher/bin/Debug/QuantConnect.Algorithm.Python.dll

‎AlgorithmFactory/Loader.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ private bool TryCreatePythonAlgorithm(string assemblyPath, out IAlgorithm algori
162162
try
163163
{
164164
Log.Trace("Loader.TryCreatePythonAlgorithm(): Importing python module...");
165-
var scope = engine.Runtime.ImportModule("main");
165+
var algorithmName = Config.Get("algorithm-type-name");
166+
var scope = engine.Runtime.ImportModule(algorithmName);
166167
items = (List<KeyValuePair<string, dynamic>>)scope.GetItems();
167168
}
168169
catch (Exception err)

0 commit comments

Comments
 (0)