Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.39 KB | None | 0 0
  1. //@version=3
  2. //study(title = "Alerts", shorttitle = "AL", overlay = true)
  3. strategy(title = "Strategy", shorttitle = "strategy", overlay = true, initial_capital=10000, currency=currency.NONE, default_qty_value=100, default_qty_type=strategy.percent_of_equity, pyramiding=1000, commission_value=0)
  4.  
  5. // === Script Here ===
  6.  
  7. len = input(33, minval=1, title="Length")
  8. src = input(close, title="Source")
  9. out = ema(src, len)
  10. lasth = highest(close, 8)
  11. lastl8 = lowest(close, 8)
  12.  
  13. short1 = cross(close,out) and close[1] > close ? 1 : 0
  14. long1 = cross(close,out) and close[1] < close ? 1 : 0
  15.  
  16. long_entry = long1 //Long Or Buy Condition Here
  17.  
  18. short_entry = short1 //Short Or Sell Condition Here
  19.  
  20. ////////////////////////////////////////////////////////////////////////////
  21.  
  22. //Replace Only If Valid Exit Conditions
  23. long_exit = short_entry //Close Long Condition Here (Optional)
  24.  
  25. short_exit = long_entry //Close Short Condition Here (Optional)
  26.  
  27. ///////////////////////////////////////////////////////////////////////////
  28. // === /END
  29.  
  30. // === Inverse Trading Positions ===
  31. inv = input(false, "Inverse Trading")
  32. // === /END
  33.  
  34. // === Long Pyramiding Control ===
  35. longo = 0
  36. longo := nz(longo[1])
  37. longc = 0
  38. longc := nz(longc[1])
  39. if (inv==false ? long_entry : short_entry)
  40. longo := longo + 1
  41. longc := 0
  42. if (inv==false ? long_exit : short_exit)
  43. longc := longc + 1
  44. longo := 0
  45. // === /END
  46.  
  47. // === Short Pyramiding Control ===
  48. shorto = 0
  49. shorto := nz(shorto[1])
  50. shortc = 0
  51. shortc := nz(shortc[1])
  52. if (inv==false ? short_entry : long_entry)
  53. shorto := shorto + 1
  54. shortc := 0
  55. if (inv==false ? short_exit : long_exit)
  56. shortc := shortc + 1
  57. shorto := 0
  58. // === /END
  59.  
  60. // === Pyramiding Settings ===
  61. pyr = input(1, title="Pyramiding Control")
  62. longCondition = (inv==false ? long_entry : short_entry) and longo <= pyr ? 1:0
  63. longX = (inv==false ? long_exit : short_exit) and longc <= pyr ? 1:0
  64. shortCondition = (inv==false ? short_entry : long_entry) and shorto <=pyr ? 1:0
  65. shortX = (inv==false ? short_exit : long_exit) and shortc <=pyr ? 1:0
  66. // === /END
  67.  
  68. // === Market Type ===
  69. isMarket = input(title="Market Type", defval="Percentage", options=["Percentage", "Points"])
  70. leverage = input(1, "Leverage")
  71. isPoints = input(title="How Many Points/Pips?", defval=4, minval=2, maxval=8)
  72. isQty = isPoints == 2 ? 100 : isPoints == 3 ? 1000 : isPoints == 4 ? 10000 : isPoints == 5 ? 100000 : isPoints == 6 ? 1000000 : isPoints == 7 ? 10000000 : isPoints == 8 ? 100000000 : 10
  73. isType = isMarket == "Percentage" ? 1 : 0
  74. qty = isType ? 100 : isQty
  75. // === /END
  76.  
  77. // === Get Last Position Price ===
  78. last_open_longCondition = na
  79. last_open_shortCondition = na
  80. last_open_longCondition := longCondition ? close : nz(last_open_longCondition[1])
  81. last_open_shortCondition := shortCondition ? close : nz(last_open_shortCondition[1])
  82. // === /END
  83.  
  84. // === Check For Long/Short ===
  85. last_longCondition = na
  86. last_shortCondition = na
  87. last_longCondition := longCondition ? time : nz(last_longCondition[1])
  88. last_shortCondition := shortCondition ? time : nz(last_shortCondition[1])
  89. in_longCondition = last_longCondition > last_shortCondition
  90. in_shortCondition = last_shortCondition > last_longCondition
  91. // === /END
  92.  
  93. // === Stop Loss ===
  94. isSL = input(false, "Stop Loss")
  95. sl = input(0, "Stop Loss", type=float) / qty
  96. long_call_sl = isType ? last_open_longCondition * (1 - sl/leverage) : last_open_longCondition - sl
  97. short_call_sl = isType ? last_open_shortCondition * (1 + sl/leverage) : last_open_shortCondition + sl
  98. long_sl = isSL and low <= long_call_sl and longCondition == 0
  99. short_sl = isSL and high >= short_call_sl and shortCondition == 0
  100. slColor = isSL and (in_longCondition or in_shortCondition) ? red : white
  101. plot(isSL and in_longCondition ? long_call_sl : na, "Long SL", slColor, style=3, linewidth=2)
  102. plot(isSL and in_shortCondition ? short_call_sl : na, "Short SL", slColor, style=3, linewidth=2)
  103. // === /END
  104.  
  105. // === Take profit ===
  106. isTP = input(false, "Take Profit")
  107. tp = input(0, "Take Profit", type=float) / qty
  108. long_call_tp = isType ? last_open_longCondition * (1 + tp/leverage) : last_open_longCondition + tp
  109. short_call_tp = isType ? last_open_shortCondition * (1 - tp/leverage) : last_open_shortCondition - tp
  110. long_tp = isTP and high >= long_call_tp and longCondition == 0
  111. short_tp = isTP and low <= short_call_tp and shortCondition == 0
  112. tpColor = isTP and (in_longCondition or in_shortCondition) ? purple : white
  113. plot(isTP and in_longCondition ? long_call_tp : na, "Long TP", tpColor, style=3, linewidth=2)
  114. plot(isTP and in_shortCondition ? short_call_tp : na, "Short TP", tpColor, style=3, linewidth=2)
  115. // === /END
  116.  
  117. // === Trailing Stop ===
  118. last_high = na
  119. last_low = na
  120. last_high_short = na
  121. last_low_short = na
  122. last_high := not in_longCondition ? na : in_longCondition and (na(last_high[1]) or high > nz(last_high[1])) ? high : nz(last_high[1])
  123. last_high_short := not in_shortCondition ? na : in_shortCondition and (na(last_high[1]) or high > nz(last_high[1])) ? high : nz(last_high[1])
  124. last_low := not in_shortCondition ? na : in_shortCondition and (na(last_low[1]) or low < nz(last_low[1])) ? low : nz(last_low[1])
  125. last_low_short := not in_longCondition ? na : in_longCondition and (na(last_low[1]) or low < nz(last_low[1])) ? low : nz(last_low[1])
  126.  
  127. isTS = input(false, "Trailing Stop")
  128. tsi = input(0, "Activate Trailing Stop Price", type=float) / qty
  129. ts = input(0, "Trailing Stop", type=float) / qty
  130. long_call_ts = isType ? last_high * (1 - ts/leverage) : last_high - ts
  131. short_call_ts = isType ? last_low * (1 + ts/leverage) : last_low + ts
  132.  
  133. long_call_tsi = isType ? last_open_longCondition * (1 + tsi/leverage) : last_open_longCondition + tsi
  134. short_call_tsi = isType ? last_open_shortCondition * (1 - tsi/leverage) : last_open_shortCondition - tsi
  135.  
  136. long_ts = isTS and not na(last_high) and low <= long_call_ts and longCondition == 0 and high >= long_call_tsi
  137. short_ts = isTS and not na(last_low) and high >= short_call_ts and shortCondition == 0 and low <= short_call_tsi
  138.  
  139. tsColor = isTS and (in_longCondition or in_shortCondition) ? blue : white
  140. tsiColor = isTS and (in_longCondition or in_shortCondition) ? white : blue
  141. plot(isTS and in_longCondition ? long_call_tsi : na, "Long Trailing", tsiColor, style=3, linewidth=2)
  142. plot(isTS and in_shortCondition ? short_call_tsi : na, "Short Trailing", tsiColor, style=3, linewidth=2)
  143. plot(isTS and in_longCondition ? long_call_ts : na, "Long Trailing", tsColor, style=2, linewidth=2)
  144. plot(isTS and in_shortCondition ? short_call_ts : na, "Short Trailing", tsColor, style=2, linewidth=2)
  145. // === /END
  146.  
  147. // === Create a single close for all the different closing conditions
  148. closelong = long_tp or long_sl or long_ts or longX ? 1 : 0
  149. closeshort = short_tp or short_sl or short_ts or shortX ? 1 : 0
  150.  
  151. // Get the time of the last close
  152. last_long_close = na
  153. last_short_close = na
  154. last_long_close := closelong ? time : nz(last_long_close[1])
  155. last_short_close := closeshort ? time : nz(last_short_close[1])
  156.  
  157. // Check for a close since your last open
  158. if closelong and last_long_close[1] > last_longCondition
  159. closelong := 0
  160. if closeshort and last_short_close[1] > last_shortCondition
  161. closeshort := 0
  162. // === /END
  163.  
  164. /////////////////////////===STUDY=CODE===///////////////////////////////////
  165. // === Signal Shapes ===
  166. plotshape(longCondition, title='OPEN LONG', style=shape.triangleup, size=size.large, color=#02CB80, location= location.belowbar)
  167. plotshape(shortCondition, title='OPEN SHORT', style=shape.triangledown, size=size.large, color=#DC143C)
  168. plotshape(closelong, title='CLOSE LONG', style=shape.triangledown, size=size.large, color=#006cff)
  169. //plotshape(closeshort, title='CLOSE SHORT', style=shape.xcross, location=location.abovebar, color=gray, text="EXIT\nSHORT\n\n\n\n", offset=0,transp=0)
  170. // === /END
  171.  
  172. // === Alarm Alerts ===
  173. alertcondition(longCondition, title='OPEN LONG', message='OPEN LONG')
  174. alertcondition(closelong, title='CLOSE LONG', message='CLOSE LONG')
  175. alertcondition(shortCondition, title='OPEN SHORT', message='OPEN SHORT')
  176. alertcondition(closeshort, title='CLOSE SHORT', message='CLOSE SHORT')
  177. // === /END
  178.  
  179. ///////////////////////===STUDY=CODE=END===/////////////////////////////////
  180. // //
  181. // REMOVE THE CODE BELOW IF THIS IS A STRATEGY //
  182. // //
  183. ////////////////////////===STRATEGY=CODE===/////////////////////////////////
  184.  
  185. // === Backtesting Dates ===
  186. testPeriodSwitch = input(false, "Custom Backtesting Dates")
  187. testStartYear = input(2017, "Backtest Start Year")
  188. testStartMonth = input(8, "Backtest Start Month")
  189. testStartDay = input(1, "Backtest Start Day")
  190. testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,0,0)
  191. testStopYear = input(2017, "Backtest Stop Year")
  192. testStopMonth = input(12, "Backtest Stop Month")
  193. testStopDay = input(31, "Backtest Stop Day")
  194. testPeriodStop = timestamp(testStopYear,testStopMonth,testStopDay,0,0)
  195. testPeriod() =>
  196. time >= testPeriodStart and time <= testPeriodStop ? true : false
  197. isPeriod = testPeriodSwitch == true ? testPeriod() : true
  198. // === /END
  199.  
  200. // === Long/Short Switch ===
  201. allowlong = input(true, "Open Long Positions")
  202. allowshort = input(false, "Open Short Positions")
  203. isAllow = allowlong==true and allowshort==true ? 0 : allowlong==true ? 1 : allowshort == true ? -1 : na
  204. // === /END
  205.  
  206. // === Strategy ===
  207. if isPeriod and isAllow==0
  208. if (longCondition)
  209. strategy.entry("Long",strategy.long)
  210. if (closelong) and not shortCondition
  211. strategy.close("Long")
  212. if (shortCondition)
  213. strategy.entry("Short",strategy.short)
  214. if (closeshort) and not longCondition
  215. strategy.close("Short")
  216.  
  217. if isPeriod and isAllow==1
  218. if (longCondition)
  219. strategy.entry("Long",strategy.long)
  220. if (closelong)
  221. strategy.close("Long")
  222.  
  223. if isPeriod and isAllow==-1
  224. if (shortCondition)
  225. strategy.entry("Short",strategy.short)
  226. if (closeshort)
  227. strategy.close("Short")
  228. // === /END
  229.  
  230. //////////////////////===STRATEGY=CODE=END===///////////////////////////////
  231. // //
  232. // ULTIMATE PINE INJECTOR //
  233. // //
  234. //////////////////////===ANION=CODE=END====/////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement