-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_functions.py
More file actions
55 lines (45 loc) · 1.4 KB
/
Copy path03_functions.py
File metadata and controls
55 lines (45 loc) · 1.4 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
"""
03_functions.py
----------------
Concept: Functions
Context: Reusable code for calculating position sizes and PnL.
Functions allow you to write logic once and reuse it multiple times.
"""
# 1. Simple Function
def calculate_pnl(entry_price, exit_price, position_size):
"""
Calculates Profit and Loss (PnL).
PnL = (Exit - Entry) * Size
"""
diff = exit_price - entry_price
pnl = diff * position_size
return pnl
# 2. Function with Default Arguments
def get_fee(trade_amount, fee_rate=0.001):
"""
Calculates trading fee. Default fee rate is 0.1% (0.001).
"""
return trade_amount * fee_rate
# 3. Main Logic
def main():
# Scenario 1: Quick Scalp
entry = 50000
exit_p = 50500
size = 0.1 # BTC
profit = calculate_pnl(entry, exit_p, size)
print(f"Trade 1 PnL: ${profit}")
# Scenario 2: Swing Trade
entry_2 = 48000
exit_2 = 52000
size_2 = 0.5
profit_2 = calculate_pnl(entry_2, exit_2, size_2)
print(f"Trade 2 PnL: ${profit_2}")
# Checking Fees
trade_value = entry * size # 50000 * 0.1 = 5000
fee_standard = get_fee(trade_value) # Uses default 0.1%
fee_vip = get_fee(trade_value, fee_rate=0.0005) # VIP rate 0.05%
print(f"\nFee (Standard): ${fee_standard}")
print(f"Fee (VIP): ${fee_vip}")
# Best practice: Execute main() only if script is run directly
if __name__ == "__main__":
main()