Sumber dimuat naik... memuat...

Strategi Dagangan Kuantitatif Berdasarkan Ichimoku dengan Pelbagai Isyarat

Penulis:ChaoZhang, Tarikh: 2023-11-13 10:24:35
Tag:

img

Ringkasan

Strategi ini mengintegrasikan penunjuk Ichimoku Kinko Hyo dan pelbagai penunjuk teknikal lain untuk menggabungkan pelbagai isyarat perdagangan, untuk memanfaatkan kelebihan sistem Ichimoku sambil mengesahkan entri dengan pelbagai isyarat untuk menapis isyarat palsu dengan berkesan dan mengawal risiko sambil mengejar kadar kemenangan yang tinggi.

Logika Strategi

Komponen utama strategi ini ialah:

  1. Pengiraan penunjuk Ichimoku Kinko Hyo termasuk Tenkan-sen, Kijun-sen, Senkou Span A, Senkou Span B, dan Kumo.

  2. Pelbagai penapis termasuk Kumo, Kijun, MACD, RSI, Fractals, SuperTrend, Parabolic SAR dan ADX untuk mengesahkan arah trend dan mengelakkan whipsaws.

  3. Pelbagai isyarat perdagangan termasuk penembusan harga, hubungan Chikou, hubungan Tenkan & Kijun yang berjumlah 23 isyarat Ichimoku, ditambah isyarat dari MACD, RSI, Fractals dll untuk mengenal pasti peluang perdagangan yang berpotensi.

  4. Penapis dua peringkat untuk isyarat masuk untuk mengelakkan terperangkap semasa masuk.

  5. Penapis dua peringkat untuk isyarat keluar yang serupa dengan penapis masuk.

  6. Gabungan isyarat perdagangan yang dipilih dan pengesahan penapis untuk membuat keputusan perdagangan akhir.

  7. Pengaturan mengambil keuntungan dan menghentikan kerugian boleh dikonfigurasi.

  8. Tetapan tempoh backtest.

Kelebihan

Kelebihan strategi ini termasuk:

  1. Menggunakan indikator dan isyarat Ichimoku sambil menggabungkan trend berikut dan penapisan isyarat.

  2. Mengelakkan perangkap semasa masuk melalui penapis dua peringkat dan kawalan risiko yang berkesan.

  3. Menyediakan pelbagai isyarat yang boleh diperdagangkan yang dapat disesuaikan dengan keadaan pasaran yang berbeza.

  4. Menawarkan pelbagai penapis pilihan yang boleh dioptimumkan untuk stok individu.

  5. Ambil keuntungan dan hentikan kerugian membantu mengunci keuntungan dan mengawal risiko.

  6. Tempoh ujian belakang yang memudahkan pengoptimuman strategi.

Risiko

Risiko strategi ini termasuk:

  1. Isyarat Ichimoku mungkin lambat dan terlepas peluang jangka pendek.

  2. Penapisan berlebihan boleh menyebabkan entri yang tidak pasti. Parameter penapisan mungkin perlu diselaraskan.

  3. Stop loss statik mungkin tidak menyesuaikan diri dengan baik dengan tindakan harga yang kompleks.

  4. Batuk ujian keterbatasan dalam tepat mensimulasikan pasaran hidup.

Arahan pengoptimuman

Cara yang mungkin untuk mengoptimumkan strategi:

  1. Sesuaikan parameter Ichimoku seperti tempoh Tenkan untuk perdagangan jangka pendek.

  2. Uji gabungan isyarat untuk mencari kecocokan terbaik untuk stok individu.

  3. Mengoptimumkan parameter penapis untuk mengimbangi kesan penapis dan kepastian kemasukan.

  4. Cuba stop loss dinamik untuk menyesuaikan diri dengan perubahan pasaran.

  5. Gunakan tempoh backtest yang lebih lama atau tanda data untuk simulasi yang lebih tepat.

  6. Tambah saiz kedudukan untuk penggunaan modal yang lebih baik.

  7. Membina dalam pengoptimuman parameter automatik untuk penyesuaian yang lebih pintar.

Kesimpulan

Strategi ini menggabungkan indikator dan isyarat Ichimoku dengan penapis tambahan dan pengesahan dari penunjuk teknikal lain, mewujudkan sistem kuantitatif yang menggabungkan trend berikut dan isyarat pecah. Ia sepenuhnya memanfaatkan kekuatan Ichimoku sambil menggunakan modul parameter untuk penyesuaian dan pengoptimuman untuk menyesuaikan diri dengan pasaran yang berubah. Ujian dan pengoptimuman lanjut mungkin akan membawa kepada keuntungan yang kuat dan stabil.


/*backtest
start: 2023-10-13 00:00:00
end: 2023-11-12 00:00:00
period: 1h
basePeriod: 15m
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/
// © ramsay09
//@version=4
strategy(title="The Strategy - Ichimoku Kinko Hyo and more",shorttitle="Strategy ", overlay=true)


backtest        = input(title= "Backtest (no comment-string)", type= input.bool, defval= false)
entry_type      = input("Both", title= "Long/Short Entry", options= ["Both", "Long", "Short"])

shared_param    = input(false, title= " Shared Filter and Entry Parameters :", type= input.bool)

fr_period       = input(2, title= "Fractals Period (Filter/Entry)", minval= 1)
rsi_period      = input(14, title= "RSI Period (Filter/Entry)", minval= 1)
mult            = input(2, type= input.float, title= "SuperTrend multiplier (Filter/Entry)", minval= 1)
len             = input(5, type= input.integer, title= "SuperTrend length (Filter/Entry)", minval= 1)
start           = 0.02//input(0.02, title= "PSAR Start (Filter/Entry)", minval= 0)
inc             = 0.02//input(0.02, title= "PSAR Increment (Filter/Entry)", minval= 0)
max             = 0.2//input(.2, title= "PSAR Maximum (Filter/Entry)", minval= 0)
adx_period      = input(10, title= "ADX Period (Filter/Entry)", minval= 1)
adx_tres        = input(25, title= "ADX threshold (Filter/Entry)", minval= 1)


X_opt           = input("Price X Kumo sig", title="Signal", options= ["---", "Inside Bar sig", "Outside Bar sig", "Sandwich Bar sig", "Bar sig", "SMA50 sig", "RSI50 sig", 
                         "Fractals sig", "Parabolic SAR sig", "SuperTrend sig", "Price X Kijun sig", "Price X Kumo sig", "Kumo flip sig", 
                         "Price filtered Kumo flip sig",  "Chikou X Price sig", "Chikou X Kumo sig", "Price X Tenkan sig", "Tenkan X Kumo sig", 
                         "Tenkan X Kijun sig", "Kumo filtered Tenkan X Kijun sig", "CB/CS sig", "IB/IS sig", "B1/S1 sig", "B2/S2 sig"])

entry_f_1       = input("---", title="Entry filter 1", options= ["---", "SMA50 filter", "MACD filter", "RSI50 filter", "Fractals filter",
                         "SuperTrend filter", "Parabolic SAR filter", "Cloud filter", "Kijun filter", "ADX filter"])

entry_f_2       = input("---", title="Entry filter 2", options= ["---", "SMA50 filter", "MACD filter", "RSI50 filter", "Fractals filter", 
                         "SuperTrend filter", "Parabolic SAR filter", "Cloud filter", "Kijun filter", "ADX filter"])


exit_f_1        = input("---", title="Exit filter 1", options= ["---", "SMA50 filter", "MACD filter", "RSI50 filter", "Fractals filter",
                         "SuperTrend filter", "Parabolic SAR filter", "Cloud filter", "Kijun filter", "ADX filter"])

exit_f_2        = input("---", title="Exit filter 2", options= ["---", "SMA50 filter", "MACD filter", "RSI50 filter", "Fractals filter", 
                         "SuperTrend filter", "Parabolic SAR filter", "Cloud filter", "Kijun filter", "ADX filter"])



//-------------------- Ichimoku --------------------

TKlength            = 9 //input(9, "Tenkan-sen length", minval= 1)
KJlength            = 26 //input(26, "Kijun-sen length", minval= 1)
CSHSlength          = 26 //input(26, "Chikouspan length/horizontal shift", minval= 1)
SBlength            = 52 //input(52, "SenkouspanB length", minval= 1)
SAlength            = 26 //input(26, "SenkouspanA length", minval= 1)

// calculation
TK                  = avg(lowest(TKlength), highest(TKlength))
KJ                  = avg(lowest(KJlength), highest(KJlength))
CS                  = close
SB                  = avg(lowest(SBlength), highest(SBlength))
SA                  = avg(TK,KJ)

kumo_high   = max(SA[CSHSlength-1], SB[CSHSlength-1])
kumo_low    = min(SA[CSHSlength-1], SB[CSHSlength-1]) 


//------------------------------------- Filters and entry signals --------------------------------------

//---------------------- Kumo filter ------------------------

kumo_buy    = close > kumo_high
kumo_sell   = close < kumo_low

//--------------------- Kijun filter ----------------------

kijun_buy   = close > KJ
kijun_sell  = close < KJ

//----------------------- macd filter -----------------------

[macdLine_f, signalLine_f, histLine_f]  = macd(close, 12, 26, 9)

macd_buy                                = macdLine_f > signalLine_f
macd_sell                               = macdLine_f < signalLine_f

//---------------------- rsi filter and entry signal------------------------

rsi_f_buy                               = rsi(close, rsi_period) > 50
rsi_f_sell                              = rsi(close, rsi_period) < 50

//---------------- Bill Williams Fractals (filter and entry signal) -----------------

up_fr           = pivothigh(fr_period, fr_period)
dn_fr           = pivotlow(fr_period, fr_period)

fractal_up_v    = valuewhen(up_fr, high[fr_period],0) 
fractal_dn_v    = valuewhen(dn_fr, low[fr_period],0)

fr_upx          = high > fractal_up_v
fr_dnx          = low < fractal_dn_v

//-------------------- SuperTrend filter and entry signal ---------------------

[SuperTrend, Dir]   = supertrend(mult, len)

sup_buy     = close > SuperTrend
sup_sell    = close < SuperTrend

//--------------------- Heikin Ashi -----------------------

//heikin_close = security(heikinashi(syminfo.tickerid), timeframe.period, close)
//heikin_open  = security(heikinashi(syminfo.tickerid), timeframe.period, open)

//h_buy       = heikin_close[1] > heikin_open[1]
//h_sell      = heikin_close[1] < heikin_open[1]

//----------------- Parabolic SAR Signal (pb/ps) and filter -------------------

psar_buy        = high > sar(start, inc, max)[0]
psar_sell       = low < sar(start, inc, max)[0]

//-------------------------- ADX filter ---------------------------

[diplus_f, diminus_f, adx_f]              = dmi(adx_period, adx_period)

//-------------------------- SMA50 filter and entry---------------------------

sma50_buy       = close[2] > sma(close, 50)
sma50_sell      = close[2] < sma(close, 50)


//-------------------------- entry filter -------------------------------

//entry buy filter 1 options
entry_filter_buy_1 = 
     entry_f_1 == "---" ? true :
     entry_f_1 == "MACD filter" ? macd_buy : 
     entry_f_1 == "RSI50 filter" ? rsi_f_buy : 
     entry_f_1 == "Fractals filter" ? fr_upx : 
     entry_f_1 == "SuperTrend filter" ? sup_buy : 
     entry_f_1 == "Parabolic SAR filter" ? psar_buy : 
     entry_f_1 == "Cloud filter" ? kumo_buy : 
     entry_f_1 == "Kijun filter" ? kijun_buy : 
     entry_f_1 == "SMA50 filter" ? sma50_buy : 
     entry_f_1 == "ADX filter" ? adx_f > 25 : true

//entry sell filter 1 options
entry_filter_sell_1 = 
     entry_f_1 == "---" ? true :
     entry_f_1 == "MACD filter" ? macd_sell : 
     entry_f_1 == "RSI50 filter" ? rsi_f_sell : 
     entry_f_1 == "Fractals filter" ? fr_dnx : 
     entry_f_1 == "SuperTrend filter" ? sup_sell : 
     entry_f_1 == "Parabolic SAR filter" ? psar_sell :
     entry_f_1 == "Cloud filter" ? kumo_sell : 
     entry_f_1 == "Kijun filter" ? kijun_sell : 
     entry_f_1 == "SMA50 filter" ? sma50_sell : 
     entry_f_1 == "ADX filter" ? adx_f > 25 : true


//entry buy filter 2 options
entry_filter_buy_2 = 
     entry_f_2 == "---" ? true :
     entry_f_2 == "MACD filter" ? macd_buy : 
     entry_f_2 == "RSI50 filter" ? rsi_f_buy : 
     entry_f_2 == "Fractals filter" ? fr_upx : 
     entry_f_2 == "SuperTrend filter" ? sup_buy : 
     entry_f_2 == "Parabolic SAR filter" ? psar_buy :
     entry_f_2 == "Cloud filter" ? kumo_buy : 
     entry_f_2 == "Kijun filter" ? kijun_buy : 
     entry_f_2 == "SMA50 filter" ? sma50_buy : 
     entry_f_2 == "ADX filter" ? adx_f > 25 : true

//entry sell filter 2 options
entry_filter_sell_2 = 
     entry_f_2 == "---" ? true :
     entry_f_2 == "MACD filter" ? macd_sell : 
     entry_f_2 == "RSI50 filter" ? rsi_f_sell : 
     entry_f_2 == "Fractals filter" ? fr_dnx : 
     entry_f_2 == "SuperTrend filter" ? sup_sell : 
     entry_f_2 == "Parabolic SAR filter" ? psar_sell :
     entry_f_2 == "Cloud filter" ? kumo_sell : 
     entry_f_2 == "Kijun filter" ? kijun_sell : 
     entry_f_2 == "SMA50 filter" ? sma50_sell : 
     entry_f_2 == "ADX filter" ? adx_f > 25 : true


//------------------------- exit filter -----------------------

//exit buy filter 1 options
exit_filter_buy_1 = 
     exit_f_1 == "---" ? false :
     exit_f_1 == "MACD filter" ? macd_buy : 
     exit_f_1 == "RSI50 filter" ? rsi_f_buy : 
     exit_f_1 == "Fractals filter" ? fr_upx : 
     exit_f_1 == "SuperTrend filter" ? sup_buy : 
     exit_f_1 == "Parabolic SAR filter" ? psar_buy :
     exit_f_1 == "Cloud filter" ? kumo_buy : 
     exit_f_1 == "Kijun filter" ? kijun_buy : 
     exit_f_1 == "SMA50 filter" ? sma50_buy : 
     exit_f_1 == "ADX filter" ? adx_f > 25 : false

//exit sell filter 1 options
exit_filter_sell_1 = 
     exit_f_1 == "---" ? false :
     exit_f_1 == "MACD filter" ? macd_sell : 
     exit_f_1 == "RSI50 filter" ? rsi_f_sell : 
     exit_f_1 == "Fractals filter" ? fr_dnx : 
     exit_f_1 == "SuperTrend filter" ? sup_sell : 
     exit_f_1 == "Parabolic SAR filter" ? psar_sell :
     exit_f_1 == "Cloud filter" ? kumo_sell : 
     exit_f_1 == "Kijun filter" ? kijun_sell : 
     exit_f_1 == "SMA50 filter" ? sma50_sell : 
     exit_f_1 == "ADX filter" ? adx_f > 25 : false


//exit buy filter 2 options
exit_filter_buy_2 = 
     exit_f_2 == "---" ? false :
     exit_f_2 == "MACD filter" ? macd_buy : 
     exit_f_2 == "RSI50 filter" ? rsi_f_buy : 
     exit_f_2 == "Fractals filter" ? fr_upx : 
     exit_f_2 == "SuperTrend filter" ? sup_buy : 
     exit_f_2 == "Parabolic SAR filter" ? psar_buy :
     exit_f_2 == "Cloud filter" ? kumo_buy : 
     exit_f_2 == "Kijun filter" ? kijun_buy : 
     exit_f_2 == "SMA50 filter" ? sma50_buy : 
     exit_f_2 == "ADX filter" ? adx_f > 25 : false

//exit sell filter 2 options
exit_filter_sell_2 = 
     exit_f_2 == "---" ? false :
     exit_f_2 == "MACD filter" ? macd_sell : 
     exit_f_2 == "RSI50 filter" ? rsi_f_sell : 
     exit_f_2 == "Fractals filter" ? fr_dnx : 
     exit_f_2 == "SuperTrend filter" ? sup_sell : 
     exit_f_2 == "Parabolic SAR filter" ? psar_sell :
     exit_f_2 == "Cloud filter" ? kumo_sell : 
     exit_f_2 == "Kijun filter" ? kijun_sell : 
     exit_f_2 == "SMA50 filter" ? sma50_sell : 
     exit_f_2 == "ADX filter" ? adx_f > 25 : false



//----------------------- i-o-s signals ------------------------

i_bar_buy       = high[1] < high[2] and low[1] > low[2] and close > high[1]
i_bar_sell      = high[1] < high[2] and low[1] > low[2] and close < low[1]

o_bar_buy       = high[1] > high[2] and low[1] < low[2] and high > high[1]
o_bar_sell      = high[1] > high[2] and low[1] < low[2] and low < low[1]

s_bar_buy       = high[2] < high[3] and low[2] > low[3] and high[1] > high[2] and low[1] < low[2] and high > high[1]
s_bar_sell      = high[2] < high[3] and low[2] > low[3] and high[1] > high[2] and low[1] < low[2] and low < low[1]


//----------------- Ichimoku Signal B1/S1 -----------------

buy_strong_B1   = (TK >= KJ) and close > kumo_high and CS > high[(26-1)] and CS > kumo_high[26-1] and SA > SB
sell_strong_S1  = (TK <= KJ) and close < kumo_low and CS < low[(26-1)] and CS < kumo_low[26-1] and SA < SB

var buy_sig     = true
var sell_sig    = true

B1_a  = buy_strong_B1 and buy_sig
S1_a  = sell_strong_S1 and sell_sig

if  sell_strong_S1 
    buy_sig    := true, sell_sig := false
if buy_strong_B1 
    sell_sig   := true, buy_sig := false
    
    
    
//----------------- Ichimoku Signal B2/S2 -----------------

buy_strong_B2    = (TK >= KJ) and close > kumo_high and CS > high[26-1]
sell_strong_S2   = (TK <= KJ) and close < kumo_low  and CS < low[26-1]

var buy_sig_B2   = true
var sell_sig_S2  = true

B2_a  = buy_strong_B2 and buy_sig_B2
S2_a  = sell_strong_S2 and sell_sig_S2

if  sell_strong_S2 
    buy_sig_B2    := true, sell_sig_S2 := false
if buy_strong_B2 
    sell_sig_S2   := true, buy_sig_B2 := false    



//---------------------------- Confluence Signal ----------------------------

long_short_trig     = 7 //input(7, type= input.float, title= "Confluence signal trigger Level", step= 0.1)
trig_gap_cbcs   = input(2, type= input.float, title= "CB/CS signal sesitivity", minval= 0, maxval= 6, step= 1)

//Indicators
// ma
sma1            = sma(close, 50)
sma2            = sma(close, 200)
ema1            = ema(close, 50)
ema2            = ema(close, 200)
[macdLine, signalLine, histLine]    = macd(close, 12, 26, 9)
rsi                                 = rsi(close, 14)
[diplus, diminus, adx]              = dmi(7, 7)
[superTrend, dir]                   = supertrend(2, 5)
//Klinger Oszillator
sv  = change(hlc3) >= 0 ? volume : -volume
kvo = ema(sv, 34) - ema(sv, 55)
sig = ema(kvo, 13)
//Vortex Indicator
VMP = sum( abs( high - low[1]), 14 )
VMM = sum( abs( low - high[1]), 14 )
STR = sum( atr(1), 14 )
VIP = VMP / STR
VIM = VMM / STR


//Signals
var float sma_sig_w     = na
var float ema_sig_w     = na
var float p_kj_sig_w    = na
var float tk_kj_sig_w   = na
var float B1_S1_sig_w   = na
var float B2_S2_sig_w   = na
var float psar_sig_w    = na
var float frac_sig_w    = na
var float macd_sig_w    = na
var float rsi_sig_w     = na
var float p_tk_sig_w    = na
var float dmi_sig_w     = na
var float klin_sig_w    = na
var float vort_sig_w    = na
var float sup_sig_w     = na


if sma1 > sma2 
    sma_sig_w := 1
else if sma1 < sma2
    sma_sig_w := 0

if ema1 > ema2 
    ema_sig_w := 1
else if ema1 < ema2
    ema_sig_w := 0

if close > KJ 
    p_kj_sig_w := 1
else if close < KJ
    p_kj_sig_w := 0

if TK > KJ 
    tk_kj_sig_w := 1
else if TK < KJ
    tk_kj_sig_w := 0

if buy_strong_B1 
    B1_S1_sig_w := 1
else if sell_strong_S1
    B1_S1_sig_w := 0

if buy_strong_B2 
    B2_S2_sig_w := 1
else if sell_strong_S2
    B2_S2_sig_w := 0

if high >= sar(start, inc, max)[0] 
    psar_sig_w := 1
else if low <= sar(start, inc, max)[0]
    psar_sig_w := 0

if high > fractal_up_v 
    frac_sig_w := 1
else if low < fractal_dn_v
    frac_sig_w := 0

if macdLine > signalLine
    macd_sig_w := 1
else if macdLine < signalLine
    macd_sig_w := 0

if rsi > 50 
    rsi_sig_w := 1
else if rsi < 50 
    rsi_sig_w := 0
    
if close[2] > TK 
    p_tk_sig_w := 1
else if close[2] < TK
    p_tk_sig_w := 0

if diplus > diminus 
    dmi_sig_w := 1
else if diplus < diminus
    dmi_sig_w := 0

if sig > 0 
    klin_sig_w := 1
else if sig < 0
    klin_sig_w := 0

if VIP > VIM 
    vort_sig_w := 1
else if VIP < VIM
    vort_sig_w := 0
    
if close > superTrend 
    sup_sig_w := 1
else if close < superTrend
    sup_sig_w := 0
    

bs_conf_sig     = sma_sig_w + ema_sig_w + p_kj_sig_w + tk_kj_sig_w + B1_S1_sig_w + B2_S2_sig_w + psar_sig_w + frac_sig_w + macd_sig_w + 
     rsi_sig_w + dmi_sig_w + klin_sig_w + vort_sig_w + sup_sig_w + p_tk_sig_w

long_c          = bs_conf_sig > long_short_trig + trig_gap_cbcs //with +- signal is less fluctuating
short_c         = bs_conf_sig < long_short_trig - trig_gap_cbcs



//---------------------------- Pure Ichimoku Confluence Signal ----------------------------

pic_l_s_trig        = 4 //input(4, type= input.float, title= "Ichimoku confluence signal trigger Level", step= 0.1)
trig_gap_ibis       = input(0, type= input.float, title= "IB/IS signal sesitivity", minval= 0, maxval= 3, step= 1)


//Signals
var float tkkh_sig_w    = na
var float csh_sig_w     = na
var float cskh_sig_w    = na
var float pkj_sig_w     = na
var float ptk_sig_w     = na
var float tkkj_sig_w    = na
var float sasb_sig_w    = na
var float ckh_sig_w     = na


if TK > kumo_high 
    tkkh_sig_w := 1
else if TK < kumo_low
    tkkh_sig_w := 0

if CS > high[(26-1)] 
    csh_sig_w := 1
else if CS < low[(26-1)]
    csh_sig_w := 0
    
if CS > kumo_high[26-1] 
    cskh_sig_w := 1
else if CS < kumo_low[26-1]
    cskh_sig_w := 0    

if close > TK 
    ptk_sig_w := 1
else if close < TK
    ptk_sig_w := 0

if close > KJ 
    pkj_sig_w := 1
else if close < KJ
    pkj_sig_w := 0

if TK > KJ 
    tkkj_sig_w := 1
else if TK < KJ
    tkkj_sig_w := 0

if SA > SB 
    sasb_sig_w := 1
else if SA < SB
    sasb_sig_w := 0

if close > kumo_high 
    ckh_sig_w := 1
else if close < kumo_low
    ckh_sig_w := 0
    

bs_pic_sig          = tkkh_sig_w + csh_sig_w + cskh_sig_w + ptk_sig_w + pkj_sig_w + tkkj_sig_w + sasb_sig_w + ckh_sig_w

long_pic            = bs_pic_sig > pic_l_s_trig + trig_gap_ibis
short_pic           = bs_pic_sig < pic_l_s_trig - trig_gap_ibis



//--------------------------- Entry Signal Options ---------------------------

var buy_sig_opt   = true
var sell_sig_opt  = true

// cross conditions for "Strong" bg's
var bool sasb_x  = true
if crossover(SA, SB) and low > kumo_high 
    sasb_x := true 

if crossunder(SA, SB) and high < kumo_low 
    sasb_x := false
    
    
var bool tkkj_x  = true
if crossover(TK, KJ) and TK > kumo_high and KJ > kumo_high
    tkkj_x := true 
    
if crossunder(TK, KJ) and TK < kumo_low and KJ < kumo_low
    tkkj_x := false

// buy signal options
opt_sig_buy = 
     X_opt == "---" ? na :
     X_opt == "Inside Bar sig" ? i_bar_buy : 
     X_opt == "Outside Bar sig" ? o_bar_buy : 
     X_opt == "Sandwich Bar sig" ? s_bar_buy : 
     X_opt == "Bar sig" ? close > high[1] : 
     X_opt == "SMA50 sig" ? close[2] > sma(close, 50) : 
     X_opt == "Fractals sig" ? fr_upx : 
     X_opt == "RSI50 sig" ? rsi_f_buy : 
     X_opt == "Parabolic SAR sig" ? psar_buy : 
     X_opt == "SuperTrend sig" ? sup_buy : 
     X_opt == "Price X Kijun sig" ? close > KJ : 
     X_opt == "Price X Kumo sig" ? close > kumo_high : 
     X_opt == "Kumo flip sig" ? SA > SB :  
     X_opt == "Price filtered Kumo flip sig" ? sasb_x and low > kumo_high : 
     X_opt == "Chikou X price sig" ? CS > high[(26-1)] : 
     X_opt == "Chikou X Kumo sig" ? CS > kumo_high[26-1] :  
     X_opt == "Price X Tenkan sig" ? close > TK : 
     X_opt == "Tenkan X Kumo sig" ? TK > kumo_high :  
     X_opt == "Tenkan X Kijun sig" ? TK > KJ : 
     X_opt == "Kumo filtered Tenkan X Kijun sig" ? tkkj_x and TK > kumo_high and KJ > kumo_high and TK > KJ : 
     X_opt == "CB/CS sig" ? long_c : 
     X_opt == "IB/IS sig" ? long_pic : 
     X_opt == "B1/S1 sig" ? buy_strong_B1 :  
     X_opt == "B2/S2 sig" ? buy_strong_B2 : na

// sell signal options
opt_sig_sell = 
     X_opt == "---" ? na : 
     X_opt == "Inside Bar sig" ? i_bar_sell : 
     X_opt == "Outside Bar sig" ? o_bar_sell : 
     X_opt == "Sandwich Bar sig" ? s_bar_sell : 
     X_opt == "Bar sig" ? close < low[1] : 
     X_opt == "SMA50 sig" ? close[2] < sma(close, 50) : 
     X_opt == "Fractals sig" ? fr_dnx : 
     X_opt == "RSI50 sig" ? rsi_f_sell : 
     X_opt == "Parabolic SAR sig" ? psar_sell : 
     X_opt == "SuperTrend sig" ? sup_sell : 
     X_opt == "Price X Kijun sig" ? close < KJ : 
     X_opt == "Price X Kumo sig" ? close < kumo_low : 
     X_opt == "Kumo flip sig" ? SA < SB : 
     X_opt == "Price filtered Kumo flip sig" ? not sasb_x and high < kumo_low :
     X_opt == "Chikou X price sig" ? CS < low[(26-1)] : 
     X_opt == "Chikou X Kumo sig" ? CS < kumo_high[26-1] : 
     X_opt == "Price X Tenkan sig" ? close < TK :
     X_opt == "Tenkan X Kumo sig" ? TK < kumo_low :
     X_opt == "Tenkan X Kijun sig" ? TK < KJ :
     X_opt == "Kumo filtered Tenkan X Kijun sig" ? not tkkj_x and TK < kumo_low and KJ < kumo_low and TK < KJ : 
     X_opt == "CB/CS sig" ? short_c : 
     X_opt == "IB/IS sig" ? short_pic : 
     X_opt == "B1/S1 sig" ? sell_strong_S1 : 
     X_opt == "B2/S2 sig" ? sell_strong_S2 : na

if  opt_sig_sell 
    buy_sig    := true, sell_sig_opt := false
if opt_sig_buy 
    sell_sig   := true, buy_sig_opt := false



//---------------------------- Take profit and stop loss ------------------------------

tp_en       = input(title= "Enable take profit", type= input.bool, defval= false)

qty_tp      = input(50, title= "Take profit - quantity of position (percent)", type= input.float, minval= 1, maxval= 100, step= 5)

tp_ticks    = input(1000, title= "Take profit - ticks", type= input.integer, minval= 0, step= 10)


sl_en       = input(title= "Enable stop loss", type= input.bool, defval= false)

sl_ticks    = input(1000, title= "Stop loss - ticks", type= input.integer, minval= 0, step= 10)


//----------------------- Backtest periode --------------------------------

start_year       = input(2018, "Start year")
start_month      = input(1, "Start month", minval= 1, maxval= 12)
start_day        = input(1, "Start day", minval= 1, maxval= 31)
period_start     = timestamp(start_year, start_month, start_day, 0, 0)

stop_year        = input(2021, "Stop year")
stop_month       = input(12, "Stop month", minval= 1, maxval= 12)
stop_day         = input(31, "Stop day", minval= 1, maxval= 31)
period_stop      = timestamp(stop_year, stop_month, stop_day, 0, 0)

backtest_period() => time >= period_start and time <= period_stop ? true : false



//--------------------- strategy entry ---------------------

long        = entry_type != "Short"
short       = entry_type != "Long"
not_both    = entry_type != "Both"

if not backtest 
    if long
        strategy.entry("os_buy", strategy.long, when = opt_sig_buy and entry_filter_buy_1 and entry_filter_buy_2, 
             comment= "")    


             
        strategy.close("os_buy", when = exit_filter_sell_1 or exit_filter_sell_2 or not_both ? opt_sig_sell : na
             , comment= "")
             
        strategy.exit("tpl", "os_buy", qty_percent= tp_en ? qty_tp : na, profit= tp_en ? tp_ticks : na, loss= sl_en ? sl_ticks : na)
    
    if short
        strategy.entry("os_sell",strategy.short, when = opt_sig_sell and entry_filter_sell_1 and entry_filter_sell_2, 
             comment= "")      


             
        strategy.close("os_sell", when = exit_filter_buy_1 or exit_filter_buy_2 or not_both ? opt_sig_buy : na
             , comment= "")
             
        strategy.exit("tps", "os_sell", qty_percent= tp_en ? qty_tp : na, profit= tp_en ? tp_ticks : na, loss= sl_en ? sl_ticks : na)


if backtest_period() and backtest 
    if long
        strategy.entry("os_buy", strategy.long, when = opt_sig_buy and entry_filter_buy_1 and entry_filter_buy_2)
        strategy.close("os_buy", when = exit_filter_sell_1 or exit_filter_sell_2 or not_both ? opt_sig_sell : na)
        strategy.exit("tpl", "os_buy", qty_percent= tp_en ? qty_tp : na, profit= tp_en ? tp_ticks : na, loss= sl_en ? sl_ticks : na)
    
    if short
        strategy.entry("os_sell",strategy.short, when = opt_sig_sell and entry_filter_sell_1 and entry_filter_sell_2)
        strategy.close("os_sell", when = exit_filter_buy_1 or exit_filter_buy_2 or not_both ? opt_sig_buy : na)
        strategy.exit("tps", "os_sell", qty_percent= tp_en ? qty_tp : na, profit= tp_en ? tp_ticks : na, loss= sl_en ? sl_ticks : na)



    

Lebih lanjut