Python | Finance

Sets are object types in Python that refer to distinct or unique values.

Duplicate values are not included, they are unordered and can be modified.

They can be written with the built-in feature ‘set’.

fruit = set(['banana', 'apple'])

A string can also be passed into a set with this built in method (similar to a list).

greet = 'hello'

not_unique = list(greet)
unique = set(greet)
--------------------------------------------------------------------['h', 'e', 'l', 'l', 'o']
{'o', 'h', 'e', 'l'} # Duplicate removed

In this basic example the set returns all unique values. The repeated ‘l’ is excluded unlike in the list. …

Image for post
Image for post

This article will follow up from the accessing price data with python piece.

We will go through an introduction to MatplotLib (charting package for Python) and combine this with real financial market data.

The initial step is to install the necessary packages….

Command-Linepip install pandas                           # Data Analysis
pip install yfinance # API
pip install matplotlib # Graphs

….and then subsequently import the correct dependencies.

Text-Editorimport pandas as pd
import yfinance as yf
from datetime import date, timedelta
from matplotlib import pyplot as plt

As per the previous tutorial we will create a datetime clamp to standardise the…

Image for post
Image for post

If you are new to Python, interested in financial markets and wish to get started building your own tools/programs this will be for you.

We will be using Python and more specifically Pandas (data analysis package). Further information on Pandas can be found here if you are unfamiliar.

We will use ‘yfinance’ as the API to access daily closing prices for a handful of asset classes. What you wish to do with the data is completely up to you. …

To continue on from learning about lists we’ll now extend to another data type. Dictionaries.

There are several commonalities here. Dictionaries are mutable and can be moulded in many ways. They can also be ‘nested’ meaning that a dictionary can contain another dictionary.

When we extract specific items from a list we use indexes.

With dictionaries we access ‘keys’.

Dictionaries contain a ‘key’ and a ‘value’ (forming a pair).

A shown below, dictionaries are written with curly braces, a colon seperating the key/value and a comma seperating each pair.

Dictionary = { 'Uno': 1, 'Uno' = KEY | 1 =…

Lists are argubly the most versatile, common and most frequently used data types in Python.

Lists are expressed inside square brackets [] and have the following characteristics:

  • Ordered (maintained structure from inception)
  • Can contain objects and even functions/classes
  • Are mutable
  • Can extract specific indexes

Basic list example:

NumsInTheHat = [2, 91, 34, 56, 32, 5, 65, 3]

We can grab specific indexes from a list using square brackets as well. In Python indexes begin at 0, so the second item in a list will equate to an index of [1].

Num2 = NumsInTheHat[1]

If the list is…

Model forms in Django are a seamless way to create web-based forms with the ability to store data in the database.

Assuming your Django project is created and you have created an application folder (if not, see the documentation for the setup process), let’s get started creating a model form.

Firstly, we have created a simple model in for a ‘client’ with 2 name fields. The ‘str’ function enables us to view the model outputs cleanly in the admin panel.

from django.db import models

class Client(models.Model):
first = models.CharField(max_length=50)
surname = models.CharField(max_length=50)

def __str__(self):
return f'{self.first}, {self.surname}'

Once saved…

Models are a core pillar of the Django web framework. They contain the essential fields and data that you will be saving into the database for storage.

You will find the file inside the application folder of your project. This is what we will use to write our models.

Let’s create your first model.

  1. Ensure you have models imported from django.db
  2. Create a class with an appropriate name (i.e. Customer)
  3. Define each data point that you wish to include for each Customer. …

Django is a fantastic web framework that allows you to create an entire back-end with little limit to the size and complexity of the database involved.

The setup process is very straight-forward and can be implemented using the official guide here.

This small guide below will help you to understand the basics behind rendering a functional webpage (having the necessary components ‘click’ in the back-end).

You will need to have open the following to complete this:

  1. (from main project directory)
  2. (which you will create in the app directory)
  3. (app directory)

As part of the setup it is…

Decorators in Python may appear confusing at first, but after you start implementing them, they’re very intuitive.

To put it simply, decorators allow us to modify the behaviour of a function without changing the code.

The prerequisite to grasping decorators is to understand the mechanics behind functions. As you may know functions can return values based on inputted arguments. A basic example shown here. The function is called with 10 being the argument (10*10 = 100).

def tentimes(number):
print(number * 10)


Functions can be manipulated and used as arguments in higher order functions. A function within a…

Iterating over a block of code and looping over and over again. Iteration is a core piece of the language and empowers an individual to perform countless tasks.

Iteration can be split into 2 categories:

  • Definite — the number of times the loop is repeated is determined prior to execution
  • Indefinite — the loop executes and only stops when specific conditions are met (while loops)

For Loops

In the example below, the variable ‘each’ will iterate through all pieces of the ‘tens’ object. The statement (final line) determines what we wish to have outputted. …

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store