Skip to content

Commit 21f0222

Browse files
committed
Add template algorithms
Two demonstration algorithms showing how to incorporate Python into a C# algorithm and vice versa
1 parent bf4a8b1 commit 21f0222

4 files changed

Lines changed: 102 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3+
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System;
17+
using QuantConnect.Data;
18+
using Python.Runtime;
19+
namespace QuantConnect.Algorithm.CSharp
20+
{
21+
public class BasicPythonIntegrationTemplateAlgorithm : QCAlgorithm
22+
{
23+
24+
public override void Initialize()
25+
{
26+
SetStartDate(2018, 11, 10); //Set Start Date
27+
SetEndDate(2018, 12, 1);
28+
SetCash(100000); //Set Strategy Cash
29+
AddEquity("SPY", Resolution.Minute);
30+
31+
}
32+
33+
private decimal ComputeSin(decimal value)
34+
{
35+
using (Py.GIL())
36+
{
37+
dynamic np = Py.Import("numpy");
38+
return (decimal)np.sin(value);
39+
}
40+
}
41+
42+
/// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
43+
/// Slice object keyed by symbol containing the stock data
44+
public override void OnData(Slice data)
45+
{
46+
if (!Portfolio.Invested)
47+
{
48+
SetHoldings("SPY", 1);
49+
var sin = ComputeSin(10);
50+
var sinOfTen = Math.Sin(10);
51+
Log($"According to Python, the value of sin(10) is: {sin}");
52+
Log($"According to C#, the value of sin(10) is: {sinOfTen}");
53+
}
54+
}
55+
}
56+
}

‎Algorithm.CSharp/QuantConnect.Algorithm.CSharp.csproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@
138138
<Compile Include="Alphas\TriangleExchangeRateArbitrageAlpha.cs" />
139139
<Compile Include="Alphas\TripleLeveragedETFPairVolatilityDecayAlpha.cs" />
140140
<Compile Include="Alphas\VixDualThrustAlpha.cs" />
141+
<Compile Include="BasicPythonIntegrationTemplateAlgorithm.cs" />
141142
<Compile Include="BasicSetAccountCurrencyAlgorithm.cs" />
142143
<Compile Include="Benchmarks\StatefulCoarseUniverseSelectionBenchmark.cs" />
143144
<Compile Include="Benchmarks\StatelessCoarseUniverseSelectionBenchmark.cs" />
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
from clr import AddReference
15+
AddReference("System")
16+
AddReference("QuantConnect.Algorithm")
17+
AddReference("QuantConnect.Common")
18+
AddReference("System.Windows.Forms")
19+
20+
from System import *
21+
from System.Collections import *
22+
from QuantConnect import *
23+
from QuantConnect.Algorithm import *
24+
import numpy as np
25+
26+
class BasicCSharpIntegrationTemplateAlgorithm(QCAlgorithm):
27+
28+
def Initialize(self):
29+
self.SetStartDate(2013,10, 7) #Set Start Date
30+
self.SetEndDate(2013,10,11) #Set End Date
31+
self.SetCash(100000) #Set Strategy Cash
32+
33+
self.AddEquity("SPY", Resolution.Second)
34+
self.Debug(f'According to Python, the value of sin(10) is {np.sin(10)}')
35+
self.Debug(f'According to C#, the value of sin(10) is {Math.Sin(10)}')
36+
37+
def OnData(self, data):
38+
'''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
39+
40+
Arguments:
41+
data: Slice object keyed by symbol containing the stock data
42+
'''
43+
if not self.Portfolio.Invested:
44+
self.SetHoldings("SPY", 1)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
<Content Include="Alphas\ShareClassMeanReversionAlpha.py" />
5454
<Content Include="Alphas\TripleLeverageETFPairVolatilityDecayAlpha.py" />
5555
<Content Include="Alphas\VIXDualThrustAlpha.py" />
56+
<Content Include="BasicCSharpIntegrationTemplateAlgorithm.py" />
5657
<Content Include="BasicSetAccountCurrencyAlgorithm.py" />
5758
<Content Include="USEnergyInformationAdministrationAlgorithm.py" />
5859
<None Include="RawPricesUniverseRegressionAlgorithm.py" />

0 commit comments

Comments
 (0)