程序代写代做 finance algorithm CIS 191 Final Project

CIS 191 Final Project
You will write a stock market trading algorithm. You will use historical stock market data and decide when and how much to buy or sell a stock. Your program, as a simulation, will be fed this historical stock data to ‘replay’ the days of trading for that stock. You will have your program act as if days are passing (day 0, day 1, day 2, …, etc), albeit much, much faster. Each ‘day’, you will look back at previous days, and make decisions on how you intend to trade your stock that day (if at all.)
Topics related to the final project: ​numpy, pandas, matplotlib. Submission
● Please submit final_project_.py to Blackboard.
Rule​: Independent Open Book Final Project.
● You’re encouraged to check books, notes, class materials or google for the scope of this final project.
● The instruction team will NOT provide help on this final project other than simple clarifications on the problems.
● Please keep your work independent. Please do not check with each other on solutions. Plagiarism is a serious
problem.
Notes
● For all ​Account Balance​, please keep 2 decimal places.
● Assume there’s only 1 type of stock in this world, that’s the input stock. In this whole project, we refer to the
same stock, keep buying and selling.
● Please make your code nice and clean. Repeated code blocks will result in deductions, for example, read from
the same file more than once.
● Please do not use GLOBAL VARIABLES for this project. (google: python global variable). Initial account value
should be initiated in ‘main’ function.
● If you want to change a function’s signature (how many variables, what type), it’s totally ok to do so. As long as
you can achieve the requirement.
Part 1: Read Data & Understand Data
● Write a function called ​read_data​: read a file, the file name should be input variable for the function. The function will ​return a pandas dataframe representing the data in the file. Then, call this function in the ​main function. (​google keyword: how to read csv using Pandas​)
● Write a function called ​data_statistics​: take the pandas dataframe that you returned with ​read_data as an input, print out statistics of all numeric columns, including count, mean, standard deviation, min, Q1, median, Q3, min and max. Note that you only need to print out the statistics to screen(not to file). This function does not return anything. Use this function in the ​main function to ​print out statistics of the created dataframe.(​hint: which dataframe method can do all these? Try Google: python dataframe statistics​).

Part 2: Bookkeepings
● Write a function called ​make_transaction(account, price, quantity, buy)​, where ​account is a dictionary that holds two values: ​balance and ​stock_quantity​. You should set an initial ​account value in the ​main ​function​. Price is the current price of the stock, ​quantity is the number of stocks you would like to buy/sell, and ​buy is a boolean to indicate ​buy or ​sell (​True is buy and ​False is sell). This function handles the selling and buying, updating ​account​, no need to return anything, and should ​print nice formatted information about what transaction is done, what’s current balance, quantity of stock left and other information you believe is important to trading. The function will be used in Part 3.
● Note: If the transaction you would like to make is not possible (selling more stocks than you already have or buying more stock that values more than your balance), this function should adjust the quantity so that maximum quantity of transactions can be made. For example, if you only have 5 stocks and the input wants you to sell 10, you should only sell 5 stocks
Part 3: Trading with Moving Average Algorithm
● Write function ​get_sma(dataframe)​, which returns the 10 day sma (Simple Moving Average) as a list. Details please refer to homework5. Here the input ​dataframe comes from the result of ​read_data​. The input dataframe has many columns, please use the “​Open​” column as the price for the day. This function should calculate each day’s SMA by calculating the previous 10 day’s average (NOT including current day), then ​return a ​list of all the days’ sma data. First 10 entries of the returned list will be 0, since we don’t have enough data to calculate sma.
● Write a function called trade(dataframe, sma, account)​. Your function should make decision to buy, sell or do nothing EACH DAY following rules: (please use the ‘​Open​’ price in input data file as today’s price) (​hint:Google-pandas select rows and columns​)
○ If the value of the day’s stock is 5% LESS than the SMA value, you should BUY 10 stocks
○ If the value of the day’s stock is 5% HIGHER than the SMA value, you should SELL 10 stocks
○ You should use ​make_transaction​(​function in part2​) to trade. Note that ​make_transaction has
implemented correction for “impossible trades”
● Your function should return a list to indicate any buy or sell for each day. and make bookkeeping accordingly.
(​hint: for loop, nested if-else)​
● You can NOT borrow balance or borrow stocks (no negative quantity of stocks in your account).
● If ​BUY​, the entry of the day should be 1, if ​SELL the entry should be -1, and else the entry is 0. So after X
number of days, it ​returns a ​list of X number of entries of 1 or -1 or 0. For the first 10 days of input data, since we don’t have enough data to ​get_sma​, let’s assume we have all 0 here in this list for the first 10 days.
Part 4: Plotting
● ​Write a function called ​make_plot(dataframe, sma, sell, buy)​. Your function will do the following:
○ Plot the stock price and SMA value for each day, similar to what we did in Homework 5. Note we use ‘​Open​’ column as each day’s price. (h​ int: use return value from ​sma​, extract ​sell and ​buy values from
return values in t​ rade function)​

○ Plot a ​BIG, RED DOT at the daily stock price if any ​SELL is made. Print a BIG, GREEN DOT at the daily stock price if any ​BUY is made.(​hint:how do you handle BUY value when you are plotting SELL? plot hint: Google-python pyplot colors; Google-pyplot tutorial)​
○ On the horizontal axis, you should print out the dates as ticks.(​hint: Google-python plot dates​) If printing all dates makes the xticks too crowded, you can print weekly or even monthly dates.
○ Please add title, labels, legends. The overall goal is to make the plot nice and clear.
Part 5: Main
● ​Write the ​main​ function called ​main( )​. Your function will do the following:
○ Set an initial value of ​account: ​choose any initial amount from 1000 to 100000 as you like. Initial quantity
should be 0.
○ For input data, please download the 1 year data from ​finance.yahoo.com according to the instruction on
homework5 for a stock you pick from this list. Then save the input data as input.csv.
https://www.tradingview.com/markets/stocks-usa/market-movers-active/
○ Call functions ​read_data, data_statisticss
○ Print records of ​buys​ and ​sells ​(​hint: a list of 0/1/-1 in part 3​)
○ Make the plot required in Part 4