Git Product home page Git Product logo

WebClinic's Projects

project2_machinelearning icon project2_machinelearning

Stock Selector uses machine learning algorithms to suggest stocks that have a higher probability of beating the sector index

project3-4 icon project3-4

UNCC FinTech Boot Camp Project 3 (Stock grader)

project3-5 icon project3-5

Analysis on the how data can answer the question "How's the Economy?" depending on Institutional data (Big Business) versus Individual data (Average Person) and it's impact.

projectaeora icon projectaeora

🏆 A virtual assistant to assist traders with financial queries.

projectalpha icon projectalpha

PYTHON CODE WALKTHROUGH Data Sourcing In order to run a discounted cash flow model (DCF), I needed data, so I found a free API that provided us with everything I needed. I wrote a code that saved every financial statement of every company in a separate text file. In this code, I asked to ping the API’s URL for every ticker, open a text file for one of the financial statements for one company ticker, dump all the data found by the code into this file, and close it. This process was repeated for every company in our company list and every statement I have a code for. By doing so I Ire able to store the data for every company locally and did not need to ping the API every time I ran our code. Once all the financial data for each company was stored in form of a balance sheet, income statement, cash flow statement, and company profile text file, I needed to pick out specific items required for our DCF model. Thus, I defined the functions that selected all required items from the respective financial statements of each company and assigned them to a variable using utils.py. Discounted Cash Flow Model First of all, I needed to import the functions I defined in utils.py before defining the DCF model function, which would run for every company in our list. Next, I ensured to have 5 consecutive years of past data to compute the average. Thus, the first few lines of code checked whether the last year on record was 2019 from which point I would go back 5 years; if the last year was 2018, this would be taken as the first data entry from which I would go back 5 years. The second part mentioned above is important because companies file their 10-K, i.e. their annual report, at different times throughout the year so there may be companies that already filed their reports while others had not. After this step, five-year averages of every item’s percentage of revenue Ire calculated as Ill as the average revenue growth over the same period. These items included EBIT, depreciation & amortization, capital expenditures, and the change in net working capital. Once that was done, there Ire only three variables missing before calculating free cash flows for the next few years: a discount or hurdle rate; industry-specific perpetual growth rates; and a tax rate. After these three variables Ire set up, the next step was to calculate the free cash flows to the firm (fcff) for the next 5 years and determine the terminal value at the end of the period using the growth rate for the corresponding industry. For the former, I use a loop to calculate the fcff for all the year, discount it, and add it to one variable called fcffpv. Once the terminal value was calculated, these two additional numbers captured the enterprise value of the firm. Since I Ire interested in the equity value, I subtracted debt and add cash, which left us with the equity value. In one final step, I divided this value by the number of shares to end up with an intrinsic value per share. After calculating the intrinsic value per share, I compared it to the current share price with two additions. First, I added a buffer to minimize our downside risk for inaccuracy in calculations, which is called the margin of safety. Here, the intrinsic value should at least be 115% of the current share price. I also set an upper limit at 130% to ensure I would not include companies with extraordinarily high valuations, compared to their current price. If the share price calculated fell within this window, I added its ticker to a dataframe, which was the last step in the function. As such, the DCF function would run for every company and provide a dataframe with the tickers of all those companies that Ire undervalued at the time and fell within the 115% - 130% range. Portfolio Optimization The dataframe with the tickers of all the undervalued companies that was previously created has now become the portfolio, which I converted into a list and used as the source for further optimization that is about to come. Some general inputs for the rest of the code Ire the start and end date of the data I requested for optimization, as Ill as the risk-free rate and the number of simulations I wanted to run our optimizations for. Now that the general framework has been created, it is time to choose some conditioning variables to measure the performance of investment in one sector or across a combination of some/all sectors, respectively. Project Alpha uses the following conditioning variables to optimize its portfolios: • Sharpe Ratio: It measures the performance of an investment compared to the risk-free asset, i.e. the 10-year Treasury Bond, after adjusting for its risk factor or standard deviation. The Sharpe ratio would be given a higher Iight for investors who have a higher risk tolerance. In terms of code, I used the bt package to retrieve the data betIen the predetermined start and end date for the companies in our ticker list. This data was then used to find the portfolio with the highest Sharpe ratio. For that, random Iights Ire assigned to each company and the ratio was computed. After running the number of simulations previously determined, the Iights with the highest Sharpe ratio will be located using loc() and labeled ‘sharpe_portfolio’ which is a dataframe containing the excess return, the volatility, Sharpe ratio, as Ill as the Iights for every company. I also located the portfolio with the loIst volatility, put it in a dataframe called ‘min_volatility_port’ which has the same attributes. The rest of the code of this segment simply created a picture with all the portfolios generated, displaying the efficient frontier and highlighting the portfolio with the highest Sharpe ratio and loIst volatility. • Value at Risk (VaR): VaR was chosen as a diagnostic tool to assess the model. In our case, it basically indicated the percentage of time in which a loss greater than 1% would occur over a period of 5 years. Its limitation is that although it measures how bad the best of the bad is, it does not measure how bad it can get, meaning the worst of the worst. In regards to the code, I first requested the adjusted closing for the companies in our ticker list in the determined time horizon. I then retrieved the Iights from our Sharpe portfolio, set the number of days I wanted to simulate as Ill as the cutoff, before calculating the returns of every company in every period; here: daily. Thereafter, I created a new variable called ‘sigma’, which was be a copy of our return variable, in order to ensure the right format and type for our Monte Carlo loop. The simulation is pretty straight forward, as it measures how many runs the returns fall within 1% or outside of it. I then Iighed the resulting returns by the Iight of the company in the portfolio and whenever the portfolio return was outside the set boundary, it would count as a ‘bad simulation’. Once that is done, the number of bad simulations was divided by the total number of simulations to end up with a percentage of how many simulations were bad, which equals our VaR • Treynor Ratio: For the investors that already have a perfectly diversified portfolio and would like to add more assets to it, there would be a higher Iight on the Treynor ratio. It basically uses beta as a risk factor because it carries the risk relative to the market, instead of standard deviation as in Sharpe, meaning only systematic or non-diversifiable risk. For the code, I first calculated the portfolio’s beta. For that, I defined a function ‘beta’ that reads the beta of every company and returns it. The next step is to run a loop that would enter the beta of every company in our ticker list into a new dataframe. After setting the index equal to the tickers and transposing the Sharpe portfolio Iights, I can concat the two thus resulting in two columns: one is the beta of every company and the second is the corresponding Iight in the portfolio. I then created a third column as the product of columns one and two. The sum of all entries in that column is the portfolio beta, which was then used as the denominator for the ratio. The nominator was already calculated as ‘Excess Return’ in the Sharpe portfolio. • Sortino Ratio: The Sortino ratio measures only the downside risk (downside deviation or semi-deviation) by measuring returns against a minimum acceptable return, 𝜏. It is surprising to know that most of the industry ignores the total number of periods taken and just calculates the downside deviation by choosing the periods with downside risk, which results in misleading results. Project Alpha uses all the periods to calculate the same, so as to have an advantage over those robo-advisors/financial advisors that do not follow this process. The alpha in the future would be generated by going long on companies with high correct Sortino and low incorrect Sortino as they are undervalued, and shorting those with low correct Sortino and high incorrect Sortino as these are overvalued. The Sortino ratio would be given more Iight for investors who are more risk averse. This part of the code started with retrieving the data for our benchmark, the S&P 500, for the period and the calculating the average daily and annual return. After that, I calculate the portfolio returns, ‘returns[“Returns”]’, by adding the products of every company’s Iight times its return, which gave us the portfolio return for every period. From here, I calculated the downside risk by comparing the portfolio return in every period to the daily average return of our benchmark in a for loop. Before I did that, I defined a new variable called ‘semi’, which is a data series and will be filled with whatever comes out of the loop every single time. If the portfolio return minus the average daily return of the benchmark was greater than 0 – meaning the portfolio earned more than the average of the S&P500 – the value for the period was set to 0 and added to the semi data series. If it is 0, which is extremely unlikely, but whatever, it would also be 0. If it is less than 0, hoIver, which indicates underperformance, I would square the portfolio return, which already gives us the semi variance I need for our next step. From here, I can simply take the square root of the average of the ‘semi’ data series to get the daily downside risk and multiplying it by the square root of 252, which gives us the annual number. After that, I have all the numbers to calculate the Sortino ratio. • Information Ratio: The information ratio measures the portfolio returns compared to the returns of a benchmark index, i.e. S&P500, after adjusting for its additional risk. It only looks at the excess return of the portfolio over the benchmark and the volatility or risk associated with it. I already have all the inputs I need to calculate his ratio. Thus, I simply created a new dataframe with the portfolio returns of every period and the benchmark returns of every period. To find the excess return, i.e. the nominator, I simply subtracted the latter from the former and assigned it to a new variable, which I called ‘excess_return’. The nominator would be the average return of the portfolio minus the average return of the benchmark, and the denominator would be the standard deviation of the ‘excess_return’ series. Finally, I printed short sentences with the results for every conditioning variable just described as an output in the console.

projectneptune icon projectneptune

A sandbox project to build a charting and portfolio analysis components using React, Redux, and D3

projectreward icon projectreward

A software to shortlist and find the best options spread available for a given stock and help it visualise using payoff graphs.

projects-1 icon projects-1

This repository includes programming projects that I have gone through.

projects-4 icon projects-4

This repository includes everything from earth to moon and back.

projectthade icon projectthade

A Vietnamese-Stock tradebot called Thade. (Moving average is its current algorithm)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.