-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04_data_structures.py
More file actions
55 lines (44 loc) · 1.72 KB
/
Copy path04_data_structures.py
File metadata and controls
55 lines (44 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""
04_data_structures.py
---------------------
Concept: Data Structures (Lists, Dictionaries, Tuples)
Context: Order books, Trade history, and OHLC data.
"""
# 1. Lists (Ordered, Mutable sequence)
# Storing a sequence of executed trade prices
trade_history = [59000, 59200, 58800, 60100]
print("--- Lists: Trade History ---")
print(f"All trades: {trade_history}")
trade_history.append(60500) # New trade executed
print(f"Added new trade: {trade_history}")
print(f"First trade: {trade_history[0]}")
print(f"Last trade: {trade_history[-1]}")
# 2. Dictionaries (Key-Value pairs)
# Representing an Order Book or a specific Coin's metadata
coin_metadata = {
"symbol": "ETH/USDT",
"exchange": "Binance",
"min_qty": 0.001,
"price_precision": 2
}
print("\n--- Dictionaries: Coin Metadata ---")
print(f"Trading Pair: {coin_metadata['symbol']}")
print(f"Exchange: {coin_metadata['exchange']}")
# Simulating an Order Book
order_book = {
"bids": [{"price": 2990, "qty": 1.5}, {"price": 2985, "qty": 2.0}],
"asks": [{"price": 3005, "qty": 0.5}, {"price": 3010, "qty": 1.2}]
}
best_bid = order_book["bids"][0] # First element of the list of bids
print(f"Best Bid: Price ${best_bid['price']}, Qty {best_bid['qty']}")
# 3. Tuples (Ordered, Immutable sequence)
# OHLC (Open, High, Low, Close) data for a single candle
# We use tuples because historical candle data shouldn't change once closed.
# Format: (Open, High, Low, Close)
candle_1h = (3000, 3100, 2950, 3050)
print("\n--- Tuples: OHLC Data ---")
print(f"Open: {candle_1h[0]}")
print(f"Close: {candle_1h[3]}")
# Trying to change a tuple will cause an error:
# candle_1h[0] = 3050 # This would raise TypeError
print("Tuples are immutable (cannot be changed), safe for historical data.")