Skip to content

Instantly share code, notes, and snippets.

@jcayzac
Created July 23, 2022 05:17
Show Gist options
  • Save jcayzac/c3db6088e53f20cdd898b797256bcf77 to your computer and use it in GitHub Desktop.
Save jcayzac/c3db6088e53f20cdd898b797256bcf77 to your computer and use it in GitHub Desktop.
Information Discreteness [TradingView] [PineScript]
// Based on Da, Gurun and Warachka's 2014 paper:
// "Frog in the Pan: Continuous Information and Momentum"
// PDF: https://www3.nd.edu/~zda/Frog.pdf
//@version=5
indicator("Information Discreteness", overlay=false, precision=1, timeframe="D")
LENGTH = input.int(title="Window Length", defval=365, minval=7)
SKIP_RECENT = input.int(title="Skip most recent", defval=30, minval=0)
MA_LENGTH = input.int(title="Average returns over", defval=7, minval=1)
POSITIVE_ID_COLOR = input.color(title="Discrete", defval=#ff0000, group="Colors")
NEGATIVE_ID_COLOR = input.color(title="Continuous", defval=#00ff00, group="Colors")
priceAt(daysAgo) => ta.sma(close[daysAgo], MA_LENGTH)
float recentPrice = priceAt(SKIP_RECENT)
float ancientPrice = priceAt(LENGTH)
float PRET = recentPrice / ancientPrice
int upDays = 0
int downDays = 0
for i = 1 to LENGTH
delta = close[i-1] - close[i]
if delta > 0
upDays += 1
if delta < 0
downDays += 1
upDaysPercent = upDays / (upDays + downDays)
downDaysPercent = downDays / (upDays + downDays)
ID = math.sign(PRET) * (downDaysPercent - upDaysPercent)
// -1: continuous
// ...
// +1: discrete
plot(ID, "ID", color=ID > 0 ? POSITIVE_ID_COLOR : NEGATIVE_ID_COLOR, style=plot.style_area)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment