TradingView
kocurekc
Jul 26, 2020 4:44 AM

Bernoulli Process - Binary Entropy Function 

SPDR S&P 500 ETF TRUSTArca

Description

This indicator is the Bernoulli Process or Wikipedia - Binary Entropy Function. Within Information Theory, Entropy is the measure of available information, here we use a binary variable 0 or 1 (P) and (1-P) (Bernoulli Function/Distribution), and combined with the Shannon Entropy measurement. As you can see below, it produces some wonderful charts and signals, using price, volume, or both summed together. The chart below shows you a couple of options and some critical details on the indicator. The best part about this is the simplicity, all of this information in a couple of lines of code.

Using the indicator:
The longer the Entropy measurement the more information you are capturing, so the analogy is, the shorter the signal, the less information you have available to utilize. You'll run into your Nyquist frequencies below a length of 5. I've found values between 9 and 22 work well to gather enough measurements. You also have an averaging summation that measures the weight or importance of the information over the summation period. This is also used for highlighting when you have an information signal above the 5% level (2 sigma) and then can be adjusted using the Percent Rank Variable. Finally, you can plot the individual signals (Price or Volume) to get another set of measurements to utilize. As can be seen in the chart below, the volume moves before price (but hopefully you already knew that)


At its core, this is taking the Binary Entropy measurement (using a Bernoulli distribution) for price and volume. I've subtracted the volume from the price so that you can use it like a MACD, also for shorter time frames (7, 9, 11) you can get divergences on the histogram. These divergences are primarily due to the weekly nature of the markets (5 days, 10 days is two weeks,...so 9 is measuring the last day of the past two weeks...so 11 is measuring the current day and the past two weeks).

Here are a couple of other examples, assuming you just love BTC, Stocks, or FOREX. I fashioned up a strategy to show the potential of the indicator.
BTC-Strategy


Stock-Strategy (#loveyouNFLX)


FOREX - (for everyone hopped up on 40X leverage)

Release Notes

Added a shoutout to @Picte for his inspiring idea

Release Notes

Added Alerts for Up/Down Triangle Start

Release Notes

Version 2.0, Thanks for all the comments and love.
  • Added a broad array of Price/Volume averaging options, kept default method the same
  • Added an option to calculate the Original Binary Entropy equation. Added a negative
  • Added items to functions. Feel free to grab them.
  • Added an extra highlighting option, user request.


The default option will give you the same behavior as previous versions. Several new features were added based on user comments. We've included an adjusted Binary Entropy equation (the original equation), noted as the "Purest Option". Give similar highlights based on price and volume, but is less consistent on the direction. As with the previous version, when it is highlighed, it demands your attention.


Same functionality, where you can independently isolate price and volume. With the added purest option, the individual components will always be negative (information entropy, Shannon's basically). The more negative, the more new information is generated from the signal.


We also combined the averaging options from my CDF scripts. Finally, because everyone loves choices, we’ve included the ability to measure the probability of:
  • the current value (Price and volume )
  • change
  • percent change
  • momentum (change over a period of time)
  • Acceleration (change of the change)
  • contribution (amount of the current bar over the sum)
  • volatility (natural log ratio of today and the previous bar)



https://www.tradingview.com/script/Is3ats7l-Cumulative-distribution-function-Probability/
Comments
PineCoders
kocurekc
@PineCoders, Wonderful way to ring in the new year, thanks!
PineCoders
This publication will be featured in our "Best Scripts of The Month" selection. Thank you for your valuable contribution to the TradingView community, and congrats!
kocurekc
@PineCoders, Wow, awesome! #thanksfortheLove
anantgupta970
I AM NOT GETTING SIGNALS ON PRICE CHART ?
kentskip
@anantgupta970, I am not getting the arrow signals either.
kocurekc
@kentskip, @anantgupta970 Thanks for the comments. This is just an indicator and not a strategy which gives you buy and sell signals. You can take this code and turn it into a Tradingview strategy.
tradingview.com/pine-script-docs/en/v4/essential/Strategies.html?highlight=strategy

You could also add this code to paint bars for a set of rules:
BarColors = input(title="Painting bars", type=input.bool, defval=true)
signal = sma(info2,3)
nColor = BarColors ? signal > 0 and signal >= range ? color.green : signal < 0 and signal <= -range ? color.red : color.blue : na
barcolor(nColor)

TrendmasterOfficial
Hi there! This indicator is hitting over 80% at the BTC 1hr for me.
Is there a way I can set alerts for the green and red triangles?
kocurekc
@The03Swede, I don't normally do alerts, but will review some pinescript tonight and see about an update. Just as an FYI, this is fundamentally what I'm doing with the strategy script. Do you want the alert on the first or last triangle?

TrendmasterOfficial
@kocurekc, Definitely the 1st triangle, that way I can assess potentially opening a position based on this indicator, other indicators, and basic analysis
More