-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths6.py
More file actions
64 lines (51 loc) · 1.59 KB
/
s6.py
File metadata and controls
64 lines (51 loc) · 1.59 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
59
60
61
62
63
64
def compute_rsi(prices):
if len(prices) < 2:
return None
gain = 0.0
loss = 0.0
for i in range(1, len(prices)):
change = prices[i] - prices[i-1]
if change > 0:
gain += change
else:
loss += abs(change)
if gain + loss == 0:
return 50
return 100 * gain / (gain + loss)
n = int(input().strip())
dates = []
opens = []
highs = []
lows = []
closes = []
for _ in range(n):
line = input().strip()
date, open_, high_, low_, close_ = line.split(',')
dates.append(date)
opens.append(float(open_))
highs.append(float(high_))
lows.append(float(low_))
closes.append(float(close_))
initial_capital = 10000.0
portfolio = initial_capital # موجودی نقدی
shares = 0.0 # تعداد سهمهای خریداریشده
buy_price = None # قیمت خرید (برای استاپلاس)
for i in range(n):
current_close = closes[i]
if i >= 19:
sma = sum(closes[i-19:i+1]) / 20
rsi = compute_rsi(closes[:i])
if shares == 0:
if rsi is not None and rsi < 30 and current_close > sma:
shares = portfolio / current_close
buy_price = current_close
portfolio = 0.0
else:
if (rsi > 70 and current_close < sma) or (current_close < 0.95 * buy_price):
portfolio = shares * current_close
shares = 0.0
buy_price = None
if shares > 0:
portfolio = shares * closes[-1]
shares = 0.0
print(f"{portfolio:.2f}")