Chat_GPT

 avatar
s7s
javascript
5 months ago
4.4 kB
7
Indexable
// Swing Inputs
var string GROUP_SWING = "Swing For Signal BUY/SELL"
i_rng_src = input.source(high, "Source", group=GROUP_SWING)
i_rng_per = input.int(20, "Period", 1, 30, 1, group=GROUP_SWING)
i_rng_qty = input.float(3.5, "Mult", 0.0000001, group=GROUP_SWING)

// SL/TP Inputs
var string GROUP_SLTP = "Stop Loss / Take Profit"
i_long_tp = input.float(3, "Long Take Profit (%)", 0.0, step=0.5, group=GROUP_SLTP)
i_long_sl = input.float(1, "Long Stop Loss (%)", 0.0, step=0.5, group=GROUP_SLTP)

// Display Settings
i_display_tpsl = input.bool(true, "Display TP/SL Lines", group="Display Settings")

// --------

rng_filt(float src, float rng, int n) =>
    var float[2] rfilt = array.new_float(2, src)
    array.set(rfilt, 1, array.get(rfilt, 0))
    if src - rng > array.get(rfilt, 1)
        array.set(rfilt, 0, src - rng)
    else if src + rng < array.get(rfilt, 1)
        array.set(rfilt, 0, src + rng)
    float rng_filt1 = array.get(rfilt, 0)
    [rng_filt1 + rng, rng_filt1 - rng, rng_filt1]

rng_size(float src, float qty, int n) =>
    float wper = n * 2 - 1
    float avrng = ta.ema(math.abs(src - src[1]), n)
    ta.ema(avrng, wper) * qty

f_security(string _sym, string _res, series float _src, bool _rep) =>
    request.security(_sym, _res, _src[not _rep and barstate.isrealtime ? 1 : 0])[_rep or barstate.isrealtime ? 0 : 1]

// -------

// Define arrays for session high and low values
var float[] sessionHighs = array.new_float(14, 0)
var float[] sessionLows = array.new_float(14, 0)

// Function to update session high and low arrays
updateSessionArrays(float currentHigh, float currentLow) =>
    // Shift all values to the right
    for i = 13 to 1
        array.set(sessionHighs, i, array.get(sessionHighs, i - 1))
        array.set(sessionLows, i, array.get(sessionLows, i - 1))
    
    // Add new values at the beginning
    array.set(sessionHighs, 0, currentHigh)
    array.set(sessionLows, 0, currentLow)

// Call this function at the start of each new session
if (ta.change(time("D")))
    updateSessionArrays(high[1], low[1])

// You can access the highest high and lowest low of the last 14 sessions like this:
float highest14 = array.max(sessionHighs)
float lowest14 = array.min(sessionLows)

// Plot the highest and lowest values if needed
plot(highest14, color=color.green, title="Highest of last 14 sessions")
plot(lowest14, color=color.red, title="Lowest of last 14 sessions")

// Calculate Range Filter Values
float rng = rng_size(i_rng_src, i_rng_qty, i_rng_per)
[float h_band, float l_band, float filt] = rng_filt(i_rng_src, rng, i_rng_per)

// Direction Conditions
var float fdir = 0.0
fdir := filt > filt[1] ? 1 : filt < filt[1] ? -1 : fdir
bool upward = fdir == 1
bool downward = fdir == -1

// Trading Conditions
bool longCond = i_rng_src > filt and (i_rng_src > i_rng_src[1] or upward)
bool shortCond = i_rng_src < filt and (i_rng_src < i_rng_src[1] or downward)

var int CondIni = 0
CondIni := longCond ? 1 : shortCond ? -1 : CondIni[1]
bool longCondition = longCond and CondIni[1] == -1
bool shortCondition = shortCond and CondIni[1] == 1

// --------

// Colors
color filt_color = upward ? #05ff9b : downward ? #ff0583 : #cccccc
color bar_color = i_rng_src > filt ? (i_rng_src > i_rng_src[1] ? #05ff9b : #00b36b) :
                  i_rng_src < filt ? (i_rng_src < i_rng_src[1] ? #ff0583 : #b8005d) : #cccccc

// Plot signals
plotshape(longCondition, title='Buy Signal', text='BUY', textcolor=color.white, 
          style=shape.labelup, location=location.belowbar, color=color.new(color.green, 0))
plotshape(shortCondition, title='Sell Signal', text='SELL', textcolor=color.white, 
          style=shape.labeldown, location=location.abovebar, color=color.new(color.red, 0))

// Plot TP/SL lines
if i_display_tpsl and strategy.position_size != 0
    var line longTP = na, var line longSL = na
    float avg_price = strategy.position_avg_price
    float long_tp = avg_price * (1 + i_long_tp / 100)
    float long_sl = avg_price * (1 - i_long_sl / 100)
    
    if strategy.position_size > 0
        longTP := line.new(bar_index, long_tp, bar_index, long_tp, color=color.green, style=line.style_dashed)
        longSL := line.new(bar_index, long_sl, bar_index, long_sl, color=color.red, style=line.style_dashed)
        line.delete(longTP[1])
        line.delete(longSL[1])
    else
        line.delete(longTP)
        line.delete(longSL)
        longTP := na
        longSL := na

// -------- 
Editor is loading...