Daily Score
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © JuicyTeets


//@version=4
study("Daily Score", overlay=true)

// Input parameters
smaLength = input(50, title="SMA Length")
smaLengthTwoHundred = input(200, title="SMA Length 200")
rsiLength = input(13, title="RSI Length")
rsiThreshold = input(40.99, title="RSI Threshold")

// Calculate the SMA
smaValue = sma(close, smaLength)
smaValueTwoHundred = sma(close, smaLengthTwoHundred)

// Compare current price to the SMA
isAboveSMA = close > smaValue
isAboveSMATwoHundred = close > smaValueTwoHundred

// Assign a score based on the comparison
OverFiftyScore = isAboveSMA ? 2 : 0
OverTwoHundredScore = isAboveSMATwoHundred ? 1 : 0

// Calculate the previous SMA value
prevSmaValue = sma(close, smaLength)[1]
prevSmaTwoHundredValue = sma(close, smaLengthTwoHundred)[1]

// Assess the trend
isUpTrend = smaValue > prevSmaValue
isDownTrend = smaValue < prevSmaValue
FiftyTrendScore = isUpTrend ? 2 : isDownTrend ? -2 :0

isUpTrendTwoHundred = smaValueTwoHundred > prevSmaTwoHundredValue
isDownTrendTwoHundred = smaValueTwoHundred < prevSmaTwoHundredValue
TwoHundredTrendScore = isUpTrendTwoHundred ? 2 : isDownTrendTwoHundred ? -2 :0

// Assess if 50 is above the 200
is50above200 = smaValue > smaValueTwoHundred
is50above200score = is50above200 ? 3 : 0

// Score for comparison of current price at trigger to price at end of previous bull zone
rsiValue = rsi(close, rsiLength)
rsiBelowThreshold = crossover(rsiValue, rsiThreshold)
var float priceAtRSILow = na
if (rsiBelowThreshold)
    priceAtRSILow := close

priceAboveRSILow = close > priceAtRSILow
LastPeriodScore = priceAboveRSILow ? 3 : 0

//Scoring for whether a local min or max has recently occurred
lookbackPeriod = 30
recentPeriod = 5

// Function to get the highest high and lowest low over a specified period
getExtremePrices(srcHigh, srcLow, length) =>
    highPrice = highest(srcHigh, length)
    lowPrice = lowest(srcLow, length)
    [highPrice, lowPrice]

// Get the highest high and lowest low over the last 100 days
[highestHigh100Day, lowestLow100Day] = getExtremePrices(high, low, lookbackPeriod)

// Assess how recently the extreme prices were achieved
daysSinceHigh = barssince(high == highestHigh100Day)
daysSinceLow = barssince(low == lowestLow100Day)

// Check if either the highest high or lowest low was achieved within the last 5 days and assign a score
Extremescore = (daysSinceHigh <= recentPeriod or daysSinceLow <= recentPeriod) ? -3 : 0

//Check if a golden cross has occurred within 5 days
// Input parameters
sma50 = sma(close, 50)
sma200 = sma(close, 200)

// Assess whether the 50-day SMA has crossed over the 200-day SMA in the last 5 days
PositivecrossoverOccurred = crossover(sma50, sma200) and barssince(crossover(sma50, sma200)) < 5
NegativecrossoverOccurred = crossover(sma200, sma50) and barssince(crossover(sma200, sma50)) < 5


// Score calculation
PosXscore = PositivecrossoverOccurred ?  2 : 0
NegXscore = NegativecrossoverOccurred ?  -2 : 0



// Update the score based on all assessments
combinedScore1 = OverFiftyScore + FiftyTrendScore + OverTwoHundredScore + TwoHundredTrendScore + is50above200score + LastPeriodScore + Extremescore + PosXscore + NegXscore

// Create final Score
score = combinedScore1


// Plot the score on the chart//
plot(score, color=color.rgb(131,255,82), style=plot.style_stepline, title="Score")

