Lucid SAR

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

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 trade has been initiated."

"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
trade was initiated."

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
made (if short).

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
Oct 12
릴리즈 노트: Issue #4

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
즐겨찾기 스크립트에서 빼기 즐겨찾기 스크립트에 넣기
Using the Peterbolic SAR indicator, the weekly has also triggered a reversal in trend for bitcoin on Bitstamp.
+1 응답
casey_bowman casey_bowman
Remember this is not a guarantee that a trend has reversed. The Lucid SAR and the Peterbolic SAR are merely two indicators to be used with other indicators and your own judgment
+1 응답
The weekly has been triggered for bitcoin on Bitstamp
+1 응답
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
// 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?

@MoniBearz, This code handles issue #4, discussed in my comments below in the thread starting Sep 9, 2019. Please see there for details. This fix was made on Oct 12, 2019. It implements the rule I proposed in the comment thread, handling intra-candle reversals, that is to say, reversals that occur within the same candle. There are three reversal states in a candle. State 0 is the state before any reversal happens in a given candle. States 1 and 2 are the states after a reversal happens in a given candle, 1 for a reversal to an uptrend and 2 for a reversal to a downtrend.
MoniBearz casey_bowman
@casey_bowman, Thanks. I get the business side of the logic, but I'm not understanding the code. As "reversal_state" is not declared with "var", I don't see how legs B or C could ever be executed. It seems to me that reversal_state would always be "0" at that point in the code.

Additionally, if those legs of code were capable of being executed, it seems there would be a separate problem because of the "==" used on lines 117 and 124 (instead of ":=").
@MoniBearz, The key point here is that the reversal_state is a series with a separate value for each candle, and each of these separate values can change. It is not a value that persists across successive candles, which is what a var declaration gives you. The current candle is updated multiple times before its time period has completed. It is during one or more of these updates when a candle's value might change.
+1 응답
MoniBearz casey_bowman
@casey_bowman, Are you saying that reversal_state is only initialized to 0 1x per candle, and that if it changes in leg A, it is not re-initialized until the next candle?

Are the "==" at lines 117 and 124 not an issue? I thought that those were just boolean expressions.
@MoniBearz, I think you're correct at what you're getting at on these last two questions. I had assumed that I could communicate information between updates on the same realtime candle. In order to answer your question accurately, I read the Pine Script documentation on the Execution Model

Lo and behold, user-defined variables are rolled back at each update on a realtime candle to the their state at the opening of that candle. Therefore, there is no way to convey information from one update to the next on the same realtime candle. Thus there is no way to implement the idea I had to address multiple intra-candle reversals.

Happily the code runs the same path as before, as in the initial version from July, if there are such rollbacks for each realtime candle's updates and the code stays in "leg A".

The only other change that's been made to the Lucid SAR since July is to encapsulate the code into a function, which for the Lucid SAR was based on a change sent to me by davzuchy via Telegram

So the code should behave the same as before. When I release a new version, I'll remove this attempt to handle repeated intra-candle reversals. There seems to be no way to do this given PineScript's current capabilities. I also would like to simplify the function call now that I look at it again.

Thank you for asking questions!!
홈으로 스탁 스크리너 포렉스 스크리너 크립토 스크리너 이코노믹 캘린더 사용안내 차트 특징 프라이싱 프렌드 리퍼하기 하우스룰(내부규정) 헬프 센터 웹사이트 & 브로커 솔루션 위젯 차팅 솔루션 라이트웨이트 차팅 라이브러리 블로그 & 뉴스 트위터
프로화일 프로화일설정 계정 및 빌링 프렌드 리퍼하기 나의 서포트 티켓 헬프 센터 공개아이디어 팔로어 팔로잉 비밀메시지 채팅 로그아웃