Skip to content

Commit

Permalink
Removed the 2nd layer of local .env check as it has been implemented …
Browse files Browse the repository at this point in the history
…already in lumibot __init_.py

The test script will exit with raise if it doesn't find credentials from environment variables username/password

Signed-off-by: Haochi Li <[email protected]>
  • Loading branch information
haochili committed Jul 11, 2024
1 parent 9c2a53c commit 4d6ccfd
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions tests/backtest/test_thetadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# Define the keyword globally
keyword = 'ThetaTerminal.jar'


def find_git_root(path):
# Traverse the directories upwards until a .git directory is found
original_path = path
Expand All @@ -25,12 +26,13 @@ def find_git_root(path):
path = parent_path
return path


def kill_processes_by_name(keyword):
try:
# Find all processes related to the keyword
result = subprocess.run(['pgrep', '-f', keyword], capture_output=True, text=True)
pids = result.stdout.strip().split('\n')

if pids:
for pid in pids:
if pid: # Ensure the PID is not empty
Expand All @@ -39,10 +41,11 @@ def kill_processes_by_name(keyword):
print(f"All processes related to '{keyword}' have been killed.")
else:
print(f"No processes found related to '{keyword}'.")

except Exception as e:
print(f"An error occurred during kill process: {e}")


@pytest.fixture(scope="module", autouse=True)
def run_before_and_after_tests():
# Code to execute before running any tests
Expand All @@ -55,6 +58,7 @@ def run_before_and_after_tests():
kill_processes_by_name(keyword)
print("Teardown after all tests")


try:
# Find the root of the git repository
current_dir = os.getcwd()
Expand All @@ -63,8 +67,25 @@ def run_before_and_after_tests():
except Exception as e:
print("ERROR: cannot find the root directory", str(e))

# Global parameters
# Username and Password for ThetaData API
############################################################################################################
# If you are running this test locally, make sure you save the THETADATA_USERNAME and THETADATA_PASSWORD in
# the repo parent directory: lumibot/.secrets/.env file.
THETADATA_USERNAME = os.environ.get("THETADATA_USERNAME")
THETADATA_PASSWORD = os.environ.get("THETADATA_PASSWORD")
############################################################################################################
secrets_not_found = False
if not THETADATA_USERNAME:
print("CHECK: Unable to get THETADATA_USERNAME in the environemnt variables.")
secrets_not_found = True
if not THETADATA_PASSWORD:
print("CHECK: Unable to get THETADATA_PASSWORD in the environemnt variables.")
secrets_not_found = True

if secrets_not_found:
raise "ERROR: Unable to get ThetaData API credentials from the environment variables."


if not THETADATA_USERNAME or not THETADATA_PASSWORD:
print(f"CHECK: either THETADATA_USERNAME or THETADATA_PASSWORD not found from environemnt. \
Expand All @@ -79,6 +100,7 @@ def run_before_and_after_tests():
THETADATA_USERNAME = os.environ.get("THETADATA_USERNAME")
THETADATA_PASSWORD = os.environ.get("THETADATA_PASSWORD")


class ThetadataBacktestStrat(Strategy):
parameters = {"symbol": "AMZN"}

Expand Down Expand Up @@ -117,11 +139,11 @@ def select_option_expiration(self, chain, days_to_expiration=1):
trading_days_df = market_cal.schedule(
start_date=today, end_date=today + datetime.timedelta(days=days_to_expiration + extra_days_padding)
)

# Look for the next trading day that is in the list of expiration dates. Skip the first trading day because
# that is today and we want to find the next expiration date.
# Date Format: 2023-07-31
trading_datestrs = [x.to_pydatetime().date() for x in trading_days_df.index.to_list()]

for trading_day in trading_datestrs[days_to_expiration:]:
day_str = trading_day.strftime("%Y-%m-%d")
if day_str in chain["Expirations"]:
Expand Down Expand Up @@ -220,6 +242,7 @@ def on_trading_iteration(self):
else:
self.cancel_open_orders()


class TestThetaDataBacktestFull:
def verify_backtest_results(self, poly_strat_obj):
assert isinstance(poly_strat_obj, ThetadataBacktestStrat)
Expand Down Expand Up @@ -301,6 +324,7 @@ def test_thetadata_restclient(self):
assert results
self.verify_backtest_results(strat_obj)


# This will ensure the function runs before any test in this file.
if __name__ == "__main__":
pytest.main()

0 comments on commit 4d6ccfd

Please sign in to comment.