Skip to content

Commit

Permalink
adding entry points
Browse files Browse the repository at this point in the history
  • Loading branch information
amor71 committed Jan 8, 2023
1 parent 253536e commit 2db2e3b
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 126 deletions.
2 changes: 1 addition & 1 deletion .therapist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ actions:
run: mypy --namespace-packages --show-error-context --pretty --strict-optional --ignore-missing-imports {files}
include: "*.py"
bandit:
run: bandit -ii {files}
run: bandit -s B605 -ii {files}
include: "*.py"
black:
run: black -l 79 {files}
Expand Down
Empty file.
33 changes: 14 additions & 19 deletions liualgotrader/scripts/backtester → liualgotrader/scripts/backtester.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,36 +1,29 @@
#!/usr/bin/env python

import asyncio
import getopt
import os
import pprint
import sys
import traceback
import uuid
import toml
from datetime import date, datetime
from typing import List, Optional

import pandas as pd
import importlib.util
from datetime import datetime, timedelta, date, tzinfo
from typing import List, Dict, Optional
import parsedatetime.parsedatetime as pdt
import pygit2
import pytz
import toml
from requests.exceptions import HTTPError
import parsedatetime.parsedatetime as pdt

from liualgotrader import enhanced_backtest
from liualgotrader.common import config, market_data, trading_data
from liualgotrader.common.database import create_db_connection
from liualgotrader.common.decorators import timeit
from liualgotrader.common.tlog import tlog
from liualgotrader.common.data_loader import TimeScale
from liualgotrader.common.types import AssetType
from liualgotrader.common.types import AssetType, TimeScale
from liualgotrader.fincalcs.vwap import add_daily_vwap
from liualgotrader.models.algo_run import AlgoRun
from liualgotrader.models.new_trades import NewTrade
from liualgotrader.models.trending_tickers import TrendingTickers
from liualgotrader.strategies.base import Strategy, StrategyType
from liualgotrader.scanners.base import Scanner
from liualgotrader.scanners.momentum import Momentum
from liualgotrader import enhanced_backtest
from liualgotrader.strategies.base import Strategy, StrategyType


def show_usage():
Expand Down Expand Up @@ -77,12 +70,12 @@ def dateFromString(s: str) -> date:
dt = result.date()

if dt is None:
raise ValueError("Don't understand date '" + s + "'")
raise ValueError(f"Don't understand date '{s}'")

return dt


if __name__ == "__main__":
def main_cli() -> None:
try:
config.build_label = pygit2.Repository("../").describe(
describe_strategy=pygit2.GIT_DESCRIBE_TAGS
Expand Down Expand Up @@ -128,7 +121,7 @@ def dateFromString(s: str) -> date:
asset_type = AssetType.US_EQUITIES
opts, args = getopt.getopt(
sys.argv[3:],
shortopts=[],
shortopts="",
longopts=[
"to=",
"scale=",
Expand Down Expand Up @@ -156,7 +149,9 @@ def dateFromString(s: str) -> date:
elif arg.lower() == "equity":
asset_type = AssetType.US_EQUITIES
else:
print(f"ERROR: value {arg} not supported for parameter 'asset'")
print(
f"ERROR: value {arg} not supported for parameter 'asset'"
)
sys.exit(0)
elif opt in ("--scale"):
if arg in ("day", "minute"):
Expand Down
122 changes: 87 additions & 35 deletions liualgotrader/scripts/liu → liualgotrader/scripts/liu.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import asyncio
import getopt
import os
import sys
import pygit2
import pathlib
import requests
import sys
import time
import getopt
import asyncio
import uuid

import pygit2
import requests

from liualgotrader.common import config
from liualgotrader.common.database import create_db_connection
from liualgotrader.models.accounts import Accounts
from liualgotrader.models.portfolio import Portfolio
from liualgotrader.common.database import create_db_connection


def show_version():
Expand Down Expand Up @@ -70,7 +72,9 @@ def setup_db(
f.write(resolved)
f.write("\n")

base_url = "https://raw.github.com/amor71/LiuAlgoTrader/master/database/"
base_url = (
"https://raw.github.com/amor71/LiuAlgoTrader/master/database/"
)
files = ["schema.sql", "postgres.conf"]
if restore_sample_db:
files.append("liu_dump.sql")
Expand Down Expand Up @@ -104,7 +108,9 @@ def setup_db(
print("check deployment using `\psql -h localhost -p 5400 -U liu`")


def setup_samples(samples_location: str, user: str, passwd: str, db: str) -> None:
def setup_samples(
samples_location: str, user: str, passwd: str, db: str
) -> None:
try:
print()
print("+--------------------+")
Expand All @@ -117,10 +123,12 @@ def setup_samples(samples_location: str, user: str, passwd: str, db: str) -> Non
if samples_location[-1] != "/":
samples_location += "/"

base_url = (
"https://raw.github.com/amor71/LiuAlgoTrader/master/examples/quickstart/"
)
files = ["tradeplan.toml", "vwap_short.py", "momentum_long_simplified.py"]
base_url = "https://raw.github.com/amor71/LiuAlgoTrader/master/examples/quickstart/"
files = [
"tradeplan.toml",
"vwap_short.py",
"momentum_long_simplified.py",
]

for file in files:
print(f"Downloading {base_url}{file} to {samples_location}...")
Expand All @@ -134,7 +142,9 @@ def setup_samples(samples_location: str, user: str, passwd: str, db: str) -> Non

if os.name == "nt":
with open(f"{samples_location}env_vars.bat", "w") as f:
f.write(f"set DSN=postgresql://{user}:{passwd}@localhost:5400/{db}\n")
f.write(
f"set DSN=postgresql://{user}:{passwd}@localhost:5400/{db}\n"
)
else:
with open(f"{samples_location}env_vars.sh", "w") as f:
f.write(
Expand All @@ -158,7 +168,9 @@ def quickstart():
print("| Step #1 - Alpaca & Polygon credentials |")
print("+----------------------------------------+")
print()
print("To use Liu Algo Trading Framework you need an account with Alpaca Markets,")
print(
"To use Liu Algo Trading Framework you need an account with Alpaca Markets,"
)
print("do you already have an account [Y]/n:")
i = input()
have_funded = len(i) == 0 or (i == "y" or i == "Y" or i.lower() == "yes")
Expand All @@ -169,13 +181,21 @@ def quickstart():

to_exit = False
if not config.alpaca_api_key or not config.alpaca_api_secret:
print("Liu Algo Trading Framework uses Alpaca for both LIVE and PAPER trading.")
print(
"Liu Algo Trading Framework uses Alpaca for both LIVE and PAPER trading."
)
print()
print("The Framework expects three environment variables to be set:")
print("`APCA_API_KEY_ID` and `APCA_API_SECRET_KEY`: reflecting the funded")
print(
"`APCA_API_KEY_ID` and `APCA_API_SECRET_KEY`: reflecting the funded"
)
print("account's API key and secret respectively.")
print("And `APCA_API_BASE_URL` reflecting the base URL for your account.")
print("Please set the three environment variables and re-run the wizard.")
print(
"And `APCA_API_BASE_URL` reflecting the base URL for your account."
)
print(
"Please set the three environment variables and re-run the wizard."
)
to_exit = True

if to_exit:
Expand All @@ -188,7 +208,9 @@ def quickstart():
print()
print("Do you already have a PostgreSQL instance configured [N]/y:")
i = input()
already_have_db = len(i) > 0 and (i == "y" or i == "Y" or i.lower() == "yes")
already_have_db = len(i) > 0 and (
i == "y" or i == "Y" or i.lower() == "yes"
)

if already_have_db:
print(
Expand All @@ -200,31 +222,53 @@ def quickstart():
print(
"Liu Algo Trading Framework uses `docker-compose` to run a local database."
)
print("The installation will download the docker-compose.yml, database schema")
print("and prepare the database for first time usage. You can stop and re-run")
print("the database using `docker-compose up -d ` and `docker-compose down`")
print("respectively. Your data will not be deleted. For more details RTFM.")
print(
"The installation will download the docker-compose.yml, database schema"
)
print(
"and prepare the database for first time usage. You can stop and re-run"
)
print(
"the database using `docker-compose up -d ` and `docker-compose down`"
)
print(
"respectively. Your data will not be deleted. For more details RTFM."
)
print()
print(f"Select location for database files [{pwd}/liu_data/]:")
db_location = input()
db_location = f"{pwd}/liu_data/" if not len(db_location) else db_location
db_location = (
f"{pwd}/liu_data/" if not len(db_location) else db_location
)

print()
print("** IMPORTANT NOTE**")
print("The installation wizard, aside from installing PostgreSQL in a docker")
print("may also download a sample database, with existing data to help you")
print(
"The installation wizard, aside from installing PostgreSQL in a docker"
)
print(
"may also download a sample database, with existing data to help you"
)
print("take your first steps with Liu Algo Trading Framework.")
print("However, in order to restore the sample data, you need to select")
print(
"However, in order to restore the sample data, you need to select"
)
print(
"the default username ('liu') password ('liu)', and database name ('liu')."
)
print("after you try out the sample, you can delete data and re-run this")
print(
"after you try out the sample, you can delete data and re-run this"
)
print("wizard to install a fresh copy of the database.")
print()
print("Would you like to download the sample database? [Y]/n")
i = input()
restore_sample_db = len(i) == 0 or (i == "y" or i == "Y" or i.lower() == "yes")
print("Select the database name for keeping track of your trading [liu]:")
restore_sample_db = len(i) == 0 or (
i == "y" or i == "Y" or i.lower() == "yes"
)
print(
"Select the database name for keeping track of your trading [liu]:"
)
db_name = input()
db_name = "liu" if not len(db_name) else db_name
print("Select the database user-name [liu]:")
Expand All @@ -248,7 +292,9 @@ def quickstart():
print(f"Select location for sample files [{pwd}/liu_samples]:")
sample_location = input()
sample_location = (
f"{pwd}/liu_samples" if not len(sample_location) else sample_location
f"{pwd}/liu_samples"
if not len(sample_location)
else sample_location
)

print()
Expand All @@ -267,14 +313,20 @@ def quickstart():
print("+---------------+")
print()
print("Congratulations, Liu Algo Trader is ready to go!")
print("From here, you can either run the back-testing UI, or read the docs.")
print("The full documentation is available here: `https://liualgotrader.rtfd.io`.")
print(
"From here, you can either run the back-testing UI, or read the docs."
)
print(
"The full documentation is available here: `https://liualgotrader.rtfd.io`."
)
if restore_sample_db:
print()
print(
"The restored database includes data for the first week and half of Oct 2020."
)
print("You may now run a back-testing sessions for these days, as well as ")
print(
"You may now run a back-testing sessions for these days, as well as "
)
print("analyze an existing trading session.")
print()

Expand Down Expand Up @@ -325,7 +377,7 @@ def create_portfolio(amount: float, credit: float):
print(f"Portfolio ID {portfolio_id} created")


def main_cli():
def main_cli() -> None:
config.filename = os.path.basename(__file__)

try:
Expand Down
46 changes: 20 additions & 26 deletions liualgotrader/scripts/market_miner → liualgotrader/scripts/market_miner.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
#!/usr/bin/env python

"""off-hours calculations, and data collections"""
import asyncio
import os, sys
import toml
import pygit2
from typing import Dict, List, Optional
import importlib.util
import os
import sys
import traceback
import importlib
from typing import Dict, List, Optional

import pygit2
import toml

from liualgotrader.common import config
from liualgotrader.common.tlog import tlog
from liualgotrader.common.database import create_db_connection
from liualgotrader.common.tlog import tlog
from liualgotrader.miners.base import Miner

# rom liualgotrader.miners.stock_cluster import StockCluster
Expand All @@ -27,7 +28,7 @@ def motd(filename: str, version: str) -> None:


async def main(conf_dict: Dict):
task_list: List[Optional[asyncio.tasks]] = []
task_list: List[Optional[asyncio.Task]] = []

await create_db_connection()
for miner in conf_dict["miners"]:
Expand All @@ -36,10 +37,16 @@ async def main(conf_dict: Dict):
spec = importlib.util.spec_from_file_location(
"module.name", conf_dict["miners"][miner]["filename"]
)
if not spec:
raise AssertionError(
f"could not load module {conf_dict['miners'][miner]['filename']}"
)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module) # type: ignore
else:
module = importlib.import_module(f"liualgotrader.miners.{miner}")
module = importlib.import_module(
f"liualgotrader.miners.{miner}"
)
class_name = f"{miner[0].upper()}{miner[1:]}"
miner_class = getattr(module, class_name)

Expand All @@ -48,22 +55,9 @@ async def main(conf_dict: Dict):
exit(0)
except Exception as e:
tlog(f"[ERROR] miner {miner} resulted in exception:`{e}`")
else:
try:
debug = conf_dict["miners"][miner].get("debug", False)
miner = miner_class(debug=debug, data=conf_dict["miners"][miner])
task_list.append(asyncio.create_task(miner.run()))

await asyncio.gather(*task_list)
except Exception as e:
tlog(f"[ERROR] aborted w/ exception {e}")
exc_info = sys.exc_info()
traceback.print_exception(*exc_info)
del exc_info
raise


if __name__ == "__main__":


def main_cli() -> None:
"""
starting
"""
Expand Down
Loading

0 comments on commit 2db2e3b

Please sign in to comment.