# Price Action Deviance from Expectation For ThinkOrSwim

#### mashume

##### Expert
VIP
Introduction and Theory
Many good indicators exist to give insight into oversold and overbought market conditions. They analyze many features, all differently. We have a veritable cornucopia of indicators that help us make decisions about when to open a position and when to close a position.

So why create another one? Because there's always room for one more.

We naturally see trends when we look at charts. Prices move higher and prices move lower. Consecutive upward bars form patterns, certain patterns are ingrained in our methods for looking at charts. What we're not very good at (speaking for myself, at least) is recognizing when a price movement is in line with or out of line with reasonable expectation for price movement. I will make a comparison with the MACD indicator later in this write-up -- See Eye Candy and Analysis below.

Technical Details and Calculations
First, the calculation of the Psychological Line Indicator is made:

Code:
``PLI = (count of up candles in last n bars / n ) x 100``

This line is plotted, and the usual observations can be made from it e.g. when the line is above 50 there is a general positivity about the price action, and when it ranges above 75 there is a good chance for a reversal. Similarly, values below 50 indicate a lack of conviction in upward motion about the stock and values below 25 indicate overly pessimistic and may presage a reversal.

Next, the price movements between the previous bar close and the current bar close is summed for all bars where the close is higher than the previous close for the last n bars.

Code:
``````Up Candle Movement = if close > pervious close
then close - pervious close
else 0``````

This number divided by the absolute value of the change in price between close and previous close over the past n bars. The result is multiplied by 100.

Code:
``expected = (sum(Up Candle Movement, n) / sum(absValue(close - close[1]), n)) * 100``

The distance between the expected movement line and the psychological line is then calculated and plotted as a histogram.

Code:
``deviance = expected - PLI``

How to Use this Indicator
While I really designed this indicator to be used as a confirmation / secondary signal indicator, it can be used as a stand alone with a few caveats. It is not a holy grail. It will not automatically make you a richer, smarter, better looking investor.

Now that that's out of the way, there are a few key ideas to use in trading using this tool:

1. You can trade the histogram when it crosses above and below the zero line. This is when the Expected Price Action Line has moved above the PLI line. It is a reasonable indicator used this way.

2. You can trade the Expected Price Action Line line crossing above and below the 50 line. This is an indication that the upward price action is outpacing the average price action.

3. You can modify either of the above options by looking at the PLI line and only take trades when the PLI line is below 25 (looking for an upward reversal) or exit trades when it is over 75 (again, looking for reversals -- downward this time).

Eye Candy and Commentary

Figure 1. /ES Futures @195 minutes:

a. This box shows a quick rise from 3725 to 3750, The indicator histogram moves above zero, but then turns yellow a few bars later indicating the price has moved quite quickly upward, and the histogram turning yellow a few bars later is an indication of 'frothiness' in the price. Like a spring that has been stretched, it pulls back.

b. The 'spring' effect (this time compressing a spring which then tries to return to it's original length) is demonstrated here pulling the price back up after a sudden drop.

c. The froth this time is slowly accommodated rather than collapsing as it did in (a.). This is followed by section (d.) on the chart.

d. In this section, both the Psy Line and the price deviation line are hovering just the 50 mid-point, and though the price deviation is above zero, there is really no movement until the end of box (d.) when both the Price Deviation and Psy lines drop below the 50 mid-line. A tumble in the price follows.

e. The gains on the far right of the chart are a coordination of the Psy line being above 50 and Price Deviation line crossing above it. It looks at the end of the chart as though they are both falling, in absolute terms, as well as relative terms (decrease in the histogram as well as both lines).

Figure 2. /ES Futures @195 minutes, again:

a. Rapid rise, Histogram turns yellow indicating some spring tension that should try to return to mid, along with a very high Price Deviation line (> 75). This is followed by a decline, and the histogram goes negative in section b.

b. The prolonged period with the Psy Line below mid, the Price Deviation line below 25, and the histogram well below 0 is the long downtrend. In this case, the MACD also mirrors this move.

Figure 3. /ES Futures 1 Day, last time:

a. Single box this time. The biggest difference here is that, if following the Price Deviation line being above 50 (another strategy for this indicator), the position would have been kept open (and profitable) for far longer than trading on the MACD strategy.

Results
I have been testing various combinations of trade strategies with this indicator that use various combinations of PLI, Expected Price Action Line and have seen reasonable success in ThinkorSwim's built in back testing, but further honing and refinement of the parameters is needed before I'll be comfortable endorsing any particular set of values. Please do experiment with them yourself and let us know if anything works well, or indeed fails miserably.

Indicator Code
Below is code for the initial public release of the indicator to the UseThinkScript community.

Code:
``````#####################################################
#       Price Action Deviance from Expectation
#              http://tos.mx/lM9h4nV
#             2021-01-25.V1     @mashume
#        Released under GPL V3 as applicable
#         to the UseThinkScript Community
#####################################################

declare lower;

input n = 15;
input smoothing = 10;

def isUpCandle = if close > open then 1 else 0;

plot PLI = (sum(isUpCandle, n) / n) * 100;
PLI.SetDefaultColor(getColor(3));
PLI.SetLineWeight(2);

def Dist = if close > close[1] then close - close[1] else 0;
plot expected = (sum(dist, n) / sum(absValue(close - close[1]), n)) * 100;

plot deviance = expected - PLI;
deviance.setPaintingStrategy(PAintingStrategy.HISTOGRAM);
deviance.AssignValueColor(if absValue(deviance) > 25 then color.yellow else if deviance >= 0 then getColor(9) else getColor(5));

plot mid = 50;
mid.setDefaultColor(color.gray);

plot frothy = 25;
frothy.setDefaultColor(color.gray);
frothy.SetStyle(CUrve.SHORT_DASH);

plot warning = 75;
warning.SetDefaultColor(color.gray);
warning.SetStyle(CURVE.SHORT_DASH);

plot rebound = -25;
rebound.SetDefaultColor(color.gray);
rebound.SetStyle(CURVE.SHORT_DASH);

# addCloud(mid, PSY, color.light_red, color.light_green);``````

Here is a link to the indicator: http://tos.mx/lM9h4nV

As always, Happy Trading,
- mashume

Last edited by a moderator:
@mashume Another great explanation of your work! Will give this one a spin too. I love your chart settings. Looks pleasing to my eyes. Is it possible for you to share your workspace or a grid? Thank You!

@mashume Another great explanation of your work! Will give this one a spin too. I love your chart settings. Looks pleasing to my eyes. Is it possible for you to share your workspace or a grid? Thank You!

My usual setup has a few proprietary indicators in it... I'll clean them out and post a clean version later today or tomorrow hopefully.

-mashume

This is very helpful.

@mashume Can you please your work space? I like the black and white.

For anyone interested, here's the link to my (mostly -- see above) current grid.
http://tos.mx/EqgE04q

There's a lot in there, and things come and go from time to time.

To get black and white candles, turn off the BeepBoop. That's whats coloring bars.

For anyone interested, here's the link to my (mostly -- see above) current grid.
http://tos.mx/EqgE04q

There's a lot in there, and things come and go from time to time.

To get black and white candles, turn off the BeepBoop. That's whats coloring bars.

Thanks for share, but link seems not working anymore, can you check?

Try importing manually by using "Import Shared Item" option. That worked for me last week!

### Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
424 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• Add-ons and resources
• 1 full year of unlimited support

### Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?