Multi-timeframe Adaptive Tracking Stop Loss Strategy

Author: ChaoZhang, Date: 2023-11-21 11:07:44
Tags:

img

Overview

This strategy calculates the comprehensive signals of multiple technical indicators to determine the trend direction in the current timeframe. When judged as an uptrend, a tracking stop loss line is set at a relatively high point; when judged as a downtrend, a tracking stop loss line is set at a relatively low point. The strategy can adaptively adjust the stop loss line to achieve risk control.

Principle

The strategy combines multiple indicators such as moving averages, ATR, KD, and variance rate to determine the overall trend direction in the current timeframe. Specifically, it calculates the composite value of the following sub-signals:

  1. Moving average direction signal
  2. KD indicator overboughtoversold signal
  3. Price-volume divergence signal
  4. Channel breakthrough signal
  5. Multi-timeframe combined trial-and-error signal
  6. Percent R signal
  7. Moving average regression signal
  8. ATR channel breakthrough signal

Each sub-signal is smoothed and different thresholds are set to judge buy/sell. Then weight the sub-signals to calculate the overall signal in the current timeframe. If the signal is greater than 0, it is judged as an uptrend. If the signal is less than 0, it is judged as a downtrend.

When judged as an uptrend, the strategy sets a tracking stop loss line near the previous higher point; when judged as a downtrend, it sets a tracking stop loss line near the previous lower point. This can dynamically adjust the stop loss level according to the actual price movement to achieve the purpose of risk control.

Advantages

The strategy integrates multiple indicators to judge the current trend direction, which improves the accuracy of judgment. At the same time, the strategy can adapt to different varieties and time frames with strong adaptability.

Most importantly, the strategy can dynamically adjust the stop loss line and adjust the risk control level according to the actual trend to hedge systemic risks. This is its biggest advantage.

Risks

The quality of the trend signal judgment directly affects the setting of the stop loss line. If the judgment is wrong, it may cause the stop loss level to be set too loose or too strict. In addition, the stop loss line cannot completely avoid the risk of market mutations.

The strategy also needs to balance the profit level and the stop loss distance. If the stop loss distance is too close, it may cause excessive frequency of stop loss; if the stop loss distance is too far, it cannot effectively control risks. This requires parameter optimization for different varieties and cycles.

Optimization Directions

Consider introducing machine learning algorithms to train models for judging trend directions using historical data to improve judgment accuracy.

Test different parameter combinations to optimize stop loss distance. For example, dynamically adjust ATR cycle parameters to adapt to changes in market volatility.

Volume energy indicators can also be combined to determine true trends and prevent signal errors caused by price-volume divergence.

Summary

The strategy judges the current trend direction by integrating multiple technical indicators, and accordingly dynamically adjusts the tracking stop loss line. It aims to improve the effectiveness of stop loss and control trading risks. The strategy idea is advanced and worth further optimization and verification. It is a multi-timeframe adaptive risk control strategy that can be referenced.


/*backtest
start: 2022-11-14 00:00:00
end: 2023-11-20 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

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

//@version=5
strategy("Jigga - Survival Level", shorttitle='Jigga - Survival Level', overlay=true)

doBackTesting = input(true, 'Run Back Testing')

entryCondition = false
exitCondition = false


ab21 =  14,  gh41 = ab21
gh42 = ab21, ju51 = 14
ki61 = ju51
lkolkp = true ,ab22 = 58
cd31 = 5 , ab23 = 42
aa12 = 29, cd32 = 26
op71 = 5,  aa11 = 12
aa13 = 9, op72 = 2.0
movnwx = false


kahachale(byju, h, l) =>
    mika = ta.change(h)
    awer = -ta.change(l)
    uikmhDM = na(mika) ? na : mika > awer and mika > 0 ? mika : 0
    wrtdfcDM = na(awer) ? na : awer > mika and awer > 0 ? awer : 0
    bbct = ta.rma(ta.tr, byju)
    uikmh = fixnan(100 * ta.rma(uikmhDM, byju) / bbct)
    wrtdfc = fixnan(100 * ta.rma(wrtdfcDM, byju) / bbct)
    [uikmh, wrtdfc]

trial(gh42, gh41, h, l) =>
    [uikmh, wrtdfc] = kahachale(gh42, h, l)
    uuolop = uikmh + wrtdfc
    trial = 100 * ta.rma(math.abs(uikmh - wrtdfc) / (uuolop == 0 ? 1 : uuolop), gh41)
    trial

_pr(src, byjugth) =>
    max = ta.highest(byjugth)
    min = ta.lowest(byjugth)
    100 * (src - max) / (max - min)


kyukarna(khulmkhula, mikaarwala, nichewala, bandhwala, partiwala) =>

    sig = trial(gh42, gh41, mikaarwala, nichewala)
    trialIncreasing = sig > ta.ema(sig, 5) ? lkolkp : movnwx

    rolkmn = ta.ema(bandhwala, aa11)
    psolkmn = ta.ema(bandhwala, aa12)
    ujghd = rolkmn - psolkmn
    wrtycv = ta.ema(ujghd, aa13)
    kimnjg = ujghd - wrtycv


    mikalilo = ta.rma(math.max(ta.change(bandhwala), 0), ab21)
    awerlilo = ta.rma(-math.min(ta.change(bandhwala), 0), ab21)
    lilo = awerlilo == 0 ? 100 : mikalilo == 0 ? 0 : 100 - 100 / (1 + mikalilo / awerlilo)
    juylknlilo = ta.ema(lilo, 3)


    rjuylkn = ta.ema(bandhwala, cd31)
    psjuylkn = ta.ema(bandhwala, cd32)

    percentR = _pr(bandhwala, ju51)
    juylknpercentR = ta.ema(percentR, 3)


    ad = bandhwala == mikaarwala and bandhwala == nichewala or mikaarwala == nichewala ? 0 : (2 * bandhwala - nichewala - mikaarwala) / (mikaarwala - nichewala) * partiwala
    kiloValue = math.sum(ad, ki61) / math.sum(partiwala, ki61)



    liiopn = ta.atr(op71)
    mikaliiopn = (mikaarwala + nichewala) / 2 - op72 * liiopn
    mika1liiopn = nz(mikaliiopn[1], mikaliiopn)
    mikaliiopn := bandhwala[1] > mika1liiopn ? math.max(mikaliiopn, mika1liiopn) : mikaliiopn
    dnliiopn = (mikaarwala + nichewala) / 2 + op72 * liiopn
    dn1liiopn = nz(dnliiopn[1], dnliiopn)
    dnliiopn := bandhwala[1] < dn1liiopn ? math.min(dnliiopn, dn1liiopn) : dnliiopn
    omnerliiopn = 1
    omnerliiopn := nz(omnerliiopn[1], omnerliiopn)
    omnerliiopn := omnerliiopn == -1 and bandhwala > dn1liiopn ? 1 : omnerliiopn == 1 and bandhwala < mika1liiopn ? -1 : omnerliiopn

    fitur = ujghd > 0 ? ujghd > wrtycv ? 1 : 0 : ujghd > wrtycv ? 0 : -1
    mitur = kimnjg >= 0 ? kimnjg > kimnjg[1] ? 1 : 0 : kimnjg > kimnjg[1] ? 0 : -1
    ritur = juylknlilo > ab22 ? 1 : juylknlilo < ab23 ? -1 : 0
    circuits = rjuylkn > psjuylkn ? 1 : -1
    trialPoints = trialIncreasing ? close > ta.ema(close, 3) ? 1 : -1 : 0
    virar = juylknpercentR > -ab23 ? 1 : juylknpercentR < -ab22 ? -1 : 0
    chikar = kiloValue > 0.1 ? 1 : kiloValue < -0.1 ? -1 : 0
    sitar = omnerliiopn


    p = fitur + mitur + ritur + circuits + trialPoints + virar + chikar + sitar

    p

currentP = kyukarna(open, high, low, close, volume)
currentPNew = currentP >= 0 and currentP[1] <= 0 ? 0 : currentP <= 0 and currentP[1] >= 0 ? 0 : currentP
colorPNew = currentPNew == 0 ? color.black : currentPNew >= 0 ? color.green : color.red
//plot(currentPNew, color=colorPNew, title='CurrentTimeFrame')

LTN = 0.0
LTN := nz(LTN) ? 0.0 : (currentPNew[1] < 0 and currentPNew >= 0) ? high * 1.005 : (currentPNew[1] > 0 and currentPNew <= 0) ? low * 0.995 : LTN[1]

LClr = color.green
LClr :=  (currentPNew[1] < 0 and currentPNew >= 0) ? color.green : (currentPNew[1] > 0 and currentPNew <= 0) ? color.red : LClr[1]

plot(LTN,color=LClr,title="Level", style=plot.style_circles)


entryCondition:= high > LTN and LClr == color.green ? lkolkp : movnwx
exitCondition:= low < LTN and LClr == color.red ? lkolkp : movnwx

tradeRunning = movnwx
tradeRunning := nz(tradeRunning) ? movnwx :  (not tradeRunning[1]) and entryCondition ? lkolkp : tradeRunning[1] and exitCondition ? movnwx : tradeRunning[1]


plotshape(tradeRunning and (not tradeRunning[1]) and (not doBackTesting), style=shape.labelup, location=location.belowbar, color=color.new(#00FF00, 50), size=size.tiny, title='Buy wrtycv', text='➹', textcolor=color.new(color.black,0))
plotshape((not tradeRunning) and tradeRunning[1] and (not doBackTesting), style=shape.labeldown, location=location.abovebar, color=color.new(#FF0000, 50), size=size.tiny, title='Sell wrtycv', text='➷', textcolor=color.new(color.white, 0))


if  entryCondition  and doBackTesting
    strategy.entry(id="Buy",direction=strategy.long)

if exitCondition and doBackTesting
    strategy.close(id="Buy")



More