Alpha SOPR Indicator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © mehran_khanjan



//@version=4
study(title = 'Alpha SOPR Indicator')



// ******************         ******************
// ******************         ******************
// ******************         ******************
// ******************         ******************
// ****************** Inputs  ******************
// ******************         ******************
// ******************         ******************
// ******************         ******************
// ******************         ******************
crypto_currency = input(title = 'Cryptocurrencies' , options = ['Bitcoin' , 'Ethereum' , 'Litecoin'] ,  defval = 'Bitcoin')
percentage_of_deviation = input(type = input.float , title = 'Percentage of Deviation' , defval = 3.0)
background_color = input(type = input.bool , title = 'Use Background for Deviation Bands' , defval = false)


use_sma = input(type = input.bool , title = 'Use Simple Moving Average Instead of Real Value' , defval = false , group = 'Simple Moving Average')
sma_length = input(type = input.integer , title = 'Simple Moving Average Length' , defval = 7 , group = 'Simple Moving Average')



// ******************                      ******************
// ******************                      ******************
// ******************                      ******************
// ******************                      ******************
// ****************** Global Calcualtions  ******************
// ******************                      ******************
// ******************                      ******************
// ******************                      ******************
// ******************                      ******************


// *
// * Get SOPR Data
// *
_symbol = crypto_currency == 'Ethereum' ? 'XTVCETH_SOPR' : crypto_currency == 'Litecoin' ? 'XTVCLTC_SOPR' : 'XTVCBTC_SOPR'
_sopr = security(_symbol, 'D', use_sma ? sma(close , sma_length) : close)


// *
// * Calculate Deviation Levels
// *
_up_level = 1 + (percentage_of_deviation / 100)
_down_level = 1 - (percentage_of_deviation / 100)


// *
// * Set Background
// *
final_background_color = background_color == false ? na : _sopr > _up_level ? color.new(color.red , 20) : _sopr < _down_level ? color.new(color.lime , 20) : na
bgcolor(final_background_color)


// *
// * Draw Lines
// *
plot(_sopr)
hline(price = 1.00, title = 'Base Line', color = #787B86)