![](/img/trans.png)
[英]Tradingview - pine script for Take profit and Stop loss by percentage
[英]Pine Script (TradingView) - How to move a Stop Loss to the Take Profit level
TradingView 上有一个 Pine 脚本代码,其中我们有 2 个止盈水平和 2 个止损水平: tradingview.com 。 当达到第一个止盈时,position 的一半被关闭,第一个止损移动到进场水平(盈亏平衡)。
您是否有任何想法如何使用以下逻辑制作 3 个获利水平:
当达到 TP 1 时,SL 移动到盈亏平衡
当到达 TP 2 时,SL 移动到 TP 2
达到 TP 3 时,退出 position
非常感谢您的帮助!
//@version=4
strategy("SL1 Pips after TP1 (MA)", commission_type=strategy.commission.cash_per_order, overlay=true, default_qty_value=1000, initial_capital=100)
// Strategy
Buy = input(true)
Sell = input(true)
// Date Range
start_year = input(title='Start year' ,defval=2020)
start_month = input(title='Start month' ,defval=1)
start_day = input(title='Start day' ,defval=1)
start_hour = input(title='Start hour' ,defval=0)
start_minute = input(title='Start minute' ,defval=0)
end_time = input(title='set end time?',defval=false)
end_year = input(title='end year' ,defval=2019)
end_month = input(title='end month' ,defval=12)
end_day = input(title='end day' ,defval=31)
end_hour = input(title='end hour' ,defval=23)
end_minute = input(title='end minute' ,defval=59)
// MA
ema_period = input(title='EMA period',defval=10)
wma_period = input(title='WMA period',defval=20)
ema = ema(close,ema_period)
wma = wma(close,wma_period)
// Entry Condition
buy =
crossover(ema,wma) and
nz(strategy.position_size) == 0 and Buy and
time > timestamp(start_year, start_month, start_day, start_hour, start_minute) and
(end_time?(time < timestamp(end_year, end_month, end_day, end_hour, end_minute)):true)
sell =
crossunder(ema,wma) and
nz(strategy.position_size) == 0 and Sell and
time > timestamp(start_year, start_month, start_day, start_hour, start_minute) and
(end_time?(time < timestamp(end_year, end_month, end_day, end_hour, end_minute)):true)
// Pips
pip = input(20)*10*syminfo.mintick
// Trading parameters //
var bool LS = na
var bool SS = na
var float EP = na
var float TVL = na
var float TVS = na
var float TSL = na
var float TSS = na
var float TP1 = na
var float TP2 = na
var float SL1 = na
var float SL2 = na
if buy or sell and strategy.position_size == 0
EP := close
SL1 := EP - pip * (sell?-1:1)
SL2 := EP - pip * (sell?-1:1)
TP1 := EP + pip * (sell?-1:1)
TP2 := EP + pip * 2 * (sell?-1:1)
// current trade direction
LS := buy or strategy.position_size > 0
SS := sell or strategy.position_size < 0
// adjust trade parameters and trailing stop calculations
TVL := max(TP1,open) - pip[1]
TVS := min(TP1,open) + pip[1]
TSL := open[1] > TSL[1] ? max(TVL,TSL[1]):TVL
TSS := open[1] < TSS[1] ? min(TVS,TSS[1]):TVS
if LS and high > TP1
if open <= TP1
SL2:=min(EP,TSL)
if SS and low < TP1
if open >= TP1
SL2:=max(EP,TSS)
// Closing conditions
close_long = LS and open < SL2
close_short = SS and open > SL2
// Buy
strategy.entry("buy" , strategy.long, when=buy and not SS)
strategy.exit ("exit1", from_entry="buy", stop=SL1, limit=TP1, qty_percent=50)
strategy.exit ("exit2", from_entry="buy", stop=SL2, limit=TP2)
// Sell
strategy.entry("sell" , strategy.short, when=sell and not LS)
strategy.exit ("exit3", from_entry="sell", stop=SL1, limit=TP1, qty_percent=50)
strategy.exit ("exit4", from_entry="sell", stop=SL2, limit=TP2)
// Plots
a=plot(strategy.position_size > 0 ? SL1 : na, color=#dc143c, style=plot.style_linebr)
b=plot(strategy.position_size < 0 ? SL1 : na, color=#dc143c, style=plot.style_linebr)
c=plot(strategy.position_size > 0 ? TP1 : na, color=#00ced1, style=plot.style_linebr)
d=plot(strategy.position_size < 0 ? TP1 : na, color=#00ced1, style=plot.style_linebr)
e=plot(strategy.position_size > 0 ? TP2 : na, color=#00ced1, style=plot.style_linebr)
f=plot(strategy.position_size < 0 ? TP2 : na, color=#00ced1, style=plot.style_linebr)
g=plot(strategy.position_size >= 0 ? na : EP, color=#ffffff, style=plot.style_linebr)
h=plot(strategy.position_size <= 0 ? na : EP, color=#ffffff, style=plot.style_linebr)
plot(ema,title="ema",color=#fff176)
plot(wma,title="wma",color=#00ced1)
非常感谢您的帮助!
TradingView 上有一个 Pine Script 代码,其中我们有 2 个获利水平和 2 个止损水平: tradingview.com 。 当实现第一次止盈时,平仓一半,并将第一次止损移至入场位(盈亏平衡)。
您是否有任何想法如何使用以下逻辑制作 3 个止盈水平:
当达到 TP 1 时,SL 移动到盈亏平衡
当达到 TP 2 时,SL 移动到 TP 2
当达到 TP 3 时,退出该仓位
Tkanks 提供了很多帮助!
//@version=4
strategy("SL1 Pips after TP1 (MA)", commission_type=strategy.commission.cash_per_order, overlay=true, default_qty_value=1000, initial_capital=100)
// Strategy
Buy = input(true)
Sell = input(true)
// Date Range
start_year = input(title='Start year' ,defval=2020)
start_month = input(title='Start month' ,defval=1)
start_day = input(title='Start day' ,defval=1)
start_hour = input(title='Start hour' ,defval=0)
start_minute = input(title='Start minute' ,defval=0)
end_time = input(title='set end time?',defval=false)
end_year = input(title='end year' ,defval=2019)
end_month = input(title='end month' ,defval=12)
end_day = input(title='end day' ,defval=31)
end_hour = input(title='end hour' ,defval=23)
end_minute = input(title='end minute' ,defval=59)
// MA
ema_period = input(title='EMA period',defval=10)
wma_period = input(title='WMA period',defval=20)
ema = ema(close,ema_period)
wma = wma(close,wma_period)
// Entry Condition
buy =
crossover(ema,wma) and
nz(strategy.position_size) == 0 and Buy and
time > timestamp(start_year, start_month, start_day, start_hour, start_minute) and
(end_time?(time < timestamp(end_year, end_month, end_day, end_hour, end_minute)):true)
sell =
crossunder(ema,wma) and
nz(strategy.position_size) == 0 and Sell and
time > timestamp(start_year, start_month, start_day, start_hour, start_minute) and
(end_time?(time < timestamp(end_year, end_month, end_day, end_hour, end_minute)):true)
// Pips
pip = input(20)*10*syminfo.mintick
// Trading parameters //
var bool LS = na
var bool SS = na
var float EP = na
var float TVL = na
var float TVS = na
var float TSL = na
var float TSS = na
var float TP1 = na
var float TP2 = na
var float SL1 = na
var float SL2 = na
if buy or sell and strategy.position_size == 0
EP := close
SL1 := EP - pip * (sell?-1:1)
SL2 := EP - pip * (sell?-1:1)
TP1 := EP + pip * (sell?-1:1)
TP2 := EP + pip * 2 * (sell?-1:1)
// current trade direction
LS := buy or strategy.position_size > 0
SS := sell or strategy.position_size < 0
// adjust trade parameters and trailing stop calculations
TVL := max(TP1,open) - pip[1]
TVS := min(TP1,open) + pip[1]
TSL := open[1] > TSL[1] ? max(TVL,TSL[1]):TVL
TSS := open[1] < TSS[1] ? min(TVS,TSS[1]):TVS
if LS and high > TP1
if open <= TP1
SL2:=min(EP,TSL)
if SS and low < TP1
if open >= TP1
SL2:=max(EP,TSS)
// Closing conditions
close_long = LS and open < SL2
close_short = SS and open > SL2
// Buy
strategy.entry("buy" , strategy.long, when=buy and not SS)
strategy.exit ("exit1", from_entry="buy", stop=SL1, limit=TP1, qty_percent=50)
strategy.exit ("exit2", from_entry="buy", stop=SL2, limit=TP2)
// Sell
strategy.entry("sell" , strategy.short, when=sell and not LS)
strategy.exit ("exit3", from_entry="sell", stop=SL1, limit=TP1, qty_percent=50)
strategy.exit ("exit4", from_entry="sell", stop=SL2, limit=TP2)
// Plots
a=plot(strategy.position_size > 0 ? SL1 : na, color=#dc143c, style=plot.style_linebr)
b=plot(strategy.position_size < 0 ? SL1 : na, color=#dc143c, style=plot.style_linebr)
c=plot(strategy.position_size > 0 ? TP1 : na, color=#00ced1, style=plot.style_linebr)
d=plot(strategy.position_size < 0 ? TP1 : na, color=#00ced1, style=plot.style_linebr)
e=plot(strategy.position_size > 0 ? TP2 : na, color=#00ced1, style=plot.style_linebr)
f=plot(strategy.position_size < 0 ? TP2 : na, color=#00ced1, style=plot.style_linebr)
g=plot(strategy.position_size >= 0 ? na : EP, color=#ffffff, style=plot.style_linebr)
h=plot(strategy.position_size <= 0 ? na : EP, color=#ffffff, style=plot.style_linebr)
plot(ema,title="ema",color=#fff176)
plot(wma,title="wma",color=#00ced1)
Tkanks 提供了很多帮助!
您是否设法找到解决方案? 我正在寻找相同的设置!
当达到 TP 1 时,SL 移动到盈亏平衡
当到达 TP 2 时,SL 移动到 TP 2
达到 TP 3 时,退出 position
提前致谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.