-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathchart_indicator_dual_ema.py
More file actions
71 lines (59 loc) · 2 KB
/
Copy pathchart_indicator_dual_ema.py
File metadata and controls
71 lines (59 loc) · 2 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
65
66
67
68
69
70
71
# QuantDinger chart-only indicator example.
# Paste into Indicator IDE. This file does not backtest or trade.
# @param fast_len int 12 Fast EMA period
# @param slow_len int 26 Slow EMA period
# @param confirm_next_bar bool false Show markers one bar after confirmation
my_indicator_name = "Dual EMA Viewer"
my_indicator_description = "Chart-only EMA crossover indicator with visual event markers."
df = df.copy()
fast_len = int(params.get("fast_len", 12))
slow_len = int(params.get("slow_len", 26))
confirm_next_bar = bool(params.get("confirm_next_bar", False))
close = df["close"]
high = df["high"]
low = df["low"]
def edge(condition):
s = condition.fillna(False).astype(bool)
previous = s.shift(1, fill_value=False).astype(bool)
return s & ~previous
def to_plot_list(series):
return [None if pd.isna(v) else float(v) for v in series]
ema_fast = close.ewm(span=fast_len, adjust=False).mean()
ema_slow = close.ewm(span=slow_len, adjust=False).mean()
golden = edge(ema_fast > ema_slow)
death = edge(ema_fast < ema_slow)
if confirm_next_bar:
golden = golden.shift(1, fill_value=False).astype(bool)
death = death.shift(1, fill_value=False).astype(bool)
buy_marks = [
float(low.iloc[i] * 0.995) if bool(golden.iloc[i]) else None
for i in range(len(df))
]
sell_marks = [
float(high.iloc[i] * 1.005) if bool(death.iloc[i]) else None
for i in range(len(df))
]
output = {
"name": my_indicator_name,
"plots": [
{
"name": "EMA Fast",
"data": to_plot_list(ema_fast),
"color": "#22c55e",
"type": "line",
"overlay": True,
},
{
"name": "EMA Slow",
"data": to_plot_list(ema_slow),
"color": "#3b82f6",
"type": "line",
"overlay": True,
},
],
"signals": [
{"type": "buy", "text": "Golden", "color": "#22c55e", "data": buy_marks},
{"type": "sell", "text": "Death", "color": "#ef4444", "data": sell_marks},
],
"layers": [],
}