资源加载中... loading...

EMA+AROON+ASH指标策略

Author: 雨幕(youquant), Date: 2022-05-31 09:49:15
Tags: EMAAROONASH

重要

我的目的是找到最好的策略并丢弃不好的策略,因为你可以使用这个自动策略自己检查,结果是。。。 错误的策略,避免

简介和清单

为了从YouTube上重现TRADE KING的战略,该脚本由3个指标组成。 第一个指标是EMA(指数移动平均值)(200个周期)。 其次是经典的Aroon指示器。 第三个是绝对强度直方图,由“jiehonglim”命名,名称是“绝对强度直方图v2


/*backtest
start: 2021-12-01 00:00:00
end: 2022-05-30 00:00:00
period: 4h
basePeriod: 15m
exchanges: [{"eid":"Futures_CTP","currency":"FUTURES"}]
args: [["ContractType","i2209",360008]]
*/

// © JoseMetal
//@version=5

// 

//== Constantes
c_verde_radiactivo = color.rgb(0, 255, 0, 0)
c_verde            = color.rgb(0, 128, 0, 0)
c_verde_oscuro     = color.rgb(0, 80, 0, 0)
c_rojo_radiactivo  = color.rgb(255, 0, 0, 0)
c_rojo             = color.rgb(128, 0, 0, 0)
c_rojo_oscuro      = color.rgb(80, 0, 0, 0)




//== Funciones



//== Declarar estrategia y período de testeo
//strategy("EMA + AROON + ASH (TRADE KING's STRATEGY)", shorttitle="EMA + AROON + ASH (TRADE KING's STRATEGY)", overlay=true, initial_capital=10000, pyramiding=0, default_qty_value=10, default_qty_type=strategy.percent_of_equity, commission_type=strategy.commission.percent, commission_value=0.00075, max_labels_count=500, max_bars_back=1000)
//fecha_inicio     = input.time(timestamp("1 Jan 2000"), title="• Start date", group="Test period", inline="periodo_de_pruebas")
vela_en_fecha    = true
posicion_abierta = strategy.position_size != 0
LONG_abierto     = strategy.position_size > 0
SHORT_abierto    = strategy.position_size < 0

//== Condiciones de entrada y salida de estrategia
GRUPO_P           = "Positions"
P_permitir_LONGS  = input.bool(title="多头", group=GRUPO_P, defval=true)
P_permitir_SHORTS = input.bool(title="空头", group=GRUPO_P, defval=true)

GRUPO_TPSL   = "TP y SL"
TPSL_TP_pivot_lookback = input.int(title="SL 枢轴点回看周期 / 乘数. TP", group=GRUPO_TPSL, defval=20, minval=1, step=1, inline="tp_sl")
TPSL_SL_mult = input.float(title="TPSL_SL乘数", group=GRUPO_TPSL, defval=2.0, minval=0.1, step=0.2, inline="tp_sl")



//== Inputs de indicadores
// EMA
GRUPO_EMA  = "Exponential Moving Average (EMA)"
EMA_length = input.int(200, minval=1, title="周期", group=GRUPO_EMA)
EMA_src    = input(close, title="数据源", group=GRUPO_EMA)
EMA = ta.ema(EMA_src, EMA_length)

// Aroon
GRUPO_Aroon = "Aroon"
Aroon_length = input.int(title="周期", group=GRUPO_Aroon, defval=20, minval=1)
Aroon_upper = 100 * (ta.highestbars(high, Aroon_length+1) + Aroon_length) / Aroon_length
Aroon_lower = 100 * (ta.lowestbars(low, Aroon_length+1) + Aroon_length) / Aroon_length

// ASH
GRUPO_ASH = "Absolute Strength Histogram v2 | jh"
ASH_Length = input(9, title='Evaluation周期', group=GRUPO_ASH)
ASH_Smooth = input(3, title='Smoothing周期', group=GRUPO_ASH)
ASH_src = input(close, title='数据源')
ASH_Mode = input.string(title='指标算法', defval='ADX', options=['RSI', 'STOCHASTIC', 'ADX'])
ASH_ma_type = input.string(title='均线类型', defval='EMA', options=['ALMA', 'EMA', 'WMA', 'SMA', 'SMMA', 'HMA'])
ASH_alma_offset = input.float(defval=0.85, title='仅限Arnaud Legoux(ALMA)-偏移值', minval=0, step=0.01)
ASH_alma_sigma = input.int(defval=6, title='仅限Arnaud Legoux(ALMA)-西格玛值', minval=0)

_MA(type, src, len) =>
    float result = 0
    if type == 'SMA'  // Simple
        result := ta.sma(src, len)
        result
    if type == 'EMA'  // Exponential
        result := ta.ema(src, len)
        result
    if type == 'WMA'  // Weighted
        result := ta.wma(src, len)
        result
    if type == 'SMMA'  // Smoothed
        w = ta.wma(src, len)
        result := na(w[1]) ? ta.sma(src, len) : (w[1] * (len - 1) + src) / len
        result
    if type == 'HMA'  // Hull
        result := ta.wma(2 * ta.wma(src, len / 2) - ta.wma(src, len), math.round(math.sqrt(len)))
        result
    if type == 'ALMA'  // Arnaud Legoux
        result := ta.alma(src, len, ASH_alma_offset, ASH_alma_sigma)
        result
    result

Price1 = _MA('SMA', ASH_src, 1)
Price2 = _MA('SMA', ASH_src[1], 1)

