This is the fourth homework assignment. Your goal in this assignment is to write a function that performs a calculation on sequence data and returns a value.
Your solution to this homework will also be marked on code quality. This means some part of the marks will be given for good code organisation, variable/function naming, and commenting. The marks for code quality are distinct from those for functionality; to gain full marks, your submission must be both functional and readable.
Practical information#
The assignment is due Monday the 19th of September at 9.00am (Canberra time). This is the Monday in semester week 7, that is, the first week after the teaching break. To submit your solution, you will upload a single python file via Wattle. Here is the assignment submission link.
In addition to submitting your solution, you must attend the following lab (in week 7). In the lab, your tutor will ask you some questions about your solution, and give you feedback if there is anything you need to improve. This discussion with the tutor is also part of the assessment.
If you fail to show up for the discussion with the tutor, you will receive zero marks for this assignment. If you do not submit a solution, you may still get partial marks if you are able to show the tutor that you have made some attempt to solve the homework.
The homework is individual. You must write your own solution, and you are expected to be able to explain every aspect of it.
As usual, you should have followed last week’s lectures and worked through the exercises in lab 4 and lab 5 before starting on the assignment. The assignment should not take more than one or two hours to complete.
Disclaimer#
The exercise below describes a stock trading strategy, which is totally fictitious and should not be used for actual trading.
The problem#
This homework will ask you to follow a simple trading strategy for buying and selling shares (one particular stock, not a portfolio). The stock is given as a sequence of numbers, each representing the price of one share at time i given by the sequence element index. The sequence is provided in full, but we shall treat it as a time series of values where at time i the only known sequence elements are those up to and including i — we cannot see into the future.
A stock trader, who can be a real person or a computer program (a bot) buys and sells shares of a stock using the available cash (“capital”). This can be very sophisticated activity, but we consider a simplified version of it. The rules which our stock trader follows are:
- At the beginning, the trader has a starting capital
capital
, which is used to buy shares. - The shares can be bought or sold in integer quantities, each share costing the
value of
stock_price[i]
at the time i of the transaction. - When the trader buys stock at time i they use a fixed fraction
(1-p)
of the available capital (p is a short for prudence), and purchases the maximum number of shares with the total cost not exceeding that fraction. After the purchase, the amount of available capital is reduced by the total cost of shares bought. This rule indicates that a situation when the available capital is insufficient to buy even a single share is possible. - When the trader sells the stock at time i, they sell the fraction
(1-p)
of shares owned (more accurately, the integer part of those). After the sale, the amount of available capital is increased by the total value of shares sold (and the number of owned shares decreases by the amount of shares sold). - The trader decides whether to buy or to sell shares at the time i based on the comparison of current stock price and the price which the stock had at the previous time i-1. In other words, if the price increases, the trader sells a number of owned shares, or if the price decreases, the trader buys a number of shares determined by the above rules.
- Finally, at most only one transaction happens at each time i (a transaction
may not happen if the stock price doesn’t change, or the available capital is
insuffficient). When the
stock_price
sequence ends, the trader stops their operations, and calculates the result — gain or loss in the value of the entire assets (in other words, the trader subtracts the value of starting capital from the value of available capital and the value of owned shares which are priced by the last value instock_price
).
Your task in this homework is to write a function stock_trade(stock_price, capital, p)
which takes as arguments:
stock_price
is a sequence of positive numbers,capital
is a positive number andp
is a float in the range0..1
,
and returns the profit or loss at the end of trading as a float value (if no trading
was done – for whatever reason – the return value must be 0.0
).
Assumptions and restrictions:
- The function must return a float.
- At the beginning, the “time”
i=0
, the trader buys the number of shares determined by the descibed rules involving the initial capital, the prudence coefficientp
and the current stock price. - If the capital value is too low (less that the price of one share), the buyer cannot purchase any shares, and must wait until (if) the price drops low enough.
Template file and hints
As a starting point, we provide you with a skeleton code file: stock_trading.py. Download this file and write in it your implementation of the function.
The template file contains import math
statement — you may use functions from this package in your code.
You may also benefit from the use of the built-in function round
. Consider the values of available
capital, its prudence fraction and the cost of a share carefully. For example, a trader with prudence
0.9 and avalaible capital 10, can buy 1 share of a stock priced at 1.
This is obvious for pen and paper calculation, but on computer it may give you surpises
(due to limited precision of float point arithmetic) if you are not careful.
Testing#
The skeleton file has a testing function: test_stock_trade()
. It runs
some tests on stock_trade
function, and will raise an error if
any of the tests fail. If all tests pass, the testing function prints
the message “all tests passed” at the end.
Remember that testing only checks a small number of predefined cases; it can never prove that your function works correctly for all valid arguments. You should examine the test cases that are provided, and think about whether there are any important ones that are missing.
Note that you can define additional functions, if you think it helps you decompose the problem or write a better solution. Your function definitions should contain docstrings, but you may not use strings as comments anywhere other than on the first line inside a function, or at the beginning of the file.
Marking#
Code quality
In this homework (like in the last one) we will also be marking your submission for its code quality. This includes aspects such as:
-
Using good function, parameter and variable names. The names of some functions in the homework are fixed, but if you define additional functions (to decompose the problem) then they should be given descriptive names.
-
Appropriate use of comments and docstrings. This means not too little comments, but also not gratuitous, useless or incorrect commenting.
This means not too little comments but also not too much. Comments should be accurate, relevant, and readable. A docstring should appear as the first statement in every function definition.
-
Good code organisation.
This includes appropriate use of functions to decompose a problem and avoid code repetition. Also, do not import modules that you do not use.
What to submit
You should edit the skeleton file stock_trading.py
, then upload only
this file with your implementation of the function using the
assignment submission link
on Wattle.
Remember that you must upload a single Python code file. Do NOT zip it or convert it to another format.
The file that you submit must meet the following requirements:
- It must be syntatically correct python code.
- Like the file you downloaded, it should contain only function definitions, and, optionally, import statements. However, it is not necessary to use any module to solve the problem, and you should only import modules that you actually use. Comments, including docstrings (if they are used appropriately) are of course ok to include. Anything that is not a function definition or import statement will be ignored when we test your submission. Importantly, this means you must not use any global variables.
As mentioned above, you must also attend the following lab and answer your tutor’s questions about your solution. This discussion is part of the assessment. You should be prepared to answer or demonstrate to the following questions:
- Can you download the file that you submitted from Wattle?
- Can you run that file in the python interpreter? (using an IDE of your choice)
- If the file has syntax errors, can you use the error messages from the interpreter or IDE to identify where the syntax errors are?
- Does your submitted file meet the requirements stated above? Does it contain anything that is not a function definition? If so, can you point it out?
- Does your implementation pass all the tests run by the unmodified testing function?
- Is your implementation of the function correct for any valid argument sequence?
- Do your functions always return a value of the correct type?
- Did you think of any other test cases that should be used to test your function, in addition to or in place of those provided?
- What is the difference between the
print
function and thereturn
statement?
In marking this assignment we will consider the following:
- Does your submitted file satisfy the requirements specified above?
- Does your implementation compute the correct value for all valid arguments?
- The quality of your submitted python code, including its organisation, naming and documentation (with docstrings and comments).
- Your ability to use the tools (e.g., the IDE or python interpreter), your understanding of python’s error messages, and your understanding of the solution, as demonstrated in your discussion with the tutor.
The assignment is worth 4% of your final mark. 2 marks are based on the functionality of your submission, and 2 mark on the quality and readability of your code.