[英]How to rewrite my Pinescript code to Python
I am trying to rewrite this code to Python:我正在尝试将此代码重写为 Python:
src = input.source(close, "Source")
volStop(src) =>
var max = src
var min = src
max := math.max(max, src)
min := math.min(min, src)
[max, min]
[max, min] = volStop(src)
plot(max, "Max", style=plot.style_cross)
Precisely I have a problem with these lines:准确地说,我对这些行有疑问:
max := math.max(max, src)
min := math.min(min, src)
In Python I have a function, leta call it func1
, and I want to get the same result the Pinescript is returning.在 Python 中,我有一个 function,我们称它为
func1
,我想获得 Pinescript 返回的相同结果。
I have only tried for loop since from what I understand calling a function in Pinescript works kind of like for loop.我只尝试过 for 循环,因为据我所知,在 Pinescript 中调用 function 有点像 for 循环。 And I tried to replicate the calculation but I couldn't achieve the expected results.
并且我试图复制计算但我无法达到预期的结果。
This is the line that is being plotted on Tradingview:这是在 Tradingview 上绘制的线:
And this is the line that is being plotted in Python: the area that is framed with red square is the approximate area that is visible on Tradingview screenshot.这是在 Python 中绘制的线:用红色方框框出的区域是 Tradingview 屏幕截图上可见的大致区域。
My current code:我当前的代码:
maxmin = pd.DataFrame()
maxmin["max"] = price_df[f"{name_var}"]
maxmin["min"] = price_df[f"{name_var}"]
for i in range(price_df.shape[0]):
maxmin["max"].iloc[i] = max(maxmin["max"].shift(1).fillna(0).iloc[i], price_df[f"{name_var}"].iloc[i])
maxmin["min"].iloc[i] = min(maxmin["min"].shift(1).fillna(0).iloc[i], price_df[f"{name_var}"].iloc[i])
The name_var
variable is set to 'Close' column. name_var
变量设置为“关闭”列。
How can I rewrite the Pinescript code to Python to get the same results?如何将 Pinescript 代码重写为 Python 以获得相同的结果?
Pine Script basically runs your code on every bar and store variables into a history (series). Pine Script 基本上在每个柱上运行您的代码并将变量存储到历史记录(系列)中。 This is why you can access old data.
这就是您可以访问旧数据的原因。
I think the problem here is you should give the same input data to get the same output. The calculation starts from the 1st available bar, where bar_index
is 0. You can scroll to the left to see at which time the 1st bar is.我认为这里的问题是您应该提供相同的输入数据以获得相同的 output。计算从第一个可用柱开始,其中
bar_index
为 0。您可以向左滚动以查看第一个柱的时间。 Then your input data in python should be the same.那么你在python中输入的数据应该是一样的。 Or you can restrict your Pine Script to start calculation like this:
或者你可以限制你的 Pine Script 像这样开始计算:
start_time = input.time(timestamp("1 Jan 2023 00:00 +0000"), "Date")
// ...
volStop(src) =>
var max = src
var min = src
if time >= start_time
max := math.max(max, src)
min := math.min(min, src)
[max, min]
else
[na, na]
// ...
The python code doesn't need the shift
and fillna
. python 代码不需要
shift
和fillna
。 The input data should not have na at all, because you need the same data as on TradingView, which has no na.输入数据根本不应该有 na,因为您需要与没有 na 的 TradingView 上相同的数据。 So the for cycle and the builtin max/min do the job:
所以 for 循环和内置的 max/min 完成了这项工作:
for i in range(price_df.shape[0]):
maxmin["max"].iloc[i] = max(maxmin["max"].iloc[i], price_df[f"{name_var}"].iloc[i])
maxmin["min"].iloc[i] = min(maxmin["min"].iloc[i], price_df[f"{name_var}"].iloc[i])
But it is slow because of the iteration and value access one by one.但是因为要逐个迭代和取值,速度很慢。 You can use pandas methods here:
您可以在这里使用 pandas 方法:
maxmin['max'] = price_df[name_var].cummax()
maxmin['min'] = price_df[name_var].cummin()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.