Reputation: 37
I recently added the SAR to a particular trading strategy. Even more recently I had the idea to plot horizontal lines every time the SAR changes direction because I noticed when it does change direction the very first plot is based on the low or high between the bar that changes direction and the previous bar that changed direction. At first I thought this shouldn't be too difficult for myself to implement, but reading through the SAR code I was only sort of able to understand it and had no idea how and what to reference, especially as the variables and such are not global but are inside the function itself. I thought to see if ChatGPT could help me. After much frustrations this is what I got:
indicator("Parabolic SAR Levels with Horizontal Change Lines", overlay=true)
// Define the input parameters for the Parabolic SAR
start = input.float(0.02, title="Start", step=0.01)
increment = input.float(0.02, title="Increment", step=0.01)
maximum = input.float(0.2, title="Maximum", step=0.01)
// Pine Script implementation of Parabolic SAR
pine_sar(start, inc, max) =>
var float result = na
var float maxMin = na
var float acceleration = na
var bool isBelow = na
bool isFirstTrendBar = false
if bar_index == 1
if close > close[1]
isBelow := true
maxMin := high
result := low[1]
else
isBelow := false
maxMin := low
result := high[1]
isFirstTrendBar := true
acceleration := start
result := result + acceleration * (maxMin - result)
if isBelow
if result > low
isFirstTrendBar := true
isBelow := false
result := math.max(high, maxMin)
maxMin := low
acceleration := start
else
if result < high
isFirstTrendBar := true
isBelow := true
result := math.min(low, maxMin)
maxMin := high
acceleration := start
if not isFirstTrendBar
if isBelow
if high > maxMin
maxMin := high
acceleration := math.min(acceleration + inc, max)
else
if low < maxMin
maxMin := low
acceleration := math.min(acceleration + inc, max)
if isBelow
result := math.min(result, low[1])
if bar_index > 1
result := math.min(result, low[2])
else
result := math.max(result, high[1])
if bar_index > 1
result := math.max(result, high[2])
result
// Calculate the Parabolic SAR
psar = pine_sar(start, increment, maximum)
// Plot the Parabolic SAR
plot(psar, title="Parabolic SAR", color=#000000, linewidth=1, style=plot.style_cross)
// Variables to store line information
var line currentLine = na
var float lastHighLow = na
var bool lastIsBelow = na
// Check for change in SAR direction
isBelow = psar < close
// Update the highest high or lowest low between changes
if na(lastIsBelow) or isBelow != lastIsBelow
if not na(currentLine)
line.set_x2(currentLine, bar_index - 1) // Extend the line to the previous bar
// Create a new horizontal line from the high/low of the identified bar
float highLow = na
if isBelow
highLow := high
else
highLow := low
currentLine := line.new(x1=bar_index, y1=highLow, x2=bar_index, y2=highLow, color=#000000, width=1)
// Update the last high/low price and SAR direction
lastHighLow := highLow
lastIsBelow := isBelow
else
if isBelow
if high > lastHighLow
lastHighLow := high
else
if low < lastHighLow
lastHighLow := low
// Extend the current line to the current bar
line.set_x2(currentLine, bar_index)
line.set_y1(currentLine, lastHighLow)
line.set_y2(currentLine, lastHighLow)
Well, ChatGPT has done reasonably well after much time spent prompting poorly by me, but I/it's run into a wall. It is correctly identifying those swing highs and lows between changes in direction of the SAR, but the line beginnings and ends are wrong; I can't seem to get it to plot the x1 and x2 coordinates properly. As you can see in the image the lines appear to extend backward in time (in reality I think the code is plotting the x1 at the bar that caused the previous SAR change and the x2 at the bar that caused the most recent SAR change). I used these two points of reference for ChatGPT in order to identify the swing prices (y coordinates) between them, but it seems to want to use those two points of reference for the x coordinates as well, and I have no idea why, and I can't seem to get ChatGPT to understand what I want. Now I am here asking for help.
The absolute best outcome would be for the x1 to be the same bar that the y coordinates are based on, and have its terminus be at the next SAR direction change (on chart it would look like a line drawn from a swing price point forward until there is a SAR direction change), but I'd be happy just to have each x coordinate be at a SAR direction change; in other words the line would plot from one bar that caused the SAR change to the next bar that causes a SAR change.
And the one other request/question I have is that it also be possible to use a lowest or highest close for the y coordinates, and then the option for the user to select either closes or highs/lows to use for y coordinates?
update
My preference would be for the line to start at the high price. So, when you look at the very first star or circle plot for the SAR every time it changes trend you'll see that it aligns with the highest high or lowest low from the previous SAR trend. I would really like for the line to start at that high or low. Your end point is correct: the last bar in that SAR trend. All that said, I would be happy with the line starting from where you have it drawn in that pic. Basically in this case the line would be drawn for the total extent that the SAR is plotted
Red lines are what I would like. Vertical lines just show where a new SAR starts and emphasizes my point about how the current lines look as though they're plotted in reverse or backward
Huge huge thanks to anyone who might be able to help me out with any part of this.
Upvotes: 0
Views: 209
Reputation: 1214
AI-generated code is inconsistent, buggy, and unreliable. Artificial Intelligence is very useful in many aspects, but it encodes Pinescript very poorly (for now...).
To Parabolic SAR, you can use ta.sar
psar = ta.sar(start, increment, maximum)
To plot a line on PSAR, use a Reassignment operator
//@version=5
indicator("Parabolic SAR Levels with Horizontal Change Lines", overlay=true)
// Define the input parameters for the Parabolic SAR
start = input.float(0.02, title="Start", step=0.01)
increment = input.float(0.02, title="Increment", step=0.01)
maximum = input.float(0.2, title="Maximum", step=0.01)
// Calculate the Parabolic SAR
psar = ta.sar(start, increment, maximum)
// Plot the Parabolic SAR
plot(psar, title="Parabolic SAR", color=color.blue, linewidth=1, style=plot.style_cross)
// Variables to store line information
var line currentLine = na
var float lastHighLow = na
// Check for change in SAR direction
isBelow = psar < close
// Update the highest high or lowest low between changes
if isBelow != isBelow[1]
if not na(currentLine)
line.set_x2(currentLine, bar_index - 1) // Extend the line to the previous bar
// Create a new horizontal line from the high/low of the identified bar
float highLow = na
highLow := psar
currentLine := line.new(x1=bar_index, y1=highLow, x2=bar_index, y2=highLow, color=color.aqua, width=1)
// Update the last high/low price and SAR direction
lastHighLow := psar
else
lastHighLow := lastHighLow[1]
// Extend the current line to the current bar
line.set_x2(currentLine, bar_index)
line.set_y1(currentLine, lastHighLow)
line.set_y2(currentLine, lastHighLow)
Upvotes: 0