Skip to content

Commit

Permalink
Merge pull request #15 from UBC-MDS/functions
Browse files Browse the repository at this point in the history
Functions
  • Loading branch information
MasonZhang-MZ authored Jan 11, 2025
2 parents b1ce87f + 96efee4 commit 70c4e83
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ traders_copilot_mzza_25 provides functionalities for generating trading signals,

- generate_signals: a function that identify buy/sell signals based on price trends.
- plot_signals: function that visualizes the timestamp and input values.
- fetch_market_data: a function that fetches market data from the internet.
- simulate_market_data: a function that simulate stock market data.
- buy_stock: a function that buy stock and update user account.
- sell_stock: a function that sells stock and update user account.

Expand Down
31 changes: 31 additions & 0 deletions src/traders_copilot_mzza_25/buy_stock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
def buy_stock(account, quantity, price):
"""Execute a stock purchase and update the user's account.
This function processes the purchase of a specific quantity of a stock at a given price.
It updates the user's account balance and increases the number of shares held for
the specified stock.
Parameters
----------
account : dict
A dictionary representing the user's account. Expected keys include:
- 'balance': float, the current cash balance in the account.
- 'stocks': int, the number of shares held.
quantity : int
The number of shares to buy. Must be a positive integer.
price : float
The price per share at which the stock is purchased.
Returns
-------
dict
The updated account dictionary reflecting the new balance and stock holdings.
Raises
------
ValueError
If the account balance is insufficient to complete the purchase.
Examples
--------
"""
13 changes: 0 additions & 13 deletions src/traders_copilot_mzza_25/fetch_market_data.py

This file was deleted.

12 changes: 2 additions & 10 deletions src/traders_copilot_mzza_25/sell_stock.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def sell_stock(account, stock_symbol, quantity, price):
def sell_stock(account, quantity, price):
"""Execute a stock sale and update the user's account.
This function processes the sale of a specific quantity of a stock at a given price.
Expand All @@ -10,9 +10,7 @@ def sell_stock(account, stock_symbol, quantity, price):
account : dict
A dictionary representing the user's account. Expected keys include:
- 'balance': float, the current cash balance in the account.
- 'stocks': dict, mapping stock symbols to the number of shares held.
stock_symbol : str
The symbol of the stock being sold (e.g., 'AAPL', 'GOOG').
- 'stocks': int, the number of shares held.
quantity : int
The number of shares to sell. Must not exceed the number of shares currently held.
price : float
Expand All @@ -30,10 +28,4 @@ def sell_stock(account, stock_symbol, quantity, price):
Examples
--------
>>> account = {'balance': 1000.0, 'stocks': {'AAPL': 10}}
>>> sell(account, 'AAPL', 5, 150.0)
{'balance': 1750.0, 'stocks': {'AAPL': 5}}
>>> sell(account, 'AAPL', 10, 150.0)
ValueError: Not enough shares to sell.
"""
14 changes: 14 additions & 0 deletions src/traders_copilot_mzza_25/simulate_market_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def simulate_market_data(start_date):
"""Simulate stock market data that contains date and price columns
from start date until today.
Parameters:
----------
start_date: Start date of the simulation (YYYY-MM-DD).
Returns:
----------
pd.DataFrame
The dataframe that contains columns for stock market data.
"""

0 comments on commit 70c4e83

Please sign in to comment.