Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 33.22 KB | None | 0 0
  1. //@version=3
  2. //
  3. //This script combines two different strategies for combined force. Triggers can be modified to AND/OR statements.
  4. //Checks the HullMA on 3 correlated assets for signals. Checks Ichimoku cloud score to buy low and sell high.
  5. //Ichimoku score must be above Sell or below Buy threshold for HullMA event to trigger an alert.
  6. //Designed for Buy-Sell, not shorting. May need to be modified to fit a short strategy.
  7. //Benchmark defaults on BTCUSD 1H chart.
  8. //
  9. //Toggle these next 2 lines and comment out section at the bottom for study version.
  10. //study(title = "Triple HullMA + Ichimoku Alerts", shorttitle = "3x HullMA + Ichi AL", overlay = true)
  11. strategy(title="Triple HullMA + Ichimoku Cloud Signal Score", shorttitle="3x HullMA + Ichi", overlay=false, initial_capital=100000, precision=8, currency=currency.NONE, default_qty_value=100, default_qty_type=strategy.percent_of_equity, pyramiding=0, commission_value=0.2)
  12.  
  13. // == inputs ==
  14. bst = input(-5.5, "Buy Threshold (Below Ichi)", type=float, step=0.1, minval=-13, maxval=100)
  15. sst = input(-1.9, "Sell Threshold (Above Ichi)", type=float, step=0.1, minval=-13, maxval=100)
  16. keh = input(title="HullMA Length (~7)", type=integer, defval=8, minval=1) //original default: 7
  17. secThree=input(title="Trading Pair (MA)", type=symbol, defval="COINBASE:BTCUSD")
  18. secOne=input(title="USD Strength (MA)", type=symbol, defval="DXY")
  19. secTwo=input(title="Gold Standard (MA)", type=symbol, defval="XAUUSD")
  20. tf = input(title="Resolution (MA)", type=resolution, defval="180") //1Week=W 1Day=D 4HR=240 3HR=180 2HR=120 (default 60)
  21. dt = input(0.000, title="Decision Threshold (MA)", type=float, step=0.001, minval=0.001) //Decision Threshold for confidence calculation.
  22. long_conf = input(0.006, title="(DT+) Long Confidence", type=float, step=0.001, minval=0.001) //Modifies dt on long trigger
  23. short_conf = input(0.004,title="(DT+) Short Confidence", type=float, step=0.001, minval=0.001) //Modifies dt on short trigger
  24. direction = input(title = "Strategy Direction", defval="Long", options=["Both", "Long", "Short"])// Strategy Direction Switch
  25. ot=1
  26. src = ohlc4[1]
  27.  
  28.  
  29. // === HullMA Triple Correlation Script Here ===
  30. //
  31. ////Currency Strength
  32. DXY=(security(secOne, tf, src)) //USDollar index... Quote currency strength.
  33. e2ma=2*wma(DXY,round(keh/2))
  34. ema=wma(DXY,keh)
  35. ediff=e2ma-ema
  36. esqn=round(sqrt(keh))
  37. e2ma1=2*wma(DXY[1],round(keh/2))
  38. ema1=wma(DXY[1],keh)
  39. ediff1=e2ma1-ema1
  40. esqn1=round(sqrt(keh))
  41. e1=wma(ediff,esqn)
  42. e2=wma(ediff1,esqn)
  43. ////Gold Strength
  44. XAU=(security(secTwo, tf, src)) //Gold:USD or other correlated asset.
  45. n2ma=2*wma(XAU,round(keh/2))
  46. nma=wma(XAU,keh)
  47. ndiff=n2ma-nma
  48. nsqn=round(sqrt(keh))
  49. n2ma1=2*wma(XAU[1],round(keh/2))
  50. nma1=wma(XAU[1],keh)
  51. ndiff1=n2ma1-nma1
  52. nsqn1=round(sqrt(keh))
  53. n1=wma(ndiff,nsqn)
  54. n2=wma(ndiff1,nsqn)
  55. ////Currency pair being traded on chart.
  56. CUR=(security(secThree, tf, src)) //Crypto:USD pair being traded.
  57. s2ma=2*wma(CUR,round(keh/2))
  58. sma=wma(CUR,keh)
  59. sdiff=s2ma-sma
  60. ssqn=round(sqrt(keh))
  61. s2ma1=2*wma(CUR[1],round(keh/2))
  62. sma1=wma(CUR[1],keh)
  63. sdiff1=s2ma1-sma1
  64. ssqn1=round(sqrt(keh))
  65. s1=wma(sdiff,ssqn)
  66. s2=wma(sdiff1,ssqn)
  67. confidence=(security(tickerid, 'D', src)-security(tickerid, 'D', src[1]))/security(tickerid, 'D', src[1])
  68. b=s1>s2?lime:black
  69. c=s1>s2?black:red
  70.  
  71. // === Hull Ribbon Visual === Uncomment to see on chart
  72.  
  73. //c1=plot(s1, color=b, title="hull1", linewidth=2, transp=0)
  74. //c2=plot(s2, color=c, title="hull1", linewidth=2, transp=0)
  75. //fill(c1, c2, color=c, transp=75)
  76. // === END ===
  77.  
  78.  
  79. // === Ichimoku Script Here ===
  80.  
  81. //This strategy could be improved by having it check the score from another timeframe for multiple confirmations.
  82. //This should be improved with some way to determine if the 'bst' & 'sst' are being reached on the way up or down
  83. //to prevent buying at the beginning of the dip versus at the end...
  84. //The bst could trigger a trailing limit. The sst could trigger a Trailing Stop Loss.
  85.  
  86.  
  87. //fall = input(3, "Falling Score Take Profit", type=float, step=0.1, minval=-12, maxval=100)
  88. conversionPeriods = input(20, minval=1, title="Conversion Line Periods"),
  89. basePeriods = input(60, minval=1, title="Base Line Periods")
  90. laggingSpan2Periods = input(120, minval=1, title="Lagging Span 2 Periods"),
  91. displacement = input(60, minval=1, title="Displacement")
  92.  
  93. //Used to plot lines on chart, should replace bst & sst. (update)
  94. usl = input(5, minval=1, title="Upper Line Plot", type=float, step=0.1),
  95. lsl = input(-5, maxval=-1, title="Lower Line Plot", type=float, step=0.1),
  96.  
  97. // == helpers ==
  98.  
  99. donchian(len) => avg(lowest(len), highest(len))
  100.  
  101. resolve(src, default) =>
  102. if na(src)
  103. default
  104. else
  105. src
  106.  
  107. // == generate ichimoku data ==
  108.  
  109. conversionLine = donchian(conversionPeriods)
  110. baseLine = donchian(basePeriods)
  111.  
  112. // NOTE: these senkou spans are for signal generation; not for plotting. this is
  113. // due to limitations of pinescript
  114. //
  115. // i.e. Senkou Span A
  116. leadLine1 = offset(avg(conversionLine, baseLine), displacement)
  117. // i.e. Senkou Span B
  118. leadLine2 = offset(donchian(laggingSpan2Periods), displacement)
  119.  
  120.  
  121. // == plot ichimoku == Uncomment to see on chart.
  122.  
  123. // // i.e. Tenkan Sen (turning line) (blue)
  124. // plot(conversionLine, color=#0496ff, title="Turning/Conversion Line", linewidth=3)
  125. // // i.e. Kijun Sen (base/standard line) (red)
  126. // plot(baseLine, color=#991515, title="Standard/Base Line", linewidth=3)
  127. // // i.g. Chikou Span (lagging line) (green)
  128. // plot(close, offset = -displacement, color=#459915, title="Lagging Span", linewidth=3)
  129.  
  130. // plot(conversionLine, color=#0496ff, title="Conversion Line")
  131. // plot(baseLine, color=#991515, title="Base Line")
  132.  
  133. // real_leadLine1 = avg(conversionLine, baseLine)
  134. // real_leadLine2 = donchian(laggingSpan2Periods)
  135.  
  136. // // i.e. Senkou Span A
  137. // p1 = plot(real_leadLine1, offset = displacement, color=green, title="Lead 1")
  138. // // i.e. Senkou Span B
  139. // p2 = plot(real_leadLine2, offset = displacement, color=red, title="Lead 2")
  140.  
  141. // // i.e. Kumo cloud colouring
  142. // fill(p1, p2, color = real_leadLine1 > real_leadLine2 ? green : red)
  143.  
  144. // == ichimoku cloud signals ==
  145. // source: http://www.ichimokutrader.com/signals.html
  146.  
  147. // == Tenkan Sen (turning line) / Kijun Sen (standard line) Cross ==
  148.  
  149. // tk_cross_bull = (conversionLine[1] < baseLine[1] and conversionLine >= baseLine)
  150. // tk_cross_bear = (conversionLine[1] > baseLine[1] and conversionLine <= baseLine)
  151. tk_cross_bull = crossover(conversionLine, baseLine)
  152. tk_cross_bear = crossunder(conversionLine, baseLine)
  153.  
  154. // get true y-coord of the cross
  155. cross_y = (conversionLine[1] * (baseLine - baseLine[1]) - baseLine[1] * (conversionLine - conversionLine[1])) / ((baseLine - baseLine[1]) - (conversionLine - conversionLine[1]))
  156.  
  157. tk_cross_below_kumo = cross_y <= leadLine2[1] and cross_y <= leadLine1[1] and cross_y <= leadLine2 and cross_y <= leadLine1
  158. tk_cross_above_kumo = cross_y >= leadLine2[1] and cross_y >= leadLine1[1] and cross_y >= leadLine2 and cross_y >= leadLine1
  159. tk_cross_inside_kumo = (not tk_cross_below_kumo) and (not tk_cross_above_kumo)
  160.  
  161. tk_cross_score = 0.0
  162. tk_cross_score := resolve(tk_cross_score[1], 0)
  163.  
  164. use_tk_cross = input(true, title="TS Cross")
  165. tk_cross_weight = input(1.0, title="TS Cross Importance Weight", type=float, step=0.1)
  166.  
  167. tk_cross_weak_bullish_points = input(0.5, title="TS Cross Weak Bullish Points", type=float, step=0.1)
  168. tk_cross_neutral_bullish_points = input(1.0, title="TS Cross Neutral Bullish Points", type=float, step=0.1)
  169. tk_cross_strong_bullish_points = input(2.0, title="TS Cross Strong Bullish Points", type=float, step=0.1)
  170.  
  171. tk_cross_weak_bearish_points = input(-0.5, title="TS Cross Weak Bearish Points", type=float, step=0.1)
  172. tk_cross_neutral_bearish_points = input(-1.0, title="TS Cross Neutral Bearish Points", type=float, step=0.1)
  173. tk_cross_strong_bearish_points = input(-2.0, title="TS Cross Strong Bearish Points", type=float, step=0.1)
  174.  
  175. // == TK Cross / weak bullish (below kumo) ==
  176. tk_cross_score := (tk_cross_bull and tk_cross_below_kumo) ? tk_cross_weak_bullish_points : tk_cross_score
  177.  
  178. // == TK Cross / neutral bullish (inside kumo) ==
  179. tk_cross_score := (tk_cross_bull and tk_cross_inside_kumo) ? tk_cross_neutral_bullish_points : tk_cross_score
  180.  
  181. // == TK Cross / strong bullish (above kumo) ==
  182. tk_cross_score := (tk_cross_bull and tk_cross_above_kumo) ? tk_cross_strong_bullish_points : tk_cross_score
  183.  
  184. // == TK Cross / strong bearish (below kumo) ==
  185. tk_cross_score := (tk_cross_bear and tk_cross_below_kumo) ? tk_cross_strong_bearish_points : tk_cross_score
  186.  
  187. // == TK Cross / neutral bearish (inside kumo) ==
  188. tk_cross_score := (tk_cross_bear and tk_cross_inside_kumo) ? tk_cross_neutral_bearish_points : tk_cross_score
  189.  
  190. // == TK Cross / weak bearish (above kumo) ==
  191. tk_cross_score := (tk_cross_bear and tk_cross_above_kumo) ? tk_cross_weak_bearish_points : tk_cross_score
  192.  
  193.  
  194. // == Price and Kijun Sen (standard line) Cross ==
  195.  
  196. //pk_cross_bull = (close[1] < baseLine[1] and close >= baseLine)
  197. //pk_cross_bear = (close[1] > baseLine[1] and close <= baseLine)
  198. pk_cross_bull = crossover(close, baseLine)
  199. pk_cross_bear = crossunder(close, baseLine)
  200.  
  201. cross_pk_y = (close[1] * (baseLine - baseLine[1]) - baseLine[1] * (close - close[1])) / ((baseLine - baseLine[1]) - (close - close[1]))
  202.  
  203. pk_cross_below_kumo = cross_pk_y <= leadLine2[1] and cross_pk_y <= leadLine1[1] and cross_pk_y <= leadLine2 and cross_pk_y <= leadLine1
  204. pk_cross_above_kumo = cross_pk_y >= leadLine2[1] and cross_pk_y >= leadLine1[1] and cross_pk_y >= leadLine2 and cross_pk_y >= leadLine1
  205. pk_cross_inside_kumo = (not pk_cross_below_kumo) and (not pk_cross_above_kumo)
  206.  
  207. pk_cross_score = 0.0
  208. pk_cross_score := resolve(pk_cross_score[1], 0)
  209.  
  210. use_pk_cross = input(true, title="PS Cross")
  211. pk_cross_weight = input(1.0, title="PS Cross Importance Weight", type=float, step=0.1)
  212.  
  213. pk_cross_weak_bullish_points = input(0.5, title="PS Cross Weak Bullish Points", type=float, step=0.1)
  214. pk_cross_neutral_bullish_points = input(1.0, title="PS Cross Neutral Bullish Points", type=float, step=0.1)
  215. pk_cross_strong_bullish_points = input(2.0, title="PS Cross Strong Bullish Points", type=float, step=0.1)
  216.  
  217. pk_cross_weak_bearish_points = input(-0.5, title="PS Cross Weak Bearish Points", type=float, step=0.1)
  218. pk_cross_neutral_bearish_points = input(-1.0, title="PS Cross Neutral Bearish Points", type=float, step=0.1)
  219. pk_cross_strong_bearish_points = input(-2.0, title="PS Cross Strong Bearish Points", type=float, step=0.1)
  220.  
  221. // == PK Cross / weak bullish (below kumo) ==
  222. pk_cross_score := (pk_cross_bull and pk_cross_below_kumo) ? pk_cross_weak_bullish_points : pk_cross_score
  223.  
  224. // == PK Cross / neutral bullish (inside kumo)
  225. pk_cross_score := (pk_cross_bull and pk_cross_inside_kumo) ? pk_cross_neutral_bullish_points : pk_cross_score
  226.  
  227. // == PK Cross / strong bullish (above kumo)
  228. pk_cross_score := (pk_cross_bull and pk_cross_above_kumo) ? pk_cross_strong_bullish_points : pk_cross_score
  229.  
  230. // == PK Cross / strong bearish (below kumo)
  231. pk_cross_score := (pk_cross_bear and pk_cross_below_kumo) ? pk_cross_strong_bearish_points : pk_cross_score
  232.  
  233. // == PK Cross / neutral bearish (inside kumo)
  234. pk_cross_score := (pk_cross_bear and pk_cross_inside_kumo) ? pk_cross_neutral_bearish_points : pk_cross_score
  235.  
  236. // == PK Cross / weak bearish (above kumo)
  237. pk_cross_score := (pk_cross_bear and pk_cross_above_kumo) ? pk_cross_weak_bearish_points : pk_cross_score
  238.  
  239. // == Kumo Breakouts ==
  240.  
  241. kumo_bull = (crossover(close, leadLine1) and leadLine1 > leadLine2) or (crossover(close, leadLine2) and leadLine2 > leadLine1)
  242. kumo_bear = (crossunder(close, leadLine2) and leadLine1 > leadLine2) or (crossunder(close, leadLine1) and leadLine2 > leadLine1)
  243.  
  244. kumo_breakout_score = 0.0
  245. kumo_breakout_score := resolve(kumo_breakout_score[1], 0)
  246.  
  247. use_kumo_breakout = input(true, title="Kumo Breakout")
  248. kumo_breakout_weight = input(1.0, title="Kumo Breakout Importance Weight", type=float, step=0.1)
  249.  
  250. kumo_breakout_bullish_points = input(1.0, title="Kumo Breakout Bullish Points", type=float, step=0.1)
  251. kumo_breakout_bearish_points = input(-1.0, title="Kumo Breakout Bearish Points", type=float, step=0.1)
  252.  
  253. price_below_kumo = (close < leadLine2 and close < leadLine1)
  254. price_above_kumo = (close > leadLine2 and close > leadLine1)
  255. price_inside_kumo = (not price_below_kumo) and (not price_above_kumo)
  256.  
  257. kumo_breakout_score := (kumo_bull and price_above_kumo) ? kumo_breakout_bullish_points : kumo_breakout_score
  258. kumo_breakout_score := (kumo_bear and price_below_kumo) ? kumo_breakout_bearish_points : kumo_breakout_score
  259.  
  260. // == Senkou Span Cross ==
  261. // The Senkou Span Cross signal occurs when the Senkou Span A (1st leading line) crosses the Senkou Span B (2nd leading line).
  262. // NOTE: this cross occurs ahead of the price, since it's displaced to the right; this displacement must be removed
  263.  
  264. // i.e. Senkou Span A (no displacement)
  265. no_dp_leadLine1 = avg(conversionLine, baseLine)
  266. // i.e. Senkou Span B (no displacement)
  267. no_dp_leadLine2 = donchian(laggingSpan2Periods)
  268. // TODO: debug;remove
  269. // plot(no_dp_leadLine1)
  270. // plot(no_dp_leadLine2)
  271.  
  272. lead_line_cross_bull = crossover(no_dp_leadLine1, no_dp_leadLine2)
  273. lead_line_cross_bear = crossunder(no_dp_leadLine1, no_dp_leadLine2)
  274. price_below_kumo := (close < no_dp_leadLine2 and close < no_dp_leadLine1)
  275. price_above_kumo := (close > no_dp_leadLine2 and close > no_dp_leadLine1)
  276. price_inside_kumo := (not price_below_kumo) and (not price_above_kumo)
  277. // price_inside_kumo = (no_dp_leadLine2 < close and close < no_dp_leadLine1) and (no_dp_leadLine1 < close and close < no_dp_leadLine2)
  278.  
  279. span_cross_score = 0.0
  280. span_cross_score := resolve(span_cross_score[1], 0)
  281.  
  282. use_span_cross = input(true, title="Span Cross")
  283. span_cross_weight = input(1.0, title="Span Cross Importance Weight", type=float, step=0.1)
  284.  
  285. span_cross_weak_bullish_points = input(0.5, title="Span Cross Weak Bullish Points", type=float, step=0.1)
  286. span_cross_neutral_bullish_points = input(1.0, title="Span Cross Neutral Bullish Points", type=float, step=0.1)
  287. span_cross_strong_bullish_points = input(2.0, title="Span Cross Strong Bullish Points", type=float, step=0.1)
  288.  
  289. span_cross_weak_bearish_points = input(-0.5, title="Span Cross Weak Bearish Points", type=float, step=0.1)
  290. span_cross_neutral_bearish_points = input(-1.0, title="Span Cross Neutral Bearish Points", type=float, step=0.1)
  291. span_cross_strong_bearish_points = input(-2.0, title="Span Cross Strong Bearish Points", type=float, step=0.1)
  292.  
  293. // == Senkou Span Cross / weak bullish (price below kumo) ==
  294. span_cross_score := (lead_line_cross_bull and price_below_kumo) ? span_cross_weak_bullish_points : span_cross_score
  295.  
  296. // == Senkou Span Cross / neutral bullish (price inside kumo) ==
  297. span_cross_score := (lead_line_cross_bull and price_inside_kumo) ? span_cross_neutral_bullish_points : span_cross_score
  298.  
  299. // == Senkou Span Cross / strong bullish (price above kumo) ==
  300. span_cross_score := (lead_line_cross_bull and price_above_kumo) ? span_cross_strong_bullish_points : span_cross_score
  301.  
  302. // == Senkou Span Cross / weak bearish (price above kumo) ==
  303. span_cross_score := (lead_line_cross_bear and price_above_kumo) ? span_cross_weak_bearish_points : span_cross_score
  304.  
  305. // == Senkou Span Cross / neutral bearish (price inside kumo) ==
  306. span_cross_score := (lead_line_cross_bear and price_inside_kumo) ? span_cross_neutral_bearish_points : span_cross_score
  307.  
  308. // == Senkou Span Cross / strong bearish (price below kumo) ==
  309. span_cross_score := (lead_line_cross_bear and price_below_kumo) ? span_cross_strong_bearish_points : span_cross_score
  310.  
  311. // == Chikou Span Cross ==
  312. // The Chikou Span Cross signal occurs when the Chikou Span (Lagging line) rises above or falls below the price.
  313.  
  314. past_price = offset(close, displacement)
  315. lag_line_bull_cross = close > close[displacement]
  316. lag_line_bear_cross = close < close[displacement]
  317.  
  318. // TODO: debug; remove
  319. // plot(close)
  320. // plot(close[displacement], linewidth=2)
  321.  
  322. past_price_below_kumo = (past_price < leadLine2 and past_price < leadLine1)
  323. past_price_above_kumo = (past_price > leadLine2 and past_price > leadLine1)
  324. past_price_inside_kumo = (leadLine2 < past_price and past_price < leadLine1) and (leadLine1 < past_price and past_price < leadLine2)
  325.  
  326. lag_line_cross_score = 0.0
  327. lag_line_cross_score := resolve(lag_line_cross_score[1], 0)
  328.  
  329. use_lag_line = input(true, title="Lag Line Cross")
  330. lag_line_cross_weight = input(1.0, title="Lag Line Cross Importance Weight", type=float, step=0.1)
  331.  
  332. lag_line_cross_weak_bullish_points = input(0.5, title="Lag Line Cross Weak Bullish Points", type=float, step=0.1)
  333. lag_line_cross_neutral_bullish_points = input(1.0, title="Lag Line Cross Neutral Bullish Points", type=float, step=0.1)
  334. lag_line_cross_strong_bullish_points = input(2.0, title="Lag Line Cross Strong Bullish Points", type=float, step=0.1)
  335.  
  336. lag_line_cross_weak_bearish_points = input(-0.5, title="Lag Line Cross Weak Bearish Points", type=float, step=0.1)
  337. lag_line_cross_neutral_bearish_points = input(-1.0, title="Lag Line Cross Neutral Bearish Points", type=float, step=0.1)
  338. lag_line_cross_strong_bearish_points = input(-2.0, title="Lag Line Cross Strong Bearish Points", type=float, step=0.1)
  339.  
  340. // == Chikou Span Cross / weak bullish (price below kumo)
  341. lag_line_cross_score := (lag_line_bull_cross and past_price_below_kumo) ? lag_line_cross_weak_bullish_points : lag_line_cross_score
  342.  
  343. // == Chikou Span Cross / neutral bullish (price inside kumo)
  344. lag_line_cross_score := (lag_line_bull_cross and past_price_inside_kumo) ? lag_line_cross_neutral_bullish_points : lag_line_cross_score
  345.  
  346. // == Chikou Span Cross / strong bullish (price above kumo)
  347. lag_line_cross_score := (lag_line_bull_cross and past_price_above_kumo) ? lag_line_cross_strong_bullish_points : lag_line_cross_score
  348.  
  349. // == Chikou Span Cross / weak bearish (price above kumo)
  350. lag_line_cross_score := (lag_line_bear_cross and past_price_above_kumo) ? lag_line_cross_weak_bearish_points : lag_line_cross_score
  351.  
  352. // == Chikou Span Cross / neutral bearish (price inside kumo)
  353. lag_line_cross_score := (lag_line_bear_cross and past_price_inside_kumo) ? lag_line_cross_neutral_bearish_points : lag_line_cross_score
  354.  
  355. // == Chikou Span Cross / strong bearish (price below kumo)
  356. lag_line_cross_score := (lag_line_bear_cross and past_price_below_kumo) ? lag_line_cross_strong_bearish_points : lag_line_cross_score
  357.  
  358. // == lag line releative to cloud ==
  359.  
  360. use_lag_line_location = input(true, title="Lag Line Relative to Cloud")
  361.  
  362. lag_line_location_weight = input(1.0, title="Lag Line Relative to Cloud Importance Weight", type=float, step=0.1)
  363.  
  364. lag_line_location_above_points = input(1.0, title="Lag Line Above Cloud Points", type=float, step=0.1)
  365. lag_line_location_inside_points = input(0, title="Lag Line Inside Cloud Points", type=float, step=0.1)
  366. lag_line_location_below_points = input(-1.0, title="Lag Line Below Cloud Points", type=float, step=0.1)
  367.  
  368. lag_line_placement_score = 0.0
  369. lag_line_placement_score := resolve(lag_line_placement_score[1], 0)
  370.  
  371. lag_line_placement_score := past_price_above_kumo ? lag_line_location_above_points : lag_line_placement_score
  372. lag_line_placement_score := past_price_inside_kumo ? lag_line_location_inside_points : lag_line_placement_score
  373. lag_line_placement_score := past_price_below_kumo ? lag_line_location_below_points : lag_line_placement_score
  374.  
  375. // == price relative to cloud ==
  376.  
  377. price_placement_score = 0.0
  378. price_placement_score := resolve(price_placement_score[1], 0)
  379.  
  380. use_price_location = input(true, title="Price Relative to Cloud")
  381. price_location_weight = input(1.0, title="Price Relative to Cloud Importance Weight", type=float, step=0.1)
  382.  
  383. price_location_above_points = input(1.0, title="Price Above Cloud Points", type=float, step=0.1)
  384. price_location_inside_points = input(0, title="Price Inside Cloud Points", type=float, step=0.1)
  385. price_location_below_points = input(-1.0, title="Price Below Cloud Points", type=float, step=0.1)
  386.  
  387. price_below_kumo := (close < leadLine2 and close < leadLine1)
  388. price_above_kumo := (close > leadLine2 and close > leadLine1)
  389. price_inside_kumo := (not price_below_kumo) and (not price_above_kumo)
  390.  
  391. price_placement_score := price_above_kumo ? price_location_above_points : price_placement_score
  392. price_placement_score := price_inside_kumo ? price_location_inside_points : price_placement_score
  393. price_placement_score := price_below_kumo ? price_location_below_points : price_placement_score
  394.  
  395. // == plot score ==
  396. use_tk_cross_bool = use_tk_cross ? 1 : 0
  397. use_pk_cross_bool = use_pk_cross ? 1 : 0
  398. use_kumo_breakout_bool = use_kumo_breakout ? 1 : 0
  399. use_span_cross_bool = use_span_cross ? 1 : 0
  400. use_lag_line_bool = use_lag_line ? 1 : 0
  401. use_price_location_bool = use_price_location ? 1 : 0
  402. use_lag_line_location_bool = use_lag_line_location ? 1 : 0
  403.  
  404. total_score = (use_tk_cross_bool * tk_cross_weight * tk_cross_score) + (use_pk_cross_bool * pk_cross_weight * pk_cross_score) + (use_kumo_breakout_bool * kumo_breakout_weight * kumo_breakout_score) + (use_span_cross_bool * span_cross_weight * span_cross_score) + (use_lag_line_bool * lag_line_cross_weight * lag_line_cross_score) + (use_price_location_bool * price_location_weight * price_placement_score)
  405. total_score := total_score + (use_lag_line_location_bool * lag_line_location_weight * lag_line_placement_score)
  406.  
  407. upperSignalLine = hline(usl, title="Upper Sig Line")
  408. lowerSignalLine = hline(lsl, title="Lower Sig Line")
  409.  
  410. plotColor = total_score > 0 ? total_score > usl ? green : lime : total_score < lsl ? maroon : red
  411.  
  412. plot(usl, title="Upper Signal Line", style=line, color=plotColor)
  413. plot(lsl, title="Lower Signal Line", style=line, color=plotColor)
  414. plot(0, style=line, title="Base")
  415.  
  416. plot(total_score, linewidth=3, style=line, color=plotColor, title="Total Score")
  417.  
  418. fill(upperSignalLine, lowerSignalLine, color=purple, transp=90)
  419.  
  420.  
  421. // ===Original Trigger Conditions===
  422. //
  423. //Change the "usl" variable to any fixed number to test sensitivity.
  424. //Ex.: buy @ -6, sell @ 8 to catch cycles.
  425. //Added bst & sst inputs at top of script.
  426. //longCondition = crossover(total_score, bst) //usl)
  427. //longClose = crossunder(total_score, sst) //usl)
  428. //take_profit = falling(total_score, 3) //optional Take Profit function
  429. //
  430. //if time>timestamp(2017, 11, 01, 00, 00)
  431. //strategy.entry("Long", strategy.long, when=longCondition)
  432. //
  433. //The two options below can be toggled or stacked.
  434. //strategy.close("Long", when = take_profit) //Take Profit option above.
  435. //strategy.close_all(when=longClose)
  436. //
  437. // === /END
  438.  
  439. // === Conditions ===
  440.  
  441. ////////////////////////////////////////////////////////////////////////////
  442.  
  443. long_entry = (total_score>bst) and e1<e2 and n1>n2 and s1>s2 and confidence>dt+long_conf //and e1<e2 and n1>n2 and s1>s2 and confidence>dt+0.0 //crossover(total_score, bst) //and buy_tl //Crossover is for Ichimoku, the rest is HullMA
  444.  
  445. short_entry = (total_score<sst) and e1>e2 and n1<n2 and s1<s2 and confidence>dt+short_conf //and e1>e2 and n1<n2 and s1<s2 and confidence<dt+0.0 //crossunder(total_score, sst) //Crossover is for Ichimoku, the rest is HullMA
  446.  
  447. long_exit = short_entry //or (e1>e2 and confidence<dt+0.0) //or falling(total_score, fall) //and sell_tl //optional Take Profit function & trailing limit
  448.  
  449. short_exit = long_entry //or (e1<e2 and confidence>dt+0.0)
  450.  
  451. // === /END
  452.  
  453.  
  454. ///////////////////////////////////////////////////////////////////////////
  455.  
  456. // init these values here, they will get updated later as more decisions are made
  457. last_long_close = na
  458. last_short_close = na
  459.  
  460. // === Long position detection ===
  461. // longs open
  462. longo = 0
  463. longo := nz(longo[1])
  464. // longs closed
  465. longc = 0
  466. longc := nz(longc[1])
  467. if long_entry
  468. longo := longo + 1
  469. longc := 0
  470. if long_exit
  471. longc := longc + 1
  472. longo := 0
  473. // === /END
  474.  
  475. // === Short position detection ===
  476. shorto = 0
  477. shorto := nz(shorto[1])
  478. shortc = 0
  479. shortc := nz(shortc[1])
  480. if short_entry
  481. shorto := shorto + 1
  482. shortc := 0
  483. if short_exit
  484. shortc := shortc + 1
  485. shorto := 0
  486. // === /END
  487.  
  488. // === Pyramiding Settings ===
  489. //pyr = input(1, title="Pyramiding Setting")
  490. pyr = 1
  491. longCondition = long_entry and longo <= pyr
  492. longX = long_exit and longc <= pyr
  493. shortCondition = short_entry and shorto <=pyr
  494. shortX = short_exit and shortc <=pyr
  495. // === /END
  496.  
  497. // === Get Last Position Price ===
  498. last_open_longCondition = na
  499. last_open_shortCondition = na
  500. // last open prices
  501. last_open_longCondition := longCondition ? close : nz(last_open_longCondition[1])
  502. last_open_shortCondition := shortCondition ? close : nz(last_open_shortCondition[1])
  503. // === /END
  504.  
  505. // === Check For Long/Short ===
  506. last_longCondition = na
  507. last_shortCondition = na
  508. // last open times
  509. last_longCondition := longCondition ? time : nz(last_longCondition[1])
  510. last_shortCondition := shortCondition ? time : nz(last_shortCondition[1])
  511. last_longClose = longX ? time : nz(last_long_close[1])
  512. last_shortClose = shortX ? time : nz(last_short_close[1])
  513.  
  514. in_longCondition = last_longCondition > last_shortCondition and last_longCondition >= last_longClose
  515. in_shortCondition = last_shortCondition > last_longCondition and last_shortCondition >= last_shortClose
  516. // === /END
  517.  
  518. // === Stop Loss (Long) ===
  519. isSLl = input(false, "Stop Loss (Long)")
  520. sll = input(0, "Stop Loss %", type=float, step=0.2, minval=0, maxval=100) / 100
  521. long_call_sl = last_open_longCondition * (1 - sll)
  522. long_sl = isSLl and low <= long_call_sl and longCondition == 0
  523. // === /END
  524.  
  525. // === Stop Loss (Short) ===
  526. isSLs = input(false, "Stop Loss (Short)")
  527. sls = input(0, "Stop Loss %", type=float, step=0.2, minval=0, maxval=100) / 100
  528. short_call_sl = last_open_shortCondition * (1 + sls)
  529. short_sl = isSLs and high >= short_call_sl and shortCondition == 0
  530. // === /END
  531.  
  532. // === Trailing Stop ===
  533. last_high = na
  534. last_low = na
  535. last_high := in_longCondition ? (na(last_high[1]) or high > nz(last_high[1])) ? high : nz(last_high[1]) : na
  536. last_low := in_shortCondition ? (na(last_low[1]) or low < nz(last_low[1])) ? low : nz(last_low[1]) : na
  537. isTSl = input(false, "Trailing Stop Long")
  538. tsil = input(19, "Activate Trailing Stop % Long", type=float, step=1, minval=0, maxval=100) / 100
  539. tsl = input(2, "Trailing Stop % Long", type=float, step=1, minval=0, maxval=100) / 100
  540. long_call_ts = last_high * (1 - tsl)
  541. long_call_tsi = last_open_longCondition * (1 + tsil)
  542. long_ts = isTSl and not na(last_high) and low <= long_call_ts and longCondition == 0 and last_high >= long_call_tsi
  543. isTSs = input(false, "Trailing Stop Short")
  544. tsis = input(19, "Activate Trailing Stop % Short", type=float, step=1, minval=0, maxval=100) / 100
  545. tss = input(2, "Trailing Stop % Short", type=float, step=1, minval=0, maxval=100) / 100
  546. short_call_ts = last_low * (1 + tss)
  547. short_call_tsi = last_open_shortCondition * (1 - tsis)
  548. short_ts = isTSs and not na(last_low) and high >= short_call_ts and shortCondition == 0 and last_low <= short_call_tsi
  549. // === /END
  550.  
  551. // === Trailing Limit ===
  552. tl_last_high = na
  553. tl_last_low = na
  554. tl_last_high := shortCondition ? (na(tl_last_high[1]) or high > nz(tl_last_high[1])) ? high : nz(tl_last_high[1]) : na //as of now script will only keep track of highest high/lowest low when short condition/long condition are satisfied
  555. tl_last_low := longCondition ? (na(tl_last_low[1]) or low < nz(tl_last_low[1])) ? low : nz(tl_last_low[1]) : na //that leaves the possibility that short/long condition with activate trailing limit, but short/long condition might become
  556. tl_isTSl = input(true, "Trailing Limit Long") //false before trailing limit is activated, in which case we do not end up trading...
  557. //tl_tsil = input(2, "Activate Trailing Limit % Long", type=float, step=1, minval=0, maxval=100) / 100
  558. tl_tsl = input(2, "Trailing Limit % Long", type=float, step=1, minval=0, maxval=100) / 100
  559. tl_long_call_ts = tl_last_low * (1 + tl_tsl)
  560. //tl_long_call_tsi = last_open_longCondition * (1 + tl_tsil)
  561. tl_long_ts = tl_isTSl and not na(tl_last_high) and high >= tl_long_call_ts
  562. tl_isTSs = input(true, "Trailing Limit Short")
  563. //tl_tsis = input(19, "Activate Trailing Stop % Short", type=float, step=1, minval=0, maxval=100) / 100
  564. tl_tss = input(2, "Trailing Limit % Short", type=float, step=1, minval=0, maxval=100) / 100
  565. tl_short_call_ts = tl_last_high * (1 - tl_tss)
  566. //tl_short_call_tsi = last_open_shortCondition * (1 - tl_tsis)
  567. tl_short_ts = tl_isTSs and not na(tl_last_low) and low <= tl_short_call_ts
  568. // === /END
  569.  
  570. // === Create Single Close For All Closing Conditions ===
  571. closelong = long_sl or long_ts or longX or tl_short_ts //Add 'or sell_tl' for Trailing Limit? DEBUG
  572. closeshort = short_sl or short_ts or shortX or tl_long_ts
  573.  
  574. // Get Last Close
  575. last_long_close := closelong ? time : nz(last_long_close[1])
  576. last_short_close := closeshort ? time : nz(last_short_close[1])
  577.  
  578. // Check For Close Since Last Open
  579. if closelong and last_long_close[1] > last_longCondition
  580. closelong := 0
  581.  
  582. if closeshort and last_short_close[1] > last_shortCondition
  583. closeshort := 0
  584. // === /END
  585.  
  586. ////////////////////////////////////////////////////////////////////////////
  587.  
  588. // === Alarm Settings ===
  589. alertcondition(tl_long_ts, title='LONG', message='LONG')
  590. alertcondition(closelong==1, title='EXIT LONG', message='EXIT LONG')
  591. alertcondition(tl_short_ts, title='SHORT', message='SHORT')
  592. alertcondition(closeshort==1, title='EXIT SHORT', message='EXIT SHORT')
  593. // === /END
  594.  
  595. ////////////////////////////////////////////////////////////////////////////
  596.  
  597. // === Debugs Here ===
  598. //Remove "//" To Check/Debug The Code Above
  599. // Signal Shapes
  600. //plotshape(longCondition[1]==1, title='LONG', style=shape.triangleup, size=size.large, color=#02CB80, location= location.belowbar)
  601. //plotshape(shortCondition[1]==1, title='SHORT', style=shape.triangledown, size=size.large, color=#DC143C, location=location.abovebar)
  602. //plotshape(shortCondition[1]==0 and closelong[1]==1, title='EXIT LONG', style=shape.xcross, color=#02CB80, location=location.belowbar, transp=0)
  603. //plotshape(longCondition[1]==0 and closeshort[1]==1, title='EXIT SHORT', style=shape.xcross, color=#DC143C, location=location.abovebar, transp=0)
  604. // SL Plot
  605. //slColor = (isSLl or isSLs) and (in_longCondition or in_shortCondition) ? red : white
  606. //plot(isSLl and in_longCondition ? long_call_sl : na, "Long SL", slColor, style=3, linewidth=2)
  607. //plot(isSLs and in_shortCondition ? short_call_sl : na, "Short SL", slColor, style=3, linewidth=2)
  608. // TP Plot
  609. //tpColor = isTP and (in_longCondition or in_shortCondition) ? purple : white
  610. //plot(isTP and in_longCondition ? long_call_tp : na, "Long TP", tpColor, style=3, linewidth=2)
  611. //plot(isTP and in_shortCondition ? short_call_tp : na, "Short TP", tpColor, style=3, linewidth=2)
  612. // TS Plot
  613. //tsColor = (isTSl or isTSs) and (in_longCondition or in_shortCondition) ? orange : white
  614. //tsiColor = (isTSl or isTSs) and (in_longCondition or in_shortCondition) ? white : orange
  615. //plot(isTSl and in_longCondition ? long_call_tsi : na, "Long Trailing", tsiColor, style=3, linewidth=2)
  616. //plot(isTSs and in_shortCondition ? short_call_tsi : na, "Short Trailing", tsiColor, style=3, linewidth=2)
  617. //plot(isTSl and in_longCondition and last_high > long_call_tsi ? long_call_ts : na, "Long Trailing", tsColor, style=2, linewidth=2)
  618. //plot(isTSs and in_shortCondition and last_low < short_call_tsi ? short_call_ts : na, "Short Trailing", tsColor, style=2, linewidth=2)
  619. // === /END
  620.  
  621. ////////////////////////////////////////////////////////////////////////////
  622. // //
  623. // REMOVE THE CODE BELOW FOR STUDY CONVERSION //
  624. // //
  625. ////////////////////////////////////////////////////////////////////////////
  626.  
  627. // === Backtesting Dates ===
  628. testPeriodSwitch = input(true, "Custom Backtesting Dates")
  629. testStartYear = input(2017, "Backtest Start Year")
  630. testStartMonth = input(6, "Backtest Start Month")
  631. testStartDay = input(1, "Backtest Start Day")
  632. testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,0,0)
  633. testStopYear = input(2018, "Backtest Stop Year")
  634. testStopMonth = input(12, "Backtest Stop Month")
  635. testStopDay = input(31, "Backtest Stop Day")
  636. testPeriodStop = timestamp(testStopYear,testStopMonth,testStopDay,0,0)
  637. testPeriod() =>
  638. time >= testPeriodStart and time <= testPeriodStop ? true : false
  639. isPeriod = testPeriodSwitch == true ? testPeriod() : true
  640. // === /END
  641.  
  642. // === Strategy ===
  643. if isPeriod and direction=="Both"
  644. if (tl_long_ts)
  645. strategy.entry("Long",strategy.long)
  646. if (closelong) and not shortCondition
  647. strategy.close("Long")
  648. if (tl_short_ts)
  649. strategy.entry("Short",strategy.short)
  650. if (closeshort) and not longCondition
  651. strategy.close("Short")
  652.  
  653. if isPeriod and direction=="Long"
  654. if (tl_long_ts)
  655. strategy.entry("Long",strategy.long)
  656. if (closelong)
  657. strategy.close("Long")
  658.  
  659. if isPeriod and direction=="Short"
  660. if (tl_short_ts)
  661. strategy.entry("Short",strategy.short)
  662. if (closeshort)
  663. strategy.close("Short")
  664. // === /END
  665.  
  666. ////////////////////////////////////////////////////////////////////////////
  667. // //
  668. // ULTIMATE PINE INJECTOR V1.2 //
  669. // //
  670. //////////////////////===ANION=CODE=END====/////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement