Turtle trading strategy (Donchian/ATR)

jeanobrega · strategy · 54 行 · 点赞 1,879 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=3
//Based on Turtle traders strategy: buy/sell on Donchian breakouts and stop loss on ATR 2x
// initial version considerations :
//// 1. Does not consider filter for avoiding new entries after winning trades (filtering rule from Turtle Strategy on 20 day breakout strategy) 
//// 2. Does not consider pyramiding (aditional entries after 1N price movements)
 
strategy("Turtle trading strategy (Donchian/ATR)", overlay=true)
 
enter_period = input(20, minval=1, title="Enter Channel")
exit_period = input(10, minval=1, title="Exit Channel")
offset_bar = input(0,minval=0, title ="Offset Bars")
direction = input("Long",options=["Long","Short"],title="Direction")
max_length = max(enter_period,exit_period)
atrmult = input(2,title="ATR multiplier (Stop Loss)")
atrperiod = input(20,title="ATR Period")
 
closed_pos = false
dir_long = direction == "Long"? true : false
atr = atr(atrperiod)
upper = dir_long ? highest(enter_period): highest(exit_period)
lower = dir_long ? lowest(exit_period): lowest(enter_period)
atrupper = close + atr
atrlower = close - atr
plotted_atr = dir_long ? atrlower : atrupper
 
//basis = avg(upper, lower)
 
l = plot(lower, style=line, linewidth=3, color=lime, offset=1)
u = plot(upper, style=line, linewidth=3, color=lime, offset=1)
a = plot(plotted_atr, style=line,linewidth=2,color=red,offset=1)
//plot(basis, color=yellow, style=line, linewidth=1, title="Mid-Line Average")
//break upper Donchian (with 1 candle offset) (buy signal)
break_up = (close >= upper[1])
//break lower Donchian (with 1 candle offset) (sell signal)
break_down = (close <= lower[1])
stop_loss = dir_long ? (close<=plotted_atr[1]) : (close>=plotted_atr[1])
 
if break_up and dir_long
    strategy.entry("buy", strategy.long, 1)
    closed_pos :=false
if (break_down or stop_loss) and dir_long
    strategy.close("buy")
    
if break_down and not dir_long
    strategy.entry("sell", strategy.short, 1)
    closed_pos :=false
if (break_up or stop_loss) and not dir_long
    strategy.close("sell")
    closed_pos :=true
    
losing_trade = strategy.equity[0]<strategy.equity[1]
//plotshape(losing_trade,text="Losing!")    
plotshape(stop_loss,style=dir_long?shape.labeldown:shape.labelup,text="Stop!")
//plot(strategy.equity)

← 返回列表