Zendog LONG DCA Trigger RSI+StochRSI
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © zendog123

//@version=4
study("Zendog LONG DCA Trigger RSI+StochRSI", shorttitle="LONG DCA Trigger RSI+StochRSI",  overlay=true)
var bool IS_LONG = true

//STOCH RSI condition for DCA Bot debugging
source          = input(type=input.source, defval=close, title="Source for RSI and Stochastic RSI", group="Stochastic RSI")
smoothK         = input(type=input.integer, defval=3, title="Stoch RSI SmoothK", group="Stochastic RSI")
smoothD         = input(type=input.integer, defval=3, title="Stoch RSI Smoothd", group="Stochastic RSI")
lengthStoch     = input(type=input.integer, defval=14, title="Stoch Length", group="Stochastic RSI")
lengthRSIStoch  = input(type=input.integer, defval=14, title="Stoch RSI Length", group="Stochastic RSI")
lengthRSI       = input(type=input.integer, defval=7, title="RSI Length", group="Stochastic RSI")

bool cfg_show_settings_table    = input(title="Show Table With Settings", defval=false, type=input.bool, group="UI")

string rsi_start_operation      = input(defval="<=", title="RSI Trigger ------", type=input.string, group="Deal Trigger Conditions",
                                     options=["not used", "<=", ">="], inline="1")
int rsi_trigger_value           = input(type=input.integer, defval=30, title="", inline="1", group="Deal Trigger Conditions")

string stochk_start_operation   = input(defval="<=", title="Stoch K Trigger", type=input.string,
                                     options=["not used", "<=", ">="], inline="2", group="Deal Trigger Conditions")
int stochk_trigger_value        = input(type=input.integer, defval=20, title="", inline="2", group="Deal Trigger Conditions")

string stochd_start_operation   = input(defval="not used", title="Stoch D Trigger", type=input.string,
                                      options=["not used", "<=", ">="], inline="3", group="Deal Trigger Conditions")
int stochd_trigger_value        = input(type=input.integer, defval=20, title="", inline="3", group="Deal Trigger Conditions")

rsi         = rsi(source, lengthRSI)
rsiStoch    = rsi(source, lengthRSIStoch)

//k is blue
k = sma(stoch(rsiStoch, rsiStoch, rsiStoch, lengthStoch), smoothK)
//d is orange
d = sma(k, smoothD)
crossover = crossover(k, d)
crossunder = crossunder(k, d)


//RSI
bool rsi_start_condition = false
if rsi_start_operation != "not used"
    if (rsi_start_operation == "<=")
        rsi_start_condition := (rsi <= rsi_trigger_value)
    else if (rsi_start_operation == ">=")
        rsi_start_condition := (rsi >= rsi_trigger_value)
else
    rsi_start_condition := true

//Stoch K
bool stochk_start_condition = false
if stochk_start_operation != "not used"
    if (stochk_start_operation == "<=")
        stochk_start_condition := (k <= stochk_trigger_value)
    else if (stochk_start_operation == ">=")
        stochk_start_condition := (k >= stochk_trigger_value)
else
    stochk_start_condition := true

//Stoch D
bool stochd_start_condition = false
if stochd_start_operation != "not used"
    if (stochd_start_operation == "<=")
        stochd_start_condition := (d <= stochd_trigger_value)
    else if (stochd_start_operation == ">=")
        stochd_start_condition := (d >= stochd_trigger_value)
else
    stochd_start_condition := true



signal_trigger_dca = (rsi_start_condition and stochk_start_condition and stochd_start_condition) ? 1 : 0


_colorlong  = (IS_LONG?color.new(color.green, 0):color.new(color.green, 100))
_colorshort = (IS_LONG?color.new(color.red, 100):color.new(color.red, 0))
_style      = (IS_LONG?shape.arrowup:shape.arrowdown)
_location   = (IS_LONG?location.belowbar:location.abovebar)


//just for UI
plotshape(signal_trigger_dca, color=_colorlong, textcolor=_colorlong, text="BUY", style=_style, size=size.normal, location=_location)

//for integration with DCA backtester or alerts, display none
plot(signal_trigger_dca, title="SIGNAL", display=display.none)


if cfg_show_settings_table and barstate.islastconfirmedhistory
    table settings = table.new(position.top_right, columns=1, rows=2,
          frame_width=1, frame_color=color.black)
    _row = 0
    table.cell(settings, column=0, row=0,
      text="LONG SIGNAL Settings", 
       text_halign=text.align_left, text_size=size.normal, text_color=color.white, bgcolor=#006bb3)
       
    _text = tostring(syminfo.currency)+"_"+tostring(syminfo.basecurrency)+"   "+tostring(timeframe.period)+"\n\n"
    if rsi_start_operation != "not used"
        _text   += "RSI-"+tostring(lengthRSI)+" "+tostring(rsi_start_operation)+" "+tostring(rsi_trigger_value)+"\n"
    else
        _text   += "RSI-"+tostring(lengthRSI)+" "+tostring(rsi_start_operation)+"\n"
    
    
    if stochk_start_operation != "not used"
        _text   += "Stoch-K-"+tostring(smoothK)+" "+tostring(stochk_start_operation)+" "+tostring(stochk_trigger_value)+"\n"
    else
        _text   += "Stoch-K-"+tostring(smoothK)+" "+tostring(stochk_start_operation)+"\n"
    
    
    if stochd_start_operation != "not used"
        _text   += "Stoch-D-"+tostring(smoothD)+" "+tostring(stochd_start_operation)+" "+tostring(stochd_trigger_value)+"\n"
    else
        _text   += "Stoch-D-"+tostring(smoothD)+" "+tostring(stochd_start_operation)+"\n"


    _text += "\n\n\nAll settings\nK :"+tostring(smoothK)+"   D: "+tostring(smoothD)+"   StochLength: "+tostring(lengthStoch)+"\nRSIStochLength: "+tostring(lengthRSIStoch)+"   RSI: "+tostring(lengthRSI)

    table.cell(settings, column=0, row=1,
      text=_text, text_halign=text.align_left, text_color=color.black, text_size=size.normal, bgcolor=#CACACA)

