# R-programming and Investing

**URL:** https://forum.inderes.com/t/r-ohjelmointi-ja-sijoittaminen/6815
**Category:** Sijoittaminen
**Created:** [August 18, 2020, 4:36pm UTC](https://forum.inderes.com/t/r-ohjelmointi-ja-sijoittaminen/6815 "2020-08-18T16:36:32Z")
**Posts on this page:** 11
**Page:** 2

<div class="post-metadata">

### Author: ![investorperish](https://forum.inderes.com/letter_avatar_proxy/v4/letter/i/e99b99/32.png) [@investorperish](https://forum.inderes.com/u/investorperish)
#### Post date: [August 20, 2020, 8:49pm UTC](https://forum.inderes.com/t/r-ohjelmointi-ja-sijoittaminen/6815/21 "2020-08-20T20:49:29Z")

</div>

That’s an interesting idea! It’s certainly very sensible to try and find a realistic upper and lower bound for future returns. It would surely help structure one’s investment decisions. I’ll have to consider if I could build something based on that myself. 🙂

That made me think, have you looked into investment return simulation models? About a year and a half ago, I read an interesting post “Saving is worth it, getting rich is difficult” [Säästäminen kannattaa, rikastuminen on vaikeaa - Akava Works](https://akavaworks.fi/blogit/saastaminen-kannattaa-rikastuminen-on-vaikeaa/) In it, returns were calculated with a 4% return and a standard deviation of ± 15% over a 30-year investment horizon. Apparently, this is a Monte Carlo type simulation. I would criticize it for whether it’s justified to assume that the return for each consecutive year is normally distributed, when often larger declines are followed by an upswing… I don’t know if it matters in the long run. This is a bit off-topic, and I don’t know if you find it an interesting subject, but at least these simulations are easy to calculate with R.. 😃

---

<div class="post-metadata">

### Author: ![Ripelein](https://forum.inderes.com/user_avatar/forum.inderes.com/ripelein/32/216874_2.png) [@Ripelein](https://forum.inderes.com/u/Ripelein)
#### Post date: [August 21, 2020, 3:38am UTC](https://forum.inderes.com/t/r-ohjelmointi-ja-sijoittaminen/6815/22 "2020-08-21T03:38:14Z")

</div>

A very interesting topic indeed. In the long run, however, the return should trend towards an average of 4%. I don’t remember exactly, but wouldn’t a normal distribution have meant that the return would be approximately:

68% of years 4% ± 15%  
95% of years 4% ± 30%  
98% of years 4% ± 45%

Benoit Mandelbrot (The (Mis)Behavior of Markets) has quite clearly shown that stock returns are not normally distributed. Almost every day, returns occur that should almost never appear. Even if the calculated standard deviations are not entirely accurate, they can still be better than nothing.

---

<div class="post-metadata">

### Author: ![Maucca](https://forum.inderes.com/user_avatar/forum.inderes.com/maucca/32/36301_2.png) [@Maucca](https://forum.inderes.com/u/Maucca)
#### Post date: [August 21, 2020, 9:00am UTC](https://forum.inderes.com/t/r-ohjelmointi-ja-sijoittaminen/6815/23 "2020-08-21T09:00:14Z")

</div>

Tidyverse indeed makes the R user experience incredibly much more pleasant, and my own code relies heavily on the package’s functions. One small but essentially life-simplifying operator is the magrittr package’s pipe %\u003e%, which allows you to create long command sequences instead of separate lines. It’s worth getting familiar with it early in your R career.

I personally learned R practically through trial, error, and Stack Exchange. In hindsight, it wasn’t an optimal path, and many obvious things were learned too late, e.g., the usefulness of writing your own functions and dividing larger analyses into different scripts. In hindsight, I strongly recommend going through a book/course.

I stumbled upon this channel yesterday, where they do some kind of portfolio analysis with R: [https://www.youtube.com/watch?v=2Y4HX0UUcrA](https://www.youtube.com/watch?v=2Y4HX0UUcrA). It seems like a competent source, and many people have apparently liked the videos.

My own investment success has been modest so far, and it seems I’m still paying those famous tuition fees. Partly because of this, I want to move stock selection in a more systematic direction. I understand that you can’t choose targets based solely on financial ratios, but at least some kind of screening.

I recently read a book called Dhando Investor (highly recommended), which mentioned some methods for finding potentially interesting targets. For example, “The Magic Formula” from the book The Book That Beats The Market ([https://www.thebalance.com/what-is-magic-formula-investing-4150688](https://www.thebalance.com/what-is-magic-formula-investing-4150688)). Another easily implementable method was to filter stocks whose price has reached its one-year minimum during the past week. One idea for a project would be to collectively look at what kind of companies emerge from the Helsinki Stock Exchange using these and other criteria.

These would be easy to code in R if those financial ratios can be conveniently retrieved, and you could get a really nice automatically updating report with R Markdown or R Shiny. The aforementioned BatchGetSymbols is handy for retrieving historical prices, and if anyone knows a similar tool for retrieving financial ratios, please let me know!

I have strongly adopted the principles of open science from my dissertation supervisor, so if I ever manage to create something, the intention is to make it publicly available. It’s a completely different matter, of course, whether anything truly useful ever gets created 😁

---

<div class="post-metadata">

### Author: ![Maucca](https://forum.inderes.com/user_avatar/forum.inderes.com/maucca/32/36301_2.png) [@Maucca](https://forum.inderes.com/u/Maucca)
#### Post date: [August 21, 2020, 9:16am UTC](https://forum.inderes.com/t/r-ohjelmointi-ja-sijoittaminen/6815/24 "2020-08-21T09:16:08Z")

</div>

Could you elaborate a bit on what kind of model it is?

I’ve tried fitting some time series models to curves myself. So far, just basic AR(p) processes, and I haven’t really learned anything from them.

Sometimes I thought about systematically going through stochastic processes used in financial analysis (AR(p), Ornstein-Uhlenbeck, CKLS, Black-Scholes, etc.) as a project and fitting them to time series. The benefit would probably be mainly pedagogical, but it would look good on a CV.

---

<div class="post-metadata">

### Author: ![Ripelein](https://forum.inderes.com/user_avatar/forum.inderes.com/ripelein/32/216874_2.png) [@Ripelein](https://forum.inderes.com/u/Ripelein)
#### Post date: [August 21, 2020, 12:31pm UTC](https://forum.inderes.com/t/r-ohjelmointi-ja-sijoittaminen/6815/25 "2020-08-21T12:31:06Z")

</div>

Planning something like this

1. First, collect the necessary financial statement data and stock price data from companies
2. Align the financial statement data with the correct dates
3. Calculate the daily key figures to be used
4. For each day, the key figures are now known, as well as how the stock price has developed since that day
5. For each day, find the best return forecast and standard deviation to support investment
6. For example, if three key figures are used, for each day, the aim is to create, with the help of the data, e.g., 100 scenarios that are similar in terms of key figures but likely different in terms of outcome. For the three key figures, threshold values are given, between which the key figure must be. If only 50 similar situations are found, the threshold values are increased until 100 are filled.
7. For each match, calculate the average annualized stock price development, standard deviation, and Sharpe ratio. For example, the stock price rises by an annualized 4%, standard deviation 10% → Sharpe ratio 0.4
8. Select the return forecast and standard deviation based on the highest Sharpe ratio
9. The smaller the average difference between the realized return and the return forecast, the better the prediction model
10. Create investment plans and simulate the development of an investment portfolio. There is a lot of choice in different plans here as well.

Quite laborious and many small details. Then one just has to hope that stocks are priced roughly the same way as before.

---

<div class="post-metadata">

### Author: ![Ripelein](https://forum.inderes.com/user_avatar/forum.inderes.com/ripelein/32/216874_2.png) [@Ripelein](https://forum.inderes.com/u/Ripelein)
#### Post date: [October 23, 2020, 3:07am UTC](https://forum.inderes.com/t/r-ohjelmointi-ja-sijoittaminen/6815/26 "2020-10-23T03:07:48Z")

</div>

The project has progressed a bit by incorporating machine learning into the process. Three different criteria have been used in selecting the yield forecast, which are currently being developed together. Some parameters have always been changed in each round, providing information on how the change affects performance.

 ![Machine Learning](https://eu3.dh-cdn.net/uploads/db2934/original/3X/3/f/3f132f0c1859204549feb905b6b6e6a748d2d606.jpeg)

---

<div class="post-metadata">

### Author: ![Ripelein](https://forum.inderes.com/user_avatar/forum.inderes.com/ripelein/32/216874_2.png) [@Ripelein](https://forum.inderes.com/u/Ripelein)
#### Post date: [November 7, 2020, 10:30am UTC](https://forum.inderes.com/t/r-ohjelmointi-ja-sijoittaminen/6815/27 "2020-11-07T10:30:51Z")

</div>

In Nordnet, you can trade programmatically, for example, by controlling the mouse and keyboard. The files shown at the top of the image have been saved to the Python folder using a screenshot tool. A couple of first steps in the code.

 ![Nordnetiin](https://eu3.dh-cdn.net/uploads/db2934/original/3X/4/3/435dbcd47f3d89e3a8b7aa47126c69903d04888d.png)

---

<div class="post-metadata">

### Author: ![Alfons\_Knautmauer](https://forum.inderes.com/user_avatar/forum.inderes.com/alfons_knautmauer/32/63399_2.png) [@Alfons\_Knautmauer](https://forum.inderes.com/u/Alfons_Knautmauer)
#### Post date: [November 7, 2020, 10:55am UTC](https://forum.inderes.com/t/r-ohjelmointi-ja-sijoittaminen/6815/28 "2020-11-07T10:55:38Z")

</div>

It would probably be handier to use the Selenium package if you want to trade automatically like this.

---

<div class="post-metadata">

### Author: ![Torniojaws](https://forum.inderes.com/user_avatar/forum.inderes.com/torniojaws/32/118246_2.png) [@Torniojaws](https://forum.inderes.com/u/Torniojaws)
#### Post date: [November 7, 2020, 12:20pm UTC](https://forum.inderes.com/t/r-ohjelmointi-ja-sijoittaminen/6815/29 "2020-11-07T12:20:36Z")

</div>

Technically it’s possible, but Nordnet’s official rules prohibit it:

[https://www.nordnet.fi/robots.txt](https://www.nordnet.fi/robots.txt)

> User-agent: \*  
> Disallow: /yleisnakyma  
> Disallow: /tapahtumat  
> Disallow: /luotot  
> Disallow: /tietamystesti  
> Disallow: /viimeisimmat-kaupat  
> Disallow: /seurantalistat  
> Disallow: /kuukausisaastaminen  
> Disallow: /yhtiotapahtumat  
> Disallow: /rahansiirrot  
> Disallow: /salkkuraportti  
> Disallow: /pension-information  
> Disallow: /vuositiliote  
> Disallow: /asetukset  
> Disallow: /viestit  
> Disallow: /avaa-tili  
> Disallow: /palkkioluokat  
> Disallow: /kulutiedot  
> Disallow: /etsi  
> Disallow: /rahasto-vaihtotoimeksiannot  
> Disallow: /valuutat  
> Disallow: /arvio  
> Disallow: /pensionsavings  
> Disallow: /avtal-och-abonnemang  
> Disallow: \*/toimeksianto/myy  
> Disallow: \*/toimeksianto/osta  
> Disallow: \*/mediaapi/historical/csv/

So, what could happen is that Nordnet notices your automation and sends you a cease-and-desist, either officially or unofficially. If they’re really “German” about it, they’ll lock your account.

---

<div class="post-metadata">

### Author: ![Ripelein](https://forum.inderes.com/user_avatar/forum.inderes.com/ripelein/32/216874_2.png) [@Ripelein](https://forum.inderes.com/u/Ripelein)
#### Post date: [November 7, 2020, 1:35pm UTC](https://forum.inderes.com/t/r-ohjelmointi-ja-sijoittaminen/6815/30 "2020-11-07T13:35:09Z")

</div>

My own trading is so small-scale that I can definitely make the orders manually. It didn’t even occur to me that it could be forbidden.

Through the [https://api.test.nordnet.se/](https://api.test.nordnet.se/) API, it would apparently be in accordance with the rules, or how does this really work?

---

<div class="post-metadata">

### Author: ![Torniojaws](https://forum.inderes.com/user_avatar/forum.inderes.com/torniojaws/32/118246_2.png) [@Torniojaws](https://forum.inderes.com/u/Torniojaws)
#### Post date: [November 7, 2020, 2:25pm UTC](https://forum.inderes.com/t/r-ohjelmointi-ja-sijoittaminen/6815/31 "2020-11-07T14:25:57Z")

</div>

> [@Ripelein](#):
>
> [https://api.test.nordnet.se/](https://api.test.nordnet.se/) through the API would then apparently be according to the rules, or how does this really work?

It looks that way. However, the instructions (when you open the link) state two important things:

> **The API in production is currently only available for Swedish customers**.

And:

> [Pricelist for Nordnet External API in production](https://www.nordnet.se/mux/web/nordnet/AFhandelstj.html)  
> Production access will be granted after a completed certification process which starts with reading and understanding [Potential risks to consider](https://api.test.nordnet.se/attachments/download/24/API_potential_risk_eng_2013-1.pdf).

So, proper use (meaning actual buys/sells) requires that certificate (they probably mean a cer/crt file) and a fee.

[Previous page](https://forum.inderes.com/t/r-ohjelmointi-ja-sijoittaminen/6815.md?page=1)
