Konvertieren von Kiefernskript in Python
Posted: 24 Jul 2025, 13:14
Ich konvertiere das folgende Kiefernskript in Python.
Der Python -Code funktioniert gut, aber der letzte Teil des Code
Hier ist das Problem ; Die Ausgabe des folgenden Python -Codes entspricht nicht der Ausgabe des folgenden Pine -Skripts. Schauen Sie sich das Python -Plot- und TradingView -Diagramm an, die Richtung in Python entspricht nicht dem Dir .
Hier ist das Diagramm des Python -Code. Die grüne Linie ist LongStop Variable, rote Zeile ist Shortstop , grün x IS DIR == 1 zeigt uptrend und rot x zeigt Downtrend Dir == -1 . Die blaue Linie ist der Preis schließen Preis.>
Der Python -Code funktioniert gut, aber der letzte Teil des Code
Code: Select all
//@version=4
study("STrend", overlay=true)
length = input(title="ATR Period", type=input.integer, defval=22)
mult = input(title="ATR Multiplier", type=input.float, step=0.1, defval=3)
src = input(title="Source", type=input.source, defval=hl2)
wicks = input(title="Take Wicks into Account ?", type=input.bool, defval=true)
showLabels = input(title="Show Buy/Sell Labels ?", type=input.bool, defval=true)
highlightState = input(title="Highlight State ?", type=input.bool, defval=true)
atr = mult * atr(length)
highPrice = wicks ? high : close
lowPrice = wicks ? low : close
doji4price = open == close and open == low and open == high
longStop = src - atr
longStopPrev = nz(longStop[1], longStop)
if longStop > 0
if doji4price
longStop := longStopPrev
else
longStop := (lowPrice[1] > longStopPrev ? max(longStop, longStopPrev) : longStop)
else
longStop := longStopPrev
shortStop = src + atr
shortStopPrev = nz(shortStop[1], shortStop)
if shortStop > 0
if doji4price
shortStop := shortStopPrev
else
shortStop := (highPrice[1] < shortStopPrev ? min(shortStop, shortStopPrev) : shortStop)
else
shortStop := shortStopPrev
var int dir = 1
dir :=
dir == -1 and highPrice > shortStopPrev ? 1 :
dir == 1 and lowPrice < longStopPrev ? -1 :
dir
plot(longStop, color=color.green)
plot(shortStop, color=color.red)
plotshape(dir == 1 ? dir: na, color=color.green)
plotshape(dir == -1 ? dir: na, color=color.red)
< /code>
Hier ist der Python-Code, der aus dem obigen Kiefer-Skript konvertiert wird.#!/usr/bin/env python3
import matplotlib.pyplot as plt
import pandas as pd
import pandas_ta as ta
import yfinance as yf
plt.style.use("fivethirtyeight")
df = yf.download("TSLA", start="2023-01-01")
print(df.head())
length = 22
mult = 3
df["hl2"] = (df["High"] + df["Low"]) / 2
src = df["hl2"]
atr = mult * ta.atr(df["High"], df["Low"], df["Close"], length=length)
doji4price = (
(df["Open"] == df["Close"]) & (df["Open"] == df["Low"]) & (df["Open"] == df["High"])
)
long_stop = src - atr
long_stop_prev = long_stop.shift(1).fillna(long_stop)
for i in range(1, len(long_stop)):
if long_stop[i] > 0:
if doji4price[i]:
long_stop[i] = long_stop_prev[i]
else:
long_stop[i] = (
max(long_stop[i], long_stop_prev[i])
if df["Low"][i - 1] > long_stop_prev[i]
else long_stop[i]
)
else:
long_stop[i] = long_stop_prev[i]
short_stop = src + atr
short_stop_prev = short_stop.shift(1).fillna(short_stop)
for i in range(1, len(short_stop)):
if short_stop[i] > 0:
if doji4price[i]:
short_stop[i] = short_stop_prev[i]
else:
short_stop[i] = (
min(short_stop[i], short_stop_prev[i])
if df["High"][i - 1] < short_stop_prev[i]
else short_stop[i]
)
else:
short_stop[i] = short_stop_prev[i]
d = 1
direction = []
for i in range(len(df)):
if d == -1 and df["High"][i] > short_stop_prev[i]:
d = 1
direction.append(1)
elif d == 1 and df["Low"][i] < long_stop_prev[i]:
d = -1
direction.append(-1)
else:
d = d
direction.append(d)
direction = pd.Series(direction)
direction.index = df.index
plt.plot(df["Close"], color="cyan")
plt.plot(long_stop, color="green")
plt.plot(short_stop, color="red")
plt.plot(df["Close"][direction == 1], "^", color="darkgreen")
plt.plot(df["Close"][direction == -1], "v", color="darkred")
plt.legend(["close", "long stop", "short stop", "^", "v"])
plt.show()
Code: Select all
dir :=
dir == -1 and highPrice > shortStopPrev ? 1 :
dir == 1 and lowPrice < longStopPrev ? -1 :
dir
< /code>
Der Python-Code im folgenden wurde aus dem obigen Pine-Skript geschrieben, aber er tut nicht dasselbe.
Was ist mit diesem Teil des Codes los?d = 1
direction = []
for i in range(len(df)):
if d == -1 and df["High"][i] > short_stop_prev[i]:
d = 1
direction.append(1)
elif d == 1 and df["Low"][i] < long_stop_prev[i]:
d = -1
direction.append(-1)
else:
d = d
direction.append(d)