Pine script Pine脚本(TradingView)-如何将止损移至获利回吐水平
TradingView上有一个Pine脚本代码,其中我们有2个获利级别和2个止损级别:。 当实现第一次获利时,一半头寸关闭,第一次止损移动到入门级(盈亏平衡) 你是否有任何想法,如何使3个采取以下逻辑利润水平:Pine script Pine脚本(TradingView)-如何将止损移至获利回吐水平,pine-script,Pine Script,TradingView上有一个Pine脚本代码,其中我们有2个获利级别和2个止损级别:。 当实现第一次获利时,一半头寸关闭,第一次止损移动到入门级(盈亏平衡) 你是否有任何想法,如何使3个采取以下逻辑利润水平: 当达到TP 1时,SL移动至收支平衡 当到达TP 2时,SL移动到TP 2 当到达TP 3时,退出该位置 谢谢你的帮助 //@version=4 strategy("SL1 Pips after TP1 (MA)", commission_type=stra
- 当达到TP 1时,SL移动至收支平衡
- 当到达TP 2时,SL移动到TP 2
- 当到达TP 3时,退出该位置
//@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)
/@version=4
策略(“TP1(MA)后的SL1点子”,佣金类型=策略。佣金。每订单现金,叠加=真,默认数量值=1000,初始资本=100)
//策略
购买=输入(真)
销售=输入(真)
//日期范围
起始年=输入(标题=起始年,定义=2020年)
开始月份=输入(title='start month',defval=1)
开始日=输入(title='start day',defval=1)
开始时间=输入(title='start hour',deffal=0)
开始\分钟=输入(title='start minute',deffal=0)
结束时间=输入(title='set end time',deffal=false)
年末=输入(title='end year',defval=2019)
月末=输入(title='end month',defval=12)
结束日=输入(title='end day',deffal=31)
结束时间=输入(title='结束时间',deffal=23)
结束时间=输入(title='end minute',deffal=59)
//妈
均线周期=输入(title='均线周期',deffal=10)
wma_period=输入(title='wma period',deffal=20)
均线=均线(收盘,均线周期)
wma=wma(关闭,wma_期)
//进入条件
买=
交叉(ema、wma)和
新西兰(战略位置大小)==0,买入并卖出
时间>时间戳(开始年、开始月、开始日、开始小时、开始分钟)和
(结束时间?(时间<时间戳(结束年、结束月、结束日、结束小时、结束分钟)):真)
出售=
交叉管(ema、wma)和
新西兰(战略位置大小)=0并出售和出售
时间>时间戳(开始年、开始月、开始日、开始小时、开始分钟)和
(结束时间?(时间<时间戳(结束年、结束月、结束日、结束小时、结束分钟)):真)
//点子
pip=输入(20)*10*syminfo.mintick
//交易参数//
var bool LS=na
var bool SS=na
var浮点EP=na
var浮点TVL=na
var浮点TVS=na
var浮点TSL=na
var浮动TSS=na
var浮点TP1=na
var浮点TP2=na
var float SL1=na
var浮点SL2=na
如果买入或卖出,且strategy.position_size==0
EP:=关闭
SL1:=EP-pip*(卖出?-1:1)
SL2:=EP-pip*(卖出?-1:1)
TP1:=EP+pip*(卖出?-1:1)
TP2:=EP+pip*2*(卖出?-1:1)
//当前贸易方向
LS:=买入或策略。仓位大小>0
SS:=出售或策略。头寸大小<0
//调整交易参数和尾随止损计算
TVL:=最大值(TP1,打开)-pip[1]
TVS:=min(TP1,打开)+pip[1]
TSL:=打开[1]>TSL[1]?最大值(TVL,TSL[1]):TVL
TSS:=打开[1]TP1
如果打开=TP1
SL2:=最大值(EP、TSS)
//关闭条件
关闭长=LS,打开SL2
//买
strategy.entry(“买入”,strategy.long,when=buy而非SS)
strategy.exit(“exit1”,from\u entry=“buy”,stop=SL1,limit=TP1,qty\u percent=50)
strategy.exit(“exit2”,from_entry=“buy”,stop=SL2,limit=TP2)
//出售
strategy.entry(“sell”,strategy.short,when=sell而非LS)
strategy.exit(“exit3”,from\u entry=“sell”,stop=SL1,limit=TP1,qty\u percent=50)
strategy.exit(“exit4”,from_entry=“sell”,stop=SL2,limit=TP2)
//阴谋
a=绘图(strategy.position\u size>0?SL1:na,颜色=#dc143c,样式=绘图.style\u linebr)
b=绘图(strategy.position_size<0?SL1:na,颜色=#dc143c,样式=绘图.style_linebr)
c=绘图(strategy.position_size>0?TP1:na,颜色=#00ced1,样式=绘图.style_linebr)
d=绘图(strategy.position_size<0?TP1:na,color=#00ced1,style=plot.style_linebr)
e=绘图(strategy.position\u size>0?TP2:na,颜色=#00ced1,样式=绘图。样式\u linebr)
f=绘图(strategy.position_size<0?TP2:na,color=#00ced1,style=plot.style_linebr)
g=绘图(strategy.position\u size>=0?na:EP,color=#ffffff,style=plot.style\u linebr)
h=绘图(strategy.position_size以下是您需要的示例:
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © adolgov
// @description
// when tp1 is reached, sl is moved to break-even
// when tp2 is reached, sl is moved to tp1
// when tp3 is reached - exit
//@version=4
strategy("Stepped trailing strategy example", overlay=true)
// random entry condition
longCondition = crossover(sma(close, 14), sma(close, 28))
if (longCondition)
strategy.entry("My Long Entry Id", strategy.long)
// sl & tp in points
sl = input(100)
tp1 = input(100)
tp2 = input(200)
tp3 = input(300)
curProfitInPts() =>
if strategy.position_size > 0
(high - strategy.position_avg_price) / syminfo.mintick
else if strategy.position_size < 0
(strategy.position_avg_price - low) / syminfo.mintick
else
0
calcStopLossPrice(OffsetPts) =>
if strategy.position_size > 0
strategy.position_avg_price - OffsetPts * syminfo.mintick
else if strategy.position_size < 0
strategy.position_avg_price + OffsetPts * syminfo.mintick
else
0
calcProfitTrgtPrice(OffsetPts) =>
calcStopLossPrice(-OffsetPts)
getCurrentStage() =>
var stage = 0
if strategy.position_size == 0
stage := 0
if stage == 0 and strategy.position_size != 0
stage := 1
else if stage == 1 and curProfitInPts() >= tp1
stage := 2
else if stage == 2 and curProfitInPts() >= tp2
stage := 3
stage
stopLevel = -1.
profitLevel = calcProfitTrgtPrice(tp3)
// based on current stage set up exit
// note: we use same exit ids ("x") consciously, for MODIFY the exit's parameters
curStage = getCurrentStage()
if curStage == 1
stopLevel := calcStopLossPrice(sl)
strategy.exit("x", loss = sl, profit = tp3, comment = "sl or tp3")
else if curStage == 2
stopLevel := calcStopLossPrice(0)
strategy.exit("x", stop = stopLevel, profit = tp3, comment = "breakeven or tp3")
else if curStage == 3
stopLevel := calcStopLossPrice(-tp1)
strategy.exit("x", stop = stopLevel, profit = tp3, comment = "tp1 or tp3")
else
strategy.cancel("x")
//此源代码受Mozilla公共许可证2.0的条款约束,位于https://mozilla.org/MPL/2.0/
//©阿道尔政府
//@说明
//当达到tp1时,sl移动至收支平衡
//当到达tp2时,sl移动到tp1
//当到达tp3时-退出
//@版本=4
策略(“阶梯式跟踪策略示例”,overlay=true)
//随机进入条件
长条件=交叉(sma(闭合,14),sma(闭合,28))
如果(长期条件)
strategy.entry(“我的长输入Id”,strategy.Long)
//以点为单位的sl&tp
sl=输入(100)
tp1=输入(100)
tp2=输入(200)
tp3=输入(300)
curProfitInPts()=>
如果strategy.position_size>0
(高-策略.位置/平均价格)/syminfo.mintick
如果strategy.position_size<0,则为else
(strategy.position\u avg\u price-低)/symininfo.mintick
其他的
0
calcStopLossPrice(抵销价格)=>
如果strategy.position_size>0
strategy.position_avg_price-OffsetPts*symininfo.mintick
如果strategy.position_size<0,则为else
strategy.position_avg_price+OffsetPts*symininfo.mintick
其他的
0
calcProfitTrgtPrice(偏移量)=>
calcStopLossPrice(-OffsetPts)
getCurrentStage()=>
var阶段=0
如果strategy.position_size==0
阶段:=0
如果阶段==0且策略.position\u size!=0
阶段:=1
如果阶段==1且curProfitInPts()>=tp1,则为else
阶段:=2
如果阶段==2且curProfitInPts()>=tp2,则为else
阶段:=3
阶段
stopLevel=-1。
profitLevel=CalcProfitRGTPrice(tp3)
//基于当前阶段设置出口
//注意:我们有意识地使用相同的出口ID(“x”)来修改出口的参数
curStage=getCurrentStage()
如果curStage==1
stopLevel:=calcStopLossPrice(sl)
圣