A python signal indicator code running on quantdinger platform #3
brokermr810
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
`my_indicator_name = "SuperTrend"
my_indicator_description = "SuperTrend (TradingView v4 logic). Chart outputs buy/sell only; backend normalizes execution."
df = df.copy()
Parameters (match Pine defaults)
periods = 10
multiplier = 3.0
change_atr = True # True: Wilder ATR (TradingView atr), False: SMA(TR)
Source = hl2
src = (df["high"] + df["low"]) / 2.0
True Range (TradingView tr)
prev_close = df["close"].shift(1)
tr0 = df["high"] - df["low"]
tr1 = (df["high"] - prev_close).abs()
tr2 = (df["low"] - prev_close).abs()
tr = pd.concat([tr0, tr1, tr2], axis=1).max(axis=1)
n = len(df)
close_arr = df["close"].values
--- ATR: TradingView-style Wilder ATR (seed with SMA, then recursive) ---
atr_sma = tr.rolling(periods).mean()
if change_atr:
atr = pd.Series([np.nan] * n, index=df.index, dtype="float64")
if n >= periods:
atr.iloc[periods - 1] = float(tr.iloc[:periods].mean())
for i in range(periods, n):
atr.iloc[i] = (atr.iloc[i - 1] * (periods - 1) + float(tr.iloc[i])) / periods
else:
atr = atr_sma
--- SuperTrend bands with Pine := recursion ---
up_final = [np.nan] * n
dn_final = [np.nan] * n
for i in range(n):
a = atr.iloc[i]
if pd.isna(a):
continue
--- Trend recursion (Pine trend := ...) ---
trend = [1] * n
for i in range(1, n):
if pd.isna(up_final[i - 1]) or pd.isna(dn_final[i - 1]):
trend[i] = trend[i - 1]
continue
trend_s = pd.Series(trend, index=df.index)
Plots (linebreak effect using None)
up_plot = [float(up_final[i]) if (trend[i] == 1 and not pd.isna(up_final[i])) else None for i in range(n)]
dn_plot = [float(dn_final[i]) if (trend[i] == -1 and not pd.isna(dn_final[i])) else None for i in range(n)]
Signals (exact Pine conditions)
buy_signal = (trend_s == 1) & (trend_s.shift(1) == -1)
sell_signal = (trend_s == -1) & (trend_s.shift(1) == 1)
buy_signal = buy_signal.fillna(False).astype(bool)
sell_signal = sell_signal.fillna(False).astype(bool)
Required for backend normalization (execution)
df["buy"] = buy_signal
df["sell"] = sell_signal
Chart marks: Pine uses plotshape(buySignal ? up : na) and plotshape(sellSignal ? dn : na)
buy_marks = [float(up_final[i]) if (bool(buy_signal.iloc[i]) and not pd.isna(up_final[i])) else None for i in range(n)]
sell_marks = [float(dn_final[i]) if (bool(sell_signal.iloc[i]) and not pd.isna(dn_final[i])) else None for i in range(n)]
output = {
"name": my_indicator_name,
"plots": [
{"name": "Up Trend", "type": "line", "data": up_plot, "color": "#00C853", "overlay": True},
{"name": "Down Trend", "type": "line", "data": dn_plot, "color": "#D50000", "overlay": True},
],
"signals": [
{"type": "buy", "text": "Buy", "data": buy_marks, "color": "#00E676"},
{"type": "sell", "text": "Sell", "data": sell_marks, "color": "#FF5252"},
],
}`
Beta Was this translation helpful? Give feedback.
All reactions