我如何追踪和绘制做空仓位进场以来的最低价格?

huangapple go评论58阅读模式
英文:

How can I track and plot the lowest price since entry of a short position?

问题

我正在尝试跟踪我的空头持仓自入场以来的低价,就像我对多头持仓的高价所做的那样。由于某种原因,相反的代码在空头持仓中不起作用。多头/高位价格正常绘制。通过对我的代码进行不同变化,我要么得到一个平的 '0' 绘图,要么得到一个从低点回升的低点。基本上,我想设置一个回撤水平,以便在未能完全达到 TP% 的交易中止前回撤时退出。

这是生成绘图的基本代码。多头正常工作,但这个空头版本和其他变化都没有起作用。它们要么随着价格上涨而回撤,而不是显示最低点,要么绘制 '0'。多头和空头入场价格的绘图正常,所以其他代码正确初始化了起始点。

var float high_price_since_entry = na
high_price_since_entry := math.max(long_entry_price, nz(high_price_since_entry[1]), high)
var float low_price_since_entry = na
low_price_since_entry := math.min(short_entry_price, nz(low_price_since_entry[1]), low)
英文:

I'm trying to track the low price since entry for my short positions as I have done with the high price for long positions. For some reason the reverse code does not work with shorts. The long/high plots fine. with different variations to my code I get a flat '0' plot or I get a low that retraces up from the low point. I basically want to set a trail retracement level where I could stop out of deals that don't quite reach TP% before retracing.

This is the basic code to generate the plot. Long works fine however this short version and other variations have not worked. They either retrace with the price increasing rather than showing the lowest point or they plot '0'. The plot for long and short_entry price works fine, so the other code is initialising the start point correctly.

var float high_price_since_entry = na
high_price_since_entry := math.max(long_entry_price, nz(high_price_since_entry[1]), high)
var float low_price_since_entry = na
low_price_since_entry := math.min(short_entry_price, nz(low_price_since_entry[1]), low)

答案1

得分: 1

low_price_since_entry变量被初始化为na(不是一个值),只有在进入新的空头头寸时才会更新。这意味着如果没有进入空头头寸,low_price_since_entry的值将保持为na。此外,如果进入了空头头寸,low_price_since_entry的值将在每个价格条上设置为short_entry_pricelow_price_since_entry[1]low的最小值,但如果价格上涨,它不会被更新。

尝试修改你的代码,只有在进入新的空头头寸并且当前的最低价格低于low_price_since_entry的先前值时,才更新low_price_since_entry的值。

var float low_price_since_entry = na
if (in_short_position)
    low_price_since_entry := min(nz(low_price_since_entry[1]), low)
else
    low_price_since_entry := na

只有在进入新的空头头寸(in_short_position为true)并且当前的最低价格低于low_price_since_entry的先前值时,才会更新low_price_since_entry的值。当没有活跃的空头头寸时,low_price_since_entry的值被设置为na

英文:

the low_price_since_entry variable is being initialized as na (not a value) and is only updated when a new short position is entered. This means that if no short position has been entered, the value of low_price_since_entry will remain na. Plus , if a short position is entered, the value of low_price_since_entry will be set to the minimum value of short_entry_price, low_price_since_entry[1], and low on each bar, but it will not be updated if the price moves higher.

Try modifying your code to only update the value of low_price_since_entry when a new short position is entered and when the current low price is lower than the previous value of low_price_since_entry.

var float low_price_since_entry = na
if (in_short_position)
    low_price_since_entry := min(nz(low_price_since_entry[1]), low)
else
    low_price_since_entry := na

value of low_price_since_entry is only updated when a new short position is entered (in_short_position is true) and when the current low price is lower than the previous value of low_price_since_entry. When a short position is not active, the value of low_price_since_entry is set to na.

答案2

得分: 1

在你的代码中,low_price_since_entry 的初始值为 "na",因此 nz(low_price_since_entry[1]) 将返回 0。

由于 math.min 返回最低值,math.min(short_entry_price, nz(low_price_since_entry[1]), low) 也将返回 0。
(假设 short_entry_pricelow 大于或等于 0。)

这意味着 low_price_since_entry := math.min(...) 将0添加到 low_price_since_entry 中。从那时起,你的函数将继续添加零。

尝试使用 nz(low_price_since_entry[1], 1e15) 代替(或其他类似正无穷大的巨大数值)。它将 "na" 替换为不会干扰你的 math.min(...) 的值。

low_price_since_entry := math.min(short_entry_price, nz(low_price_since_entry[1], 1e15), low)

我没有测试过这段代码,所以你需要自行检查。

英文:

In your code, the initial value of low_price_since_entry is na, so nz(low_price_since_entry[1]) will return 0.

As math.min returns the lowest value, math.min(short_entry_price, nz(low_price_since_entry[1]), low) will return 0 too.
(Assuming short_entry_price and low are greater than or equal to 0.)

That means low_price_since_entry := math.min(...) adds 0 to low_price_since_entry. And from then on your function will keep adding zeros.

Try using nz(low_price_since_entry[1], 1e15) instead (or some other huge number resembling positive infinity). It replaces na not with 0 but with a value that won't interfere with your math.min(...).

low_price_since_entry := math.min(short_entry_price, nz(low_price_since_entry[1], 1e15), low)

I didn't test this code, so you'll have to check it yourself.

huangapple
  • 本文由 发表于 2023年7月12日 20:59:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76670857.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定