Tradingview Pine Script添加止损百分比

时间:2019-10-14 10:41:33

标签: pine-script

enter image description here我正在尝试制定一项策略,在55天的高点买入,然后在20天的低点卖出。 问题是我想加止损。止损需要在入场时达到5%。 这就是我已经尝试过的。

//@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]                      

sl_inp = input(2.0, title='Stop Loss %', type=input.float)/100
stop_level = strategy.position_avg_price * (1 - sl_inp)

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

loll = float(na)
loll:=  stop_level > cond_L_P_2 ? stop_level : cond_L_P_2 > stop_level ? cond_L_P_2 :loll[1]


strategy.exit("exit ","enter long",stop=loll)

plot(stop_level,style=plot.style_circles,color=color.red)
plot(cond_L_2)
plot(cond_L_P_2, color=color.green)

有时它起作用,有时它不起作用。它真的很奇怪,我希望som1可以帮助我。

1 个答案:

答案 0 :(得分:0)

那是因为您的止损水平高于当前价格: http://dl4.joxi.net/drive/2019/10/14/0024/1758/1636062/62/f02fb255bc.jpg