-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8072724
commit 6425272
Showing
20 changed files
with
407 additions
and
19 deletions.
There are no files selected for viewing
Empty file.
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,26 @@ | ||
from Blankly.auth.abc_auth import auth_interface | ||
|
||
import warnings | ||
class alpaca_auth(auth_interface): | ||
def __init__(self, keys_file, portfolio_name): | ||
super().__init__(keys_file, portfolio_name, 'alpaca') | ||
self.API_KEY = None | ||
self.API_SECRET = None | ||
self.validate_credentials() | ||
|
||
def validate_credentials(self): | ||
""" | ||
Validate that exchange specific credentials are present | ||
""" | ||
try: | ||
self.API_KEY = self.raw_cred.pop('API_KEY') | ||
self.API_SECRET = self.raw_cred.pop('API_SECRET') | ||
except KeyError as e: | ||
print(f"One of 'API_KEY' or 'API_SECRET' not defined for Exchange: {self.__exchange} Portfolio: {self.__portfolio_name}") | ||
raise KeyError(e) | ||
|
||
if len(self.raw_cred) > 0: | ||
warnings.warn("Additional configs for Exchange: {self.__exchange} Portfolio: {self.__portfolio_name} being ignored") | ||
|
||
|
||
|
Empty file.
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,26 @@ | ||
from Blankly.auth.abc_auth import auth_interface | ||
import warnings | ||
|
||
class binance_auth(auth_interface): | ||
def __init__(self, keys_file, portfolio_name): | ||
super.__init__(keys_file, portfolio_name, 'binance') | ||
self.API_KEY = None | ||
self.API_SECRET = None | ||
self.validate_credentials() | ||
|
||
def validate_credentials(self): | ||
""" | ||
Validate that exchange specific credentials are present | ||
""" | ||
try: | ||
self.API_KEY = self.raw_cred.pop('API_KEY') | ||
self.API_SECRET = self.raw_cred.pop('API_SECRET') | ||
except KeyError as e: | ||
print(f"One of 'API_KEY' or 'API_SECRET' not defined for Exchange: {self.__exchange} Portfolio: {self.__portfolio_name}") | ||
raise KeyError(e) | ||
|
||
if len(self.raw_cred) > 0: | ||
warnings.warn(f"Additional configs for Exchange: {self.__exchange} Portfolio: {self.__portfolio_name} being ignored") | ||
|
||
|
||
|
Empty file.
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,29 @@ | ||
from Blankly.auth.abc_auth import auth_interface | ||
import warnings | ||
|
||
class coinbase_auth(auth_interface): | ||
def __init__(self, keys_file, portfolio_name): | ||
super.__init__(keys_file, portfolio_name, 'coinbase') | ||
self.API_KEY = None | ||
self.API_SECRET = None | ||
self.API_PASS = None | ||
|
||
self.validate_credentials() | ||
|
||
def validate_credentials(self): | ||
""" | ||
Validate that exchange specific credentials are present | ||
""" | ||
try: | ||
self.API_KEY = self.raw_cred.pop('API_KEY') | ||
self.API_SECRET = self.raw_cred.pop('API_SECRET') | ||
self.API_PASS = self.raw_cred.pop('API_PASS') | ||
except KeyError as e: | ||
print(f"One of 'API_KEY' or 'API_SECRET' or 'API_PASS' not defined for Exchange: {self.__exchange} Portfolio: {self.__portfolio_name}") | ||
raise KeyError(e) | ||
|
||
if len(self.raw_cred) > 0: | ||
warnings.warn(f"Additional configs for Exchange: {self.__exchange} Portfolio: {self.__portfolio_name} being ignored") | ||
|
||
|
||
|
Empty file.
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 @@ | ||
""" | ||
Logic to provide consistency across exchanges | ||
Copyright (C) 2021 Emerson Dove | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Lesser General Public License as published | ||
by the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public License | ||
along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
""" | ||
|
||
import abc | ||
from Blankly.auth.utils import load_json | ||
|
||
class auth_interface(abc.ABC): | ||
def __init__(self, keys_file, portfolio_name, exchange): | ||
""" | ||
Create a currency interface | ||
Args: | ||
keys_file (str): filepath to keys.json | ||
portfolio_name (str): name of portfolio | ||
""" | ||
assert keys_file | ||
assert portfolio_name | ||
assert exchange | ||
self.portfolio_name = portfolio_name | ||
self.exchange = exchange | ||
self.raw_cred = self.load_credentials(keys_file, portfolio_name, exchange) | ||
|
||
def load_credentials(self, keys_file, portfolio_name, exchange): | ||
""" | ||
Load credentials from keys json file | ||
""" | ||
auth_object = load_json(keys_file) | ||
exchange_keys = auth_object[exchange] | ||
credentials = exchange_keys[portfolio_name] | ||
|
||
return credentials | ||
|
||
@abc.abstractmethod | ||
def validate_credentials(self): | ||
""" | ||
Validate that exchange specific credentials are present | ||
""" | ||
pass |
Empty file.
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,16 @@ | ||
from Blankly.auth.Alpaca.auth import alpaca_auth | ||
from Blankly.auth.Binance.auth import binance_auth | ||
from Blankly.auth.Coinbase.auth import coinbase_auth | ||
|
||
|
||
class AuthFactory: | ||
@staticmethod | ||
def create_auth(self, keys_file, exchange_name, portfolio_name): | ||
if exchange_name == 'alpaca': | ||
return alpaca_auth(keys_file, portfolio_name) | ||
elif exchange_name == 'binance': | ||
return binance_auth(keys_file, portfolio_name) | ||
elif exchange_name == 'coinbase_pro': | ||
return coinbase_auth(keys_file, portfolio_name) | ||
else: | ||
raise KeyError("Exchange not supported") |
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,22 @@ | ||
import json | ||
import warnings | ||
|
||
def load_json(keys_file): | ||
try: | ||
f = open(keys_file) | ||
return json.load(f) | ||
except FileNotFoundError: | ||
raise FileNotFoundError("Make sure a Keys.json file is placed in the same folder as the project working " | ||
"directory!") | ||
|
||
def default_first_portfolio(keys_file, exchange_name): | ||
auth_object = load_json(keys_file) | ||
exchange_keys = auth_object[exchange_name] | ||
first_key = list(exchange_keys.keys())[0] | ||
warning_string = "No portfolio name to load specified, defaulting to the first in the file: " \ | ||
"(" + first_key + "). This is fine if there is only one portfolio in use." | ||
warnings.warn(warning_string) | ||
# Read the first in the portfolio | ||
portfolio = exchange_keys[first_key] | ||
name = first_key | ||
return name, portfolio |
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,32 @@ | ||
""" | ||
Coinbase Pro exchange definitions and setup | ||
Copyright (C) 2021 Emerson Dove | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Lesser General Public License as published | ||
by the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public License | ||
along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
""" | ||
|
||
from Blankly.exchanges.exchange import Exchange | ||
from Blankly.auth.auth_factory import AuthFactory | ||
from Blankly.auth.utils import default_first_portfolio | ||
from Blankly.interface.currency_factory import InterfaceFactory | ||
|
||
class Alpaca(Exchange): | ||
def __init__(self, portfolio_name=None, auth_path="Keys.json", preferences_path=None): | ||
if not portfolio_name: | ||
portfolio_name = default_first_portfolio(auth_path, 'alpaca') | ||
Exchange.__init__(self, 'alpaca', portfolio_name, preferences_path) | ||
alpaca_auth = AuthFactory(auth_path, 'alpaca', portfolio_name) | ||
self.interface = InterfaceFactory('alpaca', alpaca_auth, self.preferences) | ||
|
||
|
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
Oops, something went wrong.