HelperTA
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © leap-forward
//@version=5
// @description TODO: add library description here
library("HelperTA", true)

// @function stockRSI
// @returns <[k, d]>
export stockRSI(series float src, int K, int D, int rsiPeriod, int stochPeriod) =>
	rsi1 = ta.rsi(src, rsiPeriod)
	k = ta.sma(ta.stoch(rsi1, rsi1, rsi1, stochPeriod), K)
	d = ta.sma(k, D)
	[k, d]

// @function DCO
// @returns <[DCO, s]>
export DCO(series float price, int donchianPeriod, int smaPeriod) =>
	lower = ta.lowest(price, donchianPeriod)
	upper = ta.highest(price, donchianPeriod)
	DCO = (price-lower)/(upper-lower)*100
	s = ta.sma(DCO, smaPeriod)
	[DCO, s]


// @function MarketCycle
// @returns <aggr>
export MarketCycle(series float donchianPrice, series float rsiPrice, series float srsiPrice, simple int donchianPeriod, simple int donchianSmoothing, simple int rsiPeriod, int rsiSmoothing, simple int srsiPeriod, simple int srsiSmoothing, simple int srsiK, simple int srsiD, simple float rsiWeight, simple float srsiWeight, simple float dcoWeight) =>
	// DCO
	[DCO, DCOs] = DCO(donchianPrice, donchianPeriod, donchianSmoothing)
	// RSI
	rsiValue = ta.rsi(rsiPrice, rsiPeriod)
	rsiK = ta.sma(rsiValue, rsiSmoothing)
	// StochRSI
	[k, d] = stockRSI(srsiPrice, srsiK, srsiD, srsiPeriod, srsiSmoothing)

	// Weighted aggregate
	tw = rsiWeight + srsiWeight + dcoWeight
	aggr = ((DCO + DCOs)*dcoWeight + (rsiValue + rsiK)*rsiWeight + (k + d)*srsiWeight) / (2*(dcoWeight+rsiWeight+srsiWeight))
	aggr
