Tradingview Pine Script adding % stop loss

陌路散爱 提交于 2020-05-17 06:46:06

问题


enter image description hereIm trying to make a strategy that buys at the 55 day high and sells at the 20 day low. The probles is that i want to add a stoploss. The stoploss needs to be at 5 procent from the entry. This is what i already tried.

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

Sometimes it works and somtimes it does not work. its really wierd, i hope som1 can help me.


回答1:


That's because your stop loss level is higher than current price: http://dl4.joxi.net/drive/2019/10/14/0024/1758/1636062/62/f02fb255bc.jpg



来源:https://stackoverflow.com/questions/58375066/tradingview-pine-script-adding-stop-loss

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!