# Lucid SAR

8226浏览
8226
I wrote this script after having listened to Hyperwave with Sawcruhteez and Tyler Jenks of Lucid Investments Strategies LLC on July 3, 2019. They felt that the existing built-in Parabolic SAR indicator was not doing its calculations properly, and they hoped that someone might help them correct this. So I tried my hand at it, learning Pine Script as I went. I worked on it through the early morning hours and finished it by 4 am on July 4, 2019. I've added a few bits of code since, adding the rule regarding the SAR not advancing beyond the high (low) of the prior two candles during an uptrend (downtrend), but the core script is as it was.

This code is open source under the MIT license. If you have any improvements or corrections to suggest, please send me a pull request via the github repository https://github.com/casey-bowman/sar

For more details on the initial script, see   Parabolic SAR , where the quotes are from Section II of J. Welles Wilder, Jr.'s book New Concepts in Technical Trading Systems (1978)

--------------------------------------------------------------------------------------------------------------------------

Parabolic SAR

"The Parabolic Time / Price System derives its name from the fact that when charted, the
pattern formed by the stops resembles a parabola, or if you will, a French Curve. The system
allows room for the market to react for the first few days after a trade is initiated and then the
stop begins to move up more rapidly. The stop is not only a function of price but also a function
of time
.

"The stop never backs up. It moves an incremental amount each day, only in the direction which

"The stop is also a function of price because the distance the stop moves up is relative to the
favorable distance the price has moved... specifically, the most favorable price reached since the

A. The calculation for a bullish Parabolic SAR is:

Tomorrow’s SAR = Today’s SAR + AF (EP - Today’s SAR )

"Acceleration Factor ( AF ) is one of a progression of numbers beginning at 0.02 and ending at
0.20. The AF is increased by 0.02 each period that a new high is made" (if long) or new low is

EP is the "Extreme Price Point for the trade made so far. If Long, EP is the extreme high price for
the trade; if Short, EP is the extreme low price for the trade.”

Most websites will provide the above calculation for the Parabolic SAR but almost all of them
leave out this crucial detail:

B. "Never move the SAR into the previous day’s range or today’s range

"1. If Long, never move the SAR for tomorrow above the previous day’s low or
today’s low
. If the SAR is calculated to be above the previous day’s low or
today’s low, then use the lower low between today and the previous day as
the new SAR . Make the next days calculations based upon this SAR .

"2. If Short, never move the SAR for tomorrow below the previous day’s high or
today’s high
. If the SAR is calculated to be below the previous days’ high or
today’s high, then use the higher high between today and the previous day
as the new SAR . Make the next days calculations based upon this SAR ."

When a Bullish SAR is broken then it gets placed at the SIP (significant point) of the prior trend.
In otherwords it is placed above the current candle and at the price that was the SIP.

The inverse is true for the first Bullish SAR .

"This system is a true reversal system; that is, every stop point is also a reverse point." If breaking
through a bearish SAR (one above price) that simultaneously signals to close a short and go
long.
Oct 12

Added ability to handle multiple reversals in the current and subsequent candles

Also used new import tool, written in Rust, to generate the script from parts
Can you help me to understand part of the script?

You have this:

reversal_state = 0
if reversal_state == 0
//Do Stuff A
else if reversal_state == 1
// Do Stuff B
else
// Do Stuff C

I'm having trouble understanding how B or C is ever executed. I'm new to Pine, so I'm probably missing some idiosyncrasy of the language. Can you help me to understand?

Thanks!

For anyone Interested, I recently figured out how to have Automatic Alerts work with the Lucid SAR. Once you pick an asset, and the time frame you are Trading, ADD Lucid SAR Alert by picking Asset Name on Condition, then Keep Crossing ( o get Alerts whenever the SAR Flips either Direction), then in Value, Select Lucid SAR, then Choose how Frequent You want the Alerts to Notify you of a Change.

So professional, Bravo

mjk2019

Thank you.

Hi Casey, would it be possible for you (or anyone for that matter) to create a SAR Forecast Indicator, which forecasts the next 3 possible SAR points? Like the "Triple MA Forecast" indicator, but for the Lucid SAR. It would be ok if the SAR forecasts included multiple possible outcomes/points, based on both current trend or reversal. I'm not sure if the math would allow it, but it wold be an awesome addition to your indicator if possible.

SkyGolem
@SkyGolem, Actually, yes, I've done that with a private script I've used. This is in part how I guessed that there'd be a reversal in the weekly SAR at the end of July with a tweet I made on July 8, and the reversal happened on July 16 - /casey_bowman/status/1151180563945906176#

It's a bit tenuous for multiple candles out. You really need a guess as to how the prices will be moving, which is what I needed to furnish for figuring out my tweeted guess. Then I had to customize my code roughly, specifically for the price guesses I had in mind. It's complicated.

A forecast for just the next candle would be meaningful. It's on my list.

Today I'm working on some new features that Sawcruhteez has recommended (see my Sep 12 comment). If I can make the day-out forecast optional, I plan to try to include it, but the features recommended by Sawcruhteez are first priority for my work this time around. I also hope to add the color feature, too, and reversal alerts (see other comments below), though again they're not top priority this time. They'll be next on my list. I have two Rust reading groups to prepare for on Sunday and Monday evenings that I'll be switching my attention to, but maybe I'll be able to return to this work next week to handle the lower-priority features.

casey_bowman
@casey_bowman, Yes, complicated indeed! I'm so glad to hear the forecast for the next candle is on your list as it will be very interesting and useful. It's great to read your thoughts and ideas in all your posts, so thank you for sharing them. Of course, I look forward to any new features you might provide. Nothing expected of course, only gratitude for the content you have already selflessly provided.

It is always humbling and a reminder that there are still so many good people in this world, when people such as yourself freely share the fruits of your labour with others.

I hope your programming reading groups go well and look forward to seeing what you create in the future.

A half-hour ago Sawcruhteez gave me a heads-up on a nice, juicy bug he found when scalping at the 3 minute for bitcoin. He gave me enough information for me to see it myself and verify it. This is the very first verified bug anyone's found. I believe this bug occurs only when there is more than one reverse on a single candle. I myself had the weekly SAR in mind when creating this indicator and not scalping. So I did assume that for any one candle there would only be one reverse possible. Please don't use this indicator for very small time frames where there can be at times multiple reverses in one candle, until there is a fix. For larger time frames where reverses generally are on separate candles, this bug should have no effect.

casey_bowman
I have proposed that the new rule for intracandle multiple reverses be this -

There are only two SAR reverses ever created for one candle and they can toggle.

When one returns to a former SAR reverse, that SAR could be lower (higher) if there was a candle low (high) below (above) the SAR before that reversal if one is reversing from an uptrend (downtrend).

So in any one candle, the two SAR reverses would only spread farther apart with new highs and lows before a reversal. In other words, the low SAR might only move lower upon a reversal from the high SAR when there’s been an intervening new low for the candle, and the high SAR might only move higher upon a reversal from the low SAR when there’s been an intervening new high for the candle.

I have asked Sawcruhteez to give me feedback on this proposed intracandle rule.