Tradingview策略分享1)

加密货币市场使用效果较好,适用于5min分钟级别。

原理上基于vegas tunnel 和 ut boot 实现,

拥有广泛的可调参数,

!!!!!!!建议充分回测后再使用!!!!!!

截图展示下部分回测数据:均为屏摄

代码如下:(pine脚本)

//@version=4
strategy("策略测试V1.0 ", overlay = true)

// UT BOOT 部分 LONG

a = input(1,     title = "Long_Key Vaule. 'This changes the sensitivity'")
c = input(22,    title = "Long_ATR Period")
h = input(false, title = "Long_Signals from Heikin Ashi Candles")

xATR  = atr(c)
nLoss = a * xATR

src = h ? security(heikinashi(syminfo.tickerid), timeframe.period, close, lookahead = false) : close

xATRTrailingStop = 0.0
xATRTrailingStop := iff(src > nz(xATRTrailingStop[1], 0) and src[1] > nz(xATRTrailingStop[1], 0), max(nz(xATRTrailingStop[1]), src - nLoss),
   iff(src < nz(xATRTrailingStop[1], 0) and src[1] < nz(xATRTrailingStop[1], 0), min(nz(xATRTrailingStop[1]), src + nLoss), 
   iff(src > nz(xATRTrailingStop[1], 0), src - nLoss, src + nLoss)))
 
pos = 0   
pos :=	iff(src[1] < nz(xATRTrailingStop[1], 0) and src > nz(xATRTrailingStop[1], 0), 1,
   iff(src[1] > nz(xATRTrailingStop[1], 0) and src < nz(xATRTrailingStop[1], 0), -1, nz(pos[1], 0))) 
   
xcolor = pos == -1 ? color.red: pos == 1 ? color.green : color.blue 

ema   = ema(src,1)
above = crossover(ema, xATRTrailingStop)
below = crossover(xATRTrailingStop, ema)

buy  = src > xATRTrailingStop and above 

barbuy  = src > xATRTrailingStop 

plotshape(buy,  title = "Buy",  text = 'Buy',  style = shape.labelup,   location = location.belowbar, color= color.green, textcolor = color.white, transp = 0, size = size.tiny)

barcolor(barbuy  ? color.green : na)

alertcondition(buy,  "UT Long",  "UT Long")

// UT BOOT 部分


// UT BOOT 部分 SHORT

a_1 = input(1,     title = "Short_Key Vaule. 'This changes the sensitivity'")
c_1 = input(22,    title = "Short_ATR Period")
h_1 = input(false,  title = "Short_Signals from Heikin Ashi Candles")

xATR_1  = atr(c_1)
nLoss_1 = a_1 * xATR_1

src_1 = h_1 ? security(heikinashi(syminfo.tickerid), timeframe.period, close, lookahead = false) : close

xATRTrailingStop_1 = 0.0
xATRTrailingStop_1 := iff(src_1 > nz(xATRTrailingStop_1[1], 0) and src_1[1] > nz(xATRTrailingStop_1[1], 0), max(nz(xATRTrailingStop_1[1]), src_1 - nLoss_1),
   iff(src_1 < nz(xATRTrailingStop_1[1], 0) and src_1[1] < nz(xATRTrailingStop_1[1], 0), min(nz(xATRTrailingStop_1[1]), src_1 + nLoss_1), 
   iff(src_1 > nz(xATRTrailingStop_1[1], 0), src_1 - nLoss_1, src_1 + nLoss_1)))
 
pos_1 = 0   
pos_1 :=	iff(src_1[1] < nz(xATRTrailingStop_1[1], 0) and src_1 > nz(xATRTrailingStop_1[1], 0), 1,
   iff(src_1[1] > nz(xATRTrailingStop_1[1], 0) and src_1 < nz(xATRTrailingStop_1[1], 0), -1, nz(pos_1[1], 0))) 
   
xcolor_1 = pos_1 == -1 ? color.red: pos_1 == 1 ? color.green : color.blue 

ema_1   = ema(src_1,1)
above_1 = crossover(ema_1, xATRTrailingStop_1)
below_1 = crossover(xATRTrailingStop_1, ema_1)

sell_1 = src_1 < xATRTrailingStop_1 and below_1

barsell_1 = src_1 < xATRTrailingStop_1 

plotshape(sell_1, title = "Sell", text = 'Sell', style = shape.labeldown, location = location.abovebar, color= color.red,   textcolor = color.white, transp = 0, size = size.tiny)

barcolor(barsell_1 ? color.red   : na)

alertcondition(sell_1, "UT Short", "UT Short")

// UT BOOT 部分



//获取金额
money = strategy.initial_capital
//money = strategy.equity

//平仓条件
float long_profit_target_percent    = input(0.7, title="做多——利润率%")
float short_profit_target_percent   = input(0.7, title="做空——利润率%")

// 计算每次购买的合约数量
contracts = money / close
contract = round(contracts, 1)//交易所限制精确到1位小数

//5min级别vagas tunnel(主要使用) //用于判断趋势
ema12  = ema(close, 12)
ema144 = ema(close, 144)
ema169 = ema(close, 169)
ema576 = ema(close, 576)
ema676 = ema(close, 676)
//ema576 = ema(close, 288)
//ema676 = ema(close, 338)

//15min级别vagas tunnel(加强趋势判断)
//加强判断不至于逆势操作
ema144_15min = security(syminfo.tickerid, '15' , expression = ema(close,144))
ema169_15min = security(syminfo.tickerid, '15' , expression = ema(close,169))
ema576_15min = security(syminfo.tickerid, '15' , expression = ema(close,576))
ema676_15min = security(syminfo.tickerid, '15' , expression = ema(close,676))


//判断多空情况
result = ''
if ema576 > ema169 
    result := 'Short'
else if ema169 > ema576 
    result := 'Long'
else
    result := 'No Signal'

var starttime = time
starttime := timestamp("Asia/Shanghai", 2024, 5, 17, 19, 10, 0 )

if(time >= starttime)  
    
    //开仓条件
    if(result == 'Long')
        // RSI处于超卖状态
        if (buy)
            //ema12过滤器
            //if(ema12 > ema676 or ema12 > ema576)
            if(strategy.position_size == 0)
                    // 576 676
                    // 1 100 0.7 flase 5min
                strategy.entry("Long", strategy.long, qty = contract)
    else if(result == 'Short')
        // RSI处于超买状态
        if (sell_1)
            //ema12过滤器
            //if(ema12 < ema676 or ema12 < ema576)
            if(strategy.position_size == 0)
                    // 576 676
                    // 1 100 0.7 true 5min
                strategy.entry("Short", strategy.short, qty = contract)

    //止盈条件
    // 如果当前持有long订单并且达到利润目标,则平仓
    if (strategy.position_size > 0 and close >= strategy.position_avg_price * (1 + long_profit_target_percent/100 ))
        strategy.close(id="Long")

    // 如果当前持有Short订单并且达到利润目标,则平仓
    if (strategy.position_size < 0 and close <= strategy.position_avg_price * (1 - short_profit_target_percent/100 ))
        strategy.close(id="Short")

    // 止损条件
    if (strategy.position_size > 0)
        if(((ema144 < ema676 and ema144 < ema576) and (ema169 < ema676 and ema169 < ema576)) )
        //if(close < ema676 and ema12 < ema676)
            strategy.close(id="Long")
            
    if (strategy.position_size < 0)
        if((ema144 > ema676 and ema144 > ema576) and (ema169 > ema676 and ema169 > ema576))
        //if(close > ema144 and ema12 > ema144)
            strategy.close(id="Short")

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值