Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.56 KB | None | 0 0
  1. //* Ichimoku Signals *\\
  2.  
  3.  
  4. // == inputs ==
  5. conversionPeriods = input(10, minval=1, title="Conversion Line Periods"),
  6. basePeriods = input(30, minval=1, title="Base Line Periods")
  7. laggingSpan2Periods = input(60, minval=1, title="Lagging Span 2 Periods"),
  8. displacement = input(30, minval=1, title="Displacement")
  9.  
  10. // == helpers ==
  11.  
  12. donchian(len) => avg(lowest(len), highest(len))
  13.  
  14. resolve(src, default) =>
  15. if na(src)
  16. default
  17. else
  18. src
  19.  
  20. // == generate ichimoku data ==
  21.  
  22. conversionLine = donchian(conversionPeriods)
  23. baseLine = donchian(basePeriods)
  24.  
  25. // NOTE: these senkou spans are for signal generation; not for plotting. this is
  26. // due to limitations of pinescript
  27. //
  28. // i.e. Senkou Span A
  29. leadLine1 = offset(avg(conversionLine, baseLine), displacement)
  30. // i.e. Senkou Span B
  31. leadLine2 = offset(donchian(laggingSpan2Periods), displacement)
  32.  
  33.  
  34. // == plot ichimoku ==
  35.  
  36. // // i.e. Tenkan Sen (turning line) (blue)
  37. // plot(conversionLine, color=#0496ff, title="Turning/Conversion Line", linewidth=3)
  38. // // i.e. Kijun Sen (base/standard line) (red)
  39. // plot(baseLine, color=#991515, title="Standard/Base Line", linewidth=3)
  40. // // i.g. Chikou Span (lagging line) (green)
  41. // plot(close, offset = -displacement, color=#459915, title="Lagging Span", linewidth=3)
  42.  
  43. // plot(conversionLine, color=#0496ff, title="Conversion Line")
  44. // plot(baseLine, color=#991515, title="Base Line")
  45.  
  46. // real_leadLine1 = avg(conversionLine, baseLine)
  47. // real_leadLine2 = donchian(laggingSpan2Periods)
  48.  
  49. // // i.e. Senkou Span A
  50. // p1 = plot(real_leadLine1, offset = displacement, color=green, title="Lead 1")
  51. // // i.e. Senkou Span B
  52. // p2 = plot(real_leadLine2, offset = displacement, color=red, title="Lead 2")
  53.  
  54. // // i.e. Kumo cloud colouring
  55. // fill(p1, p2, color = real_leadLine1 > real_leadLine2 ? green : red)
  56.  
  57. // == ichimoku cloud signals ==
  58. // source: http://www.ichimokutrader.com/signals.html
  59.  
  60. // == Tenkan Sen (turning line) / Kijun Sen (standard line) Cross ==
  61.  
  62. // tk_cross_bull = (conversionLine[1] < baseLine[1] and conversionLine >= baseLine)
  63. // tk_cross_bear = (conversionLine[1] > baseLine[1] and conversionLine <= baseLine)
  64. tk_cross_bull = crossover(conversionLine, baseLine)
  65. tk_cross_bear = crossunder(conversionLine, baseLine)
  66.  
  67. // get true y-coord of the cross
  68. cross_y = (conversionLine[1] * (baseLine - baseLine[1]) - baseLine[1] * (conversionLine - conversionLine[1])) / ((baseLine - baseLine[1]) - (conversionLine - conversionLine[1]))
  69. tk_cross_score = 0.0
  70. tk_cross_score := resolve(tk_cross_score[1], 0)
  71. tk_cross_below_kumo = cross_y <= leadLine2[1] and cross_y <= leadLine1[1] and cross_y <= leadLine2 and cross_y <= leadLine1
  72. tk_cross_above_kumo = cross_y >= leadLine2[1] and cross_y >= leadLine1[1] and cross_y >= leadLine2 and cross_y >= leadLine1
  73. tk_cross_inside_kumo = (not tk_cross_below_kumo) and (not tk_cross_above_kumo)
  74.  
  75.  
  76.  
  77. use_tk_cross = input(true, title="TS Cross")
  78. tk_cross_weight = input(1.0, title="TS Cross Importance Weight", type=float, step=0.1)
  79.  
  80. tk_cross_weak_bullish_points = input(0.25, title="TS Cross Weak Bullish Points", type=float, step=0.1)
  81. tk_cross_neutral_bullish_points = input(0.5, title="TS Cross Neutral Bullish Points", type=float, step=0.1)
  82. tk_cross_strong_bullish_points = input(1.0, title="TS Cross Strong Bullish Points", type=float, step=0.1)
  83.  
  84. tk_cross_weak_bearish_points = input(-0.25, title="TS Cross Weak Bearish Points", type=float, step=0.1)
  85. tk_cross_neutral_bearish_points = input(-0.5, title="TS Cross Neutral Bearish Points", type=float, step=0.1)
  86. tk_cross_strong_bearish_points = input(-1.0, title="TS Cross Strong Bearish Points", type=float, step=0.1)
  87.  
  88. // == TK Cross / weak bullish (below kumo) ==
  89. tk_cross_score := (tk_cross_bull and tk_cross_below_kumo) ? tk_cross_weak_bullish_points : tk_cross_score
  90.  
  91. // == TK Cross / neutral bullish (inside kumo) ==
  92. tk_cross_score := (tk_cross_bull and tk_cross_inside_kumo) ? tk_cross_neutral_bullish_points : tk_cross_score
  93.  
  94. // == TK Cross / strong bullish (above kumo) ==
  95. tk_cross_score := (tk_cross_bull and tk_cross_above_kumo) ? tk_cross_strong_bullish_points : tk_cross_score
  96.  
  97. // == TK Cross / strong bearish (below kumo) ==
  98. tk_cross_score := (tk_cross_bear and tk_cross_below_kumo) ? tk_cross_strong_bearish_points : tk_cross_score
  99.  
  100. // == TK Cross / neutral bearish (inside kumo) ==
  101. tk_cross_score := (tk_cross_bear and tk_cross_inside_kumo) ? tk_cross_neutral_bearish_points : tk_cross_score
  102.  
  103. // == TK Cross / weak bearish (above kumo) ==
  104. tk_cross_score := (tk_cross_bear and tk_cross_above_kumo) ? tk_cross_weak_bearish_points : tk_cross_score
  105.  
  106.  
  107. // == Price and Kijun Sen (standard line) Cross ==
  108.  
  109. //pk_cross_bull = (close[1] < baseLine[1] and close >= baseLine)
  110. //pk_cross_bear = (close[1] > baseLine[1] and close <= baseLine)
  111. pk_cross_bull = crossover(close, baseLine)
  112. pk_cross_bear = crossunder(close, baseLine)
  113.  
  114. cross_pk_y = (close[1] * (baseLine - baseLine[1]) - baseLine[1] * (close - close[1])) / ((baseLine - baseLine[1]) - (close - close[1]))
  115.  
  116. 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
  117. 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
  118. pk_cross_inside_kumo = (not pk_cross_below_kumo) and (not pk_cross_above_kumo)
  119.  
  120. pk_cross_score = 0.0
  121. pk_cross_score := resolve(pk_cross_score[1], 0)
  122.  
  123. use_pk_cross = input(true, title="PS Cross")
  124. pk_cross_weight = input(1.0, title="PS Cross Importance Weight", type=float, step=0.1)
  125.  
  126. pk_cross_weak_bullish_points = input(0.25, title="PS Cross Weak Bullish Points", type=float, step=0.1)
  127. pk_cross_neutral_bullish_points = input(0.5, title="PS Cross Neutral Bullish Points", type=float, step=0.1)
  128. pk_cross_strong_bullish_points = input(1.0, title="PS Cross Strong Bullish Points", type=float, step=0.1)
  129.  
  130. pk_cross_weak_bearish_points = input(-0.25, title="PS Cross Weak Bearish Points", type=float, step=0.1)
  131. pk_cross_neutral_bearish_points = input(-0.5, title="PS Cross Neutral Bearish Points", type=float, step=0.1)
  132. pk_cross_strong_bearish_points = input(-1.0, title="PS Cross Strong Bearish Points", type=float, step=0.1)
  133.  
  134. // == PK Cross / weak bullish (below kumo) ==
  135. pk_cross_score := (pk_cross_bull and pk_cross_below_kumo) ? pk_cross_weak_bullish_points : pk_cross_score
  136.  
  137. // == PK Cross / neutral bullish (inside kumo)
  138. pk_cross_score := (pk_cross_bull and pk_cross_inside_kumo) ? pk_cross_neutral_bullish_points : pk_cross_score
  139.  
  140. // == PK Cross / strong bullish (above kumo)
  141. pk_cross_score := (pk_cross_bull and pk_cross_above_kumo) ? pk_cross_strong_bullish_points : pk_cross_score
  142.  
  143. // == PK Cross / strong bearish (below kumo)
  144. pk_cross_score := (pk_cross_bear and pk_cross_below_kumo) ? pk_cross_strong_bearish_points : pk_cross_score
  145.  
  146. // == PK Cross / neutral bearish (inside kumo)
  147. pk_cross_score := (pk_cross_bear and pk_cross_inside_kumo) ? pk_cross_neutral_bearish_points : pk_cross_score
  148.  
  149. // == PK Cross / weak bearish (above kumo)
  150. pk_cross_score := (pk_cross_bear and pk_cross_above_kumo) ? pk_cross_weak_bearish_points : pk_cross_score
  151.  
  152. //
  153.  
  154. price_below_kumo = (close < leadLine2 and close < leadLine1)
  155. price_above_kumo = (close > leadLine2 and close > leadLine1)
  156. price_inside_kumo = (not price_below_kumo) and (not price_above_kumo)
  157.  
  158. // == Kumo Breakouts ==
  159. kumo_breakout_score = 0.0
  160. kumo_breakout_score := resolve(kumo_breakout_score[1], 0)
  161.  
  162. kumo_bull = (crossover(close, leadLine1) and leadLine1 > leadLine2) or (crossover(close, leadLine2) and leadLine2 > leadLine1)
  163. kumo_bear = (crossunder(close, leadLine2) and leadLine1 > leadLine2) or (crossunder(close, leadLine1) and leadLine2 > leadLine1)
  164.  
  165. kumo_breakout_bullish_points = input(1.0, title="Kumo Breakout Bullish Points", type=float, step=0.1)
  166. kumo_breakout_bearish_points = input(-1.0, title="Kumo Breakout Bearish Points", type=float, step=0.1)
  167.  
  168. kumo_breakout_score := (kumo_bull and price_above_kumo) ? kumo_breakout_bullish_points : kumo_breakout_score
  169. kumo_breakout_score := (kumo_bear and price_below_kumo) ? kumo_breakout_bearish_points : kumo_breakout_score
  170.  
  171.  
  172. use_kumo_breakout = input(true, title="Kumo Breakout")
  173. kumo_breakout_weight = input(1.0, title="Kumo Breakout Importance Weight", type=float, step=0.1)
  174.  
  175.  
  176.  
  177.  
  178. // == Senkou Span Cross ==
  179. // The Senkou Span Cross signal occurs when the Senkou Span A (1st leading line) crosses the Senkou Span B (2nd leading line).
  180. // NOTE: this cross occurs ahead of the price, since it's displaced to the right; this displacement must be removed
  181.  
  182. // i.e. Senkou Span A (no displacement)
  183. no_dp_leadLine1 = avg(conversionLine, baseLine)
  184. // i.e. Senkou Span B (no displacement)
  185. no_dp_leadLine2 = donchian(laggingSpan2Periods)
  186. // TODO: debug;remove
  187. // plot(no_dp_leadLine1)
  188. // plot(no_dp_leadLine2)
  189.  
  190. lead_line_cross_bull = crossover(no_dp_leadLine1, no_dp_leadLine2)
  191. lead_line_cross_bear = crossunder(no_dp_leadLine1, no_dp_leadLine2)
  192. price_below_kumo := (close < no_dp_leadLine2 and close < no_dp_leadLine1)
  193. price_above_kumo := (close > no_dp_leadLine2 and close > no_dp_leadLine1)
  194. price_inside_kumo := (not price_below_kumo) and (not price_above_kumo)
  195. // price_inside_kumo = (no_dp_leadLine2 < close and close < no_dp_leadLine1) and (no_dp_leadLine1 < close and close < no_dp_leadLine2)
  196.  
  197. span_cross_score = 0.0
  198. span_cross_score := resolve(span_cross_score[1], 0)
  199.  
  200. use_span_cross = input(true, title="Span Cross")
  201. span_cross_weight = input(1.0, title="Span Cross Importance Weight", type=float, step=0.1)
  202.  
  203. span_cross_weak_bullish_points = input(0.25, title="Span Cross Weak Bullish Points", type=float, step=0.1)
  204. span_cross_neutral_bullish_points = input(0.5, title="Span Cross Neutral Bullish Points", type=float, step=0.1)
  205. span_cross_strong_bullish_points = input(1.0, title="Span Cross Strong Bullish Points", type=float, step=0.1)
  206.  
  207. span_cross_weak_bearish_points = input(-0.25, title="Span Cross Weak Bearish Points", type=float, step=0.1)
  208. span_cross_neutral_bearish_points = input(-0.5, title="Span Cross Neutral Bearish Points", type=float, step=0.1)
  209. span_cross_strong_bearish_points = input(-1.0, title="Span Cross Strong Bearish Points", type=float, step=0.1)
  210.  
  211. // == Senkou Span Cross / weak bullish (price below kumo) ==
  212. span_cross_score := (lead_line_cross_bull and price_below_kumo) ? span_cross_weak_bullish_points : span_cross_score
  213.  
  214. // == Senkou Span Cross / neutral bullish (price inside kumo) ==
  215. span_cross_score := (lead_line_cross_bull and price_inside_kumo) ? span_cross_neutral_bullish_points : span_cross_score
  216.  
  217. // == Senkou Span Cross / strong bullish (price above kumo) ==
  218. span_cross_score := (lead_line_cross_bull and price_above_kumo) ? span_cross_strong_bullish_points : span_cross_score
  219.  
  220. // == Senkou Span Cross / weak bearish (price above kumo) ==
  221. span_cross_score := (lead_line_cross_bear and price_above_kumo) ? span_cross_weak_bearish_points : span_cross_score
  222.  
  223. // == Senkou Span Cross / neutral bearish (price inside kumo) ==
  224. span_cross_score := (lead_line_cross_bear and price_inside_kumo) ? span_cross_neutral_bearish_points : span_cross_score
  225.  
  226. // == Senkou Span Cross / strong bearish (price below kumo) ==
  227. span_cross_score := (lead_line_cross_bear and price_below_kumo) ? span_cross_strong_bearish_points : span_cross_score
  228.  
  229. // == Chikou Span Cross ==
  230. // The Chikou Span Cross signal occurs when the Chikou Span (Lagging line) rises above or falls below the price.
  231.  
  232. past_price = offset(close, displacement)
  233. lag_line_bull_cross = close > close[displacement]
  234. lag_line_bear_cross = close < close[displacement]
  235.  
  236. // TODO: debug; remove
  237. // plot(close)
  238. // plot(close[displacement], linewidth=2)
  239.  
  240. past_price_below_kumo = (past_price < leadLine2 and past_price < leadLine1)
  241. past_price_above_kumo = (past_price > leadLine2 and past_price > leadLine1)
  242. past_price_inside_kumo = (leadLine2 < past_price and past_price < leadLine1) and (leadLine1 < past_price and past_price < leadLine2)
  243.  
  244. lag_line_cross_score = 0.0
  245. lag_line_cross_score := resolve(lag_line_cross_score[1], 0)
  246.  
  247. use_lag_line = input(true, title="Lag Line Cross")
  248. lag_line_cross_weight = input(1.0, title="Lag Line Cross Importance Weight", type=float, step=0.1)
  249.  
  250. lag_line_cross_weak_bullish_points = input(0.25, title="Lag Line Cross Weak Bullish Points", type=float, step=0.1)
  251. lag_line_cross_neutral_bullish_points = input(0.5, title="Lag Line Cross Neutral Bullish Points", type=float, step=0.1)
  252. lag_line_cross_strong_bullish_points = input(1.0, title="Lag Line Cross Strong Bullish Points", type=float, step=0.1)
  253.  
  254. lag_line_cross_weak_bearish_points = input(-0.25, title="Lag Line Cross Weak Bearish Points", type=float, step=0.1)
  255. lag_line_cross_neutral_bearish_points = input(-0.5, title="Lag Line Cross Neutral Bearish Points", type=float, step=0.1)
  256. lag_line_cross_strong_bearish_points = input(-1.0, title="Lag Line Cross Strong Bearish Points", type=float, step=0.1)
  257.  
  258. // == Chikou Span Cross / weak bullish (price below kumo)
  259. lag_line_cross_score := (lag_line_bull_cross and past_price_below_kumo) ? lag_line_cross_weak_bullish_points : lag_line_cross_score
  260.  
  261. // == Chikou Span Cross / neutral bullish (price inside kumo)
  262. lag_line_cross_score := (lag_line_bull_cross and past_price_inside_kumo) ? lag_line_cross_neutral_bullish_points : lag_line_cross_score
  263.  
  264. // == Chikou Span Cross / strong bullish (price above kumo)
  265. lag_line_cross_score := (lag_line_bull_cross and past_price_above_kumo) ? lag_line_cross_strong_bullish_points : lag_line_cross_score
  266.  
  267. // == Chikou Span Cross / weak bearish (price above kumo)
  268. lag_line_cross_score := (lag_line_bear_cross and past_price_above_kumo) ? lag_line_cross_weak_bearish_points : lag_line_cross_score
  269.  
  270. // == Chikou Span Cross / neutral bearish (price inside kumo)
  271. lag_line_cross_score := (lag_line_bear_cross and past_price_inside_kumo) ? lag_line_cross_neutral_bearish_points : lag_line_cross_score
  272.  
  273. // == Chikou Span Cross / strong bearish (price below kumo)
  274. lag_line_cross_score := (lag_line_bear_cross and past_price_below_kumo) ? lag_line_cross_strong_bearish_points : lag_line_cross_score
  275.  
  276. // == lag line releative to cloud ==
  277.  
  278. use_lag_line_location = input(true, title="Lag Line Relative to Cloud")
  279.  
  280. lag_line_location_weight = input(1.0, title="Lag Line Relative to Cloud Importance Weight", type=float, step=0.1)
  281.  
  282. lag_line_location_above_points = input(1.0, title="Lag Line Above Cloud Points", type=float, step=0.1)
  283. lag_line_location_inside_points = input(0, title="Lag Line Inside Cloud Points", type=float, step=0.1)
  284. lag_line_location_below_points = input(-1.0, title="Lag Line Below Cloud Points", type=float, step=0.1)
  285.  
  286. lag_line_placement_score = 0.0
  287. lag_line_placement_score := resolve(lag_line_placement_score[1], 0)
  288.  
  289. lag_line_placement_score := past_price_above_kumo ? lag_line_location_above_points : lag_line_placement_score
  290. lag_line_placement_score := past_price_inside_kumo ? lag_line_location_inside_points : lag_line_placement_score
  291. lag_line_placement_score := past_price_below_kumo ? lag_line_location_below_points : lag_line_placement_score
  292.  
  293. // == price relative to cloud ==
  294. price_placement_score = 0.0
  295. price_placement_score := resolve(price_placement_score[1], 0)
  296.  
  297. use_price_location = input(true, title="Price Relative to Cloud")
  298. price_location_weight = input(1.0, title="Price Relative to Cloud Importance Weight", type=float, step=0.1)
  299.  
  300. price_location_above_points = input(1.0, title="Price Above Cloud Points", type=float, step=0.1)
  301. price_location_inside_points = input(0, title="Price Inside Cloud Points", type=float, step=0.1)
  302. price_location_below_points = input(-1.0, title="Price Below Cloud Points", type=float, step=0.1)
  303.  
  304. price_below_kumo := (close < leadLine2 and close < leadLine1)
  305. price_above_kumo := (close > leadLine2 and close > leadLine1)
  306. price_inside_kumo := (not price_below_kumo) and (not price_above_kumo)
  307.  
  308. price_placement_score := price_above_kumo ? price_location_above_points : price_placement_score
  309. price_placement_score := price_inside_kumo ? price_location_inside_points : price_placement_score
  310. price_placement_score := price_below_kumo ? price_location_below_points : price_placement_score
  311.  
  312. // == plot score ==
  313.  
  314. resolve_bull_vote(vote) =>
  315. vote > 0 ? vote : 0
  316.  
  317. resolve_bear_vote(vote) =>
  318. vote < 0 ? vote * -1 : 0
  319.  
  320. number_of_metrics = 7
  321.  
  322. bull_votes = 0.0
  323. bull_votes := resolve(bull_votes[1], 0) * 0.5 + resolve(bull_votes[2], 0) * 0.25 + resolve_bull_vote(tk_cross_score) + resolve_bull_vote(pk_cross_score) + resolve_bull_vote(kumo_breakout_score) + resolve_bull_vote(span_cross_score) + resolve_bull_vote(lag_line_cross_score) + resolve_bull_vote(lag_line_placement_score) + resolve_bull_vote(price_placement_score)
  324.  
  325. bear_votes = 0.0
  326. bear_votes := resolve(bear_votes[1], 0) * 0.5 + resolve(bear_votes[2], 0) * 0.25 + resolve_bear_vote(tk_cross_score) + resolve_bear_vote(pk_cross_score) + resolve_bear_vote(kumo_breakout_score) + resolve_bear_vote(span_cross_score) + resolve_bear_vote(lag_line_cross_score) + resolve_bear_vote(lag_line_placement_score) + resolve_bear_vote(price_placement_score)
  327.  
  328.  
  329. raw_score = (bull_votes + 0.5) / (bull_votes + bear_votes + 1)
  330.  
  331.  
  332.  
  333. // plot(total_score, linewidth=3, title="Total Score")
  334.  
  335. plot(raw_score, linewidth=2, color=black)
  336.  
  337. plot(0.75, style=line, title="Bull zone", color=green)
  338. plot(0.5, style=line, title="Base", color=black)
  339. plot(0.25, style=line, title="Bear zone", color=red)
  340.  
  341. buy_signal = raw_score <= 0.2 and raw_score > raw_score[1] and raw_score[1] <= raw_score[2]
  342. sell_signal = raw_score >= 0.8 and raw_score < raw_score[1] and raw_score[1] >= raw_score[2]
  343.  
  344. bg_color = buy_signal ? green : sell_signal ? red : white
  345.  
  346. //bgcolor(bg_color, transp=70)
  347.  
  348. icLong = crossover(raw_score, 0.5)
  349. icShort = crossunder(raw_score, 0.5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement