8
8
Get quotes:
9
9
python main.py stocks historical quotes MSFT 20240101 20240131 --interval 3600000
10
10
11
- Save output to a file:
12
- python main.py stocks historical eod-report AAPL 20240101 20240131 --output-file aapl_eod.csv
13
-
14
11
Stocks Snapshot Data:
15
12
Get real-time quotes:
16
13
python main.py stocks snapshot quotes AAPL
46
43
stocks_app .add_typer (snapshot_app , name = "snapshot" )
47
44
app .add_typer (options_app , name = "options" )
48
45
49
- historical_data = ThetaDataStocksHistorical (enable_logging = True , use_df = True )
50
- snapshot_data = ThetaDataStocksSnapshot (enable_logging = True , use_df = True )
51
-
52
-
53
- def save_output (result : pd .DataFrame | dict | None , output_file : Optional [str ]):
54
- if isinstance (result , pd .DataFrame ):
55
- if output_file :
56
- result .to_csv (output_file , index = False )
57
- typer .echo (f"Data saved to { output_file } " )
58
- else :
59
- typer .echo (result .to_string ())
60
- else :
61
- typer .echo (result )
46
+ historical_data = ThetaDataStocksHistorical ()
47
+ snapshot_data = ThetaDataStocksSnapshot ()
62
48
63
49
64
50
def with_spinner (func ):
@@ -80,12 +66,15 @@ def wrapper(*args, **kwargs):
80
66
# Historical commands
81
67
@historical_app .command (name = "eod-report" )
82
68
@with_spinner
83
- def eod_report (
84
- symbol : str , start_date : str , end_date : str , output_file : Optional [str ] = None
85
- ):
69
+ def eod_report (symbol : str , start_date : str , end_date : str ):
86
70
"""Get end-of-day report for a given symbol and date range."""
87
- result = historical_data .get_eod_report (symbol , start_date , end_date )
88
- save_output (result , output_file )
71
+ result = historical_data .get_eod_report (
72
+ symbol , start_date , end_date , write_csv = True
73
+ )
74
+ if result is not None :
75
+ typer .echo ("Data retrieved successfully" )
76
+ else :
77
+ typer .echo ("Failed to retrieve data" )
89
78
90
79
91
80
@historical_app .command (name = "quotes" )
@@ -95,11 +84,15 @@ def historical_quotes(
95
84
start_date : str ,
96
85
end_date : str ,
97
86
interval : str = "900000" ,
98
- output_file : Optional [str ] = None ,
99
87
):
100
88
"""Get historical quotes for a given symbol and date range."""
101
- result = historical_data .get_quotes (symbol , start_date , end_date , interval )
102
- save_output (result , output_file )
89
+ result = historical_data .get_quotes (
90
+ symbol , start_date , end_date , interval , write_csv = True
91
+ )
92
+ if result is not None :
93
+ typer .echo ("Data retrieved successfully" )
94
+ else :
95
+ typer .echo ("Failed to retrieve data" )
103
96
104
97
105
98
@historical_app .command (name = "ohlc" )
@@ -109,96 +102,117 @@ def historical_ohlc(
109
102
start_date : str ,
110
103
end_date : str ,
111
104
interval : str = "900000" ,
112
- output_file : Optional [str ] = None ,
113
105
):
114
106
"""Get historical OHLC data for a given symbol and date range."""
115
- result = historical_data .get_ohlc (symbol , start_date , end_date , interval )
116
- save_output (result , output_file )
107
+ result = historical_data .get_ohlc (
108
+ symbol , start_date , end_date , interval , write_csv = True
109
+ )
110
+ if result is not None :
111
+ typer .echo ("Data retrieved successfully" )
112
+ else :
113
+ typer .echo ("Failed to retrieve data" )
117
114
118
115
119
116
@historical_app .command (name = "trades" )
120
117
@with_spinner
121
- def historical_trades (
122
- symbol : str , start_date : str , end_date : str , output_file : Optional [str ] = None
123
- ):
118
+ def historical_trades (symbol : str , start_date : str , end_date : str ):
124
119
"""Get historical trade data for a given symbol and date range."""
125
- result = historical_data .get_trades (symbol , start_date , end_date )
126
- save_output (result , output_file )
120
+ result = historical_data .get_trades (symbol , start_date , end_date , write_csv = True )
121
+ if result is not None :
122
+ typer .echo ("Data retrieved successfully" )
123
+ else :
124
+ typer .echo ("Failed to retrieve data" )
127
125
128
126
129
127
@historical_app .command (name = "trade-quote" )
130
128
@with_spinner
131
- def trade_quote (
132
- symbol : str , start_date : str , end_date : str , output_file : Optional [str ] = None
133
- ):
129
+ def trade_quote (symbol : str , start_date : str , end_date : str ):
134
130
"""Get historical trade and quote data for a given symbol and date range."""
135
- result = historical_data .get_trade_quote (symbol , start_date , end_date )
136
- save_output (result , output_file )
131
+ result = historical_data .get_trade_quote (
132
+ symbol , start_date , end_date , write_csv = True
133
+ )
134
+ if result is not None :
135
+ typer .echo ("Data retrieved successfully" )
136
+ else :
137
+ typer .echo ("Failed to retrieve data" )
137
138
138
139
139
140
@historical_app .command (name = "splits" )
140
141
@with_spinner
141
- def splits (
142
- symbol : str , start_date : str , end_date : str , output_file : Optional [str ] = None
143
- ):
142
+ def splits (symbol : str , start_date : str , end_date : str ):
144
143
"""Get stock split data for a given symbol and date range."""
145
- result = historical_data .get_splits (symbol , start_date , end_date )
146
- save_output (result , output_file )
144
+ result = historical_data .get_splits (symbol , start_date , end_date , write_csv = True )
145
+ if result is not None :
146
+ typer .echo ("Data retrieved successfully" )
147
+ else :
148
+ typer .echo ("Failed to retrieve data" )
147
149
148
150
149
151
@historical_app .command (name = "dividends" )
150
152
@with_spinner
151
- def dividends (
152
- symbol : str , start_date : str , end_date : str , output_file : Optional [str ] = None
153
- ):
153
+ def dividends (symbol : str , start_date : str , end_date : str ):
154
154
"""Get dividend data for a given symbol and date range."""
155
- result = historical_data .get_dividends (symbol , start_date , end_date )
156
- save_output (result , output_file )
155
+ result = historical_data .get_dividends (symbol , start_date , end_date , write_csv = True )
156
+ if result is not None :
157
+ typer .echo ("Data retrieved successfully" )
158
+ else :
159
+ typer .echo ("Failed to retrieve data" )
157
160
158
161
159
162
# Snapshot commands
160
163
@snapshot_app .command (name = "quotes" )
161
164
@with_spinner
162
- def snapshot_quotes (
163
- symbol : str , venue : Optional [str ] = None , output_file : Optional [str ] = None
164
- ):
165
+ def snapshot_quotes (symbol : str , venue : Optional [str ] = None ):
165
166
"""Get real-time quotes for a given symbol."""
166
- result = snapshot_data .get_quotes (symbol , venue )
167
- save_output (result , output_file )
167
+ result = snapshot_data .get_quotes (symbol , venue , write_csv = True )
168
+ if result is not None :
169
+ typer .echo ("Data retrieved successfully" )
170
+ else :
171
+ typer .echo ("Failed to retrieve data" )
168
172
169
173
170
174
@snapshot_app .command (name = "bulk-quotes" )
171
175
@with_spinner
172
- def bulk_quotes (
173
- symbols : List [str ], venue : Optional [str ] = None , output_file : Optional [str ] = None
174
- ):
176
+ def bulk_quotes (symbols : List [str ], venue : Optional [str ] = None ):
175
177
"""Get real-time quotes for multiple symbols."""
176
- result = snapshot_data .get_bulk_quotes (symbols , venue )
177
- save_output (result , output_file )
178
+ result = snapshot_data .get_bulk_quotes (symbols , venue , write_csv = True )
179
+ if result is not None :
180
+ typer .echo ("Data retrieved successfully" )
181
+ else :
182
+ typer .echo ("Failed to retrieve data" )
178
183
179
184
180
185
@snapshot_app .command (name = "ohlc" )
181
186
@with_spinner
182
- def snapshot_ohlc (symbol : str , output_file : Optional [ str ] = None ):
187
+ def snapshot_ohlc (symbol : str ):
183
188
"""Get real-time OHLC data for a given symbol."""
184
- result = snapshot_data .get_ohlc (symbol )
185
- save_output (result , output_file )
189
+ result = snapshot_data .get_ohlc (symbol , write_csv = True )
190
+ if result is not None :
191
+ typer .echo ("Data retrieved successfully" )
192
+ else :
193
+ typer .echo ("Failed to retrieve data" )
186
194
187
195
188
196
@snapshot_app .command (name = "bulk-ohlc" )
189
197
@with_spinner
190
- def bulk_ohlc (symbols : List [str ], output_file : Optional [ str ] = None ):
198
+ def bulk_ohlc (symbols : List [str ]):
191
199
"""Get real-time OHLC data for multiple symbols."""
192
- result = snapshot_data .get_bulk_ohlc (symbols )
193
- save_output (result , output_file )
200
+ result = snapshot_data .get_bulk_ohlc (symbols , write_csv = True )
201
+ if result is not None :
202
+ typer .echo ("Data retrieved successfully" )
203
+ else :
204
+ typer .echo ("Failed to retrieve data" )
194
205
195
206
196
207
@snapshot_app .command (name = "trades" )
197
208
@with_spinner
198
- def snapshot_trades (symbol : str , output_file : Optional [ str ] = None ):
209
+ def snapshot_trades (symbol : str ):
199
210
"""Get real-time trade data for a given symbol."""
200
- result = snapshot_data .get_trades (symbol )
201
- save_output (result , output_file )
211
+ result = snapshot_data .get_trades (symbol , write_csv = True )
212
+ if result is not None :
213
+ typer .echo ("Data retrieved successfully" )
214
+ else :
215
+ typer .echo ("Failed to retrieve data" )
202
216
203
217
204
218
if __name__ == "__main__" :
0 commit comments