Learn Python by building investment AI for fintech - Lesson1: Start the project

Duomly - May 26 '20 - - Dev Community

Intro

Today we will start the Python course and start building AI.

In the last episodes, we started the first steps to build a front-end and back-end for our fintech app.

Here are the links:

Golang course:

Golang course with building a fintech banking app - Lesson 1: Start the project

Golang course with building a fintech banking app - Lesson 2: Login and REST API

Angular course:
Angular Course with building a banking application with Tailwind CSS - Lesson 1: Start the project

Angular Course with building a banking application with Tailwind CSS – Lesson 2: Login form

Now is the time for something special!
We start building an Artificial Intelligence that will predict the stock prices and will tell us what the best option to invest now to get the biggest profit is.

I cannot wait to show you all of this.

In today's episode, I will show you the first part of the "Python with AI" course, and we will set up the first part of the project.

The first part is the project installation, configuration, and database migrations.

Let's start!

If you prefer video here is the youtube version:

Install flask and dependencies

The first step that you need to is dependencies installation.

We will need a few of them, but the most important for us is a flask.
Open terminal in your project's directory and type:

pip install flask

Next, install SQLAlchemy:

pip install flask_sqlalchemy

The third one plugin that you need to install is Migrate:

pip install flask_migrate

And the last one is the PostgreSQL driver:

pip install psycopg2

Init git repo and create .gitignore

Okay, when our most important dependencies ready, we should initialize the git repository and specify the files that git should ignore.

Open the terminal and type:

git init

Next, create the .gitignore file in the project's dir, and add a few paths, that we should ignore:

**/__pycache__/*
migrations/*
.vscode

Create app.py and import deps

It's the first step when we will really start coding with Python.

We need to create a file named "app.py", and import dependencies that we already installed.

import psycopg2
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate

Export flask variable

When we finished creating the app.py file, we should export the path to our flask's app.

After that, the flask will know what file should run as an application.

Open the terminal and type:

export FLASK_APP=app.py

Define app and configure DB connection

In the next step, we should define our flask application and create the app variable.

The second what we should do is to create code that will configure a connection with our database.

In the file named "app.py", we should add our app declaration and connection string below the imports.

app = Flask(__name__)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = "postgresql://user:password@localhost:5432/dbname"

Add app run

In this step, we will focus on the app.run command.
That will let us start the application from the command line.

We will not use it in the first lesson, but we should add it to the next ones.

On the bottom of the app.py file, we should add app.run(debug=True) if the name is equal to ‚main'.

if __name__ == '__main__':
    app.run(debug=True)

Create models directory

Great! Now we can move forward and focus on the models for the database.

In the first lesson, we will create the only model named "prices", that will let us use the SQL table with the same name.

First of all, we need to create a directory named "models", add it to your project.

Create prices.py in models directory and import DB from app

Now, when we have created a necessary directory, we can go into the file.

First, create the file named prices.py (add it in the directory called "models").

Next, you should go into the prices.py file, and import variable named "db" from the app.

from app import db

Create a model in prices.py

This step is the biggest part of our today's coding, and the most important.

We need to create the whole model that we will use for the migrations.

We should start by creating a class named "PriceMode", and pass "db.Model", as a param.

The next step is to declare a variable named "tablename" with value "prices".

Below that line, we need to create columns that we will use inside our table.

After that, we should create a function named "init", and assign our columns to the self.

Let's take a look at how I did it in the example below.

class PriceModel(db.Model):
    __tablename__ = 'prices'

    id = db.Column(db.Integer, primary_key=True)
    company = db.Column(db.String())
    date = db.Column(db.String())
    openPrice = db.Column(db.Integer())
    highPrice = db.Column(db.Integer())
    lowPrice = db.Column(db.Integer())
    closePrice = db.Column(db.Integer())
    volume = db.Column(db.Integer())

    def __init__(self, company, date, openPrice, highPrice, lowPrice, closePrice, volume):
        self.company = company
        self.date = date
        self.openPrice = openPrice
        self.highPrice = highPrice
        self.lowPrice = lowPrice
        self.closePrice = closePrice
        self.volume = volume

    def __repr__(self):
        return f"<Price for {self.company} in day {self.date} is {self.closePrice}>"

Import models in app.py and add migrate

It's the last step of coding now, and we run the code!

We should come back to the file named "app.py" and import our PriceModel.

After importing the model, we should declare a variable named "migrate", and assing the Migrate function that we introduced before.

To the function named "Migrate", we need to pass the app and db variables as params.

This code should be after variable named "db", and before the last if-statement.

Let's take a look at the example below.

from models.prices import PriceModel
migrate = Migrate(app, db)

Init DB

We are done with the code!

Now we should run it and check if everything is fine.

The first what you need to do is initialize DB by using this method in the terminal:

flask DB init

Migrate and upgrade

If all went well, you should have some pycache directories and new dir named "migrations".

If yes, you can go into the DB migrations.

The first of all what you need to do is to migrate, open the terminal, and type:

flask db migrate

After migration, you need to upgrade your with the results, open the terminal and type:

flask db upgrade

Conclusion

Congratulations!

Your first step into building investment AI is done.

Now your project is ready for the next features that we will focus on in the next lessons.

Code repository for the python course is here:

https://github.com/Duomly/python-ai-investment-fintech/tree/Python-AI-course-Lesson-1

In the next lesson, we will focus on building API and data for our AI.

Keep updated, and I cannot wait to show you the next steps of that powerful tool that we build together!

Programming courses online

Thanks for reading,
Radek from Duomly

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .