Trend Day Indentification
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Marcns_

//@version=4
study("Trend Day")

// a script that identifies trend days for the purpose of 1) counting how often they occur 
// and 2) predicting intraday reversion regime the following day 


// atr filter indicating that range expansion today is highest over previous 3 periods
r = atr(1)

RangeEx = 0.0

if r >= highest(r, 5)
    RangeEx := 1.0
else
    na


//definition of a trend day - opens in the upper or lower 15% of it's range.
// and closes in the opposite upper or lower 15% of its range

TrndExThreshold_Open = input(0.15)
TrndExThreshold_Close = input(0.15)

//#1 open within 15% of low

OpenAtLow = 0.0 

if (close > open) and (open-low) / (high-low) < TrndExThreshold_Open
    OpenAtLow := 1.0
else 
    na 

//#2 close within 15% of high
    
CloseAtHigh = 0.0

if (close > open) and (high-close) / (high-low) < TrndExThreshold_Close
    CloseAtHigh := 1.0
else
    na 

TrendDayUp = 0.0

if OpenAtLow + CloseAtHigh + RangeEx == 3.0
    TrendDayUp := 1
else
    na

plot(TrendDayUp, title = "TrdUp")



OpenAtHigh = 0.0 

if (close < open) and (high-open) / (high-low) < TrndExThreshold_Open
    OpenAtHigh := 1.0
else 
    na 

//#2 close within 15% of high
    
CloseAtLow = 0.0

if (close < open) and (close-low) / (high-low) < TrndExThreshold_Close
    CloseAtLow := 1.0
else
    na 

TrendDayDown = 0.0

if OpenAtHigh + CloseAtLow + RangeEx == 3.0
    TrendDayDown := 1
else
    na

plot(TrendDayDown, title = "TrdDn", color = color.red)


// 











