Advertisement
Guest User

Variable alerts

a guest
Feb 29th, 2020
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // © Peter_O
  3.  
  4. //@version=4
  5. // strategy(title="TradingView Alerts to MT4 MT5 Strategy example", commission_type=strategy.commission.cash_per_order, commission_value=0.00003, overlay=false, default_qty_value=100000, initial_capital=1000)
  6. study(title="TradingView Alerts to MT4 MT5 Strategy example") //uncomment this line and comment previous one to make it a study producing alerts
  7. //
  8. // This script was created for educational purposes only.
  9. // It is showing how to use dynamic variables in TradingView alerts.
  10. // And how to execute them in Forex, indices and commodities markets
  11.  
  12. TakeProfitLevel=input(400)
  13. TakePartialProfitLevel=input(150)
  14.  
  15. // **** Entries logic **** {
  16. periodK = input(13, title="K", minval=1)
  17. periodD = input(3, title="D", minval=1)
  18. smoothK = input(4, title="Smooth", minval=1)
  19. k = sma(stoch(close, high, low, periodK), smoothK)
  20. d = sma(k, periodD)
  21. plot(k, title="%K", color=color.blue)
  22. plot(d, title="%D", color=color.orange)
  23. h0 = hline(80)
  24. h1 = hline(20)
  25. fill(h0, h1, color=color.purple, transp=75)
  26.  
  27. GoLong=crossover(k,d)// and k<80
  28. GoShort=crossunder(k,d)// and k>20
  29. // } End of entries logic
  30.  
  31. // **** Pivot-points and stop-loss logic **** {
  32. piv_high = pivothigh(high,1,1)
  33. piv_low = pivotlow(low,1,1)
  34. var float stoploss_long=low
  35. var float stoploss_short=high
  36.  
  37. pl=valuewhen(piv_low,piv_low,0)
  38. ph=valuewhen(piv_high,piv_high,0)
  39.  
  40. if GoLong
  41. stoploss_long := low<pl ? low : pl
  42. if GoShort
  43. stoploss_short := high>ph ? high : ph
  44. // } End of Pivot-points and stop-loss logic
  45.  
  46. // **** Trade counter and partial closing mechanism **** {
  47. var int trade_id=0
  48. if GoLong or GoShort
  49. trade_id:=trade_id+1
  50.  
  51. TakePartialProfitLong = barssince(GoLong)<barssince(GoShort) and crossover(high,(valuewhen(GoLong,close,0)+TakePartialProfitLevel*syminfo.mintick))
  52. TakePartialProfitShort = barssince(GoLong)>barssince(GoShort) and crossunder(low,(valuewhen(GoShort,close,0)-TakePartialProfitLevel*syminfo.mintick))
  53. // } End of Trade counter and partial closing mechanism
  54.  
  55. // strategy.entry("Long", strategy.long, when=GoLong)
  56. // strategy.exit("XPartLong", from_entry="Long", qty_percent=50, profit=TakePartialProfitLevel)
  57. // strategy.exit("XLong", from_entry="Long", stop=stoploss_long, profit=TakeProfitLevel)
  58. // strategy.entry("Short", strategy.short, when=GoShort)
  59. // strategy.exit("XPartShort", from_entry="Short", qty_percent=50, profit=TakePartialProfitLevel)
  60. // strategy.exit("XShort", from_entry="Short", stop=stoploss_short, profit=TakeProfitLevel)
  61.  
  62. plot_stoploss_long=plot(stoploss_long, title="SLLong", color=na)
  63. plot_stoploss_short=plot(stoploss_short, title="SLShort", color=na)
  64. plot_trade_id=plot(trade_id, title="Trade_id", color=na)
  65. plot_tp_level=plot(TakeProfitLevel, title="TakeProfitLevel", color=na)
  66. //defining plots above are required only for the purpose of passing dynamic variables to alerts. Their color is set to 'na' so that they don't display on the screen.
  67.  
  68. alertcondition(true, title='Long', message='long slprice={{plot("SLLong")}} tradeid={{plot("Trade_id")}} tp={{plot("TakeProfitLevel")}}')
  69. alertcondition(true, title='Short', message='short slprice={{plot("SLShort")}} tradeid={{plot("Trade_id")}} tp={{plot("TakeProfitLevel")}}')
  70. alertcondition(true, title='ClosePartLong', message='closepart tradeid={{plot("Trade_id")}} part=0.5')
  71. alertcondition(true, title='ClosePartShort', message='closepart tradeid={{plot("Trade_id")}} part=0.5')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement