Python Lists in Algorithmic Trading & Crypto Strategies 4/100 Days

Python lists

Day 4: Deep study of Python List – Essential Data Structure for Algo Trading

Welcome to Day 4! In our “100 Days of Hell with Python Algorithmic Trading” journey, today we will talk about Python List, which is a very important and flexible data structure. It plays a special role in quantitative trading and algorithmic analysis.

What is Python List?

A Python List is a data type that allows you to store multiple items under the same name. It works like a dynamic array, meaning you can add or remove data from it at runtime.

For example:

trade_logs = [109, “Apple”, 111.6, True]

In this, we have stored all four types of data types – integer, string, float and boolean – in a single list – which makes it extremely flexible.

List vs Array: What is the difference?

1. Dynamic vs Fixed Size

  • When creating an array, its size has to be fixed in advance.
  • There is no such restriction in List, you can add items as you want.

2. Homogeneous vs Heterogeneous

  • Array stores the same type of data types (such as all integers or all strings).
  • You can store data of different types together in a list.

3. Ease of Use

  • In Python, it can be more difficult to manage arrays than lists because arrays may require a module like numpy.
  • Lists are an inbuilt and simple option of Python.

How are Python lists stored in memory?

Python lists are a kind of collection of objects, and every item has a reference. This means that if you change a value in a list, it only updates the memory reference of that value.

Because of this, lists are not very memory-efficient, but they are great in terms of flexibility.

Important Characteristics of Python Lists

Python lists

1. Ordered Structure
Items in lists are stored in a sequence, and their order remains.

2. Mutable
The most special thing about a list is that you can change its items – this is called mutable.

3. Allow Duplicates
You can also keep duplicate items in the list.

4. Heterogeneous Support
List supports different types of data simultaneously – like int, str, float, bool, etc.

Operations of Python Lists
You can perform many types of operations on Python Lists:

Append: my_list.append(“BTC”)

Insert: my_list.insert(1, “ETH”)

Remove: my_list.remove(“BTC”)

Pop: my_list.pop()

Sort / Reverse: Sorting or reversing the list is also easy.

Use of Python Lists in Algo Trading

Storing Trading Data
You can store the log of trades, opening and closing prices, timestamps, etc. in the list.

For Backtesting
By storing old data in the list, you can backtest your trading strategies.

Indicator Calculations
Lists are also used in the calculation of indicators like Moving Average, RSI.

Disadvantages of Python Lists

  • Low Performance: Lists have a separate reference for each item, which leads to lower performance than arrays.
  • Memory Inefficient: Occupies more memory space due to heterogeneous support and mutability.
  • Less Structured: For large datasets, they are not as structured as numpy arrays or pandas dataframes.

Three-Step Learning Strategy
In this course, we have a three-step process:

  • Learning Concepts – Understand each concept thoroughly as per today.
  • MCQs and Tasks – We have specially curated questions that will further strengthen your understanding.
  • Mini Projects – There are mini projects on each topic so that you can implement in real time.

Watch this Day 4 video tutorial

Day 4: Python Lists

1. Python lists and arrays are similar but have a key difference. What is it?

2. How are lists represented in Python’s memory?

3. Which of the following are characteristics of Python lists? (Choose all that apply)

4. What’s the output of this code?
Python
my_list = [1, 2, “hello”]
print(my_list[
1])

5. What’s the difference between the append() and extend() methods for lists?

6. What does my_list.insert(2, “new”) do?

7. How can you delete the element at index 1 from a list named numbers?

8. What is the result of [1, 2, 3] * 2?

9. How do you check if the value 5 is present in a list named my_list?

10. What does the len() function do when used with a list?

11. What does the my_list.sort() method do?

12. How would you find the minimum value in a list called numbers?

13. What does the list.reverse() method do?

14. What’s a concise way to create a list of squares of numbers 1 to 10?

15. How would you iterate through a list named colors and print both the index and value of each element?

16. Consider these lists: names = [“Alice”, “Bob”] and ages = [25, 30]. What’s the output of list(zip(names, ages))?

17. List as heterogeneous containers: Which code demonstrates that Python lists can store different data types?

18. What is a potential disadvantage of using Python lists?

19. For scenarios focusing heavily on search operations, what might be a better alternative to a Python list?

20. In which situation might using a NumPy array be more advantageous than a Python list?