-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TradeStation & Alpaca Brokerage Support (#469)
* feat: TradeStation in Readme * feat: DQH of TradeStation * Implement auth0 api client * feat: OAth implementation * feat: rename configuration of TS in Readme * feat: get auth0 configurations * feat: increase number of modules json file * refactor: README * fix: use isinstance instead of type in condition fix: Readme skip oauth type * feat: auto open browser when call authorize * Add Alpaca to readme * Pin setuptools --------- Co-authored-by: Ronit Jain <[email protected]> Co-authored-by: Martin Molinero <[email protected]>
- Loading branch information
1 parent
e377aa3
commit 13b5996
Showing
9 changed files
with
176 additions
and
9 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
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,61 @@ | ||
# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. | ||
# Lean CLI v1.0. Copyright 2021 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 lean.components.api.api_client import * | ||
from lean.components.util.logger import Logger | ||
from lean.constants import API_BASE_URL | ||
from lean.models.api import QCAuth0Authorization | ||
from lean.models.errors import RequestFailedError | ||
|
||
|
||
class Auth0Client: | ||
"""The Auth0Client class contains methods to interact with live/auth0/* API endpoints.""" | ||
|
||
def __init__(self, api_client: 'APIClient') -> None: | ||
"""Creates a new Auth0Client instance. | ||
:param api_client: the APIClient instance to use when making requests | ||
""" | ||
self._api = api_client | ||
|
||
def read(self, brokerage_id: str) -> QCAuth0Authorization: | ||
"""Reads the authorization data for a brokerage. | ||
:param brokerage_id: the id of the brokerage to read the authorization data for | ||
:return: the authorization data for the specified brokerage | ||
""" | ||
try: | ||
payload = { | ||
"brokerage": brokerage_id | ||
} | ||
|
||
data = self._api.post("live/auth0/read", payload) | ||
return QCAuth0Authorization(**data) | ||
except RequestFailedError as e: | ||
return QCAuth0Authorization(authorization=None) | ||
|
||
@staticmethod | ||
def authorize(brokerage_id: str, logger: Logger) -> None: | ||
"""Starts the authorization process for a brokerage. | ||
:param brokerage_id: the id of the brokerage to start the authorization process for | ||
:param logger: the logger instance to use | ||
""" | ||
from webbrowser import open | ||
|
||
full_url = f"{API_BASE_URL}live/auth0/authorize?brokerage={brokerage_id}" | ||
logger.info(f"Please open the following URL in your browser to authorize the LEAN CLI.") | ||
logger.info(full_url) | ||
open(full_url) | ||
|
||
|
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,45 @@ | ||
# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. | ||
# Lean CLI v1.0. Copyright 2021 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 lean.models.api import QCAuth0Authorization | ||
from lean.components.api.api_client import Auth0Client | ||
from lean.components.util.logger import Logger | ||
|
||
|
||
def get_authorization(auth0_client: Auth0Client, brokerage_id: str, logger: Logger) -> QCAuth0Authorization: | ||
"""Gets the authorization data for a brokerage, authorizing if necessary. | ||
:param auth0_client: An instance of Auth0Client, containing methods to interact with live/auth0/* API endpoints. | ||
:param brokerage_id: The ID of the brokerage to get the authorization data for. | ||
:param logger: An instance of Logger, handling all output printing. | ||
:return: The authorization data for the specified brokerage. | ||
""" | ||
from time import time, sleep | ||
|
||
data = auth0_client.read(brokerage_id) | ||
if data.authorization is not None: | ||
return data | ||
|
||
start_time = time() | ||
auth0_client.authorize(brokerage_id, logger) | ||
|
||
# keep checking for new data every 5 seconds for 7 minutes | ||
while time() - start_time < 420: | ||
logger.info("Will sleep 5 seconds and retry fetching authorization...") | ||
sleep(5) | ||
data = auth0_client.read(brokerage_id) | ||
if data.authorization is None: | ||
continue | ||
return data | ||
|
||
raise Exception("Authorization failed") |
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
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