// This Pine Scriptâ„¢ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © CertifiedWeeb

//@version=5
strategy("ETHBTC aggregate", default_qty_type = strategy.percent_of_equity, default_qty_value = 100, pyramiding = 0, slippage = 1, initial_capital = 1000000)

start_date = input.time(timestamp("2018-01-01"))

import CertifiedWeeb/ETHBTCSLAPPERSTAGGYLibrary/3 as staggy
import CertifiedWeeb/IRSETHBTCLibrary/1 as irs
import CertifiedWeeb/ETHBTCSlapperV1AriSaiLibrary/1 as arisai
import CertifiedWeeb/HappyDoggoLibraryETHBTC/2 as happydoggo
import CertifiedWeeb/DunningKrugerthanksTichiLibraryETHBTC/2 as dunningkruger
import CertifiedWeeb/ArsixR6ETHBTC/1 as arsix

use_strategy_1 = input.bool(false)
use_strategy_2 = input.bool(false)
use_strategy_3 = input.bool(false)
use_strategy_4 = input.bool(true)
use_strategy_5 = input.bool(false)
use_strategy_6 = input.bool(false)
use_roc_to_neutral_state = input.bool(true)

f_checkp() =>
    p = close
    count = 0

    while p > 1
        p /= 10
        count += 1

    math.pow(10, count)

f_profile(lenra) =>
    l = lenra
    integral = 0.0

    f_x = math.abs(ta.rsi(close, lenra)) 

    for x = low / f_checkp() to high / f_checkp() 
        integral := integral + f_x

    l * integral

puell_arisai() =>
    top = 1.1
    bottom = 5
	miningRevenue = request.security('QUANDL:BCHAIN/MIREV', 'D', close[1], barmerge.gaps_off, barmerge.lookahead_on)
	ma365 = request.security('QUANDL:BCHAIN/MIREV', 'D', ta.sma(close, 365)[1], barmerge.gaps_off, barmerge.lookahead_on)
	puellMultiple = miningRevenue / ma365

	smonadnu = puellMultiple < top
	smonavrhu = puellMultiple > bottom

	[smonadnu, smonavrhu]

on_chain_shenanigans() =>
	//Query
	MC1 = request.security("GLASSNODE:ETH_MARKETCAP","D",close)
	MCR = request.security("COINMETRICS:ETH_MARKETCAPREAL","D",close)
	OTHERS = request.security('CRYPTOCAP:OTHERS.D', 'D', close)

	//On chain calculations
	NUPL = ((MC1 - MCR)/MC1)*100
	MVRV = MC1 / MCR
	MVRV_sma_inp = input.int(10, step =1, group = 'On chain ')
	MVRV_sma = ta.sma(MVRV, MVRV_sma_inp)
	//Helpful price metrics
	PriceGrad = MC1 / MC1[1]
	OTHgrad = OTHERS / OTHERS[1]

	//Calculate the SOPR
	coin = "Bitcoin"
	Slen = 8
	bg = true
	symbol = coin == 'Ethereum' ? 'XTVCETH_SOPR' : coin == 'Litecoin' ? 'XTVCLTC_SOPR' : 'XTVCBTC_SOPR'
	sopr = request.security(symbol, 'D', ta.sma(close, Slen))

	[NUPL, MVRV_sma, sopr]

arsix_raver() =>
	lenra = 63
	spacing_profile = f_profile(lenra)
	RA = ta.rsi(ta.ema(spacing_profile, lenra), lenra)
	RAver = request.security(syminfo.tickerid, "4D", barstate.isconfirmed ? RA : RA[1])
	RAver

adaptive_rsi() =>
	length = 8
	src = close

	alpha = 2 * math.abs(ta.rsi(src, length) / 100 - 0.5)

	var float arsi = na
	arsi := alpha * src + (1 - alpha) * nz(arsi[1])

	arsilong = arsi > arsi[1]
	arsishort = arsi < arsi[1]

	arsilong ? 1 : arsishort ? -1 : 0

staggy_2d_adaptive_rsi() =>
	arsi_direction = request.security(syminfo.tickerid, "2D", adaptive_rsi()[1], lookahead = barmerge.lookahead_on)
	arsilong = arsi_direction > 0
	arsishort = arsi_direction < 0
	[arsilong, arsishort]

[NUPL, MVRV_sma, sopr] = on_chain_shenanigans()
[smonadnu, smonavrhu] = puell_arisai()
[arsilong, arsishort] = staggy_2d_adaptive_rsi()

one = staggy.get_position(arsishort)
two = irs.get_position()
three = arisai.get_position(smonadnu, smonavrhu)
[four, trend_kel, trend_fdiloxx, trend_RSIMOM, trend_cci, trend_rsi, trend_stc] = happydoggo.get_position()
five = dunningkruger.get_position(NUPL, MVRV_sma, sopr)
six = arsix.get_position(arsix_raver())

plot(trend_kel > 0 ? 100 : 98, color = color.aqua)
plot(trend_fdiloxx > 0 ? 97 : 95, color = color.yellow)
plot(trend_RSIMOM > 0 ? 94 : 92, color = color.orange)
plot(trend_cci > 0 ? 91 : 89, color = color.green)
plot(trend_rsi > 0 ? 88 : 86, color = color.lime)
plot(trend_stc > 0 ? 85 : 83, color = color.silver)
tpi = 0.0
count = 0

if use_strategy_1
    tpi += one
    count += 1
if use_strategy_2
    tpi += two
    count += 1
if use_strategy_3
    tpi += three
    count += 1
if use_strategy_4
    tpi += four
    count += 1
if use_strategy_5
    tpi += five
    count += 1
if use_strategy_6
    tpi += six
    count += 1

tpi := tpi / count
roc_long = use_roc_to_neutral_state and tpi > tpi[1] and tpi > -0.0001
roc_short = use_roc_to_neutral_state and tpi < tpi[1] and tpi < 0.0001

if (tpi > 0 or roc_long) and time > start_date
    strategy.entry("L", strategy.long)
if (tpi < 0 or roc_short) and time > start_date
    strategy.entry("S", strategy.short)

plot(tpi, color = tpi > 0 ? color.lime : tpi < 0 ? color.red : color.silver)

import EliCobra/CobraMetrics/4 as cobra

disp_ind = input.string ("None" , title = "Display Curve" , tooltip = "Choose which data you would like to display", options=["Strategy", "Equity", "Open Profit", "Gross Profit", "Net Profit", "None"])
pos_table = input.string("Middle Right", "Table Position", options = ["Top Left", "Middle Left", "Bottom Left", "Top Right", "Middle Right", "Bottom Right", "Top Center", "Bottom Center"])
type_table = input.string("Full", "Table Type", options = ["Full", "Simple", "None"])
plot(cobra.curve(disp_ind))
cobra.cobraTable(type_table, pos_table)