如何在松木脚本中设置止损?

时间:2019-10-17 20:42:41

标签: pine-script

我试图在输入头寸时绘制止损。问题是,当我进入一个仓位时,我得到2个止损,但我只想要一个。


    //@version=4
    strategy("Turtle Project",overlay= true)
    entry_long_2  =  input(55,   title="entry_long_2_input" ,minval=2)                
    profit_long_2 =  input(20,   title="profit_long_2_input",minval=1)                

    cond_L_2 = float(na)                                                             
    cond_L_2:= if high[entry_long_2] >= highest(high,entry_long_2)                   
        high[entry_long_2]                                                            
    else                                                                              
        cond_L_2[1]                                                                   


    cond_L_P_2 = float(na)                                                            
    cond_L_P_2:= if low[profit_long_2] <= lowest(low,profit_long_2)                   
        low[profit_long_2]                                                            
    else                                                                            
        cond_L_P_2[1]                      



    if high < cond_L_2
        strategy.entry("enter long",strategy.long, stop=cond_L_2)

    sl_inp_1 = input(2.0, title='Stop Loss_1 %', type=input.float)/100
    stop_level_1 = strategy.position_avg_price * (1 - sl_inp_1)
    stop_1 = max(cond_L_P_2,stop_level_1)

    strategy.exit("exit ","enter long",stop=stop_1)
    plot(stop_level_1,style=plot.style_circles,color=color.red)
    plot(cond_L_2)
    plot(cond_L_P_2, color=color.green)

    entry_long_1 =  input(20,   title="entry_long_2_input" ,minval=2)                
    profit_long_1 =  input(10,   title="profit_long_2_input",minval=1)                

    cond_L_1 = float(na)                                                             
    cond_L_1:= if high[entry_long_1] >= highest(high,entry_long_1)                   
        high[entry_long_1]                                                            
    else                                                                              
        cond_L_1[1]                                                                   


    cond_L_P_1 = float(na)                                                            
    cond_L_P_1:= if low[profit_long_1] <= lowest(low,profit_long_1)                   
        low[profit_long_1]                                                            
    else                                                                            
        cond_L_P_1[1]                      


    if high < cond_L_1
        strategy.entry("enter longj",strategy.long, stop=cond_L_1)

    sl_inp_2 = input(10.0, title='Stop Loss_2 %', type=input.float)/100
    stop_level_2 = strategy.position_avg_price * (1 - sl_inp_2)
    stop_2 = max(cond_L_P_1,stop_level_2)

    strategy.exit("exit ","enter longj",stop=stop_2)
    plot(stop_level_2,style=plot.style_circles,color=color.red)
    plot(cond_L_1)
    plot(cond_L_P_1, color=color.green)

enter image description here

您可以在图片中看到,如果我输入一个,但是止损有2个?我该如何解决?

enter image description here

1 个答案:

答案 0 :(得分:1)

此代码中的情节已被清理-希望如您所愿。查看评论以了解所做的更改。现在您的交易止损点是较大的浅红色点。

//@version=4
strategy("Turtle Project",overlay= true)
entry_long_2  =  input(55,   title="entry_long_2_input" ,minval=2)                
profit_long_2 =  input(20,   title="profit_long_2_input",minval=1)                

cond_L_2 = float(na)                                                             
cond_L_2:= if high[entry_long_2] >= highest(high,entry_long_2)                   
    high[entry_long_2]                                                            
else                                                                              
    cond_L_2[1]                                                                   


cond_L_P_2 = float(na)                                                            
cond_L_P_2:= if low[profit_long_2] <= lowest(low,profit_long_2)                   
    low[profit_long_2]                                                            
else                                                                            
    cond_L_P_2[1]                      



if high < cond_L_2
    strategy.entry("enter long",strategy.long, stop=cond_L_2)
// For debugging: Shows when your entry order issuing condition is true, but not necessarily when the order is executed.
// plotchar(high < cond_L_2, "high < cond_L_2", "▲", location.top)

sl_inp_1 = input(2.0, title='Stop Loss_1 %', type=input.float)/100
stop_level_1 = strategy.position_avg_price * (1 - sl_inp_1)
stop_1 = max(cond_L_P_2,stop_level_1)

strategy.exit("exit ","enter long",stop=stop_1)
// Commented this plot out as it's not useful on the chart.
// plot(stop_level_1, "stop_level_1", style=plot.style_circles,color=color.red)
plot(cond_L_2, "cond_L_2")
plot(cond_L_P_2, "cond_L_P_2", color=color.green)

entry_long_1 =  input(20,   title="entry_long_2_input" ,minval=2)                
profit_long_1 =  input(10,   title="profit_long_2_input",minval=1)                

cond_L_1 = float(na)                                                             
cond_L_1:= if high[entry_long_1] >= highest(high,entry_long_1)                   
    high[entry_long_1]                                                            
else                                                                              
    cond_L_1[1]                                                                   


cond_L_P_1 = float(na)                                                            
cond_L_P_1:= if low[profit_long_1] <= lowest(low,profit_long_1)                   
    low[profit_long_1]                                                            
else                                                                            
    cond_L_P_1[1]                      



if high < cond_L_1
    strategy.entry("enter longj",strategy.long, stop=cond_L_1)

sl_inp_2 = input(10.0, title='Stop Loss_2 %', type=input.float)/100
stop_level_2 = strategy.position_avg_price * (1 - sl_inp_2)
stop_2 = max(cond_L_P_1,stop_level_2)

strategy.exit("exit ","enter longj",stop=stop_2)
// This plots the actual stop used in your exit order.
plot(stop_2, "stop_2", style=plot.style_circles, color=color.maroon, linewidth = 3, transp = 80)
// Comment this line out if you don't want the initial entry's stop level to plot.
plot(stop_level_2, "stop_level_2", style=plot.style_circles,color=color.maroon)
plot(cond_L_1, "cond_L_1")
plot(cond_L_P_1, "cond_L_P_1", color=color.green)

enter image description here