Advertisement
Guest User

Pine Script Template

a guest
Jan 17th, 2024
58
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.82 KB | None | 0 0
  1. //@version=5
  2. strategy("Strategy Template", overlay = true, pyramiding = 4, max_boxes_count = 500, max_labels_count = 500, initial_capital = 30000)
  3.  
  4. //Ubiquitous Inputs
  5. bear_bull = input.string("Bull", "Bull, Bear, or Both", options=["Both", "Bull", "Bear"])
  6. lookback = input.int(12, "***")
  7. bullEntryTicks = input.int(4, "Bull - Number of ticks to add to entry price")
  8. bearEntryTicks = input.int(4, "Bear - Number of ticks to add to entry price")
  9.  
  10. stopLossTrailingTF = input(false, "Trailing stop loss")
  11. bullStopLossPercent = input.float(25, "Bull - Stop loss Percent (divides by 10e5, i.e. 25/10e5*100 = .025%)")
  12. bearStopLossPercent = input.float(25, "Bear - Stop loss Percent (divides by 10e5, i.e. 25/10e5*100 = .025%)")
  13. bullTakeProfitTF = input(false, "Bull - T/F for take profit")
  14. bullTakeProfitPercent = input.float(25, "Bull Take Profit Percent (divides by 10e5, i.e. 25/10e5*100 = .025%)")
  15. bearTakeProfitTF = input(true, "Bear - T/F for take profit")
  16. bearTakeProfitPercent = input.float(25, "Bear Take Profit Percent (divides by 10e5, i.e. 25/10e5*100 = .025%)")
  17. sizeGrow = input(false, "T/F for grow initial position size")
  18. minExcessMargin = input(1.625, "Min Port Size Over Margin Req. (for sizeGrow)")
  19. maxPos = input(5, "Max Position Size")
  20. string startTime = input("0800", "Start time in military")
  21. string endTime = input("1500", "End time in military")
  22.  
  23. posSize = 1
  24. if sizeGrow
  25. posSize := math.min(maxPos, int(math.round(strategy.equity/minExcessMargin/close, 0)))
  26. else
  27. posSize := 1
  28.  
  29. tradeHours = startTime + "-" + endTime
  30. timeAllow() => time(timeframe = timeframe.period, session = tradeHours, timezone = "America/Chicago")
  31. endTimeHour = str.tonumber(str.substring(endTime, 0, 2))
  32. timeAdjust = 0
  33. if timeframe.isintraday
  34. timeAdjust := int(60 - str.tonumber(timeframe.period)*2)
  35.  
  36. barsSinceLastEntry() =>
  37. strategy.opentrades > 0 ? bar_index - strategy.opentrades.entry_bar_index(strategy.opentrades - 1) : na
  38.  
  39. stopLossBull = bullStopLossPercent/10000
  40. stopLossBear = bearStopLossPercent/10000
  41. bullTakeProfitPercent := bullTakeProfitPercent/10000
  42. bearTakeProfitPercent := bearTakeProfitPercent/10000
  43.  
  44. /////////////////////////////////////////////////////////////////////////////////////
  45. //Strategy logic - Logic here, then add to if logic below (bullEntryLogic) for conditions met or unmet
  46.  
  47.  
  48.  
  49. //Long Enter Logic - Fill in here
  50. bullEntryLogic = array.new_bool()
  51. if timeAllow()
  52. array.push(bullEntryLogic, true)
  53. else
  54. array.push(bullEntryLogic, false)
  55.  
  56. //Ex.
  57. if ta.ema(close, 10) > ta.ema(close, 20)
  58. array.push(bullEntryLogic, true)
  59. else
  60. array.push(bullEntryLogic, false)
  61.  
  62. //Long Close Logic -- Fill in here
  63. bullCloseLogic = array.new_bool()
  64. if barsSinceLastEntry() > 1
  65. array.push(bullCloseLogic, true)
  66. else
  67. array.push(bullCloseLogic, false)
  68.  
  69. //Long Trade Logic
  70. if array.every(bullEntryLogic) and (bear_bull == "Bull" or bear_bull == "Both")
  71. // label.new(bar_index, high + 20, str.tostring(currDrawDown))
  72. bullEntryPrice = close + bullEntryTicks*syminfo.mintick
  73. bullStopLoss = math.round_to_mintick(bullEntryPrice*stopLossBull)
  74.  
  75. alertComment = "Long: Stop Loss: " + str.tostring(bullStopLoss)
  76.  
  77. strategy.order("Long Open Order", strategy.long, qty=posSize, limit=bullEntryPrice, stop=bullStopLoss, comment=alertComment, alert_message=alertComment)
  78.  
  79. if strategy.position_size > 0
  80. bullStopLoss = math.round_to_mintick(strategy.opentrades.entry_price(strategy.opentrades -1)*stopLossBull)
  81. if bullTakeProfitTF == true
  82. bullTakeProfit = math.round_to_mintick(bullTakeProfitPercent*close/syminfo.mintick)
  83.  
  84. bullTPPrice = bullTakeProfit*syminfo.mintick + strategy.opentrades.entry_price(strategy.opentrades - 1)
  85. alertComment = "Long: Stop Loss: " + str.tostring(bullTPPrice) + " - Take Profit: " + str.tostring(bullTPPrice)
  86.  
  87. strategy.exit("Long StopLoss/TakeProfit", "Long Open Order", qty=math.abs(strategy.position_size),
  88. profit=bullTakeProfit, stop=bullStopLoss, comment=alertComment,alert_message=alertComment)
  89. else
  90. alertComment = "Long: Stop Loss: " + str.tostring(bullStopLoss)
  91.  
  92. strategy.exit("Long StopLoss", "Long Open Order", qty=math.abs(strategy.position_size),
  93. stop=bullStopLoss, comment=alertComment)
  94.  
  95. //Bull Close Logic
  96. if (hour==endTimeHour and minute==timeAdjust) or array.every(bullCloseLogic)
  97. strategy.order("Long Close Order", strategy.short, qty=math.abs(strategy.position_size), limit=close)
  98.  
  99.  
  100. /////////////////////////////////////////////////////////////////////////////////////
  101. //Short Enter Logic - Fill in here
  102. bearEntryLogic = array.new_bool()
  103. if timeAllow()
  104. array.push(bearEntryLogic, true)
  105. else
  106. array.push(bearEntryLogic, false)
  107.  
  108. //Short Close Logic - Fill in here
  109. bearCloseLogic = array.new_bool()
  110. if barsSinceLastEntry() > 1
  111. array.push(bearCloseLogic, true)
  112. else
  113. array.push(bearCloseLogic, false)
  114.  
  115. //Short Trade Logic
  116. if array.every(bearEntryLogic) and (bear_bull == "Bear" or bear_bull == "Both")
  117. // label.new(bar_index, high + 40, str.tostring(math.round(array.get(dailyEMAArr, 0), 2)))
  118. bearEntryPrice = close - bearEntryTicks*syminfo.mintick
  119. bearStopLoss = math.round_to_mintick(bearEntryPrice*stopLossBear)
  120.  
  121. alertComment = "Short: Stop Loss: " + str.tostring(bearStopLoss)
  122.  
  123. strategy.order("Short Open Order", strategy.short, qty=posSize, limit=bearEntryPrice, stop=bearStopLoss, comment=alertComment, alert_message=alertComment)
  124.  
  125.  
  126. if strategy.position_size < 0
  127. bearStopLoss = math.round_to_mintick(strategy.opentrades.entry_price(strategy.opentrades -1)*stopLossBear)
  128. if bearTakeProfitTF == true
  129. bearTakeProfit = math.round_to_mintick(bearTakeProfitPercent*close/syminfo.mintick)
  130.  
  131. bearTPPrice = strategy.opentrades.entry_price(strategy.opentrades - 1) - bearTakeProfit*syminfo.mintick
  132. alertComment = "Short: Stop Loss: " + str.tostring(bearStopLoss) + " - Take Profit: " + str.tostring(bearTPPrice)
  133.  
  134. strategy.exit("Short StopLoss/TakeProfit", "Short Open Order", qty=math.abs(strategy.position_size),
  135. profit=bearTakeProfit, stop=bearStopLoss, comment=alertComment, alert_message=alertComment)
  136.  
  137. // strategy.exit("Short StopLoss", "Short Open Order", qty=math.abs(strategy.position_size),
  138. // profit=bearTakeProfit, stop=bearStopLoss, comment=alertComment)
  139. else
  140. alertComment = "Short: Stop Loss: " + str.tostring(bearStopLoss)
  141.  
  142. strategy.exit("Short StopLoss", "Short Open Order", qty=math.abs(strategy.position_size),
  143. stop=bearStopLoss, comment=alertComment)
  144.  
  145. //Bear Close Logic
  146. if (hour==endTimeHour and minute==timeAdjust) or (array.every(bearCloseLogic)) //and strategy.openprofit > 350
  147. strategy.order("Short Close Order ", strategy.long, qty=math.abs(strategy.position_size), limit=close)
Advertisement
Comments
  • # text 0.12 KB | 0 0
    1. download all types of premium tradingview indicators codes available on telegram - https://t.me/tradingview_premium_indicator
Add Comment
Please, Sign In to add comment
Advertisement