3

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)
Olyboy
  • 49
  • 4

2 Answers2

1

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.

user1874594
  • 2,277
  • 1
  • 25
  • 49
1

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.

wouterio
  • 137
  • 1
  • 10