Skip to content

Commit 21e5853

Browse files
committed
feat: add function to save stock market data to CSV
1 parent 2575215 commit 21e5853

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Diff for: example/candle_stick/example_candle_stick_mpl.py

+31
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,35 @@ def generate_sample_data(
193193
}
194194

195195

196+
def save_data_to_csv(data: Dict[str, List], filename: str = "stock_data.csv") -> None:
197+
"""
198+
Save stock market data to a CSV file.
199+
200+
Parameters
201+
----------
202+
data : Dict[str, List]
203+
Dictionary containing stock data with keys like 'Date', 'Open', 'High', etc.
204+
filename : str, optional
205+
Name of the CSV file to save data to, by default "stock_data.csv"
206+
207+
Returns
208+
-------
209+
None
210+
Function writes data to the specified file but does not return anything
211+
212+
Examples
213+
--------
214+
>>> data = generate_sample_data(start_date="2021-01-01", end_date="2021-01-10")
215+
>>> save_data_to_csv(data, "my_stock_data.csv")
216+
"""
217+
# Convert the dictionary to a pandas DataFrame
218+
df = pd.DataFrame(data)
219+
220+
# Save the DataFrame to a CSV file
221+
df.to_csv(filename, index=False)
222+
print(f"Data saved to {filename}")
223+
224+
196225
if __name__ == "__main__":
197226
data = generate_sample_data(
198227
start_date="2021-01-01",
@@ -201,6 +230,8 @@ def generate_sample_data(
201230
volatility=0.015,
202231
)
203232

233+
save_data_to_csv(data, "stock_data.csv")
234+
204235
fig, ax = generate_candlestick_chart(
205236
data, output_file="candle_stick.svg", show_volume=False
206237
)

0 commit comments

Comments
 (0)