Development of an online service for investors at pythonanywhere.com using data from Yahoo Finance

The purpose of this article is to share the experience of developing an online analytical service for investors in python. The focus is more on conceptual issues than on a detailed description of any particular technology. I hope that the reader can easily find any details with a little googling. For the sake of clarity, I give a link to the service at the very beginning of the presentation www.emarkoff.com .



image



A little about investments



I started to be interested in the topic of investments back in the summer of 2019 and immediately plunged into the sea of ​​various publications and video bloggers who, in disagreement, gave advice on how and where to invest. It became clear that in the world of investments, there are two main ways of making decisions, which are based on fundamental and technical analysis. The first method is more suitable for long-term investments, and the second for speculation.



After some time, an understanding came of the undoubted benefits of playing various historical scenarios like “what would happen if I invested in such and such a year and for such and such a period”. This kind of analysis allows you to feel the difference between securities suitable for investment and speculation and forms an understanding of the novice investor in what financial instruments and for what time frame it is worth investing.



Idea maturation, choice of host and web service architecture



Over time, the idea arose to create a service in the form of a website that would allow playing historical scenarios of investing in a variety of financial instruments.



And the next step was to identify three key points:



  • host selection;
  • choice of technology stack and service architecture;
  • search for an open source of data from which it would be possible to pull information about market quotes.


It immediately became clear that it is desirable to play scripts (that is, perform all calculations) on the server side. And there are three reasons. The first is the reluctance to open all the code to the user. The second reason is the search for a solution in which there will be no fundamental need to transfer all data over the network, and the third is independence from the computing capabilities on the user's side. The choice was made with a view to the future, if suddenly the volume of data becomes so large that it will be necessary to deploy the back end on specialized platforms such as Amazon Web Services or Microsoft Azure. As usual, given enough ambition, solutions are initially chosen to be scalable.



As for programming languages, there was no doubt about the frontend initially. Everything is pretty standard: html + css + javascript. It was decided to write the backend in python, since it is one of the most popular and supported languages ​​for rapid development and prototyping today. All subsequent decisions were to some extent a consequence of the choice made above.



Pythonanywhere.com was chosen as the host, the distinctive feature of which is that it is geared towards programming in python and offers a relatively cheap rate of $ 5 per month. Subsequent development of the website was carried out using the Django framework. Although this framework is not considered the simplest, it provides the developer with extensive tools in the form of views, templates and plug-in ready-made applications, which in total allows you to build a full-fledged website.

In terms of choosing a data source, almost immediately my attention was attracted by such a service as Yahoo Finance and, accordingly, the yfinance library providing a convenient API for python.



Some features of pythonanywhere.com



Pythonanywhere hosting allows you to work in several modes:



  • launching the python interpreter in the console;
  • work in a jupyter laptop;
  • launching full-fledged web applications.


If desired, hosting allows you to install and use any python web framework. It may not necessarily be Django, but, for example, the same Flask, which is considered easier to learn. Setting up and publishing a web application is literally a few clicks away so you can focus on the essentials.



Convenient dashboard gives quick access to files and folders, to the console and any settings. Built-in support for https, connected domain names, as well as password protection of site content. Traffic monitoring is available, as well as server logs via the link in the dashboard.



I would also like to note another undoubted convenience for the developer. In addition to server logs, when errors occur on the backend side, tracing and local values ​​of variables are often displayed right in the current browser window at the time of calling the corresponding functions and methods. Moreover, the trace can be seen even in a browser window on a smartphone.

This kind of debugging mode personally helped me a lot, allowing me to correct errors in the code literally on the fly.



Another important point that is undoubtedly worth your attention is working with version control systems. Git, svn, and hg / mercurial are available in the Bash embedded console.



Working with data



I chose the financial information provider Yahoo Finance and the yfinance library due to the lack of subscription fees and access to daily updates of stock information. Through the yfinance API, the online service pulls information on the fly from three American stock exchanges NASDAQ, NYSE and AMEX. However, you should immediately pay attention to the fact that on the developer's site yfinance there is a warning about the prohibition of commercial use.



As for the technology of web-server interaction, a template with html-markup is initially loaded into the browser. The next step is to run javascript, which uses the get-requests method to retrieve all the necessary data from the server. Updating the page content is performed using Ajax technology through the interfaces of the jQuery library.



All online service data is stored in two formats: csv and SQLite. This choice made it possible to quickly start implementing the basic logic without having to install and configure a database server.



A little about Django



I would also like to dwell separately on three software components that were built into the site architecture with ready-made blocks, which significantly accelerated the development. I mean the Django admin panel, the django.contrib.sessions session management module, and the excontrib / django_comments framework.



Django allows you to build projects from separate software components called applications. Once written, applications can be reused in different projects. In order to embed the above components into the project, you must explicitly specify them in the settings.py file



INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.sessions',
    'django_comments',
]


In addition, URL patterns should be configured appropriately for making requests from the browser side:



urlpatterns = [
    url(r'^admin/', admin.site.urls)
    url(r'^comments/', include('django_comments.urls')),
]


As you can see from the above code snippets, it is not so difficult to build ready-made components, and as a result, they add a significant part of the functionality to the project.



After deploying the django framework, starting the admin panel requires a few simple steps: check your settings.py file settings, migrate the built-in data models, and create a superuser. The administrator is assigned his personal login, password and mailbox. By default, the admin panel is accessed by adding the standard path / admin / to the domain URL.

Inside the panel there is a convenient interface for managing the built-in database. By default, this database stores information about users and user groups. This is very convenient because it is easy to implement user authorization on the site. And if the speed of website launch is fundamentally important, you can save time and not create a separate page in order to get new users and grant them rights. It is enough to go to the admin panel and add a new entry to the database via the UI. However, this solution is only suitable for a small number of users, as it does not give users the ability to register and reset their password themselves.



Another feature of the admin panel is that it gives access not only to user data, but, in principle, to any data that is stored in the database under the control of Django. For example, in addition to data about authorized users, the database partially stores the site content and comments left by visitors.



I'll tell you about a life hack that I used to add a feedback form to the About myself page. In order not to write separate code either for the browser or for processing data on the server, instead of a feedback form, a standard comment block was built into the corresponding html template, but the ability to view the history was removed. With this approach, the feedback form data is processed and stored in the same table as the comments of the site visitors.



Session management has been implemented using the built-in django.contrib.sessions module. Session objects store information about the settings selected by users when they visit the site. In particular, the sessions store the selected investment period, the historical period for performing the analysis, the language of the site.



I will dwell a little on how I made the site multilingual. It was important for me to find a simple and quick solution in which I could go to the hosting panel and edit the text of any article in just two clicks. Therefore, for storing the texts of articles, I chose the format of independent text files, and not loading into the database. Given a small number of articles (roughly speaking, up to 10) and only two language selection options (Russian and English), I consider this solution quite acceptable. Again, it all depends on the scale.

And the last thing I would like to focus on is the use of Django templates. Among other things, templates allow you to import variable values ​​into an html page directly from python code, as well as collect html pages from several independent blocks. As an illustration, I will give an example directly from my code.



Embedding several blocks with dynamic content into the general skeleton of an html page:



html:
        {% include article_file_name %}
        {% include online_service_file_name %}
        {% include comments_file_name %}

Pyhton/backend:

def view_article(request, selected_url_path = 'expected_return'):

    context = {
        'article_file_name':article_file_name,
        'online_service_file_name':online_service_file_name,
        'comments_file_name':comments_file_name,
    }
    return render(request, 'articles/base_blog_page.html', context=context)


Here article_file_name, online_service_file_name and comments_file_name are actually the names of the variables through which the paths to the html files with the added content are passed.



Conclusion. Useful experience of self-development of a web service



You can see the result of my work on the project by clicking on the link www.emarkoff.com .



The pythonanywhere platform is generally a good solution for developing Django-based web applications. And if you have your own ideas, then two or three months will be quite enough for you to understand the basics and implement some simple web application, even if you are not a professional web developer.



This experience can be very useful for managers, analysts and anyone who has to deal with web development and web services on a duty, especially during the beta testing stage. You will be able to better understand the meaning of server error messages, communicate more productively with technical support and development representatives.



All Articles