【问题标题】:Trailing Stop Pinescript Version 5追踪止损 Pinescript 版本 5
【发布时间】:2022-11-10 12:57:28
【问题描述】:

我刚学 pinescript 第 5 版,我想做追踪止损,但策略退出不起作用

//@version=5
strategy("Trailing Stop V5")


trailPerc=0.2
price_stop=0.0
aktifEMA   = input.bool(true, "Aktifkan EMA vs EMA",   inline = "01")
fastEMA = input.int(10,"Fast EMA", minval = 2)
slowEMA = input.int(50,"Slow EMA", minval = 2)
longCondition = ta.crossover(ta.sma(close, fastEMA), ta.sma(close, slowEMA))
shortCondition = ta.crossunder(ta.sma(close, fastEMA), ta.sma(close, slowEMA))
    
timePeriod = time >= timestamp(syminfo.timezone, 2018,12,6,0,0)
if (timePeriod)
    if (aktifEMA == true)
        if (longCondition)
            strategy.entry("long", strategy.long)
        
        if (strategy.position_size>0)
            stopValue=close*(1-trailPerc)
            price_stop:=math.max(stopValue,price_stop[1])
        else
            price_stop:=0
        
        if (strategy.position_size>0)
            strategy.exit("exit","long",stop=price_stop)

有人能帮我吗?谢谢

【问题讨论】:

    标签: pine-script


    【解决方案1】:

    Gostaria de ver isto funcionando...

    【讨论】:

      【解决方案2】:

      //@version=5
      
      strategy( "sim 2mov with trail" , overlay=true , max_lines_count =500 , max_labels_count=500 , max_bars_back= 4700 , default_qty_type=strategy.percent_of_equity , default_qty_value =100 , commission_type = strategy.commission.percent , commission_value = 0.0 )
      
       
      
      // Inputs
      averageData = input.source(close, title="Average Data Source")
      
      fastLength = input.int(10, title="Fast Average Length")
      slowLength = input.int(12, title="Slow Average Length")
      
      
      
      // Calculate averages 
      fastAverage = ta.sma(averageData, fastLength)
      slowAverage = ta.sma(averageData, slowLength)
      
      // Plot averages
      plot(fastAverage, color=color.orange, title="Fast SMA")
      plot(slowAverage, color=color.blue, title="Slow SMA")
      
      // Look for moving average crosses
      crossAbove = ta.crossover(fastAverage, slowAverage)
      crossUnder = ta.crossunder(fastAverage, slowAverage)
      
      
      if crossAbove and strategy.position_size == 0
          color.new(color.green, 80)
          strategy.entry("long", strategy.long  )
          
      else if crossUnder and strategy.position_size == 0
          color.new(color.red, 80)
          strategy.entry("short", strategy.short  )  
      
      
      
      
      
      
      
      stoploss = input.float( 2 , "Stop Loss percentage" , minval= 0 )
      
      takeprofit = input.float( 2 , "Take Profit percentage" , minval= 0 )
      
      trailingstop = input(true , "Trailing Stop")
      trail_p = input.float( 0.05 , "Trailing Stop percentage (of entry price)" , minval= 0 )
      
      
      
      
      av_p = strategy.position_avg_price
      
      if trailingstop 
          
          if strategy.position_size > 0 and strategy.position_size[1] <= 0
      
              mes_pa = "   start trail: "+str.tostring(   av_p + (av_p *  takeprofit/100)    ) +"
       trail offset: "+ str.tostring( av_p *(trail_p/100)   ) +"
       SL: "+ str.tostring( av_p - (av_p *  stoploss/100)    ) 
      
              label.new(bar_index, low - low *(trail_p/100) , mes_pa , style = label.style_label_up , color = color.white ,textcolor = color.black, size = size.normal)
      
          else if  strategy.position_size < 0 and strategy.position_size[1] >= 0
      
              mes_pa = "   start trail: "+str.tostring(   av_p - (av_p *  takeprofit/100)    ) +"
       trail offset: "+ str.tostring(   av_p *(trail_p/100)     )  +"
       SL: "+ str.tostring( av_p + (av_p *  stoploss/100)    ) 
              
              label.new(bar_index, high + high *(trail_p/100) , mes_pa , style = label.style_label_down  , color = color.white ,textcolor = color.black, size = size.normal)
      
      
      
      
      
          
          strategy.exit("exit_buy", "long" ,  when= strategy.position_size > 0  ,  stop  =  av_p - (av_p *  stoploss/100)  ,  trail_price = av_p + (av_p *  takeprofit/100) , trail_offset = av_p *(trail_p/100) *1/syminfo.mintick  , comment = "t buy" ) 
          
          strategy.exit("exit_sell", "short" ,when= strategy.position_size < 0,  stop  =  av_p + (av_p *  stoploss/100),  trail_price = av_p - (av_p *  takeprofit/100) , trail_offset = av_p *(trail_p/100) *1/syminfo.mintick  , comment = "t sell") 
      
          
      
      else
      
      
      
          strategy.exit("exit_buy", "long" ,  when= strategy.position_size > 0 ,  stop  =  av_p - (av_p *  stoploss/100)  ,  limit  =  av_p + (av_p *  takeprofit/100) , comment = "st buy" ) 
          
          strategy.exit("exit_sell", "short" ,when= strategy.position_size < 0 ,  stop  =  av_p + (av_p *  stoploss/100)   ,  limit  =  av_p - (av_p *  takeprofit/100) , comment = "st sell") 
          
      
      
      
      // have_long = strategy.position_size[0] > strategy.position_size[1]  
      // long_en = ta.valuewhen(have_long, open, 0) 
      
      // have_short = strategy.position_size[0] < strategy.position_size[1]  
      // short_en = ta.valuewhen(have_short, open, 0) 
      
      
      // av_p =  ta.valuewhen(false, open, 0)
      
      // if have_long
      //     av_p :=  long_en
      
      // if have_short
      //     av_p :=  short_en
      
      
      // if trailingstop 
      
      //     strategy.exit("tr_buy", "long"   , when= have_long ,   trail_price = av_p + (av_p *  start_trail/100) , trail_offset = av_p *(trail_p/100) *1/syminfo.mintick  , comment = "tr_buy" ) 
          
      //     strategy.exit("tr_sell", "short" , when= have_short ,  trail_price = av_p - (av_p *  start_trail/100) , trail_offset = av_p *(trail_p/100) *1/syminfo.mintick  , comment = "tr_sell") 
      
          
      
      
      
      
      
      // move_sl = input(true , "Moving Stop Loss")
      // move_sl_p = input.float(  1 , "move sl after x percentage of price"  , minval = -100  )
      // move_sl_p_p = input.float(  0.5 , "how percent move sl ?"  , minval = -100  )
       
       
      // strategy.cancel("sl_mo", when = strategy.position_size ==0)
      // if move_sl
      
      
          
      //     if strategy.position_size > 0
          
      //         stop_p = av_p + (av_p *  move_sl_p/100)
      //         limit_p = av_p + (av_p *  move_sl_p_p/100)
      
      //         strategy.order( "sl_mo" , strategy.short, math.abs( strategy.position_size ) , limit= limit_p , stop= stop_p)
      
          
      //     else if strategy.position_size < 0
      
      //         stop_p = av_p - (av_p *  move_sl_p/100)
      //         limit_p = av_p - (av_p *  move_sl_p_p/100)
              
      //         strategy.order( "sl_mo" , strategy.long, math.abs( strategy.position_size )  , limit= limit_p , stop= stop_p)

      【讨论】:

      • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-09
      相关资源
      最近更新 更多