Skip to content

Commit a83145c

Browse files
Bring examples to their own directory
1 parent c0d8118 commit a83145c

File tree

3 files changed

+101
-105
lines changed

3 files changed

+101
-105
lines changed

examples/__init__.py

Whitespace-only changes.

examples/get_historical_data.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import sys
2+
import os
3+
from functools import wraps
4+
5+
# Add the parent directory to sys.path
6+
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
7+
8+
from src.stocks_historical import ThetaDataStocksHistorical
9+
10+
historical_data = ThetaDataStocksHistorical(enable_logging=True, use_df=True)
11+
12+
13+
def example_runner(func):
14+
"""
15+
A decorator that wraps functions to provide consistent logging and output formatting.
16+
17+
This decorator does the following:
18+
1. Generates a human-readable description from the function name.
19+
2. Executes the wrapped function and captures its result.
20+
3. Logs appropriate messages based on the result:
21+
- Warns if the result is None (indicating a failure to retrieve data).
22+
- Warns if the result is an empty DataFrame.
23+
- Logs an info message if data was successfully received.
24+
4. Prints the description and the result to the console if data was received.
25+
26+
Args:
27+
func (callable): The function to be wrapped.
28+
29+
Returns:
30+
callable: The wrapped function with added logging and output formatting.
31+
"""
32+
33+
@wraps(func)
34+
def wrapper(*args, **kwargs):
35+
description = func.__name__.replace("_", " ").title()
36+
result = func(*args, **kwargs)
37+
38+
if result is None:
39+
historical_data.logger.warning(f"Failed to get {description}")
40+
elif result.empty:
41+
historical_data.logger.warning(f"{description} is empty")
42+
else:
43+
historical_data.logger.info(f"{description} received")
44+
print(f"\n{description}:")
45+
print(result)
46+
47+
return wrapper
48+
49+
50+
@example_runner
51+
def apple_eod_example():
52+
return historical_data.get_eod_report("AAPL", "20240101", "20240131")
53+
54+
55+
@example_runner
56+
def microsoft_quotes_example():
57+
return historical_data.get_quotes(
58+
"MSFT", "20240101", "20240131", interval="3600000"
59+
)
60+
61+
62+
@example_runner
63+
def google_ohlc_example():
64+
return historical_data.get_ohlc("GOOGL", "20240101", "20240131", interval="3600000")
65+
66+
67+
@example_runner
68+
def tesla_trades_example():
69+
return historical_data.get_trades("TSLA", "20240101", "20240102")
70+
71+
72+
@example_runner
73+
def amazon_trade_quote_example():
74+
return historical_data.get_trade_quote("AMZN", "20240101", "20240102")
75+
76+
77+
@example_runner
78+
def nvidia_splits_example():
79+
return historical_data.get_splits("NVDA", "20230101", "20240131")
80+
81+
82+
@example_runner
83+
def intel_dividends_example():
84+
return historical_data.get_dividends("INTC", "20230101", "20240131")
85+
86+
87+
# Toggle example cases
88+
if __name__ == "__main__":
89+
run_examples = {
90+
"apple_eod_example": False,
91+
"microsoft_quotes_example": False,
92+
"google_ohlc_example": False,
93+
"tesla_trades_example": False,
94+
"amazon_trade_quote_example": True,
95+
"nvidia_splits_example": True,
96+
"intel_dividends_example": True,
97+
}
98+
99+
for example, should_run in run_examples.items():
100+
if should_run:
101+
globals()[example]()

src/stocks_historical.py

Lines changed: 0 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -423,108 +423,3 @@ def get_dividends(
423423
return pd.DataFrame(data, columns=columns)
424424
else:
425425
return response
426-
427-
428-
# Usage examples
429-
430-
if __name__ == "__main__":
431-
# Toggle example cases
432-
run_apple_eod_example = False
433-
run_microsoft_quotes_example = False
434-
run_google_ohlc_example = False
435-
run_tesla_trades_example = False
436-
run_amazon_trade_quote_example = True
437-
run_nvidia_splits_example = True
438-
run_intel_dividends_example = True
439-
440-
historical_data = ThetaDataStocksHistorical(enable_logging=True, use_df=True)
441-
442-
# Example 1: Get EOD report for AAPL
443-
if run_apple_eod_example:
444-
apple_eod = historical_data.get_eod_report("AAPL", "20240101", "20240131")
445-
if apple_eod is None:
446-
historical_data.logger.warning("Failed to get Apple EOD Report")
447-
elif apple_eod.empty:
448-
historical_data.logger.warning("Apple EOD Report is empty")
449-
else:
450-
historical_data.logger.info("Apple EOD Report received")
451-
print("Apple EOD Report:")
452-
print(apple_eod)
453-
454-
# Example 2: Get quotes for MSFT
455-
if run_microsoft_quotes_example:
456-
microsoft_quotes = historical_data.get_quotes(
457-
"MSFT", "20240101", "20240131", interval="3600000"
458-
)
459-
if microsoft_quotes is None:
460-
historical_data.logger.warning("Failed to get Microsoft Quotes")
461-
elif microsoft_quotes.empty:
462-
historical_data.logger.warning("Microsoft Quotes data is empty")
463-
else:
464-
historical_data.logger.info("Microsoft Quotes received")
465-
print("\nMicrosoft Quotes:")
466-
print(microsoft_quotes)
467-
468-
# Example 3: Get OHLC for GOOGL
469-
if run_google_ohlc_example:
470-
google_ohlc = historical_data.get_ohlc(
471-
"GOOGL", "20240101", "20240131", interval="3600000"
472-
)
473-
if google_ohlc is None:
474-
historical_data.logger.warning("Failed to get Google OHLC data")
475-
elif google_ohlc.empty:
476-
historical_data.logger.warning("Google OHLC data is empty")
477-
else:
478-
historical_data.logger.info("Google OHLC data received")
479-
print("\nGoogle OHLC data:")
480-
print(google_ohlc)
481-
482-
# Example 4: Get trades for TSLA
483-
if run_tesla_trades_example:
484-
tesla_trades = historical_data.get_trades("TSLA", "20240101", "20240102")
485-
if tesla_trades is None:
486-
historical_data.logger.warning("Failed to get Tesla Trades data")
487-
elif tesla_trades.empty:
488-
historical_data.logger.warning("Tesla Trades data is empty")
489-
else:
490-
historical_data.logger.info("Tesla Trades data received")
491-
print("\nTesla Trades data:")
492-
print(tesla_trades)
493-
494-
# Example 5: Get trade quotes for AMZN
495-
if run_amazon_trade_quote_example:
496-
amazon_trade_quotes = historical_data.get_trade_quote(
497-
"AMZN", "20240101", "20240102"
498-
)
499-
if amazon_trade_quotes is None:
500-
historical_data.logger.warning("Failed to get Amazon Trade Quotes data")
501-
elif amazon_trade_quotes.empty:
502-
historical_data.logger.warning("Amazon Trade Quotes data is empty")
503-
else:
504-
historical_data.logger.info("Amazon Trade Quotes data received")
505-
print("\nAmazon Trade Quotes data:")
506-
print(amazon_trade_quotes)
507-
508-
# Example 6: Get splits for NVDA
509-
if run_nvidia_splits_example:
510-
nvidia_splits = historical_data.get_splits("NVDA", "20230101", "20240131")
511-
if nvidia_splits is None:
512-
historical_data.logger.warning("Failed to get NVIDIA Splits data")
513-
elif nvidia_splits.empty:
514-
historical_data.logger.warning("NVIDIA Splits data is empty")
515-
else:
516-
historical_data.logger.info("NVIDIA Splits data received")
517-
print("\nNVIDIA Splits data:")
518-
print(nvidia_splits)
519-
520-
# Example 7: Get dividends for INTC
521-
if run_intel_dividends_example:
522-
intel_dividends = historical_data.get_dividends("INTC", "20230101", "20240131")
523-
if intel_dividends is None:
524-
historical_data.logger.warning("Failed to get Intel Dividends data")
525-
elif intel_dividends.empty:
526-
historical_data.logger.warning("Intel Dividends data is empty")
527-
else:
528-
historical_data.logger.info("Intel Dividends data received")
529-
print("\nIntel Dividends data:")
530-
print(intel_dividends)

0 commit comments

Comments
 (0)