首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在pine-script中设置stoploss?

如何在pine-script中设置stoploss?
EN

Stack Overflow用户
提问于 2019-10-18 04:42:42
回答 1查看 504关注 0票数 1

当我建立头寸时,我正试图绘制止损图。问题是,当我进入一个位置时,我得到了2个止损,但我只想要一个。

代码语言:javascript
复制
    //@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个止损?我怎么才能修复它?

EN

回答 1

Stack Overflow用户

发布于 2019-10-18 14:05:20

这段代码中的图已经被清理了--希望是你想要的那样。请参阅注释以了解所做的更改。你的交易止损点现在是较大的浅红点。

代码语言:javascript
复制
//@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)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58440465

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档