Sign in

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'])
greet = 'hello'

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

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
Text-Editorimport pandas as pd
import yfinance as yf
from datetime import date, timedelta
from matplotlib import pyplot as plt

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…

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:

  • Can contain objects and even functions/classes
  • Are mutable
  • Can extract specific indexes
NumsInTheHat = [2, 91, 34, 56, 32, 5, 65, 3]
Num2 = NumsInTheHat[1]

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}'

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. Create a class with an appropriate name (i.e. Customer)
  2. 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. (which you will create in the app directory)
  2. (app directory)

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)


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:

  • 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