// RSI
Bulls0 = 0.5 * (math.abs(Price1 - Price2) + Price1 - Price2)
Bears0 = 0.5 * (math.abs(Price1 - Price2) - (Price1 - Price2))

// STOCHASTIC
Bulls1 = Price1 - ta.lowest(Price1, ASH_Length)
Bears1 = ta.highest(Price1, ASH_Length) - Price1

// ADX
Bulls2 = 0.5 * (math.abs(high - high[1]) + high - high[1])
Bears2 = 0.5 * (math.abs(low[1] - low) + low[1] - low)

Bulls = ASH_Mode == 'RSI' ? Bulls0 : ASH_Mode == 'STOCHASTIC' ? Bulls1 : Bulls2
Bears = ASH_Mode == 'RSI' ? Bears0 : ASH_Mode == 'STOCHASTIC' ? Bears1 : Bears2
AvgBulls = _MA(ASH_ma_type, Bulls, ASH_Length)
AvgBears = _MA(ASH_ma_type, Bears, ASH_Length)

SmthBulls = _MA(ASH_ma_type, AvgBulls, ASH_Smooth)
SmthBears = _MA(ASH_ma_type, AvgBears, ASH_Smooth)

difference = math.abs(SmthBulls - SmthBears)



//== Cálculo de condiciones
EMA_alcista = close > EMA
EMA_bajista = close < EMA
Aroon_cruce_alcista = ta.crossover(Aroon_upper, Aroon_lower)
Aroon_cruce_bajista = ta.crossunder(Aroon_upper, Aroon_lower)
ASH_alcista = SmthBulls > SmthBears
ASH_bajista = SmthBulls < SmthBears

//== Entrada (deben cumplirse todas para entrar)
longCondition1  = EMA_alcista
longCondition2  = Aroon_cruce_alcista
longCondition3  = ASH_alcista
long_conditions = longCondition1 and longCondition2 and longCondition3
entrar_en_LONG  = P_permitir_LONGS and long_conditions and vela_en_fecha and not posicion_abierta

shortCondition1  = EMA_bajista
shortCondition2  = Aroon_cruce_bajista
shortCondition3  = ASH_bajista
short_conditions = shortCondition1 and shortCondition2 and shortCondition3
entrar_en_SHORT  = P_permitir_SHORTS and short_conditions and vela_en_fecha and not posicion_abierta

var LONG_stop_loss    = 0.0
var LONG_take_profit  = 0.0
var SHORT_stop_loss   = 0.0
var SHORT_take_profit = 0.0

//psl = ta.pivotlow(TPSL_TP_pivot_lookback, TPSL_TP_pivot_lookback)
//psh = ta.pivothigh(TPSL_TP_pivot_lookback, TPSL_TP_pivot_lookback)
psl = ta.lowest(TPSL_TP_pivot_lookback)
psh = ta.highest(TPSL_TP_pivot_lookback)

if (entrar_en_LONG)
    LONG_stop_loss   := psl - close*0.001
    LONG_take_profit := close + ((close - LONG_stop_loss) * TPSL_SL_mult)
    strategy.entry("+ Long", strategy.long)
    strategy.exit("- Long", "+ Long", limit=LONG_take_profit, stop=LONG_stop_loss)

if (entrar_en_SHORT)
    SHORT_stop_loss   := psh + close*0.001
    SHORT_take_profit := close - ((SHORT_stop_loss - close) * TPSL_SL_mult)
    strategy.entry("+ Short", strategy.short)
    strategy.exit("- Short", "+ Short", limit=SHORT_take_profit, stop=SHORT_stop_loss)



//== Ploteo en pantalla
// EMA
plot(EMA, color=color.white, linewidth=2)

// Símbolo de entrada (entre o no en compra)
bgcolor = color.new(color.black, 100)

if (entrar_en_LONG or entrar_en_SHORT)
    bgcolor := color.new(color.green, 90)

bgcolor(bgcolor)

// Precio de compra, Take Profit, Stop Loss y relleno
avg_position_price_plot = plot(series=posicion_abierta ? strategy.position_avg_price : na, color=color.new(color.white, 25), style=plot.style_linebr, linewidth=2, title="Precio Entrada")

LONG_tp_plot            = plot(LONG_abierto and LONG_take_profit > 0.0 ? LONG_take_profit : na, color=color.new(color.lime, 25), style=plot.style_linebr, linewidth=3, title="LONG Take Profit")
LONG_sl_plot            = plot(LONG_abierto and LONG_stop_loss > 0.0 ? LONG_stop_loss : na, color=color.new(color.red, 25), style=plot.style_linebr, linewidth=3, title="Long Stop Loss")
fill(avg_position_price_plot, LONG_tp_plot, color=color.new(color.olive, 85))
fill(avg_position_price_plot, LONG_sl_plot, color=color.new(color.maroon, 85))

SHORT_tp_plot            = plot(SHORT_abierto and SHORT_take_profit > 0.0 ? SHORT_take_profit : na, color=color.new(color.lime, 25), style=plot.style_linebr, linewidth=3, title="SHORT Take Profit")
SHORT_sl_plot            = plot(SHORT_abierto and SHORT_stop_loss > 0.0 ? SHORT_stop_loss : na, color=color.new(color.red, 25), style=plot.style_linebr, linewidth=3, title="Short Stop Loss")
fill(avg_position_price_plot, SHORT_tp_plot, color=color.new(color.olive, 85))
fill(avg_position_price_plot, SHORT_sl_plot, color=color.new(color.maroon, 85))


相关内容

更多内容