Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Creates QuantConnect ApiPython project
This new project includes the source and tools to develop the quantconnect python package that provides interaction via API with quantconnect.com.
- Loading branch information
1 parent
fc25c3a
commit 7c5c45d
Showing
12 changed files
with
220 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
include LICENSE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0"> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<SchemaVersion>2.0</SchemaVersion> | ||
<ProjectGuid>1c47f4db-2aff-4fae-9142-b33be654a516</ProjectGuid> | ||
<ProjectHome> | ||
</ProjectHome> | ||
<StartupFile>tests\test_api.py</StartupFile> | ||
<SearchPath> | ||
</SearchPath> | ||
<WorkingDirectory>.</WorkingDirectory> | ||
<OutputPath>.</OutputPath> | ||
<Name>QuantConnect.ApiPython</Name> | ||
<RootNamespace>ApiPython</RootNamespace> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' "> | ||
<DebugSymbols>true</DebugSymbols> | ||
<EnableUnmanagedDebugging>false</EnableUnmanagedDebugging> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' "> | ||
<DebugSymbols>true</DebugSymbols> | ||
<EnableUnmanagedDebugging>false</EnableUnmanagedDebugging> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="quantconnect\api.py" /> | ||
<Compile Include="quantconnect\__init__.py" /> | ||
<Compile Include="setup.py" /> | ||
<Compile Include="tests\test_api.py" /> | ||
<Compile Include="tests\__init__.py"> | ||
<SubType>Code</SubType> | ||
</Compile> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Folder Include="tests\" /> | ||
<Folder Include="quantconnect\" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Content Include="MANIFEST.in" /> | ||
<Content Include="README.rst"> | ||
<SubType>Code</SubType> | ||
</Content> | ||
</ItemGroup> | ||
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Python Tools\Microsoft.PythonTools.targets" /> | ||
<!-- Uncomment the CoreCompile target to enable the Build command in | ||
Visual Studio and specify your pre- and post-build commands in | ||
the BeforeBuild and AfterBuild targets below. --> | ||
<!--<Target Name="CoreCompile" />--> | ||
<Target Name="BeforeBuild"> | ||
</Target> | ||
<Target Name="AfterBuild"> | ||
</Target> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
QuantConnect.com Interaction via API (python edition) | ||
========= | ||
|
||
What is it | ||
------------------ | ||
|
||
**quantconnect** is a Python package providing interaction via API with `QuantConnect.com <https://www.quantconnect.com>`_. | ||
|
||
Installation Instructions | ||
------------------ | ||
|
||
This package can be installed with pip: | ||
|
||
>>> pip install quantconnect -U | ||
|
||
Enter Python’s interpreter and type the following commands: | ||
|
||
>>> from quantconnect.api import Api | ||
>>> api = Api(your-user-id, your-token) | ||
>>> p = api.list_projects() | ||
>>> print(len(p['projects'])) | ||
|
||
For your user id and token, please visit `your account page <https://www.quantconnect.com/account>`_. | ||
|
||
Create the package | ||
------------------ | ||
|
||
Edit setup.py to set the desired package version. Then, create the distribution and upload it with `twine <https://pypi.python.org/pypi/twine>`_.: | ||
|
||
>>> python setup.py sdist | ||
>>> twine upload dist/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. | ||
# Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from hashlib import sha256 | ||
from logging import exception | ||
from requests import Request, Session | ||
from time import time | ||
|
||
def create_secure_hash(timestamp, token): | ||
"""Generate a secure hash for the authorization headers. | ||
Returns: | ||
Time based hash of user token and timestamp.""" | ||
hash_data = sha256() | ||
hash_data.update('{0}:{1}'.format(token, timestamp).encode('utf-8')) | ||
return hash_data.hexdigest() | ||
|
||
|
||
class ApiConnection: | ||
"""API Connection and Hash Manager | ||
Attributes: | ||
client(str): Authorized client to use for requests. | ||
userId(int/str): User Id number from QuantConnect.com account. Found at www.quantconnect.com/account. | ||
token(str): Access token for the QuantConnect account. Found at www.quantconnect.com/account. | ||
""" | ||
def __init__(self, userId, token): | ||
self.client = 'https://www.quantconnect.com/api/v2/' | ||
self.userId = str(userId) | ||
self.token = token | ||
if len(self.userId) * len(self.token) == 0: | ||
exception('Cannot use empty string for userId or token. Found yours at www.quantconnect.com/account') | ||
|
||
|
||
def connected(self): | ||
"""Return true if connected successfully.""" | ||
request = Request('GET', 'authenticate') | ||
result = self.try_request(request) | ||
return result['success'] | ||
|
||
def try_request(self, request): | ||
"""Place a secure request and get back an object of type T. | ||
Args: | ||
request: Result object of the request | ||
Returns: | ||
result: request response | ||
""" | ||
timestamp = int(time()) | ||
hash = create_secure_hash(timestamp, self.token) | ||
request.auth = (self.userId, hash) | ||
request.headers.update({'Timestamp': str(timestamp)}) | ||
request.url = self.client + request.url | ||
|
||
try: | ||
session = Session() | ||
response = session.send(request.prepare()) | ||
session.close() | ||
return response.json() | ||
except: | ||
exception('Failed to make REST request to {0}'.format(request.url)) | ||
return { 'success': False } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# -*- coding: utf-8 -*- | ||
# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. | ||
# Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from setuptools import setup, find_packages | ||
|
||
# https://github.com/QuantConnect/Lean/blob/master/LICENSE | ||
with open('../LICENSE') as f: | ||
license = f.read() | ||
|
||
with open('README.rst') as f: | ||
readme = f.read() | ||
|
||
setup( | ||
name='quantconnect', | ||
version='0.1', | ||
description = 'QuantConnect API', | ||
long_description=readme, | ||
author = 'QuantConnect Python Team', | ||
author_email = 'support@quantconnect.com', | ||
url='https://www.quantconnect.com/', | ||
license=license, | ||
packages = find_packages(exclude=('tests', 'docs')), | ||
install_requires=['requests'] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. | ||
# Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters