import TradingView/ta/5

// This Pine Scriptโ„ข code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// ยฉ Barnabash

//@version = 5
strategy(title = 'BNS - Bitcoin Strategy',
     shorttitle = 'BNS',
     overlay = true,
     pyramiding = 1,
     default_qty_type = strategy.percent_of_equity,
     default_qty_value = 100,
     initial_capital = 10000,
     currency = currency.USD)

// User Inputs
var g_roc           = 'Rate of Change (ROC)'
roclength           = input.int(title = 'ROC Length', defval = 10, minval = 1, group = g_roc)
rocneutral          = input.int(title = 'ROC Neutral', defval = 0, tooltip = 'Set your preferred ROC Neutral value which means if the ROC is above that, then it indicates a positive ROC.', group = g_roc)

var g_supertrend    = 'Supertrend'
supertrendfactor    = input.float(title = 'Supertrend Factor', defval = 3.0, step = 0.01, minval = 1, group = g_supertrend)
supertrendatrlength = input.int(title = 'Supertrend ATR Length', defval = 50, group = g_supertrend)

var g_ema           = 'Exponential Moving Average (EMA)'
emalength           = input.int(title = 'EMA Length', defval = 50, minval = 1, group = g_ema)
emasourcex          = input.string(title = 'EMA Source', defval = 'close', options = ['close', 'high', 'low', 'hlc3', 'none'], group = g_ema)

var g_cmf           = 'Chaikin Money Flow (CMF)'
cmflength           = input.int(title = 'CMF Length', defval = 20, minval = 1, group = g_cmf)

// Get ROC Value
roc = 100 * (close - close[roclength])/close[roclength]

// Get Supertrend Value
[supertrend, direction] = ta.supertrend(supertrendfactor, supertrendatrlength)

// Switch EMA & Get Values
float emasource = switch emasourcex
    'close'     => ta.ema(close, emalength)
    'high'      => ta.ema(high, emalength)
    'low'       => ta.ema(low, emalength)
    'hlc3'      => ta.ema(hlc3, emalength)
    'none'      => na

ema = ta.ema(emasource, emalength)

// Get CMF Value
cmfcalc = close==high and close==low or high==low ? 0 : ((2*close-low-high)/(high-low))*volume
cmf = math.sum(cmfcalc, cmflength) / math.sum(volume, cmflength)

// Strategy Rules
barstate        = barstate.isconfirmed
rocbuy          = ta.crossover(roc, rocneutral)
rocsell         = ta.crossunder(roc, rocneutral)
supertrendbuy   = direction < 0
supertrendsell  = direction > 0
emabuy          = close > ema
emasell         = close < ema
cmfbuy          = cmf > 0
cmfsell         = cmf < 0

buysignal   = barstate and rocbuy and cmfbuy and (supertrendbuy or emabuy)
sellsignal  = barstate and rocsell and cmfsell and (supertrendsell or emasell)

// Handle Strategy Positions
if buysignal
    strategy.entry(id = 'Buy', direction = strategy.long)

if sellsignal
    strategy.entry(id = 'Sell', direction = strategy.short)

// Cobra Metrics Table
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"], group = " ๐“’๐“ธ๐“ซ๐“ป๐“ช ๐“œ๐“ฎ๐“ฝ๐“ป๐“ฒ๐“ฌ๐“ผ ")
pos_table = input.string("Middle Left", "Table Position", options = ["Top Left", "Middle Left", "Bottom Left", "Top Right", "Middle Right", "Bottom Right", "Top Center", "Bottom Center"], group = " ๐“’๐“ธ๐“ซ๐“ป๐“ช ๐“œ๐“ฎ๐“ฝ๐“ป๐“ฒ๐“ฌ๐“ผ ")
type_table = input.string("None", "Table Type", options = ["Full", "Simple", "None"], group = " ๐“’๐“ธ๐“ซ๐“ป๐“ช ๐“œ๐“ฎ๐“ฝ๐“ป๐“ฒ๐“ฌ๐“ผ ")
plot(cobra.curve(disp_ind))
cobra.cobraTable(type_table, pos_table)

// Plots
plot(na)