-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07_classes_oop.py
More file actions
58 lines (49 loc) · 2.09 KB
/
Copy path07_classes_oop.py
File metadata and controls
58 lines (49 loc) · 2.09 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
56
57
58
"""
07_classes_oop.py
-----------------
Concept: Object-Oriented Programming (Classes & Objects)
Context: Creating a reusable Trading Bot Structure.
"""
class TradingBot:
# The __init__ method initializes the object's state
def __init__(self, bot_name, initial_balance, symbol):
self.name = bot_name
self.balance = initial_balance
self.symbol = symbol
self.position = 0.0 # Amount of asset held
print(f"[{self.name}] Initialized with ${self.balance} on {self.symbol}")
def buy(self, price, quantity):
cost = price * quantity
if cost > self.balance:
print(f"[{self.name}] Error: Insufficient funds to buy {quantity} {self.symbol}")
else:
self.balance -= cost
self.position += quantity
print(f"[{self.name}] BOUGHT {quantity} {self.symbol} @ ${price}. New Balance: ${self.balance:.2f}")
def sell(self, price, quantity):
if quantity > self.position:
print(f"[{self.name}] Error: Not enough position to sell {quantity} {self.symbol}")
else:
revenue = price * quantity
self.balance += revenue
self.position -= quantity
print(f"[{self.name}] SOLD {quantity} {self.symbol} @ ${price}. New Balance: ${self.balance:.2f}")
def get_status(self):
total_value = self.balance + (self.position * 60000) # Assuming current price 60k for valuation
return {
"Name": self.name,
"Balance (USDT)": self.balance,
"Position": self.position,
"Est. Total Value": total_value
}
# --- Using the Class ---
# Create two separate bot instances
scalper_bot = TradingBot("Scalper_v1", 10000, "BTC")
swing_bot = TradingBot("Swing_v1", 50000, "ETH")
print("\n--- Trading Session ---")
scalper_bot.buy(price=60000, quantity=0.1) # Cost $6000
scalper_bot.sell(price=61000, quantity=0.1) # Rev $6100, Profit $100
print("\n--- Swing Bot Attempt ---")
swing_bot.buy(price=3000, quantity=100) # Cost 300,000 -> Should fail
print("\n--- Final Status ---")
print(scalper_bot.get_status())