Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a function tradeStrategy1(verbose) that goes through all trading days in the dictionary stocks and buys and sells shares automatically. The strategy is as follows:

Write a function tradeStrategy1(verbose) that goes through all trading days in the dictionary stocks and buys and sells shares automatically. The strategy is as follows:

  • The earliest buying decision is either on the date of the portfolio or the tenth available trading day in stock (whichever is later)
  • At any time, we buy the highest possible volume of a stock given the available cash.
  • When shares have been bought, no other shares will be bought until all shares from the previous buying transaction are sold again.
  • All shares from a previous buying transaction are sold at once, so it's a simple "buy as much as possible & sell all" procedure.
  • If shares are being sold on trading day j we will only consider buying new shares on the following trading day, j+1.

Assume that j is the index of the current trading day, then we will find the stock to buy as follows:

  • For each stock s available in stocks evaluate the quotient

     Q_buy(s,j) = 10*H(s,j) / (H(s,j) + H(s,j-1) + H(s,j-2) + ... + H(s,j-9))

    where H(s,j) is the high price of stock s at the j-th trading day. Note that Q_buy(s,j) is large when the high price of stock s on trading day j is large compared the average of all previous ten high prices (including the current). This means we might enter a phase of price recovery.

  • Find the maximal quotient Q_buy(s,j) among all stocks s and buy a largest possible volume v of the corresponding stock on trading day j. (It might not be possible to buy any as there might not be enough cash left; in this case do nothing on trading day j and move to the next. If two or more stocks have exactly the same quotient, take the one whose symbol comes first in lexicographical order.)

  • Note that, as usual, our buying decision is based on the high price.

If we have automatically bought v shares of a stock s on trading day j, then from trading day k = j+1 onwards we will consider selling all of it as follows:

  • On trading day k = j+1, j+2, ... calculate the quotient

     Q_sell(k) = L(s,k) / H(s,j),

    where L(s,k) corresponds to the low price of stock s on trading day k. This quotient is high if the current low value of the stock is large compared to the high value to which we bought it.

  • Sell all v shares of s on day k if Q_sell(k) < 0.7 (we already lost at least 30%, let's get rid of these shares!) or if Q_sell(k) > 1.3 (we made a profit of at least 30%, time to cash in!).

Notes:

  • For solving this task it might be useful to first extract a list of all trading days from the stocks dictionary:
     lst = [ '2012-01-03', '2012-01-04', ..., '2018-03-13' ]
    You can assume that all loaded stocks in the stocks dictionary can be traded on exactly the same days, and that there is at least one stock in that dictionary.
  • All buying and selling transactions should be performed using the addTransaction(trans, verbose) function from Task 5. The verbose parameter of tradeStrategy1(verbose) can just be handed over to addTransaction(trans, verbose).

Example: The following code loads a portfolio of 20,000 cash (and no shares) on the 1st of January 2012, runs the tradeStrategy1(verbose=True) until the end of available data, and valuates the portfolio on the 13th of March 2018.

s.loadPortfolio('portfolio0.csv') s.loadAllStocks() s.valuatePortfolio(verbose=True) s.tradeStrategy1(verbose=True) s.valuatePortfolio('2018-03-13', verbose=True) 

The console output is as follows:

 Your portfolio on 2012-01-01: [* share values based on the lowest price on 2012-01-01] Capital type | Volume | Val/Unit* | Value in * -----------------------+--------+-----------+------------- Cash | 1 | 20000.00 | 20000.00 -----------------------+--------+-----------+------------- TOTAL VALUE 20000.00 > 2012-01-16: Bought 29 shares of PRU for a total of 19517.00 Remaining cash: 483.00 > 2012-11-21: Sold 29 shares of PRU for a total of 25520.00 Available cash: 26003.00 > 2012-11-22: Bought 37 shares of EZJ for a total of 25696.50 Remaining cash: 306.50 > 2013-01-25: Sold 37 shares of EZJ for a total of 33633.00 Available cash: 33939.50 > 2013-01-28: Bought 35 shares of EZJ for a total of 33103.00 Remaining cash: 836.50 > 2013-05-21: Sold 35 shares of EZJ for a total of 43120.00 Available cash: 43956.50 > 2013-05-22: Bought 34 shares of EZJ for a total of 43905.22 Remaining cash: 51.28 > 2014-01-22: Sold 34 shares of EZJ for a total of 58208.00 Available cash: 58259.28 > 2014-01-23: Bought 18 shares of BATS for a total of 57456.00 Remaining cash: 803.28 > 2016-04-08: Sold 18 shares of BATS for a total of 74853.00 Available cash: 75656.28 > 2016-04-11: Bought 68 shares of SMIN for a total of 75140.00 Remaining cash: 516.28 > 2016-09-29: Sold 68 shares of SMIN for a total of 98532.00 Available cash: 99048.28 > 2016-09-30: Bought 108 shares of SKY for a total of 98594.49 Remaining cash: 453.79 > 2018-02-27: Sold 108 shares of SKY for a total of 140400.00 Available cash: 140853.79 > 2018-02-28: Bought 104 shares of SKY for a total of 140192.00 Remaining cash: 661.79 Your portfolio on 2018-03-13: [* share values based on the lowest price on 2018-03-13] Capital type | Volume | Val/Unit* | Value in * -----------------------+--------+-----------+------------- Cash | 1 | 661.79 | 661.79 Shares of SKY | 104 | 1316.00 | 136864.00 -----------------------+--------+-----------+------------- TOTAL VALUE 137525.79

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Accounting questions