Advertisement
mossmanpete

Anion Pine Injector Code

Feb 8th, 2018
621
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.36 KB | None | 0 0
  1. //Add this below any Tradingview indicator/strategy and fill in trigger variables.
  2. //Adds stop loss, trailing stops, direction selection, backtesting time selection.
  3. //This script snippet also performs position tracking for Autoview alerts.
  4. //Now alerts only fire if it is the first entry condition before an exit condition, or so on with pyramiding.
  5.  
  6. // === Upgraded Conditions Framework ===
  7.  
  8. ////////////////////////////////////////////////////////////////////////////
  9.  
  10. long_entry = //Long Or Buy Condition Here
  11.  
  12. short_entry = //Short Or Sell Condition Here
  13.  
  14. long_exit = short_entry //Close Long Condition Here (Optional)
  15.  
  16. short_exit = long_entry //Close Short Condition Here (Optional)
  17.  
  18. ///////////////////////////////////////////////////////////////////////////
  19.  
  20. // init these values here, they will get updated later as more decisions are made
  21. last_long_close = na
  22. last_short_close = na
  23.  
  24. // === Long position detection ===
  25. // longs open
  26. longo = 0
  27. longo := nz(longo[1])
  28. // longs closed
  29. longc = 0
  30. longc := nz(longc[1])
  31. if long_entry
  32. longo := longo + 1
  33. longc := 0
  34. if long_exit
  35. longc := longc + 1
  36. longo := 0
  37. // === /END
  38.  
  39. // === Short position detection ===
  40. shorto = 0
  41. shorto := nz(shorto[1])
  42. shortc = 0
  43. shortc := nz(shortc[1])
  44. if short_entry
  45. shorto := shorto + 1
  46. shortc := 0
  47. if short_exit
  48. shortc := shortc + 1
  49. shorto := 0
  50. // === /END
  51.  
  52. // === Pyramiding Settings ===
  53. pyr = input(100, title="Pyramiding Setting")
  54. //pyr = 1
  55. longCondition = long_entry and longo <= pyr
  56. longX = long_exit and longc <= pyr
  57. shortCondition = short_entry and shorto <=pyr
  58. shortX = short_exit and shortc <=pyr
  59. // === /END
  60.  
  61. // === Get Last Position Price ===
  62. last_open_longCondition = na
  63. last_open_shortCondition = na
  64. // last open prices
  65. last_open_longCondition := longCondition ? close : nz(last_open_longCondition[1])
  66. last_open_shortCondition := shortCondition ? close : nz(last_open_shortCondition[1])
  67. // === /END
  68.  
  69. // === Check For Long/Short ===
  70. last_longCondition = na
  71. last_shortCondition = na
  72. // last open times
  73. last_longCondition := longCondition ? time : nz(last_longCondition[1])
  74. last_shortCondition := shortCondition ? time : nz(last_shortCondition[1])
  75. last_longClose = longX ? time : nz(last_long_close[1])
  76. last_shortClose = shortX ? time : nz(last_short_close[1])
  77.  
  78. in_longCondition = last_longCondition > last_shortCondition and last_longCondition >= last_longClose
  79. in_shortCondition = last_shortCondition > last_longCondition and last_shortCondition >= last_shortClose
  80. // === /END
  81.  
  82. // === Stop Loss (Long) ===
  83. isSLl = input(true, "Stop Loss (Long)")
  84. sll = input(6, "Stop Loss %", type=float, step=0.2, minval=0, maxval=100) / 100
  85. long_call_sl = last_open_longCondition * (1 - sll)
  86. long_sl = isSLl and low <= long_call_sl and longCondition == 0
  87. // === /END
  88.  
  89. // === Stop Loss (Short) ===
  90. isSLs = input(false, "Stop Loss (Short)")
  91. sls = input(6, "Stop Loss %", type=float, step=0.2, minval=0, maxval=100) / 100
  92. short_call_sl = last_open_shortCondition * (1 + sls)
  93. short_sl = isSLs and high >= short_call_sl and shortCondition == 0
  94. // === /END
  95.  
  96. // === Trailing Stop ===
  97. last_high = na
  98. last_low = na
  99. last_high := in_longCondition ? (na(last_high[1]) or high > nz(last_high[1])) ? high : nz(last_high[1]) : na
  100. last_low := in_shortCondition ? (na(last_low[1]) or low < nz(last_low[1])) ? low : nz(last_low[1]) : na
  101. isTSl = input(true, "Trailing Stop Long")
  102. tsil = input(25, "Activate Trailing Stop % Long", type=float, step=1, minval=0, maxval=100) / 100
  103. tsl = input(8, "Trailing Stop % Long", type=float, step=1, minval=0, maxval=100) / 100
  104. long_call_ts = last_high * (1 - tsl)
  105. long_call_tsi = last_open_longCondition * (1 + tsil)
  106. long_ts = isTSl and not na(last_high) and low <= long_call_ts and longCondition == 0 and last_high >= long_call_tsi
  107. isTSs = input(true, "Trailing Stop Short")
  108. tsis = input(25, "Activate Trailing Stop % Short", type=float, step=1, minval=0, maxval=100) / 100
  109. tss = input(8, "Trailing Stop % Short", type=float, step=1, minval=0, maxval=100) / 100
  110. short_call_ts = last_low * (1 + tss)
  111. short_call_tsi = last_open_shortCondition * (1 - tsis)
  112. short_ts = isTSs and not na(last_low) and high >= short_call_ts and shortCondition == 0 and last_low <= short_call_tsi
  113. // === /END
  114.  
  115. // === Create Single Close For All Closing Conditions ===
  116. closelong = long_sl or long_ts or longX
  117. closeshort = short_sl or short_ts or shortX
  118.  
  119. // Get Last Close
  120. last_long_close := closelong ? time : nz(last_long_close[1])
  121. last_short_close := closeshort ? time : nz(last_short_close[1])
  122.  
  123. // Check For Close Since Last Open
  124. if closelong and last_long_close[1] > last_longCondition
  125. closelong := 0
  126.  
  127. if closeshort and last_short_close[1] > last_shortCondition
  128. closeshort := 0
  129. // === /END
  130.  
  131. ////////////////////////////////////////////////////////////////////////////
  132.  
  133. // === Alarm Settings ===
  134. alertcondition(longCondition==1, title='LONG', message='LONG')
  135. alertcondition(closelong==1, title='EXIT LONG', message='EXIT LONG')
  136. alertcondition(shortCondition==1, title='SHORT', message='SHORT')
  137. alertcondition(closeshort==1, title='EXIT SHORT', message='EXIT SHORT')
  138. // === /END
  139.  
  140. ////////////////////////////////////////////////////////////////////////////
  141.  
  142. // === Visuals & Debugs Here ===
  143. //Remove "//" To Check/Debug The Code Above
  144. // Signal Shapes
  145. //plotshape(longCondition[1]==1, title='LONG', style=shape.triangleup, size=size.large, color=#02CB80, location= location.belowbar)
  146. //plotshape(shortCondition[1]==1, title='SHORT', style=shape.triangledown, size=size.large, color=#DC143C, location=location.abovebar)
  147. //plotshape(shortCondition[1]==0 and closelong[1]==1, title='EXIT LONG', style=shape.xcross, color=#02CB80, location=location.belowbar, transp=0)
  148. //plotshape(longCondition[1]==0 and closeshort[1]==1, title='EXIT SHORT', style=shape.xcross, color=#DC143C, location=location.abovebar, transp=0)
  149. // SL Plot
  150. //slColor = (isSLl or isSLs) and (in_longCondition or in_shortCondition) ? red : white
  151. //plot(isSLl and in_longCondition ? long_call_sl : na, "Long SL", slColor, style=3, linewidth=2)
  152. //plot(isSLs and in_shortCondition ? short_call_sl : na, "Short SL", slColor, style=3, linewidth=2)
  153. // TP Plot
  154. //tpColor = isTP and (in_longCondition or in_shortCondition) ? purple : white
  155. //plot(isTP and in_longCondition ? long_call_tp : na, "Long TP", tpColor, style=3, linewidth=2)
  156. //plot(isTP and in_shortCondition ? short_call_tp : na, "Short TP", tpColor, style=3, linewidth=2)
  157. // TS Plot
  158. //tsColor = (isTSl or isTSs) and (in_longCondition or in_shortCondition) ? orange : white
  159. //tsiColor = (isTSl or isTSs) and (in_longCondition or in_shortCondition) ? white : orange
  160. //plot(isTSl and in_longCondition ? long_call_tsi : na, "Long Trailing", tsiColor, style=3, linewidth=2)
  161. //plot(isTSs and in_shortCondition ? short_call_tsi : na, "Short Trailing", tsiColor, style=3, linewidth=2)
  162. //plot(isTSl and in_longCondition and last_high > long_call_tsi ? long_call_ts : na, "Long Trailing", tsColor, style=2, linewidth=2)
  163. //plot(isTSs and in_shortCondition and last_low < short_call_tsi ? short_call_ts : na, "Short Trailing", tsColor, style=2, linewidth=2)
  164. // === /END
  165.  
  166. ////////////////////////////////////////////////////////////////////////////
  167. // //
  168. // REMOVE THE CODE BELOW FOR STUDY CONVERSION //
  169. // //
  170. ////////////////////////////////////////////////////////////////////////////
  171.  
  172. // === Strategy Direction Switch ===
  173. direction = input(title = "Strategy Direction", defval="Long", options=["Both", "Long", "Short"])
  174. // === /END
  175.  
  176. // === Backtesting Dates ===
  177. testPeriodSwitch = input(false, "Custom Backtesting Dates")
  178. testStartYear = input(2017, "Backtest Start Year")
  179. testStartMonth = input(8, "Backtest Start Month")
  180. testStartDay = input(1, "Backtest Start Day")
  181. testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,0,0)
  182. testStopYear = input(2018, "Backtest Stop Year")
  183. testStopMonth = input(1, "Backtest Stop Month")
  184. testStopDay = input(14, "Backtest Stop Day")
  185. testPeriodStop = timestamp(testStopYear,testStopMonth,testStopDay,0,0)
  186. testPeriod() =>
  187. time >= testPeriodStart and time <= testPeriodStop ? true : false
  188. isPeriod = testPeriodSwitch == true ? testPeriod() : true
  189. // === /END
  190.  
  191. // === Strategy ===
  192. if isPeriod and direction=="Both"
  193. if (longCondition)
  194. strategy.entry("Long",strategy.long)
  195. if (closelong) and not shortCondition
  196. strategy.close("Long")
  197. if (shortCondition)
  198. strategy.entry("Short",strategy.short)
  199. if (closeshort) and not longCondition
  200. strategy.close("Short")
  201.  
  202. if isPeriod and direction=="Long"
  203. if (longCondition)
  204. strategy.entry("Long",strategy.long)
  205. if (closelong)
  206. strategy.close("Long")
  207.  
  208. if isPeriod and direction=="Short"
  209. if (shortCondition)
  210. strategy.entry("Short",strategy.short)
  211. if (closeshort)
  212. strategy.close("Short")
  213. // === /END
  214.  
  215. ////////////////////////////////////////////////////////////////////////////
  216. // //
  217. // ULTIMATE PINE INJECTOR V1.2 //
  218. // //
  219. //////////////////////===ANION=CODE=END====/////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement