├── .devcontainer
├── dockerfile
└── devcontainer.json
├── QuantConnectStubsGenerator
├── QuantConnectStubsGenerator.csproj
├── log4net.config
├── Model
│ ├── CodeEntity.cs
│ ├── Property.cs
│ ├── ParseContext.cs
│ ├── Parameter.cs
│ ├── Namespace.cs
│ ├── Method.cs
│ ├── Class.cs
│ └── PythonType.cs
├── Renderer
│ ├── BaseRenderer.cs
│ ├── AlgorithmImportsRenderer.cs
│ ├── ClrStubsRenderer.cs
│ ├── PyLoaderRenderer.cs
│ ├── ObjectRenderer.cs
│ ├── ClassRenderer.cs
│ ├── NamespaceRenderer.cs
│ ├── PropertyRenderer.cs
│ ├── SetupRenderer.cs
│ └── MethodRenderer.cs
├── Program.cs
├── Utility
│ ├── StringExtensions.cs
│ ├── Extensions.cs
│ ├── DependencyGraph.cs
│ └── XmlExtensions.cs
└── Parser
│ ├── ClassParser.cs
│ ├── PropertyParser.cs
│ ├── TypeConverter.cs
│ └── BaseParser.cs
├── QuantConnectStubsGenerator.Tests
├── QuantConnectStubsGenerator.Tests.csproj
├── Utility
│ ├── StringExtensionsTests.cs
│ └── DependencyGraphTests.cs
├── Model
│ ├── ParseContextTests.cs
│ ├── PythonTypeTests.cs
│ ├── NamespaceTests.cs
│ └── ClassTests.cs
├── MethodParserTests.cs
└── GeneratorTests.cs
├── .github
└── workflows
│ └── build.yml
├── .vscode
├── launch.json
└── tasks.json
├── QuantConnectStubsGenerator.sln
├── integration
├── README.md
├── pull_repos.py
├── utils.py
└── integration_tests.py
├── README.md
├── .gitignore
└── LICENSE
/.devcontainer/dockerfile:
--------------------------------------------------------------------------------
1 | # Use Lean Foundation as the base
2 | FROM node:14.19-bullseye
3 |
4 | # Dotnet install
5 | RUN wget https://packages.microsoft.com/config/debian/11/packages-microsoft-prod.deb -O packages-microsoft-prod.deb && \
6 | dpkg -i packages-microsoft-prod.deb && \
7 | rm packages-microsoft-prod.deb
8 |
9 | RUN apt-get update; \
10 | apt-get install -y apt-transport-https && \
11 | apt-get update && \
12 | apt-get install -y dotnet-sdk-5.0
13 |
14 | # pip
15 | RUN wget -q https://bootstrap.pypa.io/pip/3.6/get-pip.py \
16 | && python3 get-pip.py && \
17 | pip install pandas matplotlib
18 |
19 | # pyright
20 | RUN npm i -g pyright
--------------------------------------------------------------------------------
/QuantConnectStubsGenerator/QuantConnectStubsGenerator.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | Exe
4 | net9.0
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 | Always
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/QuantConnectStubsGenerator.Tests/QuantConnectStubsGenerator.Tests.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | net9.0
4 | false
5 |
6 |
7 |
8 |
9 | all
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/.devcontainer/devcontainer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "StubsGenerator",
3 |
4 | // Use devcontainer Dockerfile that is based on Lean foundation image
5 | "build": {
6 | "dockerfile": "dockerfile"
7 | },
8 |
9 | // Add the IDs of extensions you want installed when the container is created.
10 | "extensions": [
11 | "ms-dotnettools.csharp",
12 | "formulahendry.dotnet-test-explorer",
13 | "ms-python.python",
14 | "ms-python.vscode-pylance",
15 | "eamodio.gitlens",
16 | "yzhang.markdown-all-in-one",
17 | "ms-azuretools.vscode-docker",
18 | "mads-hartmann.bash-ide-vscode",
19 | "rogalmic.bash-debug"
20 | ],
21 |
22 | // Use 'forwardPorts' to make a list of ports inside the container available locally.
23 | // "forwardPorts": [],
24 |
25 | // Post create commands; only runs on initial creation of container
26 | "postCreateCommand": "cd integration && python3 ./pull_repos.py",
27 |
28 | // Post start commands; runs each time the container is started
29 | "postStartCommand": "",
30 | }
--------------------------------------------------------------------------------
/.github/workflows/build.yml:
--------------------------------------------------------------------------------
1 | name: Build
2 |
3 | on: [push, pull_request]
4 |
5 | jobs:
6 | build:
7 | runs-on: ubuntu-latest
8 |
9 | steps:
10 | - name: Checkout
11 | uses: actions/checkout@v3
12 |
13 | - name: Set up .NET 9
14 | uses: actions/setup-dotnet@v2
15 | with:
16 | dotnet-version: '9.0.x'
17 |
18 | - name: Build
19 | run: dotnet build
20 |
21 | - name: Run unit tests
22 | run: dotnet test -v n
23 |
24 | - name: Set up Python
25 | uses: actions/setup-python@v2
26 | with:
27 | python-version: 3.11
28 |
29 | - name: Set up Node.js
30 | uses: actions/setup-node@v1
31 | with:
32 | node-version: 14.x
33 |
34 | - name: Install integration test dependencies
35 | run: |
36 | pip install pandas matplotlib pyright==1.1.338 mypy==1.15.0 wheel
37 |
38 | - name: Run integration tests
39 | run: python integration/integration_tests.py
40 |
--------------------------------------------------------------------------------
/QuantConnectStubsGenerator/log4net.config:
--------------------------------------------------------------------------------
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 |
--------------------------------------------------------------------------------
/QuantConnectStubsGenerator/Model/CodeEntity.cs:
--------------------------------------------------------------------------------
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.Xml;
17 |
18 | namespace QuantConnectStubsGenerator.Model
19 | {
20 | public enum CodeEntityType
21 | {
22 | Class,
23 | Method,
24 | Property,
25 | Field
26 | }
27 |
28 | public abstract class CodeEntity
29 | {
30 | public string Summary { get; set; }
31 |
32 | public XmlDocument Documentation { get; set; }
33 | }
34 | }
35 |
36 |
--------------------------------------------------------------------------------
/.vscode/launch.json:
--------------------------------------------------------------------------------
1 | {
2 | "version": "0.2.0",
3 | "configurations": [
4 | {
5 | "name": "Launch Stubs Generator",
6 | "type": "coreclr",
7 | "request": "launch",
8 | "preLaunchTask": "build",
9 | "program": "${workspaceFolder}/QuantConnectStubsGenerator/bin/Debug/net5.0/QuantConnectStubsGenerator.dll",
10 | "args": [
11 | // Requires Lean and Runtime repos under workspace dir ./generated
12 | "${workspaceFolder}/generated/Lean",
13 | "${workspaceFolder}/generated/runtime",
14 | "${workspaceFolder}/generated/stubs"
15 | ],
16 | "cwd": "${workspaceFolder}/QuantConnectStubsGenerator",
17 | "console": "internalConsole",
18 | "stopAtEntry": false
19 | },
20 | {
21 | "name": "Python Integration Tests",
22 | "type": "python",
23 | "request": "launch",
24 | "program": "${workspaceFolder}/integration/integration_tests.py",
25 | "console": "integratedTerminal"
26 | },
27 | {
28 | "name": "Python: Current File",
29 | "type": "python",
30 | "request": "launch",
31 | "program": "${file}",
32 | "console": "integratedTerminal"
33 | }
34 | ]
35 | }
--------------------------------------------------------------------------------
/QuantConnectStubsGenerator/Renderer/BaseRenderer.cs:
--------------------------------------------------------------------------------
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.IO;
17 |
18 | namespace QuantConnectStubsGenerator.Renderer
19 | {
20 | public abstract class BaseRenderer
21 | {
22 | protected readonly TextWriter Writer;
23 |
24 | protected BaseRenderer(TextWriter writer)
25 | {
26 | Writer = writer;
27 | }
28 |
29 | protected virtual void Write(string value)
30 | {
31 | Writer.Write(value);
32 | }
33 |
34 | protected virtual void WriteLine(string value = "")
35 | {
36 | Writer.WriteLine(value);
37 | }
38 | }
39 | }
40 |
41 |
--------------------------------------------------------------------------------
/QuantConnectStubsGenerator/Renderer/AlgorithmImportsRenderer.cs:
--------------------------------------------------------------------------------
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.IO;
17 |
18 | namespace QuantConnectStubsGenerator.Renderer
19 | {
20 | public class AlgorithmImportsRenderer : BaseRenderer
21 | {
22 | private readonly string _leanPath;
23 |
24 | public AlgorithmImportsRenderer(TextWriter writer, string leanPath) : base(writer)
25 | {
26 | _leanPath = leanPath;
27 | }
28 |
29 | public void Render()
30 | {
31 | var algorithmImports = Path.GetFullPath("Common/AlgorithmImports.py", _leanPath);
32 | WriteLine(File.ReadAllText(algorithmImports));
33 | }
34 | }
35 | }
36 |
37 |
--------------------------------------------------------------------------------
/.vscode/tasks.json:
--------------------------------------------------------------------------------
1 | {
2 | "version": "2.0.0",
3 | "tasks": [
4 | {
5 | "label": "build",
6 | "command": "dotnet",
7 | "type": "process",
8 | "args": [
9 | "build",
10 | "${workspaceFolder}/QuantConnectStubsGenerator/QuantConnectStubsGenerator.csproj",
11 | "/property:GenerateFullPaths=true",
12 | "/consoleloggerparameters:NoSummary"
13 | ],
14 | "problemMatcher": "$msCompile"
15 | },
16 | {
17 | "label": "publish",
18 | "command": "dotnet",
19 | "type": "process",
20 | "args": [
21 | "publish",
22 | "${workspaceFolder}/QuantConnectStubsGenerator/QuantConnectStubsGenerator.csproj",
23 | "/property:GenerateFullPaths=true",
24 | "/consoleloggerparameters:NoSummary"
25 | ],
26 | "problemMatcher": "$msCompile"
27 | },
28 | {
29 | "label": "watch",
30 | "command": "dotnet",
31 | "type": "process",
32 | "args": [
33 | "watch",
34 | "run",
35 | "--project",
36 | "${workspaceFolder}/QuantConnectStubsGenerator/QuantConnectStubsGenerator.csproj"
37 | ],
38 | "problemMatcher": "$msCompile"
39 | }
40 | ]
41 | }
--------------------------------------------------------------------------------
/QuantConnectStubsGenerator.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QuantConnectStubsGenerator", "QuantConnectStubsGenerator\QuantConnectStubsGenerator.csproj", "{61A6CF0C-1044-4787-9ECB-7CF00B5DAF8C}"
4 | EndProject
5 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QuantConnectStubsGenerator.Tests", "QuantConnectStubsGenerator.Tests\QuantConnectStubsGenerator.Tests.csproj", "{C9BD53DA-57D8-4096-85D5-122224A055F5}"
6 | EndProject
7 | Global
8 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
9 | Debug|Any CPU = Debug|Any CPU
10 | Release|Any CPU = Release|Any CPU
11 | EndGlobalSection
12 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
13 | {61A6CF0C-1044-4787-9ECB-7CF00B5DAF8C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
14 | {61A6CF0C-1044-4787-9ECB-7CF00B5DAF8C}.Debug|Any CPU.Build.0 = Debug|Any CPU
15 | {61A6CF0C-1044-4787-9ECB-7CF00B5DAF8C}.Release|Any CPU.ActiveCfg = Release|Any CPU
16 | {61A6CF0C-1044-4787-9ECB-7CF00B5DAF8C}.Release|Any CPU.Build.0 = Release|Any CPU
17 | {C9BD53DA-57D8-4096-85D5-122224A055F5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
18 | {C9BD53DA-57D8-4096-85D5-122224A055F5}.Debug|Any CPU.Build.0 = Debug|Any CPU
19 | {C9BD53DA-57D8-4096-85D5-122224A055F5}.Release|Any CPU.ActiveCfg = Release|Any CPU
20 | {C9BD53DA-57D8-4096-85D5-122224A055F5}.Release|Any CPU.Build.0 = Release|Any CPU
21 | EndGlobalSection
22 | EndGlobal
23 |
--------------------------------------------------------------------------------
/QuantConnectStubsGenerator.Tests/Utility/StringExtensionsTests.cs:
--------------------------------------------------------------------------------
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 NUnit.Framework;
17 | using QuantConnectStubsGenerator.Utility;
18 |
19 | namespace QuantConnectStubsGenerator.Tests.Utility
20 | {
21 | [TestFixture]
22 | public class StringExtensionsTests
23 | {
24 | [Test]
25 | public void IndentShouldIndentByFourTimesTheLevelAmountOfSpaces()
26 | {
27 | Assert.AreEqual(" # Documentation", "# Documentation".Indent(2));
28 | }
29 |
30 | [Test]
31 | public void IndentShouldIndentAllLines()
32 | {
33 | Assert.AreEqual(" # Line 1\n # Line 2", "# Line 1\n# Line 2".Indent(2));
34 | }
35 | }
36 | }
37 |
38 |
--------------------------------------------------------------------------------
/integration/README.md:
--------------------------------------------------------------------------------
1 | # Integration Tests
2 |
3 | This directory contains the integration tests. These are written in Python because the NUnit runners don't seem to consistently log both external process output and `Console.WriteLine` messages, making debugging a lot harder.
4 |
5 | Please note that these integration tests are not meant to show no errors/warnings at all. Because of the differences between C# and Python, it is sometimes necessary to perform conversions which are invalid according to common PEPs, as long as editors can still read the stubs and provide autocomplete and the like properly.
6 |
7 | ## Usage
8 |
9 | Before running `integration_tests.py`, make sure `pandas` and `matplotlib` are installed in your Python environment and the following commands are available on your `PATH`:
10 | - `git`, to clone/pull the latest version of Lean
11 | - `dotnet`, to run the generator
12 | - `pyright`, to check the generated stubs using the [Pyright](https://github.com/microsoft/pyright) type checker used in [Pylance](https://marketplace.visualstudio.com/items?itemName=ms-python.vscode-pylance) (`npm i -g pyright` or `yarn global add pyright`)
13 |
14 | During development it is also useful to check the generated stubs in PyCharm and in VS Code with the [Pylance](https://marketplace.visualstudio.com/items?itemName=ms-python.vscode-pylance) extension. Good files to open and see if the stubs provide correct information can be found in [QuantConnect/Lean/Algorithm.Python](https://github.com/QuantConnect/Lean/tree/master/Algorithm.Python).
15 |
--------------------------------------------------------------------------------
/integration/pull_repos.py:
--------------------------------------------------------------------------------
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 utils import *
15 |
16 | # Simple setup script that gets Lean and runtime repos into our workspace
17 | # under `generated` directory
18 | def main():
19 | ensure_command_availability("git")
20 | ensure_command_availability("dotnet")
21 | ensure_command_availability("pyright")
22 |
23 | project_root = Path(__file__).absolute().parent.parent
24 | generated_dir = project_root / "generated"
25 | lean_dir = generated_dir / "Lean"
26 | runtime_dir = generated_dir / "runtime"
27 | stubs_dir = generated_dir / "stubs"
28 | generator_dir = project_root / "QuantConnectStubsGenerator"
29 |
30 | generated_dir.mkdir(parents=True, exist_ok=True)
31 |
32 | ensure_repository_up_to_date("QuantConnect/Lean", lean_dir)
33 | ensure_repository_up_to_date("dotnet/runtime", runtime_dir)
34 |
35 |
36 | if __name__ == "__main__":
37 | main()
38 |
--------------------------------------------------------------------------------
/QuantConnectStubsGenerator/Renderer/ClrStubsRenderer.cs:
--------------------------------------------------------------------------------
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.IO;
17 |
18 | namespace QuantConnectStubsGenerator.Renderer
19 | {
20 | public class ClrStubsRenderer : BaseRenderer
21 | {
22 | public ClrStubsRenderer(TextWriter writer) : base(writer)
23 | {
24 | }
25 |
26 | public void Render()
27 | {
28 | WriteLine($@"
29 | import typing
30 |
31 | import System
32 | import System.Reflection
33 |
34 |
35 | def getPreload() -> bool:
36 | ...
37 |
38 |
39 | def setPreload(preloadFlag: bool) -> None:
40 | ...
41 |
42 |
43 | def AddReference(name: str) -> System.Reflection.Assembly:
44 | ...
45 |
46 |
47 | def GetClrType(type: typing.Type[typing.Any]) -> System.Type:
48 | ...
49 |
50 |
51 | def FindAssembly(name: str) -> str:
52 | ...
53 |
54 |
55 | def ListAssemblies(verbose: bool) -> typing.List[System.Reflection.Assembly]:
56 | ...
57 | ".Trim());
58 | }
59 | }
60 | }
61 |
62 |
--------------------------------------------------------------------------------
/QuantConnectStubsGenerator/Program.cs:
--------------------------------------------------------------------------------
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 System.IO;
18 | using System.Reflection;
19 | using log4net;
20 | using log4net.Config;
21 |
22 | namespace QuantConnectStubsGenerator
23 | {
24 | internal static class Program
25 | {
26 | private static readonly ILog Logger = LogManager.GetLogger(typeof(Program));
27 |
28 | private static void Main(string[] args)
29 | {
30 | XmlConfigurator.Configure(
31 | LogManager.GetRepository(Assembly.GetEntryAssembly()),
32 | new FileInfo("log4net.config"));
33 |
34 | if (args.Length != 3)
35 | {
36 | Logger.Error("Usage: dotnet run