[KL] RSI 14 + 10 Strategy
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © DojiEmoji

//@version=4
strategy("[KL] RSI 14 + 10 Strategy v2.1",overlay=true,pyramiding=1)
// changes in Version 2.1:
// 1) Fixed a bug (related to TSL)

backtest_timeframe_start = input(defval = timestamp("01 Apr 2016 13:30 +0000"), title = "Backtest Start Time", type = input.time)
USE_ENDTIME = input(false,title="Define the ending period for backtests (If false, will test up to most recent candle)")
backtest_timeframe_end = input(defval = timestamp("19 Mar 2021 19:30 +0000"), title = "Backtest End Time", type = input.time)
TARGET_PROFIT_MODE = input(false,title="Exit when Risk:Reward met")
REWARD_RATIO = input(3,title="Risk:[Reward] (i.e. 3) for exit")
// Trailing stop loss {
TSL_ON = input(true,title="Use trailing stop loss")
var entry_price = float(0)
ATR_multi_len = 26
ATR_multi = input(2, "ATR multiplier for stop loss")
ATR_buffer = atr(ATR_multi_len) * ATR_multi
//plotchar(ATR_buffer, "ATR Buffer", "", location = location.top)
risk_reward_buffer = (atr(ATR_multi_len) * ATR_multi) * REWARD_RATIO
take_profit_long = low > entry_price + risk_reward_buffer
take_profit_short = low < entry_price - risk_reward_buffer
var trailing_SL_buffer = float(0)
var stop_loss_price = float(0)
stop_loss_price := max(stop_loss_price, close - trailing_SL_buffer)
// plot TSL line
trail_profit_line_color = color.green
if strategy.position_size == 0  or not TSL_ON
    trail_profit_line_color := color.black
    stop_loss_price := close - trailing_SL_buffer
plot(stop_loss_price,color=trail_profit_line_color)
// }
// RSI {
RSI_LOW = input(40,title="RSI entry")
RSI_HIGH = input(70,title="RSI exit")
rsi14 = rsi(close, 14)
rsi10 = rsi(close, 10)
// }
if time >= backtest_timeframe_start and (time <= backtest_timeframe_end or not USE_ENDTIME)
    buy_condition = rsi14 <= RSI_LOW and rsi10 < rsi14
    exit_condition = rsi14 >= RSI_HIGH and rsi10 > rsi14
    //ENTRY:
    if buy_condition
        if strategy.position_size == 0
            entry_price := close
            trailing_SL_buffer := ATR_buffer
            stop_loss_price := close - ATR_buffer
        
        msg = "entry"
        if strategy.position_size > 0
            msg := "pyramiding"
        strategy.entry("Long",strategy.long, comment=msg)

    //EXIT:
    // Case (A) hits trailing stop
    if TSL_ON and strategy.position_size > 0 and close <= stop_loss_price
        if close > entry_price
            strategy.close("Long", comment="take profit [trailing]")
        else if close <= entry_price 
            strategy.close("Long", comment="stop loss")
    // Case (B) take targeted profit relative to risk 
    if strategy.position_size > 0 and TARGET_PROFIT_MODE
        if take_profit_long
            strategy.close("Long", comment="take profits [risk:reward]")
    // Case (C)
    if strategy.position_size > 0 and exit_condition
        if take_profit_long
            strategy.close("Long", comment="exit[rsi]")

// Also included an additional script (Study script) - "RSI[14 + 10] Double Indicators"

// The following script is a modification of the original RSI indicator built into TradingView
// The reason that it is not included in the Strategy script is because the plotting scales are different.

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

// //@version=4
// study(title="RSI[14 + 10] Double Indicators", shorttitle="RSI", format=format.price, precision=2, resolution="")
// len1 = input(14, minval=1, title="RSI A Length")
// src1 = input(close, "RSI A Source", type = input.source)
// rsi1 = rsi(src1,len1)
// plot(rsi1, "RSI", color=#ffffff)
// len2 = input(10, minval=1, title="RSI B Length")
// src2 = input(close, "RSI B Source", type = input.source)
// rsi2 = rsi(src2,len2)
// plot(rsi2, "RSI", color=#fb8c00)

// band1 = hline(70, "Upper Band", color=#C0C0C0)
// band0 = hline(30, "Lower Band", color=#C0C0C0)
// fill(band1, band0, color=#9915FF, transp=90, title="Background")


