Stock Market Emotion Index (SMEI)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Lantzwat

//@version=5
indicator("Stock Market Emotion Index", "SMEI")
// Implementation of Charlie Q. Yang research paper “The stock market emotion index”, subtitle “A New Sentiment Measure Using Enhanced OBV and Money Flow Indicators” (2007) 
// where he combined “five simple emotion statistics”  - 
// "Close Emotion Statistic (CES), Money Flow Statistic (MFS), Supply Demand Statistic (SDS), Relative Strength Statistic (RSS), and Psychological Level Statistic (PLS)"
// - into one indicator.

_sgn (float x) =>
    _return = x == 0 ? 0 : x > 0 ? 1 : -1 

MasterTimeFrame = input.timeframe("","Set Timeframe")
period  = input(260, "Period")
autoper =input.bool(true, "script sets period")

MTF = MasterTimeFrame == "" ? timeframe.period : MasterTimeFrame
preset= MTF=="M"? 14: MTF=="W"? 14: MTF=="D"? 21: MTF=="240"? 28: MTF=="180"? 28: MTF=="120"? 35: MTF=="60"? 35: MTF=="45"? 35: MTF=="30"? 42: MTF=="15"? 42: MTF=="5"? 49: MTF=="3"? 49: MTF=="1"? 56: 14
period:= autoper ? preset : period

src_close   = request.security(syminfo.tickerid , MTF, close)
src_volume  = request.security(syminfo.tickerid , MTF, volume)
src_high    = request.security(syminfo.tickerid , MTF, high)
src_low     = request.security(syminfo.tickerid , MTF, low)
src_open    = request.security(syminfo.tickerid , MTF, open)

//Close Emotion Statistic (CES)
CES = _sgn(src_close - src_close[1]) * src_volume

//Money Flow Statistic (MFS)
MFS = _sgn(src_close - (src_high-src_low)/2) * src_volume

//Supply Demand Statistic (SDS)
SDS = _sgn(src_close - src_open) * src_volume

//Relative Strength Statistic (RSS)
RSS = _sgn(ta.rsi(src_close,1) - 0.5) * src_volume

//Psychological Level Statistic (PLS).
PLS = 0.0
if src_close > ta.highest(src_high,21)
    PLS := src_volume
if src_close > ta.lowest(src_low, 21)
    PLS := src_volume * -1
if src_close > ta.highest(src_high,21) and src_close > ta.lowest(src_low, 21)
    PLS := 0.0

//Stock Market Emotion Index =>
//SMES = (CES + MFS + SDS + RSS + PLS) / math.avg(src_volume,21) * 5
//plot (SMES)
vol_avg = math.avg(src_volume,21)
SMEI = math.sum( (CES + MFS + SDS + RSS + PLS) / (10 * 5 * vol_avg), period)

SMEI_up     = SMEI > SMEI[1]
SMEI_down   = SMEI < SMEI[1]
SMEIcol     = SMEI_up ? color.lime : SMEI_down ? color.red : color.aqua 
plot(SMEI,title = "SMEI", color = color.new(SMEIcol, 50), linewidth = 1, style = plot.style_area)

// Bull and bear market coloration
bgcolor(SMEI < 0 ? color.new(color.red,80) : SMEI > 0 ? color.new(color.green, 80) : color.new(color.white,100))

//Rating scheme from the paper:
//1. SMEI is positive and up in an early bull market - Strong Buy
//2. SMEI is positive and down in an early bull market - Hold
//3. SMEI is positive and up in a middle bull market - Buy
//4. SMEI is positive and down in a middle bull market - Hold
//5. SMEI is positive and up in a late bull market - Buy
//6. SMEI is positive and down in a late bull market - Weak Sell (reduce)
//7. SMEI is negative and up in an early bear market - Hold
//8. SMEI is negative and down in an early bear market - Strong Sell
//9. SMEI is negative and up in a middle bear market - Hold
//10. SMEI is negative and down in a middle bear market - Sell
//11. SMEI is negative and up in a late bear market - Weak Buy (accumulate)
//12. SMEI is negative and down in a late bear market - Sell
//13. The absolute value of SMEI is less than 0.2 - Wait (sideline)
