1. Introduction
An automated trading system is a program that performs trading automatically in financial markets,
executing trades according to pre-set algorithms without human intervention.
Python is a powerful programming language for automated trading development,
loved by many traders for its easy syntax and various libraries.
In this course, we will explain the basics of automated trading development through
Python’s fundamental data structures: lists, tuples, and dictionaries.
2. Basics of Python Data Structures
2.1 List
A list is a mutable and ordered data structure that can store multiple values.
Values stored in a list can be accessed through indices, and various data types can be mixed.
# Create a list
stocks = ['AAPL', 'GOOGL', 'MSFT']
# Add an element to the list
stocks.append('AMZN')
# Access list elements
print(stocks[0]) # Output: AAPL
# Length of the list
print(len(stocks)) # Output: 4
2.2 Tuple
A tuple is similar to a list, but it is an immutable data structure.
Tuples are good to use when the integrity of data is important.
Tuple elements are also accessed using indices.
# Create a tuple
stock_prices = (150.00, 2800.00, 300.00)
# Access tuple elements
print(stock_prices[1]) # Output: 2800.00
# Length of the tuple
print(len(stock_prices)) # Output: 3
2.3 Dictionary
A dictionary is a data structure that stores data in key-value pairs.
Values can be accessed using keys and it is mutable.
In automated trading systems, it is useful for storing information such as stock names and their prices together.
# Create a dictionary
stock_data = {
'AAPL': 150.00,
'GOOGL': 2800.00,
'MSFT': 300.00
}
# Access dictionary elements
print(stock_data['GOOGL']) # Output: 2800.00
# Print dictionary keys and values
for stock, price in stock_data.items():
print(stock, price)
# Output: AAPL 150.00
# Output: GOOGL 2800.00
# Output: MSFT 300.00
3. Developing an Automated Trading System Using Python
Now, we will develop a simple automated trading system using lists, tuples, and dictionaries.
This system will retrieve the current price of stocks and will operate by buying when the price falls below a specific threshold.
3.1 Importing Modules
import random # Module to generate random prices
3.2 Stock Price Generation Function
Let’s create a function that generates stock prices randomly.
In a real scenario, price information can be received through APIs.
def generate_stock_price():
return round(random.uniform(100, 3000), 2)
3.3 Automated Trading Function
def auto_trade(stock_name, target_price):
current_price = generate_stock_price()
print(f"Current price of {stock_name}: {current_price} won")
if current_price < target_price:
print(f"Buying {stock_name}!")
else:
print(f"{stock_name} has not reached the buying price yet.")
3.4 Main Code
# Stock data for automated trading
stocks_to_trade = {
'AAPL': 150.00,
'GOOGL': 2800.00,
'MSFT': 300.00
}
for stock, target_price in stocks_to_trade.items():
auto_trade(stock, target_price)
4. Conclusion
We have now created a simple automated trading system using basic Python lists, tuples, and dictionaries.
While real automated trading systems are much more complex, the approach to using data structures remains the same.
Utilize this course to solidify your understanding of Python basics and implement more advanced algorithms and trading strategies.