Crypto BTC Correlation Scalper Gaps Strategy
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © exlux99

//@version=5
strategy("Crypto BTC Correlation Scalper Gaps Strategy", overlay = true)

inverse=input.bool(false, title="Inverse Mode / Long = Short or Long = Long")

ex_symbol = input.symbol("CME:BTC1!")
ex_high = request.security(ex_symbol, timeframe.period,high)
ex_low = request.security(ex_symbol, timeframe.period,low)
ex_open = request.security(ex_symbol, timeframe.period,open)
ex_close = request.security(ex_symbol, timeframe.period,close)

apply_correlation = input.bool(true, title="Correlation Candles")
def_high = 0.0
def_low =0.0
def_open =0.0
def_close =0.0

def_high := apply_correlation? ex_high : high
def_low := apply_correlation? ex_low : low
def_open := apply_correlation? ex_open : open
def_close := apply_correlation? ex_close : close

daily_sma = request.security(ex_symbol, "D", ta.sma(request.security(ex_symbol, "D", close), input(1)))
//plot(daily_sma)

legnth_tp_sl = input(50, title="Length for Checking Highest High/Lowest Low Points")
minimalDeviationInput = nz(input.float(30.0, "Minimal Deviation (%)", minval=1, maxval=100) / 100 * ta.sma(def_high-def_low, 14))

// Detect gaps.
isGapDown = def_high < def_low[1] and def_low[1] - def_high >= minimalDeviationInput
isGapUp = def_low > def_high[1] and def_low - def_high[1] >= minimalDeviationInput
isGap = isGapDown or isGapUp
isGapClosed = false



lowest_low=ta.lowest(def_low, legnth_tp_sl)
highest_high=ta.highest(def_high, legnth_tp_sl)

long = isGapUp and def_close > daily_sma
short = isGapDown and def_close < daily_sma



var longOpened = false
var int timeOfBuyLong = na
var float lowest_low_var_tp = na
var float lowest_low_var_sl = na
var bool longEntry = na


longEntry := long and not longOpened 

if longEntry
    longOpened := true
    timeOfBuyLong := time
    lowest_low_var_tp := def_close * (1 + ((def_close / lowest_low) -1))
    lowest_low_var_sl := lowest_low

longExitSignal = short or  def_high > lowest_low_var_tp //or def_low < lowest_low_var_sl
longExit = longOpened[1] and longExitSignal


if longExit 
    longOpened := false
    timeOfBuyLong := na
    lowest_low_var_tp:=na
    lowest_low_var_sl :=na
 

    
//-------------------------------------------------------------------------------
var shortOpened = false
var int timeOfBuyShort = na
var float highest_high_var_tp = na
var float highest_high_var_sl = na
var bool shortEntry = na

shortEntry := short and not shortOpened  

if shortEntry
    shortOpened := true
    timeOfBuyShort := time
    timeOfBuyShort
    highest_high_var_tp := def_close * (1 - ((highest_high / def_close) - 1  ))
    highest_high_var_sl := highest_high
 


shortExitSignal = long  or  def_low < highest_high_var_tp //or def_high > highest_high_var_sl
shortExit = shortOpened[1] and shortExitSignal 


if shortExit
  
    shortOpened := false
    timeOfBuyShort := na
    highest_high_var_tp := na
    highest_high_var_sl := na
    
if(inverse)
    strategy.entry("long",strategy.short,when=longEntry)
    strategy.close("long",when=longExit)
    
    strategy.entry("short",strategy.long,when=shortEntry)
    strategy.close("short",when=shortExit)

if(not inverse)
    strategy.entry("long",strategy.long,when=longEntry)
    strategy.close("long",when=longExit)
    
    strategy.entry("short",strategy.short,when=shortEntry)
    strategy.close("short",when=shortExit)
