Strategi Pembalikan Julat Tidur

Penulis:ChaoZhang, Tarikh: 2023-10-30 10:54:00
Tag:

img

Ringkasan

Strategi pembalikan julat tidak aktif menggunakan tempoh turun naik turun naik sebagai isyarat kemasukan dan bertujuan untuk mendapat keuntungan apabila turun naik naik lagi. Ia mengenal pasti situasi di mana harga terkandung dalam julat tidak aktif yang sempit dan menangkap trend harga yang akan datang. Strategi ini berfungsi dengan baik apabila turun naik semasa rendah tetapi penembusan dijangka.

Logika Strategi

Strategi pertama mengenal pasti julat tidak aktif, iaitu apabila harga terkandung dalam julat harga hari dagangan sebelumnya. Ini menunjukkan turun naik telah berkurangan berbanding beberapa hari yang lalu. Kami memeriksa sama ada tinggi hari semasa < tinggi n hari yang lalu (biasanya 4 hari) dan rendah hari semasa > rendah n hari yang lalu untuk layak sebagai julat tidak aktif.

Apabila julat tidak aktif dikenal pasti, strategi meletakkan dua pesanan menunggu - henti membeli berhampiran bahagian atas julat dan henti menjual berhampiran bahagian bawah julat. Ia kemudian menunggu harga untuk memecahkan julat sama ada ke atas atau ke bawah. Jika harga pecah ke atas, pesanan beli dicetuskan untuk pergi panjang. Jika harga pecah ke bawah, pesanan jual dicetuskan untuk pergi pendek.

Selepas masuk, perintah stop loss dan take profit diletakkan. Stop loss mengawal risiko penurunan dan mengambil keuntungan menutup perdagangan untuk keuntungan. Stop loss diletakkan pada jarak % dari harga masuk seperti yang ditakrifkan dalam parameter risiko. Take profit diletakkan pada jarak yang sama dengan saiz julat tidur kerana kita mengharapkan harga bergerak serupa dengan turun naik sebelumnya.

Akhirnya, model saiz kedudukan pecahan tetap menguruskan saiz perdagangan. Ia meningkatkan saiz keuntungan dan mengurangkan saiz kerugian untuk meningkatkan pulangan yang diselaraskan risiko.

Kelebihan

Kelebihan strategi ini ialah:

  1. Mencatatkan trend yang akan datang dengan menggunakan penurunan turun naik sebagai isyarat.

  2. Perintah dua arah menangkap aliran naik atau penurunan.

  3. Stop loss dan mengambil keuntungan mengawal risiko perdagangan tunggal.

  4. Ukuran pecahan tetap meningkatkan kecekapan modal.

  5. Logik yang mudah dilaksanakan.

Risiko

Risiko yang perlu dipertimbangkan ialah:

  1. Arah yang salah jika jarak tidak jelas.

  2. Penembusan mungkin hanya pembalikan jangka pendek, bukan trend yang kekal.

  3. Berhenti kehilangan risiko yang diambil oleh pergerakan besar.

  4. Ukuran pecahan tetap boleh memperkuat kerugian apabila menambah perdagangan yang rugi.

  5. Prestasi yang buruk jika parameter tidak ditetapkan dengan betul.

Peluang Peningkatan

Beberapa cara untuk meningkatkan strategi:

  1. Tambah penapis seperti perbezaan untuk mengelakkan penyebaran palsu.

  2. Memperbaiki stop loss dengan kehilangan trailing atau order stop.

  3. Tambah penapis trend untuk mengelakkan entri yang bertentangan dengan trend.

  4. Mengoptimumkan nisbah pecahan tetap untuk risiko / ganjaran yang seimbang.

  5. Perhatikan pelbagai jangka masa untuk meningkatkan kelebihan.

  6. Menggunakan pembelajaran mesin untuk pengoptimuman parameter automatik.

Kesimpulan

Strategi pembalikan julat yang tidak aktif mempunyai logik dan potensi keuntungan yang jelas. Penyesuaian halus melalui pengoptimuman, pengurusan risiko dan penapisan isyarat dapat meningkatkan konsistensi lebih lanjut. Tetapi semua strategi pembalikan bermakna membawa risiko yang melekat dan saiz kedudukan perlu dikawal. Ia sesuai dengan peniaga yang biasa dengan taktik pembalikan dan mempunyai kesedaran risiko yang baik.


/*backtest
start: 2023-09-29 00:00:00
end: 2023-10-29 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/
// © gsanson66


//This code is based on the Narrow Range strategy
//Interactive Broker fees are applied on this strategy
//@version=5
strategy("NARROW RANGE BACKTESTING", shorttitle="NR BACKTESTING", overlay=true, initial_capital=1000, default_qty_type=strategy.fixed, commission_type=strategy.commission.percent, commission_value=0.18)


//--------------------------------FUNCTIONS------------------------------------//

//@function to print label
debugLabel(txt, color) =>
    label.new(bar_index, high, text = txt, color=color, style = label.style_label_lower_right, textcolor = color.black, size = size.small)

//@function which looks if the close date of the current bar falls inside the date range
inBacktestPeriod(start, end) => (time >= start) and (time <= end)


//--------------------------------USER INPUTS------------------------------------//

//Narrow Range Length 
nrLength = input.int(4, minval=2, title="Narrow Range Length", group="Strategy parameters")
//Risk Management
stopLossInput = input.float(0.5, title="Stop Loss (in percentage of reference range)", group="Strategy parameters")
//Money Management
fixedRatio = input.int(defval=400, minval=1, title="Fixed Ratio Value ($)", group="Money Management")
increasingOrderAmount = input.int(defval=200, minval=1, title="Increasing Order Amount ($)", group="Money Management")
//Backtesting period
startDate = input(title="Start Date", defval=timestamp("1 Janv 2020 00:00:00"), group="Backtesting Period")
endDate = input(title="End Date", defval=timestamp("1 July 2024 00:00:00"), group="Backtesting Period")


//--------------------------------VARIABLES INITIALISATION--------------------------//
strategy.initial_capital = 50000
bool nr = na
var bool long = na
var bool short = na
var float stopPriceLong = na
var float stopLossLong = na
var float takeProfitLong = na
var float stopPriceShort = na
var float stopLossShort = na
var float takeProfitShort = na
var float takeProfit = na
var float stopLoss = na
bool inRange = na
int closedtrades = strategy.closedtrades
equity = math.abs(strategy.equity - strategy.openprofit)
var float capital_ref = strategy.initial_capital
var float cashOrder = strategy.initial_capital * 0.95


//------------------------------CHECKING SOME CONDITIONS ON EACH SCRIPT EXECUTION-------------------------------//

//Checking if the date belong to the range
inRange := true

//Checking performances of the strategy
if equity > capital_ref + fixedRatio
    spread = (equity - capital_ref)/fixedRatio
    nb_level = int(spread)
    increasingOrder = nb_level * increasingOrderAmount
    cashOrder := cashOrder + increasingOrder
    capital_ref := capital_ref + nb_level*fixedRatio
if equity < capital_ref - fixedRatio
    spread = (capital_ref - equity)/fixedRatio
    nb_level = int(spread)
    decreasingOrder = nb_level * increasingOrderAmount
    cashOrder := cashOrder - decreasingOrder
    capital_ref := capital_ref - nb_level*fixedRatio

//We check if a trade has been closed to cancel all previous orders
if closedtrades > closedtrades[1]
    strategy.cancel("Long")
    strategy.cancel("Short")
    stopPriceLong := na
    stopPriceShort := na

//Checking if we close all trades in case where we exit the backtesting period
if strategy.position_size!=0 and not inRange
    debugLabel("END OF BACKTESTING PERIOD : we close the trade", color=color.rgb(116, 116, 116))
    strategy.close_all()
    long := na
    short := na
    stopPriceLong := na
    stopLossLong := na
    takeProfitLong := na
    stopPriceShort := na
    stopLossShort := na
    takeProfitShort := na
    takeProfit := na
    stopLoss := na

//----------------------------------FINDING NARROW RANGE DAY------------------------------------------//

// We find the Narrow Range Day
if low > low[nrLength] and high < high[nrLength]
    nr := true 


//------------------------------------STOP ORDERS--------------------------------------------//

// We handle plotting of stop orders and cancellation of other side order if one order is triggered
if strategy.position_size > 0 and not na(stopPriceLong) and not na(stopPriceShort)
    long := true
    strategy.cancel("Short")
    stopPriceLong := na
    stopPriceShort := na
    takeProfit := takeProfitLong
    stopLoss := stopLossLong
if strategy.position_size < 0 and not na(stopPriceLong) and not na(stopPriceShort)
    short := true
    strategy.cancel("Long") 
    stopPriceLong := na
    stopPriceShort := na
    takeProfit := takeProfitShort
    stopLoss := stopLossShort


//------------------------------------STOP LOSS & TAKE PROFIT--------------------------------//

// If an order is triggered we plot TP and SL
if not na(takeProfit) and not na(stopLoss) and long
    if high >= takeProfit and closedtrades == closedtrades[1] + 1
        takeProfit := na
        stopLoss := na
        long := na
    if low <= stopLoss and closedtrades == closedtrades[1] + 1
        takeProfit := na
        stopLoss := na
        long := na
if not na(takeProfit) and not na(stopLoss) and short
    if high >= stopLoss and closedtrades == closedtrades[1] + 1
        takeProfit := na
        stopLoss := na
        short := na
    if low <= takeProfit and closedtrades == closedtrades[1] + 1
        takeProfit := na
        stopLoss := na
        short := na


//-----------------------------LONG/SHORT CONDITION-------------------------//

// Conditions to create two stop orders (one for Long and one for Short) and SL & TP calculation
if nr and inRange and strategy.position_size == 0
    stopPriceLong := high[4]
    takeProfitLong := high[4] + (high[4] - low[4])
    stopLossLong := high[4] - (high[4] - low[4])*stopLossInput
    qtyLong = cashOrder/stopPriceLong
    strategy.entry("Long", strategy.long, qtyLong, stop=stopPriceLong)
    strategy.exit("Exit Long", "Long", limit=takeProfitLong ,stop=stopLossLong)
    stopPriceShort := low[4]
    takeProfitShort := low[4] - (high[4] - low[4])
    stopLossShort := low[4] + (high[4] - low[4])*stopLossInput
    qtyShort = cashOrder/stopPriceShort
    strategy.entry("Short", strategy.short, qtyShort, stop=stopPriceShort)
    strategy.exit("Exit Short", "Short", limit=takeProfitShort ,stop=stopLossShort)


//--------------------------PLOTTING ELEMENT----------------------------//

plotshape(nr, "NR", shape.arrowdown, location.abovebar, color.rgb(255, 132, 0), text= "NR4", size=size.huge)
plot(stopPriceLong, "Stop Order", color.blue, 3, plot.style_linebr)
plot(stopPriceShort, "Stop Order", color.blue, 3, plot.style_linebr)
plot(takeProfit, "Take Profit", color.green, 3, plot.style_linebr)
plot(stopLoss, "Stop Loss", color.red, 3, plot.style_linebr)

Lebih lanjut