Advertisement
Guest User

Untitled

a guest
Dec 26th, 2022
499
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. TP1Perc = input.float(title=Long Take Profit 1 (%), minval=0.0, step=0.1, defval=2, group=TP & SL)
  2. TP2Perc = input.float(title=Long Take Profit 2 (%), minval=0.0, step=0.1, defval=4, group=TP & SL)
  3. TP3Perc = input.float(title=Long Take Profit 3 (%), minval=0.0, step=0.1, defval=4, group=TP & SL)
  4. TP4Perc = input.float(title=Long Take Profit 4 (%), minval=0.0, step=0.1, defval=4, group=TP & SL)
  5.  
  6. TP1_Ratio = input.float(title=Sell Postion Size % @ TP1, defval=50, step=1, group=TP & SL, tooltip=Example: 50 closing 50% of the position once TP1 is reached)/100
  7. TP2_Ratio = input.float(title=Sell Postion Size % @ TP2, defval=50, step=1, group=TP & SL, tooltip=Example: 50 closing 50% of the position once TP2 is reached)/100
  8. TP3_Ratio = input.float(title=Sell Postion Size % @ TP3, defval=50, step=1, group=TP & SL, tooltip=Example: 50 closing 50% of the position once TP3 is reached)/100
  9. TP4_Ratio = input.float(title=Sell Postion Size % @ TP4, defval=50, step=1, group=TP & SL, tooltip=Example: 50 closing 50% of the position once TP4 is reached)/100
  10.  
  11. // Calculate moving averages
  12. //fastSMA = ta.sma(close, FastPeriod)
  13. //slowSMA = ta.sma(close, SlowPeriod)
  14.  
  15. // Calculate trading conditions
  16. //enterLong = ta.crossover(fastSMA, slowSMA)
  17.  
  18. // Plot moving averages
  19. //plot(series=fastSMA, color=color.green, title=Fase MA)
  20. //plot(series=slowSMA, color=color.red, title=Slow MA)
  21.  
  22. // STEP 2:
  23. // Figure out take profit price
  24. percentAsPoints(pcnt) =>
  25. strategy.position_size != 0 ? math.round(pcnt / 100.0 * strategy.position_avg_price / syminfo.mintick) : float(na)
  26.  
  27. percentAsPrice(pcnt) =>
  28. strategy.position_size != 0 ? ((pcnt / 100.0) + 1.0) * strategy.position_avg_price : float(na)
  29.  
  30. current_position_size = math.abs(strategy.position_size)
  31. initial_position_size = math.abs(ta.valuewhen(strategy.position_size[1] == 0.0, strategy.position_size, 0))
  32.  
  33. TP1 = strategy.position_avg_price + percentAsPoints(TP1Perc) * syminfo.mintick * strategy.position_size / math.abs(strategy.position_size)
  34. TP2 = strategy.position_avg_price + percentAsPoints(TP2Perc) * syminfo.mintick * strategy.position_size / math.abs(strategy.position_size)
  35. TP3 = strategy.position_avg_price + percentAsPoints(TP3Perc) * syminfo.mintick * strategy.position_size / math.abs(strategy.position_size)
  36. TP4 = strategy.position_avg_price + percentAsPoints(TP4Perc) * syminfo.mintick * strategy.position_size / math.abs(strategy.position_size)
  37.  
  38. // Submit entry orders
  39. if buySignal and in_date_range == true
  40. strategy.entry('Long', strategy.long, when=buySignal, alert_message='Open Long Position')
  41.  
  42. if sellSignal and in_date_range == true
  43. strategy.entry('Short', strategy.short, when=sellSignal, alert_message='Open Short Position')
  44.  
  45. // STEP 3:
  46. // Submit exit orders based on take profit price
  47.  
  48. if strategy.position_size > 0
  49. strategy.exit(TP1, from_entry=Long, qty = initial_position_size * TP1_Ratio, limit = TP1)
  50. strategy.exit(TP2, from_entry=Long, qty = initial_position_size * TP2_Ratio, limit = TP2)
  51. strategy.exit(TP3, from_entry=Long, qty = initial_position_size * TP3_Ratio, limit = TP3)
  52. strategy.exit(TP4, from_entry=Long, qty = initial_position_size * TP4_Ratio, limit = TP4)
  53. strategy.close('Long', when=sellSignal, alert_message='Close Long Position')
  54.  
  55. if strategy.position_size < 0
  56. strategy.exit(TP1, from_entry=Short, qty = initial_position_size * TP1_Ratio, limit = TP1)
  57. strategy.exit(TP2, from_entry=Short, qty = initial_position_size * TP2_Ratio, limit = TP2)
  58. strategy.exit(TP3, from_entry=Short, qty = initial_position_size * TP3_Ratio, limit = TP3)
  59. strategy.exit(TP4, from_entry=Short, qty = initial_position_size * TP4_Ratio, limit = TP4)
  60. strategy.close('Short', when=buySignal, alert_message='Close Short Position')
  61.  
  62. // Plot take profit values for confirmation
  63. plot(series=(strategy.position_size > 0) ? TP1 : na, color=color.green, style=plot.style_circles, linewidth=1, title=Take Profit 1)
  64. plot(series=(strategy.position_size > 0) ? TP2 : na, color=color.green, style=plot.style_circles, linewidth=1, title=Take Profit 2)
  65. plot(series=(strategy.position_size > 0) ? TP3 : na, color=color.green, style=plot.style_circles, linewidth=1, title=Take Profit 3)
  66. plot(series=(strategy.position_size > 0) ? TP4 : na, color=color.green, style=plot.style_circles, linewidth=1, title=Take Profit 4)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement